Skip to content

Assistants API の中継

2024.1.4 この中継は Assistants API に対応しました

注意事項

  • 1、Code InterpreterFunction calling に対応しています
  • 2、現時点では Knowledge Retrieval には対応していません
  • 3、課金はトークンに加えて、会話スレッドの費用も発生します
  • 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
}