autogen_ext.models.azure#

class AzureAIChatCompletionClient(**kwargs: Unpack)[source]#

基类: ChatCompletionClient

用于托管在 Azure AI Foundry 或 GitHub Models 上的模型的聊天完成客户端。有关更多信息,请参见这里

参数:
  • endpoint (str) – 要使用的端点。 必需。

  • credential (union, AzureKeyCredential, AsyncTokenCredential) – 要使用的凭据。必需

  • model_info (ModelInfo) – 模型的模型系列和功能。必需。

  • model (str) – 模型的名称。如果模型托管在 GitHub Models 上,则为必需。

  • frequency_penalty – (可选,float)

  • presence_penalty – (可选,float)

  • temperature – (可选,float)

  • top_p – (可选,float)

  • max_tokens – (可选,int)

  • response_format – (可选, literal[“text”, “json_object”])

  • stop – (可选,List[str])

  • tools – (可选,List[ChatCompletionsToolDefinition])

  • tool_choice – (可选,Union[str, ChatCompletionsToolChoicePreset, ChatCompletionsNamedToolChoice]])

  • seed – (可选,int)

  • model_extras – (可选,Dict[str, Any])

要使用此客户端,您必须安装 azure extra

pip install "autogen-ext[azure]"

以下代码段展示了如何将客户端与 GitHub Models 一起使用

import asyncio
import os
from azure.core.credentials import AzureKeyCredential
from autogen_ext.models.azure import AzureAIChatCompletionClient
from autogen_core.models import UserMessage


async def main():
    client = AzureAIChatCompletionClient(
        model="Phi-4",
        endpoint="https://models.inference.ai.azure.com",
        # To authenticate with the model you will need to generate a personal access token (PAT) in your GitHub settings.
        # Create your PAT token by following instructions here: https://githubdocs.cn/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
        credential=AzureKeyCredential(os.environ["GITHUB_TOKEN"]),
        model_info={
            "json_output": False,
            "function_calling": False,
            "vision": False,
            "family": "unknown",
            "structured_output": False,
        },
    )

    result = await client.create([UserMessage(content="What is the capital of France?", source="user")])
    print(result)

    # Close the client.
    await client.close()


if __name__ == "__main__":
    asyncio.run(main())

要使用流式传输,您可以使用 create_stream 方法

import asyncio
import os

from autogen_core.models import UserMessage
from autogen_ext.models.azure import AzureAIChatCompletionClient
from azure.core.credentials import AzureKeyCredential


async def main():
    client = AzureAIChatCompletionClient(
        model="Phi-4",
        endpoint="https://models.inference.ai.azure.com",
        # To authenticate with the model you will need to generate a personal access token (PAT) in your GitHub settings.
        # Create your PAT token by following instructions here: https://githubdocs.cn/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
        credential=AzureKeyCredential(os.environ["GITHUB_TOKEN"]),
        model_info={
            "json_output": False,
            "function_calling": False,
            "vision": False,
            "family": "unknown",
            "structured_output": False,
        },
    )

    # Create a stream.
    stream = client.create_stream([UserMessage(content="Write a poem about the ocean", source="user")])
    async for chunk in stream:
        print(chunk, end="", flush=True)
    print()

    # Close the client.
    await client.close()


if __name__ == "__main__":
    asyncio.run(main())
actual_usage() RequestUsage[source]#
add_usage(usage: RequestUsage) None[source]#
property capabilities: ModelInfo#
async close() None[source]#
count_tokens(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = []) int[source]#
async create(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = [], json_output: bool | type[BaseModel] | None = None, extra_create_args: Mapping[str, Any] = {}, cancellation_token: CancellationToken | None = None) CreateResult[source]#

从模型创建一个单独的响应。

参数:
  • messages (Sequence[LLMMessage]) – 要发送到模型的消息。

  • tools (Sequence[Tool | ToolSchema], optional) – 与模型一起使用的工具。默认为 []。

  • json_output (Optional[bool | type[BaseModel]], optional) – 是否使用 JSON 模式、结构化输出,或都不使用。默认为 None。如果设置为 Pydantic BaseModel 类型,它将被用作结构化输出的输出类型。如果设置为布尔值,它将被用于确定是否使用 JSON 模式。 如果设置为 True,请确保指示模型在指令或提示中生成 JSON 输出。

  • extra_create_args (Mapping[str, Any], optional) – 传递到底层客户端的额外参数。默认为 {}。

  • cancellation_token (Optional[CancellationToken], optional) – 用于取消的令牌。默认为 None。

返回:

CreateResult – 模型调用的结果。

async create_stream(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = [], json_output: bool | type[BaseModel] | None = None, extra_create_args: Mapping[str, Any] = {}, cancellation_token: CancellationToken | None = None) AsyncGenerator[str | CreateResult, None][source]#

从模型创建一个字符串块的流,以 CreateResult 结尾。

参数:
  • messages (Sequence[LLMMessage]) – 要发送到模型的消息。

  • tools (Sequence[Tool | ToolSchema], optional) – 与模型一起使用的工具。默认为 []。

  • json_output (Optional[bool | type[BaseModel]], optional) –

    是否使用 JSON 模式、结构化输出,或都不使用。默认为 None。如果设置为 Pydantic BaseModel 类型,它将被用作结构化输出的输出类型。如果设置为布尔值,它将被用于确定是否使用 JSON 模式。 如果设置为 True,请确保指示模型在指令或提示中生成 JSON 输出。

  • extra_create_args (Mapping[str, Any], optional) – 传递到底层客户端的额外参数。默认为 {}。

  • cancellation_token (Optional[CancellationToken], optional) – 用于取消的令牌。默认为 None。

返回:

AsyncGenerator[Union[str, CreateResult], None] – 一个生成器,它产生字符串块,并以 CreateResult 结束。

property model_info: ModelInfo#
remaining_tokens(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = []) int[source]#
total_usage() RequestUsage[source]#
class AzureAIChatCompletionClientConfig[source]#

基类:dict

credential: AzureKeyCredential | AsyncTokenCredential#
endpoint: str#
frequency_penalty: float | None#
max_tokens: int | None#
model: str | None#
model_extras: Dict[str, Any] | None#
model_info: ModelInfo#
presence_penalty: float | None#
response_format: Literal['text', 'json_object'] | None#
seed: int | None#
stop: List[str] | None#
temperature: float | None#
tool_choice: str | ChatCompletionsToolChoicePreset | ChatCompletionsNamedToolChoice | None#
tools: List[ChatCompletionsToolDefinition] | None#
top_p: float | None#