Add initial GigaChat CLI implementation with environment setup
- Created .env_example for environment variable configuration. - Added .gitignore to exclude sensitive files and directories. - Implemented gigachat_cli.py for command-line interaction with the GigaChat API. - Updated README.md with setup instructions and usage examples. - Included requirements.txt for necessary dependencies.
This commit is contained in:
commit
2de2f499fd
1
.env_example
Normal file
1
.env_example
Normal file
@ -0,0 +1 @@
|
||||
GIGACHAT_CREDENTIALS=
|
||||
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
.env
|
||||
.venv/
|
||||
__pycache__/
|
||||
|
||||
36
README.md
Normal file
36
README.md
Normal file
@ -0,0 +1,36 @@
|
||||
# GigaChat CLI Tester
|
||||
|
||||
Simple command-line helper for sending prompts to the GigaChat API.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Python 3.9+
|
||||
- GigaChat credentials (set `GIGACHAT_CREDENTIALS`). Get them from [GigaChat Studio](https://developers.sber.ru/studio).
|
||||
|
||||
## Setup
|
||||
|
||||
```bash
|
||||
python -m venv .venv
|
||||
.venv\Scripts\activate # PowerShell: .venv\Scripts\Activate.ps1
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Send a single prompt:
|
||||
|
||||
```bash
|
||||
python gigachat_cli.py "Explain how AI works in a few words"
|
||||
```
|
||||
|
||||
Interactive mode:
|
||||
|
||||
```bash
|
||||
python gigachat_cli.py
|
||||
```
|
||||
|
||||
Specify a different model:
|
||||
|
||||
```bash
|
||||
python gigachat_cli.py --model GigaChat-Pro
|
||||
```
|
||||
115
gigachat_cli.py
Normal file
115
gigachat_cli.py
Normal file
@ -0,0 +1,115 @@
|
||||
import argparse
|
||||
import base64
|
||||
import os
|
||||
import sys
|
||||
import uuid
|
||||
from typing import Optional
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from gigachat import GigaChat
|
||||
|
||||
|
||||
DEFAULT_MODEL = "GigaChat"
|
||||
|
||||
|
||||
def build_client(credentials: Optional[str], scope: Optional[str], model: Optional[str] = None) -> GigaChat:
|
||||
# verify_ssl_certs=False is sometimes needed depending on the environment,
|
||||
# but we will start with default.
|
||||
kwargs = {"verify_ssl_certs": False}
|
||||
if credentials:
|
||||
kwargs["credentials"] = credentials
|
||||
if scope:
|
||||
kwargs["scope"] = scope
|
||||
if model:
|
||||
kwargs["model"] = model
|
||||
|
||||
return GigaChat(**kwargs)
|
||||
|
||||
|
||||
def generate_text(client: GigaChat, model: str, prompt: str) -> str:
|
||||
# GigaChat client handles the model parameter in the constructor usually.
|
||||
# If we need to change model dynamically, we might need to rebuild client or check documentation
|
||||
# but for now, we rely on client being initialized with correct model or default.
|
||||
# The `chat` method only accepts string prompt in some versions.
|
||||
response = client.chat(prompt)
|
||||
return response.choices[0].message.content or ""
|
||||
|
||||
|
||||
def cli() -> None:
|
||||
load_dotenv()
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Send prompts to the GigaChat API and print the response."
|
||||
)
|
||||
parser.add_argument(
|
||||
"prompt",
|
||||
nargs="?",
|
||||
help="Optional prompt to send immediately. If omitted, the program enters interactive mode.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--model",
|
||||
default=DEFAULT_MODEL,
|
||||
help=f"GigaChat model to use (default: {DEFAULT_MODEL}).",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
credentials = os.getenv("GIGACHAT_CREDENTIALS")
|
||||
scope = os.getenv("GIGACHAT_SCOPE")
|
||||
|
||||
if not credentials:
|
||||
print(
|
||||
"Missing environment variable GIGACHAT_CREDENTIALS. "
|
||||
"Get your credentials from https://developers.sber.ru/studio and export them (or set in .env).",
|
||||
file=sys.stderr,
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
client = build_client(credentials, scope, args.model)
|
||||
except Exception as e:
|
||||
print(f"Failed to initialize GigaChat client: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
if args.prompt:
|
||||
prompt = args.prompt.strip()
|
||||
if not prompt:
|
||||
print("Prompt is empty. Nothing to send.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
print_response(client, args.model, prompt)
|
||||
return
|
||||
|
||||
print(
|
||||
f"GigaChat CLI (model: {args.model}). Enter an empty line to exit.",
|
||||
file=sys.stderr,
|
||||
)
|
||||
while True:
|
||||
try:
|
||||
prompt = input("> ").strip()
|
||||
except (EOFError, KeyboardInterrupt):
|
||||
print("\nExiting.", file=sys.stderr)
|
||||
break
|
||||
|
||||
if not prompt:
|
||||
print("Goodbye!", file=sys.stderr)
|
||||
break
|
||||
|
||||
print_response(client, args.model, prompt)
|
||||
|
||||
|
||||
def print_response(client: GigaChat, model: str, prompt: str) -> None:
|
||||
try:
|
||||
text = generate_text(client, model, prompt)
|
||||
except Exception as exc:
|
||||
# It might be a specific GigaChat error related to auth
|
||||
print(f"Request failed: {exc}", file=sys.stderr)
|
||||
return
|
||||
|
||||
if not text:
|
||||
print("(No text returned by the model.)", file=sys.stderr)
|
||||
return
|
||||
|
||||
print(text)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
cli()
|
||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
gigachat
|
||||
python-dotenv
|
||||
Loading…
Reference in New Issue
Block a user