cache

package
v1.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package cache implements a caching framework for LLM responses, mirroring langchain-core's caches. It provides a Cache interface, an in-memory implementation, and a wrapper that can be used to cache model calls keyed by the messages and model options.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Lookup returns a cached value for the key, or nil if not present.
	Lookup(ctx context.Context, key Key) (*Value, error)
	// Update stores a value for the key.
	Update(ctx context.Context, key Key, value *Value) error
	// Clear removes all entries.
	Clear(ctx context.Context) error
}

Cache is the interface for a response cache.

type Func

type Func func(ctx context.Context, key Key) (*Value, error)

Func is a model call that can be cached.

func Wrap

func Wrap(c Cache, fn Func) Func

Wrap returns a function that checks the cache before calling fn and stores the result on a miss. It mirrors langchain's cache wrapper.

type InMemory

type InMemory struct {
	// contains filtered or unexported fields
}

InMemory is a thread-safe in-memory cache.

func NewInMemory

func NewInMemory() *InMemory

NewInMemory builds an empty in-memory cache.

func (*InMemory) Clear

func (c *InMemory) Clear(_ context.Context) error

Clear implements Cache.

func (*InMemory) Lookup

func (c *InMemory) Lookup(_ context.Context, key Key) (*Value, error)

Lookup implements Cache.

func (*InMemory) Update

func (c *InMemory) Update(_ context.Context, key Key, value *Value) error

Update implements Cache.

type Key

type Key struct {
	// Model is the model identifier.
	Model string
	// Messages is a canonical serialization of the input messages.
	Messages string
	// Options is a canonical serialization of the relevant call options.
	Options string
}

Key identifies a cached model call.

type Noop

type Noop struct{}

Noop is a cache that never stores anything, useful as a default.

func (Noop) Clear

func (Noop) Clear(context.Context) error

Clear implements Cache.

func (Noop) Lookup

func (Noop) Lookup(context.Context, Key) (*Value, error)

Lookup implements Cache.

func (Noop) Update

func (Noop) Update(context.Context, Key, *Value) error

Update implements Cache.

type Value

type Value struct {
	// Content is the generated text.
	Content string
	// ToolCalls is a serialized form of any tool calls in the response.
	ToolCalls []byte
	// Raw is optional provider-specific raw data.
	Raw any
}

Value is a cached model response.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL