{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Models\n", "\n", "在许多情况下,代理需要访问模型服务,例如 OpenAI、Azure OpenAI 和本地模型。AgentChat 使用由 [`autogen-ext`](../../core-user-guide/framework/model-clients.ipynb) 包提供的模型客户端。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## OpenAI\n", "\n", "要访问 OpenAI 模型,您需要安装 `openai` 扩展以使用 {py:class}`~autogen_ext.models.OpenAIChatCompletionClient`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "shellscript" } }, "outputs": [], "source": [ "pip install 'autogen-ext[openai]==0.4.0.dev6'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "您还需要从 OpenAI 获取一个[API key](https://platform.openai.com/account/api-keys)。" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "from autogen_ext.models import OpenAIChatCompletionClient\n", "\n", "opneai_model_client = OpenAIChatCompletionClient(\n", " model=\"gpt-4o-latest\",\n", " api_key=\"sk-...\", # Optional if you have an OPENAI_API_KEY environment variable set.\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "要测试模型客户端,您可以使用以下代码:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CreateResult(finish_reason='stop', content='The capital of France is Paris.', usage=RequestUsage(prompt_tokens=15, completion_tokens=7), cached=False, logprobs=None)\n" ] } ], "source": [ "from autogen_core.components.models import UserMessage\n", "\n", "result = await opneai_model_client.create([UserMessage(content=\"What is the capital of France?\", source=\"user\")])\n", "print(result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "您可以将此客户端与托管在 OpenAI 兼容端点上的模型一起使用,但是我们尚未测试此功能。\n", "查看 {py:class}`~autogen_ext.models.OpenAIChatCompletionClient` 了解更多信息。\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Azure OpenAI\n", "\n", "安装 `azure` 和 `openai` 扩展以使用 {py:class}`~autogen_ext.models.AzureOpenAIChatCompletionClient`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "shellscript" } }, "outputs": [], "source": [ "pip install 'autogen-ext[openai,azure]==0.4.0.dev6'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "要使用此客户端,您需要提供部署 ID、Azure 认知服务端点、API 版本和模型功能。\n", "对于身份验证,您可以提供 API 密钥或 Azure Active Directory (AAD) 令牌凭据。\n", "\n", "以下代码片段展示了如何使用 AAD 身份验证。\n", "使用的身份必须被分配 [认知服务 OpenAI 用户](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/role-based-access-control#cognitive-services-openai-user) 角色。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from autogen_ext.models import AzureOpenAIChatCompletionClient\n", "from azure.identity import DefaultAzureCredential, get_bearer_token_provider\n", "\n", "# Create the token provider\n", "token_provider = get_bearer_token_provider(DefaultAzureCredential(), \"https://cognitiveservices.azure.com/.default\")\n", "\n", "az_model_client = AzureOpenAIChatCompletionClient(\n", " model=\"{your-azure-deployment}\",\n", " api_version=\"2024-06-01\",\n", " azure_endpoint=\"https://{your-custom-endpoint}.openai.azure.com/\",\n", " azure_ad_token_provider=token_provider, # Optional if you choose key-based authentication.\n", " api_key=\"sk-...\", # For key-based authentication.\n", " model_capabilities={\n", " \"vision\": True,\n", " \"function_calling\": True,\n", " \"json_output\": True,\n", " },\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "查看 [here](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/managed-identity#chat-completions) 了解如何直接使用 Azure 客户端或更多信息。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 本地模型\n", "\n", "我们正在开发中。敬请期待!" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }