autogen_ext.models.azure#
- class AzureAIChatCompletionClient(**kwargs: Unpack)[source]#
-
适用于托管在 Azure AI Foundry 或 GitHub Models 上的模型的聊天补全客户端。更多信息请参阅此处。
- 参数:
endpoint (str) – 要使用的端点。必填。
credential (union, AzureKeyCredential, AsyncTokenCredential) – 要使用的凭据。必填
model_info (ModelInfo) – 模型的模型家族和能力。必填。
model (str) – 模型的名称。如果模型托管在 GitHub Models 上,则必填。
frequency_penalty – (可选,浮点数)
presence_penalty – (可选,浮点数)
temperature – (可选,浮点数)
top_p – (可选,浮点数)
max_tokens – (可选,整数)
response_format – (可选,字面量 ["text", "json_object"])
stop – (可选,字符串列表)
tools – (可选,ChatCompletionsToolDefinition 列表)
tool_choice – (可选,字符串、ChatCompletionsToolChoicePreset 或 ChatCompletionsNamedToolChoice 的联合)
seed – (可选,整数)
model_extras – (可选,字符串到任意类型的字典)
要使用此客户端,您必须安装 azure 额外功能
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.github.ai/inference", # 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.github.ai/inference", # 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())
- add_usage(usage: RequestUsage) None [source]#
- async create(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = [], tool_choice: Tool | Literal['auto', 'required', 'none'] = 'auto', 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], 可选) – 与模型一起使用的工具。默认为 []。
tool_choice (Tool | Literal["auto", "required", "none"], 可选) – 单个 Tool 对象,用于强制模型使用,"auto" 允许模型选择任何可用工具,"required" 强制使用工具,"none" 禁用工具使用。默认为 "auto"。
json_output (Optional[bool | type[BaseModel]], 可选) – 是否使用 JSON 模式、结构化输出或两者都不使用。默认为 None。如果设置为 Pydantic BaseModel 类型,它将用作结构化输出的输出类型。如果设置为布尔值,它将用于确定是否使用 JSON 模式。如果设置为 True,请确保在指令或提示中指示模型生成 JSON 输出。
extra_create_args (Mapping[str, Any], 可选) – 传递给底层客户端的额外参数。默认为 {}。
cancellation_token (Optional[CancellationToken], 可选) – 用于取消的令牌。默认为 None。
- 返回:
CreateResult – 模型调用的结果。
- async create_stream(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = [], tool_choice: Tool | Literal['auto', 'required', 'none'] = 'auto', 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], 可选) – 与模型一起使用的工具。默认为 []。
tool_choice (Tool | Literal["auto", "required", "none"], 可选) – 单个 Tool 对象,用于强制模型使用,"auto" 允许模型选择任何可用工具,"required" 强制使用工具,"none" 禁用工具使用。默认为 "auto"。
json_output (Optional[bool | type[BaseModel]], 可选) –
是否使用 JSON 模式、结构化输出或两者都不使用。默认为 None。如果设置为 Pydantic BaseModel 类型,它将用作结构化输出的输出类型。如果设置为布尔值,它将用于确定是否使用 JSON 模式。如果设置为 True,请确保在指令或提示中指示模型生成 JSON 输出。
extra_create_args (Mapping[str, Any], 可选) – 传递给底层客户端的额外参数。默认为 {}。
cancellation_token (Optional[CancellationToken], 可选) – 用于取消的令牌。默认为 None。
- 返回:
AsyncGenerator[Union[str, CreateResult], None] – 生成器,产生字符串块并以
CreateResult
结束。
- actual_usage() RequestUsage [source]#
- total_usage() RequestUsage [source]#
- count_tokens(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = []) int [source]#
- remaining_tokens(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = []) int [source]#