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

编译:幂简集成

相关文章
|
3天前
|
API Python
[AIGC] 使用Python刷LeetCode:常用API及技巧指南
[AIGC] 使用Python刷LeetCode:常用API及技巧指南
|
4天前
|
测试技术 API 网络架构
Python的api自动化测试 编写测试用例
【4月更文挑战第18天】使用Python进行API自动化测试,可以结合`requests`库发送HTTP请求和`unittest`(或`pytest`)编写测试用例。以下示例: 1. 安装必要库:`pip install requests unittest` 2. 创建`test_api.py`,导入库,定义基础URL。 3. 创建继承自`unittest.TestCase`的测试类,包含`setUp`和`tearDown`方法。 4. 编写测试用例,如`test_get_users`,检查响应状态码和内容。 5. 运行测试:`python -m unittest test_api.py`
12 2
|
4天前
|
JSON 测试技术 API
Python的Api自动化测试使用HTTP客户端库发送请求
【4月更文挑战第18天】在Python中进行HTTP请求和API自动化测试有多个库可选:1) `requests`是最流行的选择,支持多种请求方法和内置JSON解析;2) `http.client`是标准库的一部分,适合需要低级别控制的用户;3) `urllib`提供URL操作,适用于复杂请求;4) `httpx`拥有类似`requests`的API,提供现代特性和异步支持。根据具体需求选择,如多数情况`requests`已足够。
9 3
|
4天前
|
人工智能 机器人 API
【Python+微信】【企业微信开发入坑指北】3. 如何利用企业微信API给微信群推送消息
【Python+微信】【企业微信开发入坑指北】3. 如何利用企业微信API给微信群推送消息
8 0
|
4天前
|
缓存 人工智能 API
【Python+微信】【企业微信开发入坑指北】2. 如何利用企业微信API主动给用户发应用消息
【Python+微信】【企业微信开发入坑指北】2. 如何利用企业微信API主动给用户发应用消息
9 0
|
2月前
|
人工智能 vr&ar
OpenAI发布Sora,引领多模态大模型再突破
OpenAI发布Sora,引领多模态大模型再突破
167 4
OpenAI发布Sora,引领多模态大模型再突破
|
2月前
|
人工智能 编解码 自然语言处理
OpenAI 全新发布文生视频模型 Sora,支持 60s 超长长度,有哪些突破?将带来哪些影响?
OpenAI 全新发布文生视频模型 Sora,支持 60s 超长长度,有哪些突破?将带来哪些影响?
|
1月前
|
人工智能 自然语言处理 前端开发
王者Claude 3大模型!!!OpenAI竞争对手Anthropic推出Claude 3大模型,各项性能全面碾压GPT-4!
王者Claude 3大模型!!!OpenAI竞争对手Anthropic推出Claude 3大模型,各项性能全面碾压GPT-4!
29 0
|
1月前
|
机器学习/深度学习 人工智能 自然语言处理
Sora是什么?Sora如何使用?带你快速了解OpenAI发布的Sora大模型
OpenAI自2015年成立以来,一直是人工智能领域的佼佼者。他们在深度学习和自然语言处理等多个方面取得了显著进展,GPT-4的推出更是巩固了他们在技术创新和应用开发上的领导地位。OpenAI的目标不仅仅是技术突破,更重要的是推动AI技术的安全和伦理发展,以造福人类社会。
Sora是什么?Sora如何使用?带你快速了解OpenAI发布的Sora大模型
|
1月前
|
机器学习/深度学习 人工智能 自然语言处理
什么是 OpenAI 的 Dall-E 模型
什么是 OpenAI 的 Dall-E 模型
29 0