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

【智能体agent】入门之--1.初体验

every blog every motto: You can do more than you think.
https://blog.csdn.net/weixin_39190382?type=blog

0. 前言

agent初体验,使用semantic_kernel

1. 正文

import os 
from typing import Annotated
from openai import AsyncOpenAIfrom dotenv import load_dotenvfrom semantic_kernel.agents import ChatCompletionAgent, ChatHistoryAgentThread
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.functions import kernel_function

创建客户端

import random
from typing import Annotated
# from semantic_kernel import kernel_function# Define a sample plugin for the sampleclass DestinationsPlugin:"""A List of Random Destinations for a vacation."""def __init__(self):# List of vacation destinationsself.destinations = ["Barcelona, Spain","Paris, France","Berlin, Germany","Tokyo, Japan","Sydney, Australia","New York, USA","Cairo, Egypt","Cape Town, South Africa","Rio de Janeiro, Brazil","Bali, Indonesia"]# Track last destination to avoid repeatsself.last_destination = None@kernel_function(description="Provides a random vacation destination.")def get_random_destination(self) -> Annotated[str, "Returns a random vacation destination."]:# Get available destinations (excluding last one if possible)available_destinations = self.destinations.copy()if self.last_destination and len(available_destinations) > 1:available_destinations.remove(self.last_destination)# Select a random destinationdestination = random.choice(available_destinations)# Update the last destinationself.last_destination = destinationreturn destination
load_dotenv()
client = AsyncOpenAI(# api_key=os.environ.get("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"), api_key="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", base_url="https://models.inference.ai.azure.com/",
)# Create an AI Service that will be used by the `ChatCompletionAgent`
chat_completion_service = OpenAIChatCompletion(ai_model_id="gpt-4o-mini",async_client=client,
)

创建agent

agent = ChatCompletionAgent(service=chat_completion_service, plugins=[DestinationsPlugin()],name="TravelAgent",instructions="You are a helpful AI Agent that can help plan vacations for customers at random destinations",
)
async def main():# Create a new thread for the agent# If no thread is provided, a new thread will be# created and returned with the initial responsethread: ChatHistoryAgentThread | None = None# "Plan me a day trip.",user_inputs = ["创建一个旅游计划"]for user_input in user_inputs:print(f"# User: {user_input}\n")first_chunk = Trueasync for response in agent.invoke_stream(messages=user_input, thread=thread,):# 5. Print the responseif first_chunk:print(f"# {response.name}: ", end="", flush=True)first_chunk = Falseprint(f"{response}", end="", flush=True)thread = response.threadprint()# Clean up the threadawait thread.delete() if thread else Noneawait main()
# User: 创建一个旅游计划# TravelAgent: 你的旅游计划将会是去美国纽约!以下是一个为期五天的旅游计划建议:### 第一天:抵达纽约
- **上午**: 抵达纽约,前往酒店办理入住。
- **下午**: 游览中央公园,可以租自行车或者步行,享受绿意盎然的环境。
- **晚上**: 前往时代广场,感受繁华的夜景,可以在附近的餐馆用餐。### 第二天:名胜古迹
- **上午**: 参观自由女神像和埃利斯岛,可以提前在线购票以避免排队。
- **下午**: 回到曼哈顿,参观911纪念馆,了解悲惨的历史和英雄的故事。
- **晚上**: 享受一场百老汇的音乐剧,提前预定票。### 第三天:文化日
- **上午**: 参观大都会艺术博物馆,欣赏世界各地的艺术珍品。
- **下午**: 漫步到第五大道,体验购物和时尚。
- **晚上**: 在附近的餐厅品尝意大利或中餐。### 第四天:探索街区
- **上午**: 走访格林尼治村,体验独特的文化和历史。
- **下午**: 前往布鲁克林大桥,走过大桥,享受曼哈顿的美景。
- **晚上**: 在布鲁克林的餐厅用餐,体验当地美食。### 第五天:最后的时光
- **上午**: 参观联合国总部,了解其运作和历史。
- **下午**: 前往洛克菲勒中心,爬上观景台,俯瞰纽约全景。
- **晚上**: 准备离开,结束愉快的纽约之行。希望你会喜欢这个计划,祝旅途愉快!如果你需要进一步的帮助或修改,请告诉我。

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

相关文章:

  • OpenCV学习day2
  • RabbitMQ的特点和消息可靠性保障
  • 【neo4j】跨版本升级数据库
  • 《Java 程序设计》第 14 章 - JavaFX 基础
  • MySQL 8.0 OCP 1Z0-908 题目解析(42)
  • 企业级部署 (基于tomcat与nginx)
  • Linux和shell
  • 【运维基础】Linux 文件系统基本管理
  • Side band ECC、Inline ECC、On-die ECC、Link ECC
  • chrome.storage 和 localStorage
  • Android 基础入门学习目录(持续更新)
  • kettle插件-kettle http client plus插件,轻松解决https接口无法调用文件流下载问题
  • 面试笔记【16:9区域问题】
  • SQL注入SQLi-LABS 靶场less25a-28a详细通关攻略
  • ESP32 外设驱动开发指南 (ESP-IDF框架)——GPIO篇:基础配置、外部中断与PWM(LEDC模块)应用
  • 机械学习--逻辑回归
  • 第1章:基础篇——第1节:基础操作与认识界面
  • Vercel 全面介绍与网站部署指南
  • 老旧远程控制管理模块(物联网设备)渗透实战:SNMP泄露+内核提权攻击链深度解析
  • Gold 序列
  • 7月31号打卡
  • nvm安装nodejs后提示No installations recognized
  • 爱普生002墨水与004墨水基本参数及支持机型
  • pyspark使用
  • 火焰图(Flame Graph)深度指南:CPU性能分析与瓶颈定位
  • STM32——HAL 库MDK工程创建
  • 计算机网络知识【推荐!!!】按照OSI七层模型梳理
  • 动手学习深度学习-深度学习知识大纲
  • Spring Boot + MinIO + KKFile:三步搭建企业级文件预览系统
  • SpringBoot3.x入门到精通系列:1.2 开发环境搭建