Files
..
2026-05-27 11:18:33 +00:00
2026-05-27 11:18:34 +00:00

GigaChat API Function Calling Demo

A lightweight Python demo that shows how to interact with the Sberbank GigaChat REST API using the Function Calling feature.
The script sends a user prompt, receives a response from the model, detects if it requests a function call, executes the local function, and feeds the result back into the conversation.

⚠️ Important Replace YOUR_GIGACHAT_API_KEY in client.py with your real GigaChat API key before running.


Table of Contents


What It Does

  1. Sends a user prompt to GigaChat via the /chat endpoint.
  2. Parses the models response for a function_call.
  3. If a function call is detected:
    • Extracts the function name and arguments.
    • Executes the corresponding local Python function (currently only calculate_sum).
    • Sends the function result back to GigaChat as part of the conversation.
  4. Prints the final assistant message.

Prerequisites

Component Minimum Version
Python 3.11+
pip

The demo uses only standard libraries (json, time) and the thirdparty package:

pip install requests

No external services (Ollama, Qdrant, etc.) are required.


Installation

  1. Clone the repository (or copy the files into a folder):

    git clone https://github.com/your-username/gigachat-function-calling.git
    cd gigachat-function-calling
    
  2. Create a virtual environment (recommended):

    python -m venv .venv
    source .venv/bin/activate  # On Windows: .venv\Scripts\activate
    
  3. Install dependencies:

    pip install -r requirements.txt
    

requirements.txt contains only requests.


Running the Demo

Single Prompt Example

python client.py "What is 12 plus 30?"

The script will:

  • Send "What is 12 plus 30?" to GigaChat.
  • Detect that the model wants to call calculate_sum(a=12, b=30).
  • Execute the function locally and return the result (42) back to the model.
  • Print the final assistant response.

Interactive Session

If you run the script without arguments, it will start an interactive prompt:

python client.py

You can then type any question or request. The session continues until you exit (CtrlC).


Example Usage

Command

python client.py "Add 7 and 15."

Output

User: Add 7 and 15.
Assistant: Sure! Let me calculate that for you...
Function call detected: calculate_sum(a=7, b=15)
Result of calculate_sum(7, 15): 22
Assistant: The sum of 7 and 15 is 22. Is there anything else I can help with?

Extending with More Functions

  1. Define a new function in client.py:

    def get_current_time() -> str:
        return time.strftime("%Y-%m-%d %H:%M:%S")
    
  2. Add it to the FUNCTION_MAP:

    FUNCTION_MAP = {
        "calculate_sum": calculate_sum,
        "get_current_time": get_current_time,
    }
    
  3. Update the models function schema (if you control the prompt or system message) so GigaChat knows about the new function.

The script will automatically detect and execute any function present in FUNCTION_MAP.


Happy coding! 🚀