【智能体agent】入门之--2.2框架---autoGen
every blog every motto: You can do more than you think.
https://blog.csdn.net/weixin_39190382?type=blog
0. 前言
智能体框架之semantic_kernal
1. 正文
import os
from dotenv import load_dotenvfrom autogen_agentchat.agents import AssistantAgent
from autogen_core.models import UserMessage
from autogen_ext.models.azure import AzureAIChatCompletionClient
from azure.core.credentials import AzureKeyCredential
from autogen_core import CancellationTokenfrom autogen_agentchat.messages import TextMessage
from autogen_agentchat.ui import Console
创建client
load_dotenv()
client = AzureAIChatCompletionClient(model="gpt-4o-mini",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://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokenscredential=AzureKeyCredential(os.getenv("GITHUB_TOKEN")),model_info={"json_output": True,"function_calling": True,"vision": True,"family": "unknown",},
)# result = await client.create([UserMessage(content="What is the capital of France?", source="user")])
result = await client.create([UserMessage(content="中国出生人口变化", source="user")])
print(result)
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0000024E2E3211C0>finish_reason='stop' content='中国的出生人口在近几年经历了显著变化。自20世纪80年代以来,中国实施了严格的计划生育政策,导致出生人口逐年下降。尽管在2015年放宽了一胎政策,允许夫妻生育两个孩子,但出生人数仍未显著回升。\n\n根据国家统计局的数据,2020年中国出生人口为1062万人,2021年下降至1000万以下,2022年进一步减少至约900万人。这个趋势引发了关于人口老龄化和劳动力 shortages 的广泛关注。\n\n为了应对低出生率,政府开始推出一系列政策,鼓励生育,包括改善育儿支持、提供经济补助以及推动家庭友好的工作环境等。然而,许多年轻人面临着经济压力、住房负担和职业竞争等问题,这些因素使得他们推迟结婚和生育。\n\n总体来看,中国的出生人口变化不仅反映了政策的影响,也受到社会经济环境、文化观念和个体选择等多方面因素的影响。' usage=RequestUsage(prompt_tokens=11, completion_tokens=225) cached=False logprobs=None thought=None
定义agent
既然我们已经设置好了client并确认其运行正常,接下来让我们创建一个AssistantAgent。每个智能体可配置以下属性:
name: - 简短的标识名,便于在多智能体协作流程中调用
client: - 之前步骤中创建的客户端实例
tools: - 该智能体可调用以完成任务的功能工具
system_message: - 定义大语言模型任务目标、行为规范和语调风格的元提示词
agent = AssistantAgent(name="assistant",model_client=client,tools=[],system_message="You are a travel agent that plans great vacations",
)
运行
from IPython.display import display, HTMLasync def assistant_run():# Define the queryuser_query = "Plan me a great sunny vacation"# Start building HTML outputhtml_output = "<div style='margin-bottom:10px'>"html_output += "<div style='font-weight:bold'>User:</div>"html_output += f"<div style='margin-left:20px'>{user_query}</div>"html_output += "</div>"# Execute the agent responseresponse = await agent.on_messages([TextMessage(content=user_query, source="user")],cancellation_token=CancellationToken(),)# Add agent response to HTMLhtml_output += "<div style='margin-bottom:20px'>"html_output += "<div style='font-weight:bold'>Assistant:</div>"html_output += f"<div style='margin-left:20px; white-space:pre-wrap'>{response.chat_message.content}</div>"html_output += "</div>"# Display formatted HTMLdisplay(HTML(html_output))# Run the function
await assistant_run()
User:
定制一个一日游
Assistant:
当然可以!请您提供一些信息,以便我为您定制一个完美的一日游计划:
- 地点:您希望在哪里进行一日游?(城市或景点)
- 活动偏好:您对哪些活动感兴趣?例如,观光、购物、自然徒步、美食、文化体验等。
- 出行方式:您打算使用什么交通工具?(自己的车、公共交通,等等)
- 旅行伴侣:您是独自旅行还是与家人或朋友一起?
- 饮食偏好:是否有特别想尝试的餐厅或美食?
提供这些信息后,我将为您制定一个详细的一日游行程!