llm

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package llm provides a provider-agnostic LLM client interface and implementations for use by stringer's analysis features.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnthropicOption

type AnthropicOption func(*anthropicConfig)

AnthropicOption configures an AnthropicProvider.

func WithAPIKey

func WithAPIKey(key string) AnthropicOption

WithAPIKey sets the API key. If not provided, the provider reads ANTHROPIC_API_KEY from the environment.

func WithBaseURL added in v1.0.0

func WithBaseURL(baseURL string) AnthropicOption

WithBaseURL overrides the API base URL. This is intended for testing with httptest servers and should not be used in production.

func WithMaxRetries

func WithMaxRetries(n int) AnthropicOption

WithMaxRetries sets the maximum number of retries for transient errors.

func WithModel

func WithModel(model string) AnthropicOption

WithModel overrides the default model for all requests.

type AnthropicProvider

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

AnthropicProvider implements Provider using the official Anthropic SDK.

func NewAnthropicProvider

func NewAnthropicProvider(opts ...AnthropicOption) (*AnthropicProvider, error)

NewAnthropicProvider creates a new Anthropic provider. It returns an error if no API key is available (neither via option nor env).

func (*AnthropicProvider) Complete

func (p *AnthropicProvider) Complete(ctx context.Context, req Request) (*Response, error)

Complete sends a completion request to the Anthropic Messages API.

func (*AnthropicProvider) MaxRetries

func (p *AnthropicProvider) MaxRetries() int

MaxRetries returns the configured max retry count.

func (*AnthropicProvider) Model

func (p *AnthropicProvider) Model() string

Model returns the default model configured for this provider.

type MockProvider

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

MockProvider is a test double that returns pre-configured responses in sequence. After all responses are exhausted, it keeps returning the last one. It records every request for later assertion.

func NewMockProvider

func NewMockProvider(responses ...MockResponse) *MockProvider

NewMockProvider creates a mock that returns the given responses in order. If no responses are provided, Complete returns an empty Response.

func (*MockProvider) Calls

func (m *MockProvider) Calls() []Request

Calls returns a copy of all requests received by this mock.

func (*MockProvider) Complete

func (m *MockProvider) Complete(ctx context.Context, req Request) (*Response, error)

Complete returns the next canned response and records the request. It respects context cancellation.

func (*MockProvider) Reset

func (m *MockProvider) Reset()

Reset clears call history and resets the response index to zero.

type MockResponse

type MockResponse struct {
	Content string
	Err     error
}

MockResponse defines a canned response for the mock provider.

type Provider

type Provider interface {
	// Complete sends a prompt to the LLM and returns the response.
	// Implementations must respect context cancellation and deadlines.
	Complete(ctx context.Context, req Request) (*Response, error)
}

Provider abstracts an LLM API behind a single synchronous completion method.

type Request

type Request struct {
	// Prompt is the user message to send.
	Prompt string

	// Model overrides the provider's default model. If empty, the provider
	// uses its configured default.
	Model string

	// MaxTokens limits the response length. If zero, the provider uses its
	// own default.
	MaxTokens int

	// Temperature controls randomness. If nil, the provider uses its default.
	Temperature *float64

	// SystemPrompt sets the system instruction for the completion.
	SystemPrompt string
}

Request describes a single completion request.

type Response

type Response struct {
	// Content is the text returned by the model.
	Content string

	// Model is the model that actually served the request (may differ from
	// the requested model if the provider remapped it).
	Model string

	// Usage reports token consumption.
	Usage Usage
}

Response holds the result of a completion call.

type Usage

type Usage struct {
	InputTokens  int
	OutputTokens int
}

Usage tracks input and output token counts for a single request.

Jump to

Keyboard shortcuts

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