> ## Documentation Index
> Fetch the complete documentation index at: https://docs.venice.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Function Calling

> Lass Venice-Chatmodelle über OpenAI-kompatibles Function Calling und die Chat-Completions-API die Tools deiner Anwendung aufrufen.

Mit Function Calling kann ein Modell strukturierte Tool-Aufrufe auswählen, die deine Anwendung ausführt. Das Modell führt die Funktion nicht selbst aus. Es gibt den Funktionsnamen und die Argumente zurück, dein Code führt die Funktion aus und du sendest das Ergebnis an das Modell zurück.

Verwende Function Calling, wenn das Modell Live-Daten, Anwendungsaktionen, Datenbankabfragen oder deterministische Berechnungen benötigt.

## Grundlegende Tool-Definition

Definiere Tools mit dem OpenAI-kompatiblen `tools`-Array:

<CodeGroup>
  ```python Python theme={"system"}
  import os
  from openai import OpenAI

  client = OpenAI(
      api_key=os.environ["VENICE_API_KEY"],
      base_url="https://api.venice.ai/api/v1",
  )

  tools = [
      {
          "type": "function",
          "function": {
              "name": "get_weather",
              "description": "Get the current weather in a location",
              "parameters": {
                  "type": "object",
                  "properties": {
                      "location": {
                          "type": "string",
                          "description": "City and state, such as San Francisco, CA",
                      }
                  },
                  "required": ["location"],
              },
          },
      }
  ]

  response = client.chat.completions.create(
      model="zai-org-glm-5",
      messages=[{"role": "user", "content": "What is the weather in San Francisco?"}],
      tools=tools,
  )

  print(response.choices[0].message.tool_calls)
  ```

  ```javascript Node.js theme={"system"}
  import OpenAI from "openai";

  const client = new OpenAI({
    apiKey: process.env.VENICE_API_KEY,
    baseURL: "https://api.venice.ai/api/v1",
  });

  const tools = [
    {
      type: "function",
      function: {
        name: "get_weather",
        description: "Get the current weather in a location",
        parameters: {
          type: "object",
          properties: {
            location: {
              type: "string",
              description: "City and state, such as San Francisco, CA",
            },
          },
          required: ["location"],
        },
      },
    },
  ];

  const response = await client.chat.completions.create({
    model: "zai-org-glm-5",
    messages: [{ role: "user", content: "What is the weather in San Francisco?" }],
    tools,
  });

  console.log(response.choices[0].message.tool_calls);
  ```

  ```bash cURL theme={"system"}
  curl https://api.venice.ai/api/v1/chat/completions \
    -H "Authorization: Bearer $VENICE_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "zai-org-glm-5",
      "messages": [
        {"role": "user", "content": "What is the weather in San Francisco?"}
      ],
      "tools": [
        {
          "type": "function",
          "function": {
            "name": "get_weather",
            "description": "Get the current weather in a location",
            "parameters": {
              "type": "object",
              "properties": {
                "location": {
                  "type": "string",
                  "description": "City and state, such as San Francisco, CA"
                }
              },
              "required": ["location"]
            }
          }
        }
      ]
    }'
  ```
</CodeGroup>

## Das Tool ausführen

Wenn das Modell ein Tool auswählt, prüfe `message.tool_calls`, parse die Argumente, führe deine Anwendungsfunktion aus und sende das Ergebnis als `tool`-Nachricht zurück.

```python Python theme={"system"}
import json

message = response.choices[0].message
tool_call = message.tool_calls[0]
arguments = json.loads(tool_call.function.arguments)

weather = get_weather(arguments["location"])

follow_up = client.chat.completions.create(
    model="zai-org-glm-5",
    messages=[
        {"role": "user", "content": "What is the weather in San Francisco?"},
        message.model_dump(),
        {
            "role": "tool",
            "tool_call_id": tool_call.id,
            "content": json.dumps(weather),
        },
    ],
    tools=tools,
)

print(follow_up.choices[0].message.content)
```

## Ein Modell auswählen

Die Unterstützung für Function Calling ist modellabhängig. Nutze die Seite [Textmodelle](/models/text) oder die [Models-API](/api-reference/endpoint/models/list), um Modelle mit `supportsFunctionCalling` zu finden.

<Warning>
  Behandle Tool-Argumente als nicht vertrauenswürdige Eingaben. Validiere Argumente, bevor du sie in Datenbankabfragen, Shell-Befehlen, Zahlungen oder anderen Aktionen mit Nebenwirkungen verwendest.
</Warning>

## Design-Tipps

* Halte Tool-Namen und Beschreibungen kurz und wörtlich.
* Verwende JSON Schema, damit das Modell gültige Argumente leichter erzeugen kann.
* Bevorzuge eng gefasste Tools mit klaren Eingaben gegenüber einem breiten Tool mit vielen optionalen Verhaltensweisen.
* Gib knappe Tool-Ergebnisse zurück, damit die finale Antwort genügend Kontext hat, ohne Tokens zu verschwenden.

## Verwandte Ressourcen

* [Chat-Completions-API](/api-reference/endpoint/chat/completions)
* [Textmodelle](/models/text)
* [Anleitung: Strukturierte Antworten](/guides/features/structured-responses)
* [LangChain-Integration](/guides/integrations/langchain#function-calling-with-agents)
