Riferimento API OpenAI
testato
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!"
}
]
}'
Interfaccia Agente
Utilizzo di CloudFlare
Basare l'URL
https://api.openai.com/v1
Sostituire per
https://gateway.ai.cloudflare.com/v1/[xxx]/[yyy]/openai
Per esempio,
curl https://gateway.ai.cloudflare.com/v1/45a93dd9034ce3d27b263a074d05f09a/ververv/openai/chat/completions \
Definizione di chat
Impostazione del formato della risposta
risposta _ formato
Deve essere uno di text
o json _ object
.
Impostazione di { "type": "json _ object"}
Abilita la modalità JSON, che garantisce che il messaggio generato dal modello sia JSON valido.
-
- Importante: * * quando si utilizza la modalità JSON, * * deve anche * * istruire il modello a produrre JSON da solo tramite un messaggio di sistema o utente. Senza questo, il modello può generare un flusso infinito di spazi bianchi fino a quando la generazione raggiunge il limite dei token, con conseguente una richiesta lunga e apparentemente "bloccata". Si noti anche che il contenuto del messaggio può essere parzialmente tagliato se
finish _ reason="length"
, che indica che la generazione ha superatomax _ tokens
o la conversazione ha superato la lunghezza massima del contesto.
- Importante: * * quando si utilizza la modalità JSON, * * deve anche * * istruire il modello a produrre JSON da solo tramite un messaggio di sistema o utente. Senza questo, il modello può generare un flusso infinito di spazi bianchi fino a quando la generazione raggiunge il limite dei token, con conseguente una richiesta lunga e apparentemente "bloccata". Si noti anche che il contenuto del messaggio può essere parzialmente tagliato se
Funzione Call / Tools
Strumenti: Un elenco di strumenti
Ogni strumento è un oggetto, ogni oggetto ha un
type: Attualmente, solo funzione
è supportata.
funzione: nome, descrizione, parametri
Definizione di ciascun parametro:
Nome, tipo di argomento, descrizione, valori facoltativi, quali argomenti sono richiesti
Esempio
-
- richiesta * *
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"
}'
-
- Risposta * *
{
"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
}
}
Il grande quadro
{
"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同级
}
Parametri
{
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": ["location"],
}