# epicgazel/chain-as-tool architecture

> Architecture diagram of the GitHub repository epicgazel/chain-as-tool, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/epicgazel/chain-as-tool
- Repository: https://github.com/epicgazel/chain-as-tool

## Overview

##### Project type and purpose
- Chain-As-Tool is an open-source Python library (tool) that helps developers build and run linear prompt‐based chains against language models (via LangChain and OpenAI).
- It is not a webapp or microservice but a CLI-driven library that orchestrates prompts, tools, memory, and the LLM into a conversational agent.

##### File structure
- Top-level modules:
- ChainTools.py – core library code (defines create_chain_tool, core chain classes)
- using_chaintool.py – example CLI application showing how to wire up the library
- Config files:
- .gitignore and .gitattributes – version control
- No database or frontend directories
- Dependencies implied by README: langchain, dotenv, openai

##### Architecture components to capture
a. Core library module (“ChainTools”)
– Handles prompt template wrapping, chain‐tool definitions, tool registration functions
b. Example application (“using_chaintool”)
– CLI loop: reads user input, passes it to the AgentExecutor, prints responses
c. LLM Service Layer
– ChatOpenAI (from LangChain) – communicates with OpenAI API (external HTTP service)
d. Tool Manager
– load_tools function – manages plugins (llm-math, wikipedia, ddg-search) that the agent can call
e. Memory Store
– ConversationBufferMemory – in‐memory store of conversation history
f. Agent Executor
– OpenAIFunctionsAgent + AgentExecutor – orchestrates calls to LLM, tools, memory, and error handling
g. Environment Configuration
– dotenv loader reads API keys from .env

##### Relationships and data flow
1. User (CLI) → AgentExecutor.run(input)
2. AgentExecutor uses Memory Store to retrieve past chat_history
3. AgentExecutor builds prompt (via PromptTemplate and chain-tool definitions)
4. AgentExecutor calls LLM Service (ChatOpenAI) over HTTP
5. LLM may ask to invoke a Tool → Tool Manager dispatches request to the appropriate external function (e.g., wiki search)
6. Tool Manager returns results to AgentExecutor → incorporated in conversation
7. AgentExecutor updates Memory Store with new messages
8. AgentExecutor returns response to CLI → printed to user

##### Architectural patterns and design principles
- Pipeline/Chain‐of‐Responsibility: each “tool” is a link in the chain
- Plugin architecture: tools are loaded dynamically via load_tools
- Separation of concerns: Core library (ChainTools) vs example application (using_chaintool) vs external services (OpenAI, search APIs)
- Stateless LLM service with stateful in‐memory conversation history

- Notes on data formats (e.g., “JSON chat_history,” “Function call payload”)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Example User and CLI Application
    User["User (CLI)"]:::example
    usingCLI["using_chaintool.py"]:::example

    %% Environment Configuration
    config[".env"]:::config

    %% Core Library Module
    subgraph "Core Library (ChainTools.py)"
        direction TB
        prompt["PromptTemplate Factory"]:::core
        ChainToolAPI["create_chain_tool API"]:::core
        ToolManager["Tool Manager (load_tools)"]:::core
        MemoryStore["ConversationBufferMemory"]:::core
        AgentExecutor["OpenAIFunctionsAgent & AgentExecutor"]:::core
    end

    %% LLM Service Layer
    subgraph "LLM Service Layer"
        direction TB
        ChatClient["ChatOpenAI"]:::external
        OpenAIAPI[(OpenAI API)]:::external
    end

    %% Tool Plugins
    subgraph "Tool Plugins"
        direction TB
        MathTool["llm-math"]:::external
        WikiTool["wikipedia"]:::external
        SearchTool["ddg-search"]:::external
    end

    %% Connections: CLI to Core
    User -->|"inputs command"| usingCLI
    usingCLI -->|"calls run()"| AgentExecutor

    %% Core interactions
    AgentExecutor -->|"retrieves history"| MemoryStore
    AgentExecutor -->|"builds prompt via"| prompt
    AgentExecutor -->|"uses API"| ChainToolAPI
    AgentExecutor -->|"calls LLM"| ChatClient
    ChatClient -->|"HTTP request"| OpenAIAPI
    AgentExecutor -->|"invokes tool"| ToolManager
    ToolManager --> MathTool
    ToolManager --> WikiTool
    ToolManager --> SearchTool
    ToolManager -->|"returns result"| AgentExecutor
    AgentExecutor -->|"updates history"| MemoryStore
    AgentExecutor -->|"responds"| usingCLI

    %% Configuration
    config --> AgentExecutor
    config --> ChatClient
    config --> ToolManager

    %% Click Events
    click prompt "https://github.com/epicgazel/chain-as-tool/blob/main/ChainTools.py"
    click ChainToolAPI "https://github.com/epicgazel/chain-as-tool/blob/main/ChainTools.py"
    click ToolManager "https://github.com/epicgazel/chain-as-tool/blob/main/ChainTools.py"
    click MemoryStore "https://github.com/epicgazel/chain-as-tool/blob/main/ChainTools.py"
    click AgentExecutor "https://github.com/epicgazel/chain-as-tool/blob/main/ChainTools.py"
    click usingCLI "https://github.com/epicgazel/chain-as-tool/blob/main/using_chaintool.py"
    click config "https://github.com/epicgazel/chain-as-tool/blob/main/.env"

    %% Styles
    classDef core fill:#bbdefb,stroke:#1f497d,stroke-width:1px
    classDef external fill:#c8e6c9,stroke:#256029,stroke-width:1px
    classDef example fill:#ffe0b2,stroke:#805000,stroke-width:1px
    classDef config fill:#eeeeee,stroke:#666666,stroke-width:1px
```
