如何使用Python SDK与OpenAI Assistants API构建助手?

简介: 在这篇文章中,我们将介绍如何使用Python SDK构建最基本的Assistant,你只需要在代码中添加你自己的OpenAI API密钥即可。

介绍

OpenAI和其Assistant功能旨在为制造商提供一个SDK,用于开发有状态、无提示的Assistant。

其目标是简化虚拟助理的创建。目前,Assistant可以使用三种类型的工具:函数、RAG和代码解释器。

在这篇文章中,我们将介绍如何使用Python SDK构建最基本的Assistant你只需要在代码中添加你自己的OpenAI API密钥即可。

一些注意事项

OpenAI推出的Assistant能力适用于实验、探索和作为短期的解决方案,也可以充当更大的自治代理实例的扩展。然而,为了实现高度可扩展、可检查和可观察的目标,需要更加详细的方法论。

基于安全的角度,一个项目组织不会选择在OpenAI的环境中管理和存储对话记录,而是更更倾向于隐私性强的大语言模型(LLM)。此外,OpenAI Assistant的无状态特性要求开发者手动管理会话状态、工具定义、文档检索和代码执行,这同样造成了一些使用阻碍。

解决上述的挑战,或许可以通过实现LLM和框架的独立性,摆脱对特定框架的依赖。

如果将对话记录管理交给OpenAI,虽然减轻了开发者的管理负担,但每次运行都会被收取整个对话历史记录的token费用,这就给框架内的token使用带来了一定程度的不透明性,容易引起误解。

与Chat Completions API中的完成不同,创建Run是一个异步操作。要想得知Assistant何时完成处理,需要在循环中轮询运行,尽管这种方法会增加复杂性和支出,但优势在于运行时可以查询状态值,可用于管理会话和通知用户。

完整的OpenAI Assistant开发流程

接下来,我们将详细展示使用Python SDK构建Assistant的过程:

!pip install — upgrade openai
!pip show openai | grep Version

AssistantAPI的Python SDK要求OpenAI版本>1.2.3。

Version: 1.3.7

定义API密钥。

import json
import os
def show_json(obj):
    display(json.loads(obj.model_dump_json()))
os.environ['OPENAI_API_KEY'] = str("Your OpenAI API Key goes here.")

代理已创建完成。

# You can also create Assistants directly through the Assistants API
from openai import OpenAI
client = OpenAI()
assistant = client.beta.assistants.create(
    name="History Tutor",
    instructions="You are a personal history tutor. Answer questions briefly, in three sentence or less.",
    model="gpt-4-1106-preview",
)
show_json(assistant)

在输出的JSON中。创建代理后,您将看到ID、型号、Assistant命名和其他详细信息。

{'id': 'asst_qlaTYRSyl9EWeftjKSskdaco',
 'created_at': 1702009585,
 'description': None,
 'file_ids': [],
 'instructions': 'You are a personal history tutor. Answer questions briefly, in three sentence or less.',
 'metadata': {},
 'model': 'gpt-4-1106-preview',
 'name': 'History Tutor',
 'object': 'assistant',
 'tools': []
 }

一旦创建了Assistant,就可以通过OpenAI仪表板看到它,并显示其名称、说明和ID。

无论您是通过仪表板还是使用API创建Assistant,都需要跟踪AssistantI D。

image.png

首先创建线程。

# Creating a new thread:
thread = client.beta.threads.create()
show_json(thread)

下面是输出,包括线程ID等。

{'id': 'thread_1flknQB4C8KH4BDYPWsyl0no',
 'created_at': 1702009588,
 'metadata': {},
 'object': 'thread'}

在这里,一条消息被添加到线程中。

# Now we add a message to the thread:
message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="What year was the USA founded?",
)
show_json(message)

结果如下。

在这里,您需要注意的是,即使每次都没有发送会话历史记录,您仍要为每次运行整个会话历史记录支付token费用。

{'id': 'msg_5xOq4FV38cS98ohBpQPbpUiE',
 'assistant_id': None,
 'content': [{'text': {'annotations': [],
    'value': 'What year was the USA founded?'},
   'type': 'text'}],
 'created_at': 1702009591,
 'file_ids': [],
 'metadata': {},
 'object': 'thread.message',
 'role': 'user',
 'run_id': None,
 'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no'}

当前面提到定义run时,必须同时指定Assistant和Thread。

run = client.beta.threads.runs.create(
    thread_id=thread.id,
    assistant_id=assistant.id,
)
show_json(run)

再次输出:

{'id': 'run_PnwSECkqDDdjWkQ5P7Hcyfor',
 'assistant_id': 'asst_qlaTYRSyl9EWeftjKSskdaco',
 'cancelled_at': None,
 'completed_at': None,
 'created_at': 1702009598,
 'expires_at': 1702010198,
 'failed_at': None,
 'file_ids': [],
 'instructions': 'You are a personal history tutor. Answer questions briefly, in three sentence or less.',
 'last_error': None,
 'metadata': {},
 'model': 'gpt-4-1106-preview',
 'object': 'thread.run',
 'required_action': None,
 'started_at': None,
 'status': 'queued',
 'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no',
 'tools': []}

