llm

package
v0.1.0-devel.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2025 License: Unlicense Imports: 13 Imported by: 0

Documentation

Overview

Package llm provides a minimal OpenAI compatible client with chat, completion and embedding helpers.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoModelSelected         = errors.New("no model specified")
	ErrNoEmbeddingReturned     = errors.New("no embedding returned")
	ErrEmptyCompletionResponse = errors.New("empty completion response")
)

Functions

func IsRetryableError

func IsRetryableError(err error) bool

IsRetryableError returns true if the error is retryable. It handles common HTTP codes and network timeouts.

func StripThinking

func StripThinking(s string) string

Types

type APIError

type APIError struct {
	StatusCode int
	Message    string
	Err        error
}

APIError wraps an HTTP error returned by the LLM provider.

func (*APIError) Error

func (e *APIError) Error() string

func (*APIError) Unwrap

func (e *APIError) Unwrap() error

type ApproxTokenCounter

type ApproxTokenCounter struct{}

ApproxTokenCounter estimates token usage by assuming roughly one token corresponds to four runes.

func (ApproxTokenCounter) Count

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Model         string
	Prompt        string
	ContextLength int
	Temperature   *float64
}

type ChatMessage

func TruncateHistory

func TruncateHistory(tc TokenCounter, msgs []ChatMessage, limit int) []ChatMessage

type ChatResponse

type ChatResponse struct {
	Content string // assistant text
	Usage   any
}

ChatResponse is a non-streaming chat response.

type ChatResponseIterator

type ChatResponseIterator iter.Seq2[ChatResponse, error]

ChatResponseIterator is a streaming sequence of chat responses.

type ChatSession

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

ChatSession represents a single conversational context. Not thread safe, create a separate ChatSession per goroutine or protect calls with a mutex.

func NewChat

func NewChat(c *Client, systemPrompt string, opts ...SessionOpt) *ChatSession

NewChat creates a new chat session with optional system prompt.

func (*ChatSession) ContextUsed

func (s *ChatSession) ContextUsed() ContextUsage

ContextUsed returns the number of tokens currently used in the session context.

func (*ChatSession) NewChat

func (s *ChatSession) NewChat(opts ...SessionOpt) *ChatSession

func (*ChatSession) Send

Send sends user messages and returns a response. The assistant's reply is appended to the internal history.

func (*ChatSession) SendStreaming

SendStreaming sends user messages and returns a streaming response iterator. The assistant's full reply is added to history after streaming completes.

type Client

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

Client implements an open ai api compatible client.

func NewClient

func NewClient(opts ...Option) *Client

NewClient creates a new OpenAI client.

func (*Client) Close

func (*Client) Close() error

Close releases any resources (no-op for OpenAI).

func (*Client) Embed

func (c *Client) Embed(ctx context.Context, req EmbedRequest) (*EmbedResponse, error)

Embed returns the embedding for a single input.

func (*Client) EmbedBatch

func (c *Client) EmbedBatch(ctx context.Context, req EmbedBatchRequest) (*EmbedBatchResponse, error)

EmbedBatch returns embeddings for multiple inputs.

func (*Client) GenerateCompletion

func (c *Client) GenerateCompletion(ctx context.Context, req CompletionRequest) (string, error)

GenerateCompletion creates a single-turn completion from a prompt.

func (*Client) ListModels

func (c *Client) ListModels(ctx context.Context) ([]string, error)

ListModels returns available model IDs.

type CompletionRequest

type CompletionRequest struct {
	Model         string
	SystemPrompt  string
	Prompt        string
	ContextLength int
	Temperature   *float64
}

type ContextUsage

type ContextUsage struct{ Used, Max int }

type EmbedBatchRequest

type EmbedBatchRequest struct {
	Model string
	Input []string
}

EmbedBatchRequest contains multiple inputs to embed with a model.

type EmbedBatchResponse

type EmbedBatchResponse struct {
	Vectors [][]float64
	Usage   *openai.CreateEmbeddingResponseUsage
}

type EmbedRequest

type EmbedRequest struct {
	Model string
	Input string
}

EmbedRequest specifies a model and input string for embedding.

type EmbedResponse

type EmbedResponse struct {
	Vector []float64
	Usage  *openai.CreateEmbeddingResponseUsage
}

type Option

type Option func(*config)

Option configures the OpenAI client.

func WithAPIKey

func WithAPIKey(key string) Option

WithAPIKey sets a custom API key.

func WithBaseURL

func WithBaseURL(baseURL string) Option

WithBaseURL sets a custom API base URL.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets a custom slog.Logger.

func WithModel

func WithModel(model string) Option

WithModel sets a model to use.

func WithTemperature

func WithTemperature(t *float64) Option

WithTemperature sets the LLM completion temperature.

type SessionOpt

type SessionOpt func(*ChatSession)

func WithDefaultContextLength

func WithDefaultContextLength(l int) SessionOpt

WithDefaultContextLength sets the maximum context length (in tokens) for a session.

func WithSessionLogger

func WithSessionLogger(logger *slog.Logger) SessionOpt

WithSessionLogger sets a session custom slog.Logger.

func WithSessionTemperature

func WithSessionTemperature(t *float64) SessionOpt

WithSessionTemperature sets the session LLM completion temperature.

func WithTokenCounter

func WithTokenCounter(tc TokenCounter) SessionOpt

WithTokenCounter sets a custom TokenCounter for estimating token usage.

type TokenCounter

type TokenCounter interface {
	Count(msgs ...openai.ChatCompletionMessageParamUnion) int
}

TokenCounter reports the number of tokens in a set of messages.

Jump to

Keyboard shortcuts

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