🦜️🔗LangChain Haskell

⚡ Building applications with LLMs through composability in Haskell! ⚡

Introduction

LangChain Haskell is a robust port of the original LangChain library, bringing its powerful natural language processing capabilities to the Haskell ecosystem. This library enables developers to build applications powered by large language models (LLMs) with ease and flexibility.

Documentation

Hackage API reference

Features

  • LLM Integration: Seamlessly interact with various language models, including OpenAI’s GPT series and others.
  • Prompt Templates: Create and manage dynamic prompts for different tasks.
  • Memory Management: Implement conversational memory to maintain context across interactions.
  • Agents and Tools: Develop agents that can utilize tools to perform complex tasks.
  • Document Loaders: Load and process documents from various sources for use in your applications.
  • Text Splitter: Components for splitting text into smaller chunks for processing.
  • Output Parser: Components for parsing and processing the output of LLMs.
  • VectorStore and Retriever: Mechanism for storing and retrieving document embeddings.
  • Embeddings: Components for generating vector representations of text.

Current Supported Providers

  • Ollama
  • OpenAI
  • Huggingface
  • More to come…

Installation

To use LangChain Haskell in your project, add it to your package dependencies. If you’re using Stack, include it in your package.yaml:

dependencies:
  - base < 5
  - langchain-hs

Then, run the build command for your respective build tool to fetch and compile the dependency.

Quickstart

Here’s a simple example demonstrating how to use LangChain Haskell to interact with an LLM:

{-# LANGUAGE OverloadedStrings #-}
module Main (main) where

import Langchain.LLM.Ollama
import Langchain.LLM.Core
import Langchain.PromptTemplate
import Langchain.Callback
import qualified Data.Map.Strict as Map
import qualified Data.Text as T

main :: IO ()
main = do 
  let ollamaLLM = Ollama "llama3.2" [stdOutCallback]
      prompt = PromptTemplate "Translate the following English text to French: {text}"
      input = Map.fromList [("text", "Hello, how are you?")]
      
  case renderPrompt prompt input of
    Left e -> putStrLn $ "Error: " ++ e
    Right renderedPrompt -> do
      eRes <- generate ollamaLLM renderedPrompt Nothing
      case eRes of
        Left err -> putStrLn $ "Error: " ++ err
        Right response -> putStrLn $ "Translation: " ++ (T.unpack response)

Contributing

Contributions are welcome! If you’d like to contribute, please fork the repository and submit a pull request. For major changes, please open an issue first to discuss what you’d like to change.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgements

This project is inspired by and builds upon the original LangChain library and its various ports in other programming languages. Special thanks to the developers of those projects for their foundational work.

Changes

Changelog for langchain-hs

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to the Haskell Package Versioning Policy.

Unreleased

0.0.2.0 - 2025-05-04

Added

  • Added OpenAI LLM integration.
  • Added DirectoryLoader for loading Documents from a directory.
  • Added HuggingFace LLM integration.
  • Added docusaurus documentation.
  • Added OpenAI embeddings integration.
  • Added GHC CI matrix build.
  • Added TokenBufferMemory Memory integration.
  • Added RetrievalQA chain.
  • Added CalculatorTool tool.

Fixed

  • Fixed loadAndSplit function for PdfLoader.
  • Minor documentation fixes.
  • Fixed WebScraper to only scrape textual content.
  • Made langchain-hs buildable till stack-lts-19.33
  • Fixed React agent.

Changed

  • Generalized LLMParams to accept different type per LLM.