!pip show openai | grep Version

Assistant API的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.")

代理已創(chuàng)建完成。

# 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中。創(chuàng)建代理后,您將看到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': []
}

一旦創(chuàng)建了Assistant,就可以通過OpenAI儀表板看到它,并顯示其名稱、說明和ID。

無論您是通過儀表板還是使用API創(chuàng)建Assistant,都需要跟蹤AssistantI D。

首先創(chuàng)建線程。

# 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)

結(jié)果如下。

在這里,您需要注意的是,即使每次都沒有發(fā)送會話歷史記錄,您仍要為每次運行整個會話歷史記錄支付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'}

當(dāng)前面提到定義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中的完成不同,創(chuàng)建Run是一個異步操作。它將立即返回運行元數(shù)據(jù),其中包括一個 status ,初始設(shè)置為 queued 。該值將隨著Assistant執(zhí)行操作而更新。

下面的循環(huán)檢查while循環(huán)中的運行狀態(tài),直到運行狀態(tài)達到完整狀態(tài)。

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)

低于運行結(jié)果。

{'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)

根據(jù)結(jié)果,考慮內(nèi)容價值…

{'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)

 結(jié)果再次出現(xiàn)。

{'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}
熱門推薦
一個賬號試用1000+ API
助力AI無縫鏈接物理世界 · 無需多次注冊
3000+提示詞助力AI大模型
和專業(yè)工程師共享工作效率翻倍的秘密
熱門推薦
一個賬號試用1000+ API
助力AI無縫鏈接物理世界 · 無需多次注冊
返回頂部
上一篇
內(nèi)容審核服務(wù),高效安全的信息安全屏障
下一篇
Java?API設(shè)計實戰(zhàn)指南:打造穩(wěn)健、用戶友好的API
国内精品久久久久影院日本,日本中文字幕视频,99久久精品99999久久,又粗又大又黄又硬又爽毛片
亚洲一区二区av在线| 日韩不卡一区二区三区| 美女视频网站久久| 欧美亚洲日本一区| 亚洲国产日韩精品| 国产美女久久久久| 7777精品伊人久久久大香线蕉的 | 日韩欧美国产麻豆| 精品国产乱码久久久久久夜甘婷婷 | 琪琪一区二区三区| 国产三级精品三级| 国产久卡久卡久卡久卡视频精品| 欧美成人精精品一区二区频| 国产成人免费视频网站| 国产精品国产自产拍高清av王其| 国产一区激情在线| 一区二区三区在线高清| 欧美一区二区三区婷婷月色| 奇米精品一区二区三区四区| 久久网这里都是精品| 4438亚洲最大| 久久精品欧美一区二区三区不卡| 色综合久久天天综合网| 久久天天做天天爱综合色| 91视频一区二区| 欧美性感一类影片在线播放| 亚洲成人精品一区| 中文字幕一区二区三中文字幕| 91网站在线播放| 成人99免费视频| 欧美疯狂做受xxxx富婆| 国内久久婷婷综合| 韩国毛片一区二区三区| 欧美日韩国产一区二区三区地区| 欧美午夜精品免费| 91黄色小视频| 亚洲精品在线免费播放| 久久亚洲春色中文字幕久久久| 国产精品久久99| 亚洲国产视频在线| 日韩精品一区国产麻豆| 日韩亚洲欧美一区| 日韩一级成人av| 亚洲欧美电影一区二区| 蜜桃精品视频在线观看| 成人一区在线观看| 成人av网址在线| 91香蕉国产在线观看软件| 亚洲综合激情小说| 日韩电影免费在线看| 成人av资源在线观看| 日韩一区在线看| 久久久久国产精品厨房| 精品美女在线播放| 久久奇米777| 日本中文字幕一区二区有限公司| 中文字幕一区二| 亚洲免费在线观看视频| 亚洲国产精品久久艾草纯爱| 亚洲妇女屁股眼交7| 国产精品一区2区| 欧美一区二区三区精品| 一区二区高清免费观看影视大全| 国产一区二区三区蝌蚪| 欧美一级二级在线观看| 亚洲国产综合色| 99精品1区2区| 欧美电影免费观看高清完整版在线观看| 在线播放中文字幕一区| 免费精品视频最新在线| 国产精品拍天天在线| 成人综合日日夜夜| 亚洲欧美视频一区| 免费在线观看成人| 欧美三级视频在线播放| 午夜精品成人在线视频| 国产日韩欧美精品电影三级在线| 久久久国产午夜精品| 免费成人av在线| 一区二区中文视频| 欧美一区二区大片| 亚洲综合视频在线观看| 久久综合国产精品| 亚洲精品一区二区三区99| 欧美日韩aaa| 性做久久久久久久免费看| 欧美一级理论片| 9191精品国产综合久久久久久| 国产激情一区二区三区四区| 精品免费视频.| 日韩亚洲欧美在线| 久久网站最新地址| 99久久久久免费精品国产| 久久99久久精品欧美| 奇米精品一区二区三区在线观看| 欧美日韩亚洲综合在线| 国产精品福利电影一区二区三区四区| 午夜影院久久久| 日韩成人dvd| 成人自拍视频在线观看| 国产精品久久久久久久久免费桃花| 成人久久视频在线观看| 成人免费的视频| 懂色中文一区二区在线播放| 国产乱人伦精品一区二区在线观看| 91福利精品视频| 亚洲美女视频一区| 99久久伊人精品| 欧美一区二区三区在线观看| 五月婷婷久久丁香| 26uuu色噜噜精品一区| 久久成人av少妇免费| 久久精品夜色噜噜亚洲a∨| 99在线热播精品免费| 91一区二区在线| 色狠狠桃花综合| 国产嫩草影院久久久久| 久久99精品久久久久婷婷| 欧美日韩一区三区四区| 亚洲人成伊人成综合网小说| 亚洲一区二区三区免费视频| 福利一区二区在线观看| 2023国产精品| 成人综合婷婷国产精品久久免费| 久久久不卡网国产精品一区| 国产美女一区二区| 中文字幕日韩精品一区 | 2021中文字幕一区亚洲| 国产精品一二一区| 亚洲欧美日韩国产另类专区| 亚洲成a人在线观看| 日韩精品在线一区| 成人免费看视频| 琪琪久久久久日韩精品| 亚洲精品视频在线看| 日韩一卡二卡三卡四卡| 一本一道波多野结衣一区二区| 国产在线日韩欧美| 波多野结衣91| 欧美人体做爰大胆视频| 99精品欧美一区二区三区综合在线| 不卡视频免费播放| 91国偷自产一区二区三区成为亚洲经典| 欧美高清视频www夜色资源网| 2020国产精品| 老司机免费视频一区二区| 成人午夜在线视频| 热久久久久久久| 天天综合日日夜夜精品| 99亚偷拍自图区亚洲| 玉米视频成人免费看| 欧美高清www午色夜在线视频| 国产精品丝袜久久久久久app| 五月综合激情日本mⅴ| 日韩欧美国产系列| 欧美丝袜第三区| 欧美久久久久中文字幕| 欧美日韩在线综合| 精品国产乱码久久久久久久| 欧美一区二区三区色| 日韩一区二区免费视频| 亚洲精品在线一区二区| 久久久午夜精品理论片中文字幕| 精品免费视频.| 中文字幕一区二区5566日韩| 一区二区三区在线观看国产| 久久久高清一区二区三区| 欧美日韩高清在线| 91国偷自产一区二区开放时间| 欧美一区二区三级| 波多野结衣一区二区三区| 亚洲午夜国产一区99re久久| 美日韩一级片在线观看| 一本大道久久a久久精二百| 欧美日韩aaa| 午夜影院在线观看欧美| 美女网站色91| 欧美日韩国产一级片| 一区二区三区日韩精品视频| 91在线播放网址| 日本韩国欧美国产| 欧美日韩中字一区| 欧美色偷偷大香| 亚洲人快播电影网| 97精品国产露脸对白| 欧美三级视频在线观看| 亚洲精品在线观| 日韩激情中文字幕| 日韩欧美一区电影| 国产精品主播直播| 欧美一级片在线观看| 亚洲视频一区二区在线| 在线观看成人免费视频| 激情六月婷婷久久| 久久精品免视看| 欧美色网一区二区| 成人av网在线| 激情亚洲综合在线| 一区二区三区毛片| 国产亚洲自拍一区|