مرجع API OpenAI
الاختبار
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Hello!"
}
]
}'
واجهة الوكيل
استخدام CloudFlare
قاعدة URL
https://api.openai.com/v1
الاستعاضة عن
https://gateway.ai.cloudflare.com/v1/[xxx]/[yyy]/openai
مثلاً
curl https://gateway.ai.cloudflare.com/v1/45a93dd9034ce3d27b263a074d05f09a/ververv/openai/chat/completions \
تعريف واجهة Chat
تحديد شكل الاستجابة
response_format
يجب أن يكون واحدا من text
أو json_object
.
يتيح الإعداد إلى {"type": "json_object" }
وضع JSON ، مما يضمن أن الرسالة التي يولدها النموذج هي JSON صالحة.
**مهم: ** عند استخدام وضع JSON ، يجب عليك ** أيضًا إصدار تعليمات للنموذج لإنتاج JSON بنفسك عبر نظام أو رسالة مستخدم.وبدون ذلك، قد يولد النموذج تيارًا لا نهاية له من المساحات البيضاء حتى يصل الجيل إلى الحد الأقصى للرمز المميز، مما يؤدي إلى طلب طويل الأمد و"عالق" على ما يبدو.لاحظ أيضًا أنه قد يتم قطع محتوى الرسالة جزئيًا إذا كان finish_reason="length"
، مما يشير إلى أن الجيل تجاوز max_tokens
أو تجاوز المحادثة الحد الأقصى لطول السياق.
دعوة الوظيفة / الأدوات
الأدوات:قائمة الأدوات
كل أداة هي كائن وكل كائن لديه
type:Currently, only function
is supported.
وظيفة:اسم، وصف، معلمات
تعريف كل parameter:
الاسم, نوع المعلمة, وصف, قيمة اختيارية, ما هي المعلمات المطلوبة
أمثلة
** الطلبات**
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "What is the weather like in Boston?"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
}
],
"tool_choice": "auto"
}'
** الردود**
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1699896916,
"model": "gpt-3.5-turbo-0125",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_current_weather",
"arguments": "{\n\"location\": \"Boston, MA\"\n}"
}
}
]
},
"logprobs": null,
"finish_reason": "tool_calls"
}
],
"usage": {
"prompt_tokens": 82,
"completion_tokens": 17,
"total_tokens": 99
}
}
الإطار الكبير
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "What is the weather like in Boston?"
}
],
"tools": [ // tool列表,与model同级
{ // 具体某个tool
"type": "function", // 类型:暂时只支持function
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
// 更具体定义
}
}
}
],
"tool_choice": "auto" // 选择哪个tool,与model同级
}
المعلمات
{
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": ["location"],
}