Skip to content

Ретрансляция Assistants API

2024.1.4 Данная ретрансляция уже поддерживает Assistants API

Примечание

    1. Поддерживается Code Interpreter и Function calling
    1. Пока не поддерживается Knowledge Retrieval
    1. Тарификация включает не только токены, но и стоимость потоков сеансов
    1. Аккаунт может быть отключен по непредвиденным причинам, поэтому невозможно гарантировать постоянную доступность 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. Запуск

assistant_id из шага 2 используется здесь

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
}