Copyright | (c) 2025 Tushar Adhatrao |
---|---|
License | MIT |
Maintainer | Tushar Adhatrao <tusharadhatrao@gmail.com> |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Langchain.Runnable.Core
Description
This module defines the Runnable
typeclass, which is the fundamental abstraction in the
Haskell implementation of LangChain Expression Language (LCEL). A Runnable
represents any
component that can process an input and produce an output, potentially with side effects.
The Runnable
abstraction enables composition of various LLM-related components into
processing pipelines, including:
- Language Models
- Prompt Templates
- Document Retrievers
- Text Splitters
- Embedders
- Vector Stores
- Output Parsers
By implementing the Runnable
typeclass, components can be combined using the combinators
provided in Langchain.Runnable.Chain.
Synopsis
- class Runnable r where
- type RunnableInput r
- type RunnableOutput r
- invoke :: r -> RunnableInput r -> IO (Either String (RunnableOutput r))
- batch :: r -> [RunnableInput r] -> IO (Either String [RunnableOutput r])
- stream :: r -> RunnableInput r -> (RunnableOutput r -> IO ()) -> IO (Either String ())
Documentation
class Runnable r where Source #
The core Runnable
typeclass represents anything that can "run" with an input and produce an output.
This typeclass is the foundation of the LangChain Expression Language (LCEL) in Haskell, allowing different components to be composed into processing pipelines.
To implement a Runnable
, you must:
- Define the input and output types using associated type families
- Implement the
invoke
method - Optionally override
batch
andstream
for specific optimizations
Example implementation:
data TextSplitter = TextSplitter { chunkSize :: Int, overlap :: Int } instance Runnable TextSplitter where type RunnableInput TextSplitter = String type RunnableOutput TextSplitter = [String] invoke splitter text = do -- Implementation of text splitting logic let chunks = splitTextIntoChunks (chunkSize splitter) (overlap splitter) text return $ Right chunks
Minimal complete definition
Associated Types
type RunnableInput r Source #
The type of input the runnable accepts.
For example, an LLM might accept String
or PromptValue
as input.
type RunnableOutput r Source #
The type of output the runnable produces.
For example, an LLM might produce String
or LLMResult
as output.
Methods
invoke :: r -> RunnableInput r -> IO (Either String (RunnableOutput r)) Source #
Core method to invoke (run) this component with a single input.
This is the primary method that must be implemented for any Runnable
.
It processes a single input and returns either an error message or the output.
Example usage:
let model = OpenAI { temperature = 0.7, model = "gpt-3.5-turbo" } result <- invoke model "Explain monads in simple terms." case result of Left err -> putStrLn $ "Error: " ++ err Right response -> putStrLn response
batch :: r -> [RunnableInput r] -> IO (Either String [RunnableOutput r]) Source #
Batch process multiple inputs at once.
This method can be overridden to provide more efficient batch processing, particularly for components like LLMs that may have batch APIs.
The default implementation simply maps invoke
over each input and
sequences the results.
Example usage:
let retriever = VectorDBRetriever { ... } questions <- ["What is Haskell?", "Explain monads.", "How do I install GHC?"] result <- batch retriever questions case result of Left err -> putStrLn $ "Batch processing failed: " ++ err Right docs -> mapM_ print docs
stream :: r -> RunnableInput r -> (RunnableOutput r -> IO ()) -> IO (Either String ()) Source #
Stream results for components that support streaming.
This method is particularly useful for LLMs that can stream tokens as they're generated, allowing for more responsive user interfaces.
The callback function is called with each piece of the output as it becomes available.
Example usage:
let model = OpenAI { temperature = 0.7, model = "gpt-3.5-turbo", streaming = True } result stream model "Write a story about a programmer." $ chunk - do putStr chunk hFlush stdout case result of Left err -> putStrLn $ "\nError: " ++ err Right _ -> putStrLn "\nStreaming completed successfully."