langchain-hs-0.0.1.0: Haskell implementation of Langchain
Copyright(c) 2025 Tushar Adhatrao
LicenseMIT
MaintainerTushar Adhatrao <tusharadhatrao@gmail.com>
Stabilityexperimental
Safe HaskellSafe-Inferred
LanguageHaskell2010

Langchain.Callback

Description

This module provides a callback system for Langchain's language model operations. Callbacks allow users to perform actions at different stages of an LLM operation, such as when it starts, completes, or encounters an error. This is useful for logging, monitoring, or integrating with external systems.

The callback system is inspired by the Langchain Python library's callback functionality: Langchain Callbacks.

Examples

See the documentation for stdOutCallback for a basic example, or check the examples for generate, chat, and stream in the Ollama module for practical usage in LLM operations.

Synopsis

Event Types

data Event Source #

Represents different events that can occur during a language model operation. These events can be used to trigger callbacks at various stages.

Constructors

LLMStart

Indicates the start of an LLM operation, such as generating text or chatting.

LLMEnd

Indicates the successful completion of an LLM operation.

LLMError String

Indicates an error occurred during the LLM operation, with the error message.

Instances

Instances details
Show Event Source # 
Instance details

Defined in Langchain.Callback

Methods

showsPrec :: Int -> Event -> ShowS #

show :: Event -> String #

showList :: [Event] -> ShowS #

Eq Event Source # 
Instance details

Defined in Langchain.Callback

Methods

(==) :: Event -> Event -> Bool #

(/=) :: Event -> Event -> Bool #

Callback Interface

type Callback = Event -> IO () Source #

A callback is a function that takes an Event and performs some IO action. This allows users to react to different stages of LLM operations, such as logging or updating a UI.

Examples

To create a custom callback that logs events to a file:

import System.IO
myCallback :: Callback
myCallback event = do
  handle <- openFile "llm_log.txt" AppendMode
  case event of
    LLMStart -> hPutStrLn handle "LLM operation started"
    LLMEnd -> hPutStrLn handle "LLM operation completed"
    LLMError err -> hPutStrLn handle $ "LLM error: " ++ err
  hClose handle

Standard Implementations

stdOutCallback :: Callback Source #

A standard callback that prints event messages to the standard output. This is useful for simple debugging or monitoring of LLM operations.

Examples

Using stdOutCallback in an LLM operation:

let callbacks = [stdOutCallback]
result <- generate (Ollama "llama3.2:latest" callbacks) "What is 2+2?" Nothing
-- Output will include:
-- Model operation started
-- Model completed with
-- (depending on success or error)