与Chat Completions API中的完成不同,创建Run是一个异步操作。它将立即返回运行元数据,其中包括一个 status初始设置为 queued该值将随着Assistant执行操作而更新。

下面的循环检查while循环中的运行状态,直到运行状态达到完整状态。

import time
def wait_on_run(run, thread):
    while run.status == "queued" or run.status == "in_progress":
        run = client.beta.threads.runs.retrieve(
            thread_id=thread.id,
            run_id=run.id,
        )
        time.sleep(0.5)
    return run
run = wait_on_run(run, thread)
show_json(run)

低于运行结果。

{'id': 'run_PnwSECkqDDdjWkQ5P7Hcyfor',
 'assistant_id': 'asst_qlaTYRSyl9EWeftjKSskdaco',
 'cancelled_at': None,
 'completed_at': 1702009605,
 'created_at': 1702009598,
 'expires_at': None,
 'failed_at': None,
 'file_ids': [],
 'instructions': 'You are a personal history tutor. Answer questions briefly, in three sentence or less.',
 'last_error': None,
 'metadata': {},
 'model': 'gpt-4-1106-preview',
 'object': 'thread.run',
 'required_action': None,
 'started_at': 1702009598,
 'status': 'completed',
 'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no',
 'tools': []}

一旦运行完成,我们就可以列出线程中的所有消息。

# Now that the Run has completed, list the Messages in the Thread to 
# see what got added by the Assistant. 
messages = client.beta.threads.messages.list(thread_id=thread.id)
show_json(messages)

再次输出如下…

{'data': [{'id': 'msg_WhzkHcPnszsmbdrn0H5Ugl7I',
   'assistant_id': 'asst_qlaTYRSyl9EWeftjKSskdaco',
   'content': [{'text': {'annotations': [],
      'value': 'The United States of America was founded in 1776, with the adoption of the Declaration of Independence on July 4th of that year.'},
     'type': 'text'}],
   'created_at': 1702009604,
   'file_ids': [],
   'metadata': {},
   'object': 'thread.message',
   'role': 'assistant',
   'run_id': 'run_PnwSECkqDDdjWkQ5P7Hcyfor',
   'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no'},
  {'id': 'msg_5xOq4FV38cS98ohBpQPbpUiE',
   'assistant_id': None,
   'content': [{'text': {'annotations': [],
      'value': 'What year was the USA founded?'},
     'type': 'text'}],
   'created_at': 1702009591,
   'file_ids': [],
   'metadata': {},
   'object': 'thread.message',
   'role': 'user',
   'run_id': None,
   'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no'}],
 'object': 'list',
 'first_id': 'msg_WhzkHcPnszsmbdrn0H5Ugl7I',
 'last_id': 'msg_5xOq4FV38cS98ohBpQPbpUiE',
 'has_more': False}

一条消息被附加到线程…

# Create a message to append to our thread
message = client.beta.threads.messages.create(
    thread_id=thread.id, role="user", content="Could you give me a little more detail on this?"
)
# Execute our run
run = client.beta.threads.runs.create(
    thread_id=thread.id,
    assistant_id=assistant.id,
)
# Wait for completion
wait_on_run(run, thread)
# Retrieve all the messages added after our last user message
messages = client.beta.threads.messages.list(
    thread_id=thread.id, order="asc", after=message.id
)
show_json(messages)

根据结果,考虑内容价值…

{'data': [{'id': 'msg_oIOfuARjk20zZRn6lAytf0Hz',
   'assistant_id': 'asst_qlaTYRSyl9EWeftjKSskdaco',
   'content': [{'text': {'annotations': [],
      'value': 'Certainly! The founding of the USA is marked by the Declaration of Independence, which was ratified by the Continental Congress on July 4, 1776. This act declared the thirteen American colonies free and independent states, breaking away from British rule.'},
     'type': 'text'}],
   'created_at': 1702009645,
   'file_ids': [],
   'metadata': {},
   'object': 'thread.message',
   'role': 'assistant',
   'run_id': 'run_9dWR1QFrN983q1AG1cjcQ9Le',
   'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no'}],
 'object': 'list',
 'first_id': 'msg_oIOfuARjk20zZRn6lAytf0Hz',
 'last_id': 'msg_oIOfuARjk20zZRn6lAytf0Hz',
 'has_more': False}

运行完成后,可以在线程中列出消息。

# Now that the Run has completed, list the Messages in the Thread to see 
# what got added by the Assistant.
messages = client.beta.threads.messages.list(thread_id=thread.id)
show_json(messages)

结果再次出现。

