Skip to main content

Fake LLM

Overview

This documentation provides an overview of the fake package, which offers a simulated implementation of a Language Learning Model (LLM) for testing purposes in Go applications.

Installation

To use the fake package, import it into your Go project:

go get "github.com/tmc/langchaingo"

Prerequisites

Ensure you have Go programming language installed on your machine (version 1.15 or higher recommended).

Example Usage

Here is an example demonstrating how to use the fake package:

package main

import (
"context"
"fmt"
"log"

"github.com/tmc/langchaingo/llms/fake"
)

func main() {
// Creating a fake LLM with initial responses.
responses := []string{
"Hello!",
"How are you?",
"I'm fine, thanks.",
}
llm := fake.NewFakeLLM(responses)

// Calling the fake LLM with a prompt.
ctx := context.Background()
response, err := llm.Call(ctx, "Hi there!")
if err != nil {
log.Printf("Error calling LLM: %v\n", err)
} else {
fmt.Println("LLM Response:", response)
}

// Adding a new response and testing again.
llm.AddResponse("Goodbye!")
response, err = llm.Call(ctx, "See you later!")
if err != nil {
log.Printf("Error calling LLM: %v\n", err)
} else {
fmt.Println("LLM Response:", response)
}
}

API Reference

Constructor

func NewFakeLLM(responses []string) *LLM

Creates a new instance of the fake LLM with the provided responses.

Methods

Call

func (f *LLM) Call(ctx context.Context, prompt string, options ...llms.CallOption) (string, error)

Simulates calling the model with a specific prompt and returns a predefined response. Supports all standard LLM options like WithTemperature, WithMaxTokens, etc.

GenerateContent

func (f *LLM) GenerateContent(ctx context.Context, messages []llms.MessageContent, options ...llms.CallOption) (*llms.ContentResponse, error)

Simulates generating content from message sequences. This is the modern interface that supports multi-modal inputs.

Reset

func (f *LLM) Reset()

Resets the internal response index, allowing responses to cycle through from the beginning again.

AddResponse

func (f *LLM) AddResponse(response string)

Adds a new response to the list of possible responses of the fake LLM.

Purpose

The fake package is designed to facilitate testing of applications that interact with language learning models, without relying on real model implementations. It helps validate application logic and behavior in a controlled environment.