GigaChat api: README.md
This commit is contained in:
@@ -0,0 +1,148 @@
|
|||||||
|
# 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](#what-it-does)
|
||||||
|
- [Prerequisites](#prerequisites)
|
||||||
|
- [Installation](#installation)
|
||||||
|
- [Running the Demo](#running-the-demo)
|
||||||
|
- [Single Prompt Example](#single-prompt-example)
|
||||||
|
- [Interactive Session](#interactive-session)
|
||||||
|
- [Example Usage](#example-usage)
|
||||||
|
- [Extending with More Functions](#extending-with-more-functions)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## What It Does
|
||||||
|
|
||||||
|
1. **Sends a user prompt** to GigaChat via the `/chat` endpoint.
|
||||||
|
2. **Parses the model’s 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 third‑party package:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install requests
|
||||||
|
```
|
||||||
|
|
||||||
|
No external services (Ollama, Qdrant, etc.) are required.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
1. **Clone the repository** (or copy the files into a folder):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone https://github.com/your-username/gigachat-function-calling.git
|
||||||
|
cd gigachat-function-calling
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Create a virtual environment** (recommended):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python -m venv .venv
|
||||||
|
source .venv/bin/activate # On Windows: .venv\Scripts\activate
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Install dependencies**:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
> `requirements.txt` contains only `requests`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Running the Demo
|
||||||
|
|
||||||
|
### Single Prompt Example
|
||||||
|
|
||||||
|
```bash
|
||||||
|
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:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python client.py
|
||||||
|
```
|
||||||
|
|
||||||
|
You can then type any question or request. The session continues until you exit (Ctrl‑C).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Example Usage
|
||||||
|
|
||||||
|
**Command**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
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`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def get_current_time() -> str:
|
||||||
|
return time.strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Add it to the `FUNCTION_MAP`:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
FUNCTION_MAP = {
|
||||||
|
"calculate_sum": calculate_sum,
|
||||||
|
"get_current_time": get_current_time,
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **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! 🚀
|
||||||
Reference in New Issue
Block a user