- 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.
116 lines
3.3 KiB
Python
116 lines
3.3 KiB
Python
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()
|