Skip to content

中转 Assistants API

2024.1.4 本中转已支持 Assistants API

注意

  • 1、支持 Code InterpreterFunction calling
  • 2、暂时不支持 Knowledge Retrieval
  • 3、计费除了tokens外,还有会话线程费用
  • 4、账号由于无法预知的原因下线,所以无法一直保持 assistants id thread id run id 一直可用

参考文档

参考文档

1.新建一个Assistant

shell
curl "https://api.openai-hk.com/v1/assistants" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer hk-你的key" \
  -H "OpenAI-Beta: assistants=v1" \
  -d '{
    "instructions": "You are a personal math tutor. Write and run code to answer math questions.",
    "name": "Math Tutor",
    "tools": [{"type": "code_interpreter"}],
    "model": "gpt-4"
  }'
curl "https://api.openai-hk.com/v1/assistants" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer hk-你的key" \
  -H "OpenAI-Beta: assistants=v1" \
  -d '{
    "instructions": "You are a personal math tutor. Write and run code to answer math questions.",
    "name": "Math Tutor",
    "tools": [{"type": "code_interpreter"}],
    "model": "gpt-4"
  }'

返回体

json
{
	"id": "asst_AXBqN2YyydsTQcdhxNN22Sy5", //这个在run步骤要用到
	"object": "assistant",
	"created_at": 1704298806,
	"name": "Math Tutor",
	"description": null,
	"model": "gpt-4",
	"instructions": "You are a personal math tutor. Write and run code to answer math questions.",
	"tools": [
		{
			"type": "code_interpreter"
		}
	],
	"file_ids": [],
	"metadata": {}
}
{
	"id": "asst_AXBqN2YyydsTQcdhxNN22Sy5", //这个在run步骤要用到
	"object": "assistant",
	"created_at": 1704298806,
	"name": "Math Tutor",
	"description": null,
	"model": "gpt-4",
	"instructions": "You are a personal math tutor. Write and run code to answer math questions.",
	"tools": [
		{
			"type": "code_interpreter"
		}
	],
	"file_ids": [],
	"metadata": {}
}

2.创建线程(会话)

必须 post

shell
curl https://api.openai-hk.com/v1/threads \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer hk-你的key" \
  -H "OpenAI-Beta: assistants=v1" \
  -d ''
curl https://api.openai-hk.com/v1/threads \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer hk-你的key" \
  -H "OpenAI-Beta: assistants=v1" \
  -d ''

返回

json
{
	"id": "thread_PRhqIIzk5g2IKjTdo7jsLd5f", //后面的步骤都要用到
	"object": "thread",
	"created_at": 1704298816,
	"metadata": {}
}
{
	"id": "thread_PRhqIIzk5g2IKjTdo7jsLd5f", //后面的步骤都要用到
	"object": "thread",
	"created_at": 1704298816,
	"metadata": {}
}

3.往会话里面加入消息

shell
curl https://api.openai-hk.com/v1/threads/thread_PRhqIIzk5g2IKjTdo7jsLd5f/messages \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer hk-你的key" \
  -H "OpenAI-Beta: assistants=v1" \
  -d '{
      "role": "user",
      "content": "y=-5x^2+7x+89 顶点是多少?"
    }'
curl https://api.openai-hk.com/v1/threads/thread_PRhqIIzk5g2IKjTdo7jsLd5f/messages \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer hk-你的key" \
  -H "OpenAI-Beta: assistants=v1" \
  -d '{
      "role": "user",
      "content": "y=-5x^2+7x+89 顶点是多少?"
    }'

返回体

json
{
	"id": "msg_baXqZT49fuB7nSp2uDaDcxay",
	"object": "thread.message",
	"created_at": 1704301942,
	"thread_id": "thread_PRhqIIzk5g2IKjTdo7jsLd5f",
	"role": "user",
	"content": [
		{
			"type": "text",
			"text": {
				"value": "y=-5x^2+7x+89 顶点是多少?",
				"annotations": []
			}
		}
	],
	"file_ids": [],
	"assistant_id": null,
	"run_id": null,
	"metadata": {}
}
{
	"id": "msg_baXqZT49fuB7nSp2uDaDcxay",
	"object": "thread.message",
	"created_at": 1704301942,
	"thread_id": "thread_PRhqIIzk5g2IKjTdo7jsLd5f",
	"role": "user",
	"content": [
		{
			"type": "text",
			"text": {
				"value": "y=-5x^2+7x+89 顶点是多少?",
				"annotations": []
			}
		}
	],
	"file_ids": [],
	"assistant_id": null,
	"run_id": null,
	"metadata": {}
}

4.运行

步骤2的 assistant_id 这边用到了

