{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 快速入门\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "通过AgentChat,您可以使用预设代理快速构建应用程序。\n为了说明这一点,我们将从创建一个能够使用工具的单代理开始。\n\n首先,我们需要安装AgentChat和扩展包。\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "shellscript" } }, "outputs": [], "source": [ "pip install -U \"autogen-agentchat\" \"autogen-ext[openai,azure]\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "本示例使用OpenAI模型,但您也可以使用其他模型。\n只需将`model_client`更新为所需的模型或模型客户端类即可。\n\n要使用Azure OpenAI模型和AAD身份验证,\n您可以按照[此处](./tutorial/models.ipynb#azure-openai)的说明操作。\n要使用其他模型,请参阅[模型](./tutorial/models.ipynb)。\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "---------- user ----------\n", "What is the weather in New York?\n", "---------- weather_agent ----------\n", "[FunctionCall(id='call_bE5CYAwB7OlOdNAyPjwOkej1', arguments='{\"city\":\"New York\"}', name='get_weather')]\n", "---------- weather_agent ----------\n", "[FunctionExecutionResult(content='The weather in New York is 73 degrees and Sunny.', call_id='call_bE5CYAwB7OlOdNAyPjwOkej1', is_error=False)]\n", "---------- weather_agent ----------\n", "The current weather in New York is 73 degrees and sunny.\n" ] } ], "source": [ "from autogen_agentchat.agents import AssistantAgent\n", "from autogen_agentchat.ui import Console\n", "from autogen_ext.models.openai import OpenAIChatCompletionClient\n", "\n", "# 定义一个模型客户端。\n", "# 您可以使用其他实现了`ChatCompletionClient`接口的模型客户端。\n", "model_client = OpenAIChatCompletionClient(\n", " model=\"gpt-4o\",\n", " # api_key=\"YOUR_API_KEY\",\n", ")\n", "\n", "\n", "# 定义一个简单的函数工具供代理使用。本例中,\n", "# 我们使用一个模拟天气工具进行演示。\n", "async def get_weather(city: str) -> str:\n", " \"\"\"Get the weather for a given city.\"\"\"\n", " return f\"The weather in {city} is 73 degrees and Sunny.\"\n", "\n", "\n", "# 定义一个AssistantAgent,配置模型、工具、\n", "# 系统消息并启用反思功能。系统消息通过自然语言指导代理行为。\n", "agent = AssistantAgent(\n", " name=\"weather_agent\",\n", " model_client=model_client,\n", " tools=[get_weather],\n", " system_message=\"You are a helpful assistant.\",\n", " reflect_on_tool_use=True,\n", " model_client_stream=True, # 启用从模型客户端流式传输令牌的功能。\n", ")\n", "\n", "\n", "# 运行代理并将消息流式传输到控制台。\n", "async def main() -> None:\n", " await Console(agent.run_stream(task=\"What is the weather in New York?\"))\n", " # 关闭与模型客户端的连接。\n", " await model_client.close()\n", "\n", "\n", "# 注意:如果在 Python 脚本中运行此代码,需要使用 asyncio.run(main())。\n", "await main()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 后续步骤\n\n现在您已经掌握了单个代理的基本使用方法,建议继续学习[教程](./tutorial/index.md)来了解 AgentChat 的其他功能特性。\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.7" } }, "nbformat": 4, "nbformat_minor": 2 }