GigaChain Documentation

repository·master·Indexed 20 days ago

https://github.com/ai-forever/gigachain

A comprehensive ecosystem for developing LLM-powered applications and multi-agent systems in Russian, optimized for GigaChat. It includes integrations for LangChain, LangGraph, and LangChain4j, as well as direct SDKs, OpenAI-compatible proxies, and MCP servers. The ecosystem provides a cookbook with examples for RAG, vision, structured output, and specialized agents like the Lean Canvas AI Agent.

Tokens
23.5K
Snippets
75
Records
90
Agent score
69%

What's inside GigaChain

  1. Overview of GigaChain solutions

    master

    GigaChain is a suite of solutions designed for developing LLM applications and multi-agent systems in Russian, with support for GigaChat. The ecosystem is divided into four main categories:

    1. Frameworks: Integration libraries for LangChain, LangGraph, and LangChain4j (available in Python, JavaScript/TypeScript, and Java).
    2. SDKs: Wrapper libraries for direct interaction with the GigaChat REST API, managing authorization and message handling.
    3. Agents: Orchestrators like GigaAgent for complex task solving.
    4. Utilities & MCP Servers: Tools like gpt2giga (OpenAI API proxy) and Model Context Protocol (MCP) servers for extending LLM capabilities.

    Prerequisite: You must have a GigaChat API authorization key to use these services.

  2. Prompt template versioning

    master
    New versions of GigaChain prompt templates are stored in separate files (e.g., hello.yaml becomes hello_v2.yaml). This is because templates are stored separately from the main GigaChain library and are loaded directly via links. When building your own projects, always use the latest available versions of the templates.
  3. Use LLMMathChain for complex math problems

    master

    The LLMMathChain is a specialized chain designed to solve complex word math problems. It achieves this by using a Large Language Model (LLM) to parse the problem and a Python REPL (Read-Eval-Print Loop) to execute the actual mathematical computations, ensuring higher accuracy for arithmetic operations.

    Input Variables

    VariableDescription
    questionThe math problem or word problem you want the chain to solve.
    # Example usage pattern (conceptual)
    # chain = LLMMathChain.from_llm(llm)
    # response = chain.run("What is 15% of 200 plus 42?")
  4. Extend LLMs with MCP Servers

    master

    GigaChain provides several Model Context Protocol (MCP) servers to connect GigaChat to external tools and data sources:

    • Think MCP: Implements 'reasoning' (think) capabilities for AI agents.
    • MCP Giga Checker: Verifies if text was generated by neural networks.
    • MCP Voice Salute: Provides tools for the SaluteSpeech API (speech synthesis and recognition).
    • MCP Kandinsky: Enables image generation using the Kandinsky 3.1 model.
  5. Use GigaChat SDKs for direct API access

    master

    If you do not need a full orchestration framework like LangChain, use the official GigaChat SDKs. These libraries wrap the GigaChat REST API, managing authorization and simplifying message transmission.

    Available SDKs:

    • Python: gigachat package.
    • JavaScript/TypeScript: gigachat package.
    • Java: gigachat-java package.
  6. Quickstart: Run the Lean Canvas AI Agent

    master

    The Lean Canvas AI Agent is a multi-agent system built on GigaChain that automatically generates a 9-block Lean Canvas (Customer Segments, Problem, Unique Value Proposition, Solution, Channels, Revenue Streams, Cost Structure, Key Metrics, and Unfair Advantage) based on a brief business idea. It uses LangGraph for a multi-step process, Pydantic for structured output, and can perform competitor analysis via web search.

    To get started, configure your environment variables and run the provided Jupyter notebook lean_canvas_agent.ipynb.

    # After setting up environment variables, run the notebook:
    # lean_canvas_agent.ipynb
  7. Run a local GigaChat MCP Agent

    master

    You can run a GigaChat agent that interacts with an MCP server locally using the agent.py implementation. This version implements an MCP client for local interaction and does not require a separate server process to be managed independently.

    To run the local client, execute:

    python agent.py
  8. Run a GigaChat MCP Agent via HTTP (SSE)

    master

    For interacting with an MCP server over HTTP, use the agent_http.py implementation. This requires the MCP server to be running in SSE (Server-Sent Events) mode.

    Steps to run:

    1. Start the MCP server in SSE mode:
      python math_server.py sse
    2. Start the HTTP client:
      python agent_http.py
    python math_server.py sse
    python agent_http.py
  9. Summarize books and large texts

    master

    For summarizing books or extremely large documents, use the specialized prompt templates summarize_book_map.yaml and summarize_book_combine.yaml. These are optimized for the long-form context required for book-length summarization.

    Note: It is highly recommended to use models with a large token capacity for these tasks to ensure the context window can accommodate the necessary text segments.

  10. Create a ReAct agent with GigaChat functions

    master

    To create an agent that can call functions, use langgraph.prebuilt.create_react_agent. You must first bind the functions to the GigaChat model using .bind_functions(functions).

    Basic Agent

    from langgraph.prebuilt import create_react_agent
    
    functions = [send_sms]
    giga_with_functions = giga.bind_functions(functions)
    agent_executor = create_react_agent(giga_with_functions, functions)

    Agent with Memory and System Prompt

    To maintain conversation state, use a MemorySaver checkpointer and a state_modifier to define the agent's persona and instructions.

    from langgraph.checkpoint.memory import MemorySaver
    from langgraph.prebuilt import create_react_agent
    
    # Bind functions to the model
    giga_with_functions = giga.bind_functions(functions)
    
    # Create agent with memory and system instructions
    agent_executor = create_react_agent(
        giga_with_functions, 
        functions, 
        checkpointer=MemorySaver(),
        state_modifier="""Ты бот для отправки смс. Спроси у пользователя все нужные данные перед отправкой."""
    )
    
    # Running the agent
    config = {"configurable": {"thread_id": "user_session_1"}}
    resp = agent_executor.invoke(
        {"messages": [HumanMessage(content="Отправь смс с текстом привет на номер 2223334445")]}, 
        config=config
    )
    print(resp['messages'][-1].content)
  11. Run the Agent Debates example

    master

    The Agent Debates example demonstrates a multi-agent system where two AI agents debate a given topic. To run this application locally, follow these steps:

    1. Clone the repository.
    2. Create a clean Python environment.
    3. Install the required dependencies:
      pip install -r requirements.txt
    4. Configure your GigaChat API credentials in a .env file:
      GIGACHAT_CREDENTIALS=your_authorization_key
      GIGACHAT_BASE_URL=your_base_url
      Note: A sample .env file can be found in cookbook/sample_data.
    5. Launch the Streamlit application:
      streamlit run debates.py
    6. Access the interface at http://localhost:8501/ in your browser.
    pip install -r requirements.txt
    
    GIGACHAT_CREDENTIALS=ключ_авторизации
    GIGACHAT_BASE_URL=...
    
    streamlit run debates.py
  12. Debug Agent Debates using LangGraph Studio

    master

    You can debug the multi-agent debate logic using LangGraph Studio.

    1. Install the LangGraph CLI with in-memory support:
      pip install -U "langgraph-cli[inmem]"
    2. Start the development server:
      langgraph dev
    3. Open the provided URL in your browser to access the LangGraph Studio interface.
    4. To begin a debate, enter a topic in the Main Topic field.
    pip install -U "langgraph-cli[inmem]"
    langgraph dev