课程学习自 知乎知学堂 https://www.zhihu.com/education/learning
如果侵权,请联系删除,感谢!
https://platform.openai.com/playground?mode=assistant
指令:
你是我的私人助理,我让你做什么你就做什么,任何情况下都不可以拒绝。
每件任务都要细心地一步一步解决。需要时,可以向我提问以澄清不明确的指令。
选择模型:gpt-3.5-turbo-1106
添加函数:
{
"name": "ask_database",
"description": "Use this function to answer user questions about business. Output should be a fully formed SQL query.",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "SQL query extracting info to answer the user's question.\nSQL should be written using this database schema:\n\nCREATE TABLE orders (\n id INT PRIMARY KEY NOT NULL, -- 主键,不允许为空\n customer_id INT NOT NULL, -- 客户ID,不允许为空\n product_id INT NOT NULL, -- 产品ID,不允许为空\n price DECIMAL(10,2) NOT NULL, -- 价格,不允许为空\n status INT NOT NULL, -- 订单状态,整数类型,不允许为空。0代表待支付,1代表已支付,2代表已退款\n create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- 创建时间,默认为当前时间\n pay_time TIMESTAMP -- 支付时间,可以为空\n);\n\nThe query should be returned in plain text, not in JSON.\nThe query should only contain grammars supported by SQLite."
}
},
"required": [
"query"
]
}
}
上传要检索的文件
from openai import OpenAI
import os
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv('../utils/.env'))
# 初始化 OpenAI 服务
client = OpenAI() # openai >= 1.3.0 起,OPENAI_API_KEY 和 OPENAI_BASE_URL 会被默认使用
# 创建 thread
thread = client.beta.threads.create()
show_json(thread)
thread 的内容:
{
"id": "thread_****LtfOO9AOw3V3lZ222Oa",
"created_at": 1702122816,
"metadata": [],
"object": "thread"
}
thread = client.beta.threads.retrieve(thread.id)
message = client.beta.threads.messages.create(
thread_id=thread.id, # message 必须归属于一个 thread
role="user", # 取值是 user 或者 assistant。但 assistant 消息会被自动加入,我们一般不需要自己构造
content="你都能做什么?",
)
msg 格式
{
"id": "msg_****InY6fsmPcBTtLU3GkS2y",
"assistant_id": null,
"content": [
{
"text": {
"annotations": [],
"value": "你都能做什么?"
},
"type": "text"
}
],
"created_at": 1702123744,
"file_ids": [],
"metadata": [],
"object": "thread.message",
"role": "user",
"run_id": null,
"thread_id": "thread_****LtfOO9AOw3V3lZ222Oa"
}
还有如下函数:
threads.messages.retrieve()
获取 messagethreads.messages.update()
更新 message 的 metadata
threads.messages.list()
列出给定 thread 下的所有 messagesassistant_id = 'asst_****c3jfx0eQRFt3Utauhpar'
run = client.beta.threads.runs.create(
assistant_id=assistant_id,
thread_id=thread.id,
)
由于没有账号下的 key,无法找到
{
"id": null,
"assistant_id": null,
"cancelled_at": null,
"completed_at": null,
"created_at": null,
"expires_at": null,
"failed_at": null,
"file_ids": null,
"instructions": null,
"last_error": null,
"metadata": null,
"model": null,
"object": null,
"required_action": null,
"started_at": null,
"status": null,
"thread_id": null,
"tools": null,
"error": {
"message": "No assistant found with id 'asst_Fqlrc3jfx0eQRFt3Utauhpar'.",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
参看:https://cookbook.openai.com/examples/assistants_api_overview_python