当前位置: 首页 > news >正文

【智能体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:
当然可以!请您提供一些信息,以便我为您定制一个完美的一日游计划:
  1. 地点:您希望在哪里进行一日游?(城市或景点)
  2. 活动偏好:您对哪些活动感兴趣?例如,观光、购物、自然徒步、美食、文化体验等。
  3. 出行方式:您打算使用什么交通工具?(自己的车、公共交通,等等)
  4. 旅行伴侣:您是独自旅行还是与家人或朋友一起?
  5. 饮食偏好:是否有特别想尝试的餐厅或美食?

提供这些信息后,我将为您制定一个详细的一日游行程!

http://www.lryc.cn/news/606344.html

相关文章:

  • Cesium 快速入门(一)快速搭建项目
  • 【05】大恒相机SDK C#开发 —— Winform中采集图像并显示
  • 提示词增强工程(Prompt Enhancement Engineering)白皮书草稿
  • 【大模型理论篇】混合思考之自适应思维链
  • uv使用教程
  • FastMCP本地构建Server和Clinet交互
  • 用Python绘制SM2国密算法椭圆曲线:一场数学与视觉的盛宴
  • 时间戳 + 签名机制
  • 学习日志23 python
  • 因为想开发新项目了~~要给老Python项目整个虚拟环境
  • HTML基础复习:全面回顾核心概念
  • 谷歌V3插件热更新
  • 【0基础PS】Photoshop (PS) 理论知识
  • 【刷题】东方博宜oj 1412-快速幂(零基础,简单易懂)
  • Mysql-视图,函数,存储过程,触发器
  • 【Kiro Code】Chat 聊天功能
  • 某讯视频风控参数逆向分析
  • Docker部署的PostgreSQL慢查询日志配置指南
  • pytorch的自定义 CUDA 扩展怎么学习
  • pytorch程序语句固定开销分析
  • 排序算法-选择排序(选择排序、堆排序)(动图演示)
  • Next实习项目总结串联讲解(一)
  • 基于京东评论的文本挖掘与分析,使用LSTM情感分析算法以及网络语义分析
  • 正则化都是放在模型的哪个位置呢?
  • 案例开发 - 日程管理 - 第四期
  • 【C语言学习】scanf函数
  • 【源力觉醒 创作者计划】文心一言与deepseek集成springboot开发哪个更方便
  • 3.Linux 系统文件类型与文件权限
  • AI与AGI:从狭义智能到通用智能
  • 上证50期权2400是什么意思?