commit 2de2f499fd33d449a66351c8e04a875161206824 Author: litoq Date: Thu Nov 20 22:32:27 2025 +0300 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. diff --git a/.env_example b/.env_example new file mode 100644 index 0000000..b2ef115 --- /dev/null +++ b/.env_example @@ -0,0 +1 @@ +GIGACHAT_CREDENTIALS= diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4dddc27 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.env +.venv/ +__pycache__/ + diff --git a/README.md b/README.md new file mode 100644 index 0000000..824eb17 --- /dev/null +++ b/README.md @@ -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 +``` diff --git a/gigachat_cli.py b/gigachat_cli.py new file mode 100644 index 0000000..5fc1d39 --- /dev/null +++ b/gigachat_cli.py @@ -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() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..08e8cf1 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +gigachat +python-dotenv