shell
curl https://api.openai-hk.com/v1/threads/thread_PRhqIIzk5g2IKjTdo7jsLd5f/runs \
  -H "Authorization: Bearer hk-你的key" \
  -H "Content-Type: application/json" \
  -H "OpenAI-Beta: assistants=v1" \
  -d '{
    "assistant_id": "asst_AXBqN2YyydsTQcdhxNN22Sy5", 
    "instructions": "Please address the user as Jane Doe. The user has a premium account."
  }'
curl https://api.openai-hk.com/v1/threads/thread_PRhqIIzk5g2IKjTdo7jsLd5f/runs \
  -H "Authorization: Bearer hk-你的key" \
  -H "Content-Type: application/json" \
  -H "OpenAI-Beta: assistants=v1" \
  -d '{
    "assistant_id": "asst_AXBqN2YyydsTQcdhxNN22Sy5", 
    "instructions": "Please address the user as Jane Doe. The user has a premium account."
  }'

返回

json
{
	"id": "run_8eBTGBWCmUEZICJyu7wEtUCt",
	"object": "thread.run",
	"created_at": 1704301949,
	"assistant_id": "asst_AXBqN2YyydsTQcdhxNN22Sy5",
	"thread_id": "thread_PRhqIIzk5g2IKjTdo7jsLd5f",
	"status": "queued",
	"started_at": null,
	"expires_at": 1704302549,
	"cancelled_at": null,
	"failed_at": null,
	"completed_at": null,
	"last_error": null,
	"model": "gpt-4",
	"instructions": "Please address the user as Jane Doe. The user has a premium account.",
	"tools": [
		{
			"type": "code_interpreter"
		}
	],
	"file_ids": [],
	"metadata": {}
}
{
	"id": "run_8eBTGBWCmUEZICJyu7wEtUCt",
	"object": "thread.run",
	"created_at": 1704301949,
	"assistant_id": "asst_AXBqN2YyydsTQcdhxNN22Sy5",
	"thread_id": "thread_PRhqIIzk5g2IKjTdo7jsLd5f",
	"status": "queued",
	"started_at": null,
	"expires_at": 1704302549,
	"cancelled_at": null,
	"failed_at": null,
	"completed_at": null,
	"last_error": null,
	"model": "gpt-4",
	"instructions": "Please address the user as Jane Doe. The user has a premium account.",
	"tools": [
		{
			"type": "code_interpreter"
		}
	],
	"file_ids": [],
	"metadata": {}
}

5.检查状态

shell
curl https://api.openai-hk.com/v1/threads/thread_PRhqIIzk5g2IKjTdo7jsLd5f/runs/run_8eBTGBWCmUEZICJyu7wEtUCt \
  -H "Authorization: Bearer hk-你的key" \
  -H "OpenAI-Beta: assistants=v1"
curl https://api.openai-hk.com/v1/threads/thread_PRhqIIzk5g2IKjTdo7jsLd5f/runs/run_8eBTGBWCmUEZICJyu7wEtUCt \
  -H "Authorization: Bearer hk-你的key" \
  -H "OpenAI-Beta: assistants=v1"

6.获取结果

shell
curl https://api.openai-hk.com/v1/threads/thread_PRhqIIzk5g2IKjTdo7jsLd5f/messages \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer hk-你的key" \
  -H "OpenAI-Beta: assistants=v1"
curl https://api.openai-hk.com/v1/threads/thread_PRhqIIzk5g2IKjTdo7jsLd5f/messages \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer hk-你的key" \
  -H "OpenAI-Beta: assistants=v1"

获取结果内容:

