Saltar al contenido principal

Referencia de API OpenAI

Prueba

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

Interfaz proxy

Uso de CloudFlare

官方教程

Base de URL

https://api.openai.com/v1

Sustituido por

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

Por ejemplo

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

Definición de Chat Interface

官网

Formatear la respuesta

官网

Respuesta _ Format

Debe ser uno de 'text' o 'json _ object'.

Configurar a { "type": "json_object" } habilita el modo JSON, lo que garantiza que el mensaje que el modelo genera es JSON válido.

    • Importante: * * Al utilizar el modo JSON, también debe instruir al modelo a producir JSON por sí mismo a través de un mensaje del sistema o del usuario. Sin esto, el modelo puede generar un flujo interminable de espacios en blanco hasta que la generación alcanza el límite de token, lo que resulta en una solicitud de larga duración y aparentemente "stuck". También tenga en cuenta que el contenido del mensaje puede ser parcialmente cortado si finish _ reason="length", lo que indica que la generación excedió max _ tokens o la conversación excedió la longitud máxima del contexto.

Llamadas a funciones / herramientas

官网

Herramientas: Lista de herramientas

Cada herramienta es un objeto y cada objeto tiene

type: Actualmente, sólo se admite función.

Función: nombre, descripción, parámetros

Definición de cada uno de los parámetros

Nombre, tipo de parámetro, descripción, valor opcional, qué parámetros son necesarios

Ejemplos

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

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

El gran marco

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

Parámetros

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