ai

package
v0.1.0-dev.20260127203210 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package ai provides AI provider abstraction and lazy configuration. All AI operations go through this package, which handles provider selection, configuration prompting, and LLM calls.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigPath

func ConfigPath() (string, error)

ConfigPath returns the path to the devlore config file.

func SaveConfig

func SaveConfig(cfg *Config) error

SaveConfig saves AI configuration to the config file.

Types

type AnthropicProvider

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

AnthropicProvider implements Provider for Anthropic Claude.

func NewAnthropicProvider

func NewAnthropicProvider(apiKey, model string) *AnthropicProvider

NewAnthropicProvider creates an Anthropic provider.

func (*AnthropicProvider) Available

func (a *AnthropicProvider) Available(ctx context.Context) bool

Available checks if the API key is valid.

func (*AnthropicProvider) Chat

Chat sends a chat completion request to Anthropic.

func (*AnthropicProvider) Name

func (a *AnthropicProvider) Name() string

Name returns "anthropic".

type ChatRequest

type ChatRequest struct {
	SystemPrompt string    // System prompt (instructions)
	Messages     []Message // Conversation messages
	Temperature  float64   // 0.0 for deterministic, higher for creativity
	MaxTokens    int       // Maximum response tokens (0 = provider default)
	JSONMode     bool      // Request JSON output if supported
}

ChatRequest represents a chat completion request.

type ChatResponse

type ChatResponse struct {
	Content      string // Response text
	FinishReason string // "stop", "length", etc.
	TokensUsed   int    // Total tokens consumed
}

ChatResponse contains the AI response.

type Config

type Config struct {
	Provider string `yaml:"provider"` // ollama, anthropic, openai, azure-openai
	Model    string `yaml:"model"`    // Model name (e.g., "llama3.1:8b", "claude-sonnet-4-20250514")
	Endpoint string `yaml:"endpoint"` // API endpoint (optional, for custom/azure)
	APIKey   string `yaml:"api_key"`  // API key (for cloud providers)
}

Config holds AI provider configuration per ADR-017.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default configuration (Ollama).

func LoadConfig

func LoadConfig() (*Config, error)

LoadConfig loads AI configuration from the config file. Returns nil config (not error) if no config exists.

type Message

type Message struct {
	Role    Role   // user, assistant
	Content string // Message content
}

Message represents a conversation turn.

type OllamaProvider

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

OllamaProvider implements Provider for local Ollama.

func NewOllamaProvider

func NewOllamaProvider(endpoint, model string) *OllamaProvider

NewOllamaProvider creates an Ollama provider.

func (*OllamaProvider) Available

func (o *OllamaProvider) Available(ctx context.Context) bool

Available checks if Ollama is running and the model is available.

func (*OllamaProvider) Chat

Chat sends a chat completion request to Ollama.

func (*OllamaProvider) Name

func (o *OllamaProvider) Name() string

Name returns "ollama".

type OpenAIProvider

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

OpenAIProvider implements Provider for OpenAI and compatible APIs.

func NewOpenAIProvider

func NewOpenAIProvider(apiKey, model, endpoint string) *OpenAIProvider

NewOpenAIProvider creates an OpenAI provider. endpoint can be empty for api.openai.com, or set for Azure/compatible APIs.

func (*OpenAIProvider) Available

func (o *OpenAIProvider) Available(ctx context.Context) bool

Available checks if the API key is set.

func (*OpenAIProvider) Chat

Chat sends a chat completion request to OpenAI.

func (*OpenAIProvider) Name

func (o *OpenAIProvider) Name() string

Name returns "openai".

type Provider

type Provider interface {
	// Chat sends messages and returns a response.
	Chat(ctx context.Context, req ChatRequest) (*ChatResponse, error)

	// Name returns the provider name (e.g., "ollama", "anthropic").
	Name() string

	// Available returns true if the provider is ready to use.
	Available(ctx context.Context) bool
}

Provider is the interface for AI backends.

func EnsureProvider

func EnsureProvider(ctx context.Context, noAI bool) (Provider, error)

EnsureProvider returns a configured AI provider, prompting the user if needed. If noAI is true, returns nil without prompting.

func NewProvider

func NewProvider(cfg Config) (Provider, error)

NewProvider creates a provider from configuration.

type Role

type Role string

Role identifies the message sender.

const (
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
)

Jump to

Keyboard shortcuts

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