Ana içeriğe geç

OpenAI API Referansları

Testler

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!"
}
]
}'

Vekil Arabirimi

CloudFlare Kullanımı

官方教程

Base URL 'yi

https://api.openai.com/v1

Değiştir

https://gateway.ai.cloudflare.com/v1/[xxx]/[yyy]/openai

Örneğin...

curl https://gateway.ai.cloudflare.com/v1/45a93dd9034ce3d27b263a074d05f09a/ververv/openai/chat/completions \

Chat Arayüzü Tanımı

官网

Yanıtların formatını ayarlama

官网

response _ format

text veya json_object arasından biri olmalıdır.

Setting to { "type": "json_object" } enables JSON mode, which guarantees the message generated by the model is valid JSON.

** Önemli: ** JSON modunu kullanırken, modeline bir sistem veya kullanıcı mesajı aracılığıyla JSON'ı kendiniz üretmesini de talimat vermelisiniz. Bu olmadan, model, nesil belirteç sınırına ulaşana kadar bitmeyen bir beyaz alan akışı üretebilir, bu da uzun süren ve görünüşte "sıkışmış" bir isteğe neden olabilir. Ayrıca, finish_reason="length" durumunda ileti içeriğinin kısmen kesilebileceğini unutmayın, bu da neslin max_tokensyi aştığını veya konuşmanın maksimum bağlam uzunluğunu aştığını gösterir.

İşlev Çağrısı / Araçlar

官网

Araçlar: A List of tools

Her araç bir nesnedir, her nesne bir nesne

type:Currently, only function is supported.

fonksiyon:name,description,parameters

Her parametre için tanımlama:

Ad, parametreler türü, açıklama, isteğe bağlı değerler, hangi parametreler gereklidir

Örnekler

** Tavsiye**

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"
}'

** Yanıt**

{
"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
}
}

Büyük bir çerçeve

{
"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同级
}

parametre

{
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": ["location"],
}