How epicgazel/chain-as-tool works
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”)