Tema
Idioma

Llamadas a funciones (herramientas)

Cómo usar function calling (uso de herramientas) con GetVibe API: los parámetros tools y tool_choice, y una conversación de herramientas de varios turnos. Bucle completo del agente en Python y curl.

Las llamadas a funciones (uso de herramientas) permiten que el modelo solicite llamadas a tus funciones y reciba el resultado. Son compatibles con todos los modelos compatibles con OpenAI.

Parámetros

ParámetroTipoDescripción
toolsarrayGPT · ClaudeLista de herramientas. Cada objeto: { type: "function", function: { name, description, parameters } }.
tool_choicestring | objectGPT · Claude"auto" (el modelo decide), "none" (no llamar), "required" (debe llamar) o { type: "function", function: { name } } — una herramienta específica.
parallel_tool_callsbooleanGPTtrue — el modelo solicita varias herramientas a la vez. El valor predeterminado es true. Claude no admite esta marca, pero realiza llamadas en paralelo por su cuenta.

Ejemplo: una sola llamada

curl https://getvibeapi.com/v1/chat/completions \
  -H "Authorization: Bearer $GETVIBE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-6.1-sol",
    "tools": [{
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "Returns the current weather in a city",
        "parameters": {
          "type": "object",
          "properties": {
            "city": { "type": "string", "description": "City name" }
          },
          "required": ["city"]
        }
      }
    }],
    "tool_choice": "auto",
    "messages": [{"role": "user", "content": "What is the weather in London?"}]
  }'

Multi-turn: enviar el resultado de vuelta

Después de llamar a la herramienta, agrega un mensaje con el rol tool y el resultado, y envía la siguiente solicitud.

# messages after the first response with tool_call:
messages = [
    {"role": "user", "content": "What is the weather in London?"},
    # model response with the tool call
    {"role": "assistant", "tool_calls": msg.tool_calls, "content": None},
    # the result of your function
    {
        "role": "tool",
        "tool_call_id": msg.tool_calls[0].id,
        "content": json.dumps({"city": "London", "temp": 18, "desc": "Cloudy"}),
    },
]
resp2 = client.chat.completions.create(model="gpt-6.1-sol", tools=tools, messages=messages)
print(resp2.choices[0].message.content)
# → "It is currently 18°C and cloudy in London."

Respuesta de ejemplo (tool_call)

{
  "choices": [{
    "message": {
      "role": "assistant",
      "content": null,
      "tool_calls": [{
        "id": "call_abc123",
        "type": "function",
        "function": {
          "name": "get_weather",
          "arguments": "{\"city\":\"London\"}"
        }
      }]
    },
    "finish_reason": "tool_calls"
  }]
}
Los campos tools y tool_choice se aceptan en el formato compatible con OpenAI; su compatibilidad real depende del modelo elegido. No dependas de una transmisión byte por byte ni de campos de respuesta específicos del upstream.
Llamadas a funciones de GetVibe API: tools, tool_choice, ejemplos en Python | GetVibe API