How brainlid/langchain works
Project type and purpose
• This is an Elixir library/framework (“LangChain”) for wiring together LLMs (large language models), tools, data sources and application logic into higher-level “chains.” It is not a stand-alone app but a collection of modules you pull into your Elixir project to build AI-powered workflows.
File structure
• Top-level: mix.exs, config/, lib/, notebooks/, test/
• lib/ splits into feature modules:
• LangChain.Chains (LLMChain, DataExtractionChain, RoutingChain, etc.)
• LangChain.ChatModels (ChatOpenAI, ChatAnthropic, ChatBumblebee, etc.)
• LangChain.Function (custom tool integration) + lib/tools (Calculator example)
• LangChain.Message & MessageProcessors (JSON decoding, deltas)
• LangChain.PromptTemplate
• LangChain.TextSplitter (various splitting strategies)
• LangChain.Images (generated/open_ai)
• LangChain.Utils (parsers, decoders, chain_result)
• Config and Telemetry modules
• notebooks/ show usage and demos, test/ exercises every module.
README insights
• Entry point: LangChain.Chains.LLMChain
• Supports many LLM services via modular adapters
• Key abstractions: Chains (higher-level workflows), Components (prompt templates, text splitters), ChatModels (adapters), Functions (tools), Message objects, Prompt caching, etc.
Main components & interactions
a. Components
• Your Elixir Application (uses LangChain)
• Public API layer: Chains (LLMChain, other off-the-shelf chains) + Function/Tool registration + PromptTemplate
• Core engine: builds Message structs, applies MessageProcessors, splits text, tracks TokenUsage, handles Telemetry
• Adapters: ChatModels modules (OpenAI, Anthropic, Google, Ollama, Bumblebee, Perplexity, etc.)
• External services/hosts: cloud APIs or self-hosted model servers
• Optional local Tools: LangChain.Function hooks (e.g., Calculator)
• Auxiliary modules: TextSplitter, Images, Utils, Config, Telemetry
b. Relationships & data flow
1. App calls LLMChain.new!(opts) |> add_message(...) |> run()
2. LLMChain composes a prompt via PromptTemplate, attaches context, tools
3. It serializes to JSON, hands off to a ChatModel adapter
4. Adapter invokes external service (Req HTTP client or Nx.Serving)
5. Streaming or batch response flows back through MessageProcessors
6. Library updates ChainResult, may invoke registered Functions (tool calls)
7. Final ChainResult returned to App
c. Architectural patterns
• Functional pipelines (|> syntax)
• Adapter pattern for ChatModels
• Strategy for splitting text (TextSplitter modules)
• Modular components (prompt templates, message processors, tools)
• Dependency injection of context and tools into chains
d. Technologies & libs
• Elixir/OTP modules
• Req for HTTP calls
• Nx & Bumblebee for self-hosted models
• JSON parsing / streaming via LangChain.MessageProcessors
• Telemetry for metrics