json
{
	"object": "list",
	"data": [
		{
			"id": "msg_oNRxXuvkaMpbkcEIpOmgvpVO",
			"object": "thread.message",
			"created_at": 1704299111,
			"thread_id": "thread_PRhqIIzk5g2IKjTdo7jsLd5f",
			"role": "assistant",
			"content": [
				{
					"type": "text",
					"text": {
						"value": "这个二次函数的顶点纵坐标为约 89.8。因此,这个二次函数的顶点为 (2/5, 89.8)。",
						"annotations": []
					}
				}
			],
			"file_ids": [],
			"assistant_id": "asst_AXBqN2YyydsTQcdhxNN22Sy5",
			"run_id": "run_lIpFnVSVSWmlBmdtL0I2p3Mx",
			"metadata": {}
		},
		{
			"id": "msg_Ho2FvaWxhdiPQwxXSFTZEYc2",
			"object": "thread.message",
			"created_at": 1704299107,
			"thread_id": "thread_PRhqIIzk5g2IKjTdo7jsLd5f",
			"role": "assistant",
			"content": [
				{
					"type": "text",
					"text": {
						"value": "这个二次函数的顶点横坐标为 2/5。接下来,我们可以将 x = 2/5 代入原方程,求得纵坐标。",
						"annotations": []
					}
				}
			],
			"file_ids": [],
			"assistant_id": "asst_AXBqN2YyydsTQcdhxNN22Sy5",
			"run_id": "run_lIpFnVSVSWmlBmdtL0I2p3Mx",
			"metadata": {}
		},
		{
			"id": "msg_wWIkXbSbb2ClY4y6TGgOu183",
			"object": "thread.message",
			"created_at": 1704299100,
			"thread_id": "thread_PRhqIIzk5g2IKjTdo7jsLd5f",
			"role": "assistant",
			"content": [
				{
					"type": "text",
					"text": {
						"value": "您想要找到这个二次函数的顶点吗?",
						"annotations": []
					}
				}
			],
			"file_ids": [],
			"assistant_id": "asst_AXBqN2YyydsTQcdhxNN22Sy5",
			"run_id": "run_lIpFnVSVSWmlBmdtL0I2p3Mx",
			"metadata": {}
		},
		{
			"id": "msg_j0c12LisQjAIDjZYQGTrgqtu",
			"object": "thread.message",
			"created_at": 1704298850,
			"thread_id": "thread_PRhqIIzk5g2IKjTdo7jsLd5f",
			"role": "user",
			"content": [
				{
					"type": "text",
					"text": {
						"value": "y=-5x^2+4x+89 定点是多少?",
						"annotations": []
					}
				}
			],
			"file_ids": [],
			"assistant_id": null,
			"run_id": null,
			"metadata": {}
		}
	],
	"first_id": "msg_oNRxXuvkaMpbkcEIpOmgvpVO",
	"last_id": "msg_j0c12LisQjAIDjZYQGTrgqtu",
	"has_more": false
}
{
	"object": "list",
	"data": [
		{
			"id": "msg_oNRxXuvkaMpbkcEIpOmgvpVO",
			"object": "thread.message",
			"created_at": 1704299111,
			"thread_id": "thread_PRhqIIzk5g2IKjTdo7jsLd5f",
			"role": "assistant",
			"content": [
				{
					"type": "text",
					"text": {
						"value": "这个二次函数的顶点纵坐标为约 89.8。因此,这个二次函数的顶点为 (2/5, 89.8)。",
						"annotations": []
					}
				}
			],
			"file_ids": [],
			"assistant_id": "asst_AXBqN2YyydsTQcdhxNN22Sy5",
			"run_id": "run_lIpFnVSVSWmlBmdtL0I2p3Mx",
			"metadata": {}
		},
		{
			"id": "msg_Ho2FvaWxhdiPQwxXSFTZEYc2",
			"object": "thread.message",
			"created_at": 1704299107,
			"thread_id": "thread_PRhqIIzk5g2IKjTdo7jsLd5f",
			"role": "assistant",
			"content": [
				{
					"type": "text",
					"text": {
						"value": "这个二次函数的顶点横坐标为 2/5。接下来,我们可以将 x = 2/5 代入原方程,求得纵坐标。",
						"annotations": []
					}
				}
			],
			"file_ids": [],
			"assistant_id": "asst_AXBqN2YyydsTQcdhxNN22Sy5",
			"run_id": "run_lIpFnVSVSWmlBmdtL0I2p3Mx",
			"metadata": {}
		},
		{
			"id": "msg_wWIkXbSbb2ClY4y6TGgOu183",
			"object": "thread.message",
			"created_at": 1704299100,
			"thread_id": "thread_PRhqIIzk5g2IKjTdo7jsLd5f",
			"role": "assistant",
			"content": [
				{
					"type": "text",
					"text": {
						"value": "您想要找到这个二次函数的顶点吗?",
						"annotations": []
					}
				}
			],
			"file_ids": [],
			"assistant_id": "asst_AXBqN2YyydsTQcdhxNN22Sy5",
			"run_id": "run_lIpFnVSVSWmlBmdtL0I2p3Mx",
			"metadata": {}
		},
		{
			"id": "msg_j0c12LisQjAIDjZYQGTrgqtu",
			"object": "thread.message",
			"created_at": 1704298850,
			"thread_id": "thread_PRhqIIzk5g2IKjTdo7jsLd5f",
			"role": "user",
			"content": [
				{
					"type": "text",
					"text": {
						"value": "y=-5x^2+4x+89 定点是多少?",
						"annotations": []
					}
				}
			],
			"file_ids": [],
			"assistant_id": null,
			"run_id": null,
			"metadata": {}
		}
	],
	"first_id": "msg_oNRxXuvkaMpbkcEIpOmgvpVO",
	"last_id": "msg_j0c12LisQjAIDjZYQGTrgqtu",
	"has_more": false
}