{'data': [{'id': 'msg_oIOfuARjk20zZRn6lAytf0Hz',
   'assistant_id': 'asst_qlaTYRSyl9EWeftjKSskdaco',
   'content': [{'text': {'annotations': [],
      'value': 'Certainly! The founding of the USA is marked by the Declaration of Independence, which was ratified by the Continental Congress on July 4, 1776. This act declared the thirteen American colonies free and independent states, breaking away from British rule.'},
     'type': 'text'}],
   'created_at': 1702009645,
   'file_ids': [],
   'metadata': {},
   'object': 'thread.message',
   'role': 'assistant',
   'run_id': 'run_9dWR1QFrN983q1AG1cjcQ9Le',
   'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no'},
  {'id': 'msg_dDeGGSj4w3CIVRd5hsQpGHmF',
   'assistant_id': None,
   'content': [{'text': {'annotations': [],
      'value': 'Could you give me a little more detail on this?'},
     'type': 'text'}],
   'created_at': 1702009643,
   'file_ids': [],
   'metadata': {},
   'object': 'thread.message',
   'role': 'user',
   'run_id': None,
   'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no'},
  {'id': 'msg_WhzkHcPnszsmbdrn0H5Ugl7I',
   'assistant_id': 'asst_qlaTYRSyl9EWeftjKSskdaco',
   'content': [{'text': {'annotations': [],
      'value': 'The United States of America was founded in 1776, with the adoption of the Declaration of Independence on July 4th of that year.'},
     'type': 'text'}],
   'created_at': 1702009604,
   'file_ids': [],
   'metadata': {},
   'object': 'thread.message',
   'role': 'assistant',
   'run_id': 'run_PnwSECkqDDdjWkQ5P7Hcyfor',
   'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no'},
  {'id': 'msg_5xOq4FV38cS98ohBpQPbpUiE',
   'assistant_id': None,
   'content': [{'text': {'annotations': [],
      'value': 'What year was the USA founded?'},
     'type': 'text'}],
   'created_at': 1702009591,
   'file_ids': [],
   'metadata': {},
   'object': 'thread.message',
   'role': 'user',
   'run_id': None,
   'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no'}],
 'object': 'list',
 'first_id': 'msg_oIOfuARjk20zZRn6lAytf0Hz',
 'last_id': 'msg_5xOq4FV38cS98ohBpQPbpUiE',
 'has_more': False}


原文链接:https://cobusgreyling.medium.com/openai-assistants-api-python-sdk-abce8518b645

编译:幂简集成

相关文章
|
7月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
7月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
7月前
|
人工智能 API 开发工具
构建AI智能体:一、初识AI大模型与API调用
本文介绍大模型基础知识及API调用方法,涵盖阿里云百炼平台密钥申请、DashScope SDK使用、Python调用示例(如文本情感分析、图像文字识别),助力开发者快速上手大模型应用开发。
2680 18
构建AI智能体:一、初识AI大模型与API调用
|
7月前
|
Cloud Native 算法 API
Python API接口实战指南:从入门到精通
🌟蒋星熠Jaxonic,技术宇宙的星际旅人。深耕API开发,以Python为舟,探索RESTful、GraphQL等接口奥秘。擅长requests、aiohttp实战,专注性能优化与架构设计,用代码连接万物,谱写极客诗篇。
1495 1
Python API接口实战指南:从入门到精通
|
8月前
|
JSON API 数据安全/隐私保护
Python采集淘宝评论API接口及JSON数据返回全流程指南
Python采集淘宝评论API接口及JSON数据返回全流程指南
|
7月前
|
缓存 监控 前端开发
顺企网 API 开发实战:搜索 / 详情接口从 0 到 1 落地(附 Elasticsearch 优化 + 错误速查)
企业API开发常陷参数、缓存、错误处理三大坑?本指南拆解顺企网双接口全流程,涵盖搜索优化、签名验证、限流应对,附可复用代码与错误速查表,助你2小时高效搞定开发,提升响应速度与稳定性。
|
8月前
|
数据可视化 测试技术 API
从接口性能到稳定性:这些API调试工具,让你的开发过程事半功倍
在软件开发中,接口调试与测试对接口性能、稳定性、准确性及团队协作至关重要。随着开发节奏加快,传统方式已难满足需求,专业API工具成为首选。本文介绍了Apifox、Postman、YApi、SoapUI、JMeter、Swagger等主流工具,对比其功能与适用场景,并推荐Apifox作为集成度高、支持中文、可视化强的一体化解决方案,助力提升API开发与测试效率。
|
8月前
|
JSON 前端开发 API
如何调用体育数据足篮接口API
本文介绍如何调用体育数据API:首先选择可靠服务商并注册获取密钥,接着阅读文档了解基础URL、端点、参数及请求头,然后使用Python等语言发送请求、解析JSON数据,最后将数据应用于Web、App或分析场景,同时注意密钥安全、速率限制与错误处理。
830 152
|
9月前
|
JSON 算法 安全
淘宝商品详情API接口系列,json数据返回
淘宝开放平台提供了多种API接口用于获取商品详情信息,主要通过 淘宝开放平台(Taobao Open Platform, TOP) 的 taobao.tbk.item.info.get(淘宝客商品详情)或 taobao.item.get(标准商品API)等接口实现。以下是关键信息及JSON返回示例:

热门文章

最新文章

推荐镜像

更多