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

openai-agents 安装与测试

目录

    • 安装
    • 第一个例子

安装

  1. 克隆仓库:
git clone https://github.com/openai/openai-agents-python.git
  1. 创建 conda 环境:
conda create --name oi_agents python=3.12 -y
  1. 激活环境并安装依赖:
activate oi_agents && pip install -e .

这三个命令分别用于:

  1. 从 GitHub 下载项目代码
  2. 创建一个 Python 3.12 的 conda 虚拟环境
  3. 激活创建的环境并以开发模式安装项目依赖

请注意在执行这些命令时要确保:

  • 已安装 Git
  • 已安装 Conda
  • 在正确的目录下执行命令

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

第一个例子

# 查看 openai-agents 包的详细信息
pip show openai-agents
# 输出显示包的版本、安装位置、依赖项等信息

# 从 agents 包中导入必要的类
from agents import Agent, Runner, RunConfig, OpenAIProvider

# 创建 AI 助手实例
# - name: 设置助手名称为 "Assistant"
# - instructions: 设置助手的基本指令
# - model: 使用 glm-4-flash 模型
agent = Agent(name="Assistant", instructions="You are a helpful assistant", model="glm-4-flash")

# 配置运行环境
# - api_key: 智谱 AI 的 API 密钥
# - base_url: 智谱 AI 的 API 接口地址
# - use_responses: 禁用 responses 库
run_config = RunConfig(model_provider = OpenAIProvider(
    api_key="your api key",
    base_url="https://open.bigmodel.cn/api/paas/v4/",
    use_responses=False)
)

# 导入并应用 nest_asyncio 来解决 Jupyter 中的异步运行问题
import nest_asyncio
nest_asyncio.apply()

# 同步运行 AI 助手
# - 让助手创作一首关于编程中递归的俳句
# - 使用之前配置的 run_config
result = Runner.run_sync(agent, "Write a haiku about recursion in programming.", run_config=run_config)
# 打印助手生成的俳句
print(result.final_output)

运行结果:

pip show openai-agents
Name: openai-agents
Version: 0.0.3
Summary: OpenAI Agents SDK
Home-page: https://github.com/openai/openai-agents-python
Author: 
Author-email: OpenAI <support@openai.com>
License-Expression: MIT
Location: d:\soft\anaconda\envs\oi_agents\Lib\site-packages
Editable project location: D:\llm\openai-agents-python
Requires: griffe, openai, pydantic, requests, types-requests, typing-extensions
Required-by: 
Note: you may need to restart the kernel to use updated packages.
from agents import Agent, Runner,RunConfig,OpenAIProvider

agent = Agent(name="Assistant", instructions="You are a helpful assistant",model="glm-4-flash")

run_config = RunConfig(model_provider = OpenAIProvider(
    api_key="your api key",
    base_url="https://open.bigmodel.cn/api/paas/v4/",
    use_responses=False)
)

import nest_asyncio
nest_asyncio.apply()

result = Runner.run_sync(agent, "Write a haiku about recursion in programming.",run_config=run_config)
print(result.final_output)
Branches of code weave,
Echoing loops in endless dance,
Logic's intricate maze.

相关文章:

  • 信息学奥赛c++语言:整数去重
  • 3-001:MySQL 中的回表是什么?
  • 中国软件供应链安全技术指南|DevSecOps敏捷安全技术金字塔V3.0正式发布
  • MQ消息发送不在MySQL事务中,该如何保证一致性?
  • 蓝桥 2109统计子矩阵
  • Ubuntu22.04安装数据
  • 5.1 程序调试
  • 什么是双机热备系统?双机热备现在是否已经过时了?
  • 职坐标C语言数据结构算法核心精讲
  • Deep research深度研究:ChatGPT/ Gemini/ Perplexity/ Grok哪家最强?(实测对比分析)
  • 内容中台的实施基石是什么?
  • vue2双向绑定解析
  • 单片机设计暖脚器研究
  • 投资晚报 3.12
  • 【论文笔记】FLARE:feed-forward+posegeometry estimate+GS
  • 调优案例一:堆空间扩容提升吞吐量实战记录
  • 适合二次开发的Web组态软件推荐
  • 子母钟系统,京准电子科技助力高考精准计时
  • 机器学习常见激活函数
  • Vitis IDE 艰难切换--从传统 Vitis GUI 到 2024.1 统一软件界面
  • 德国斯图加特发生车辆冲撞人群事件,至少三人受伤
  • 跳水世界杯总决赛:程子龙/朱子锋夺男子双人10米台冠军
  • 今年五一假期出游人群规模预计比去年提升8%,哪里最热门?
  • 金砖国家外长会晤落幕,外交部:发出了反对单边霸凌行径的“金砖声音”
  • 吴志朴当选福建德化县人民政府县长
  • 俄罗斯纪念卫国战争胜利80周年阅兵式首次彩排在莫斯科举行