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_KEYinclient.pywith your real GigaChat API key before running.
Table of Contents
- What It Does
- Prerequisites
- Installation
- Running the Demo
- Example Usage
- Extending with More Functions
What It Does
- Sends a user prompt to GigaChat via the
/chatendpoint. - Parses the model’s response for a
function_call. - 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.
- Prints the final assistant message.
Prerequisites
| Component | Minimum Version |
|---|---|
| Python | 3.11+ |
| pip | – |
The demo uses only standard libraries (json, time) and the third‑party package:
pip install requests
No external services (Ollama, Qdrant, etc.) are required.
Installation
-
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 -
Create a virtual environment (recommended):
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate -
Install dependencies:
pip install -r requirements.txt
requirements.txtcontains onlyrequests.
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 (Ctrl‑C).
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
-
Define a new function in
client.py:def get_current_time() -> str: return time.strftime("%Y-%m-%d %H:%M:%S") -
Add it to the
FUNCTION_MAP:FUNCTION_MAP = { "calculate_sum": calculate_sum, "get_current_time": get_current_time, } -
Update the model’s 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! 🚀