Zum Hauptinhalt springen

OpenAI API Referenz

Testen Sie

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

Agent-Schnittstelle

Nutzung von CloudFlare

官方教程

Basis-URL

https://api.openai.com/v1

Ersetzt durch

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

Zum Beispiel

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

Chat-Schnittstelle definiert

官网

Das Format der Antwort

官网

Antwort _ Format

Muss entweder text oder json _ object sein.

Die Einstellung auf { "type": "json_object"} aktiviert den JSON-Modus, der garantiert, dass die vom Modell generierte Nachricht ein gültiges JSON ist.

    • Wichtig: * * Wenn Sie den JSON-Modus verwenden, müssen Sie * * * auch das Modell anweisen, JSON selbst über eine System - oder Benutzernachricht zu erstellen. Ohne dies kann das Modell einen endlosen Strom von Weißraum generieren, bis die Generation das Token-Limit erreicht, was zu einer lang laufenden und scheinbar "stecken steckenden" Anfrage führt. Beachten Sie auch, dass der Inhalt der Nachricht teilweise abgeschnitten werden kann, wenn finish _ reason="length" , was angibt, dass die Generation max _ tokens überschritten hat oder die Konversation die maximale Kontextlänge überschritten hat.

Funktionsaufruf / Werkzeuge

官网

Tools: Eine Liste von Werkzeugen

Jedes Werkzeug ist ein Objekt, jedes Objekt hat ein

type: Derzeit wird nur Funktion unterstützt.

Funktion: Name, Parameter, Beschreibung

Die Definition jedes Parameters:

Name, Parameter-Typ, Beschreibung, optionale Werte, welche Parameter erforderlich sind

Beispiel

    • Anfrage * *
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"
}'

    • Antwort *
{
"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
}
}

Großer Rahmen

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

Parameter

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