如何使用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

编译:幂简集成

相关文章
|
搜索推荐 API 开发工具
百宝箱开放平台 ✖️ Python SDK
百宝箱提供Python SDK,支持开发者集成其开放能力。需先发布应用,安装Python 3.6+环境后,通过pip安装tboxsdk,即可调用对话型、生成型智能体及文件上传等功能。
1233 87
百宝箱开放平台 ✖️  Python SDK
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
安全 Java API
使用 Java 构建强大的 REST API 的四个基本技巧
本文结合探险领域案例,分享Java构建REST API的四大核心策略:统一资源命名、版本控制与自动化文档、安全防护及标准化异常处理,助力开发者打造易用、可维护、安全可靠的稳健API服务。
735 116
|
缓存 监控 供应链
唯品会自定义 API 自定义操作深度分析及 Python 实现
唯品会开放平台提供丰富API,支持商品查询、订单管理、促销活动等电商全流程操作。基于OAuth 2.0认证机制,具备安全稳定的特点。通过组合调用基础接口,可实现数据聚合、流程自动化、监控预警及跨平台集成,广泛应用于供应链管理、数据分析和智能采购等领域。结合Python实现方案,可高效完成商品搜索、订单分析、库存监控等功能,提升电商运营效率。
|
人工智能 API 开发工具
构建AI智能体:一、初识AI大模型与API调用
本文介绍大模型基础知识及API调用方法,涵盖阿里云百炼平台密钥申请、DashScope SDK使用、Python调用示例(如文本情感分析、图像文字识别),助力开发者快速上手大模型应用开发。
3816 18
构建AI智能体:一、初识AI大模型与API调用
|
Cloud Native 算法 API
Python API接口实战指南:从入门到精通
🌟蒋星熠Jaxonic,技术宇宙的星际旅人。深耕API开发,以Python为舟,探索RESTful、GraphQL等接口奥秘。擅长requests、aiohttp实战,专注性能优化与架构设计,用代码连接万物,谱写极客诗篇。
2268 1
Python API接口实战指南:从入门到精通
|
JSON API 数据安全/隐私保护
Python采集淘宝评论API接口及JSON数据返回全流程指南
Python采集淘宝评论API接口及JSON数据返回全流程指南
|
开发工具 Android开发
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
1301 11
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
|
JavaScript 前端开发 Java
[Android][Framework]系统jar包,sdk的制作及引用
[Android][Framework]系统jar包,sdk的制作及引用
766 0

推荐镜像

更多