Refactor llm package

This commit is contained in:
dwrz
2026-09-20 12:49:42 +00:00
parent eba6c2a028
commit 7224fea99f
11 changed files with 1176 additions and 1627 deletions
+48 -38
View File
@@ -7,20 +7,34 @@ import (
"time"
)
// Config holds the configuration for an LLM client.
const (
// chatCompletionsPath is the endpoint for chat completion requests.
chatCompletionsPath = "/chat/completions"
// chatTemplateEnableThinking is the chat-template argument that selects
// whether a reasoning model emits reasoning tokens.
chatTemplateEnableThinking = "enable_thinking"
// defaultTimeout limits requests when Config.Timeout is empty.
defaultTimeout = 5 * time.Minute
// sseDone is the sentinel that terminates a chat completion stream.
sseDone = "[DONE]"
)
// Config configures a Client.
type Config struct {
// Key is the API key for authentication.
// Key is sent as a bearer token. An empty key disables authentication.
Key string `yaml:"key"`
// SystemMessage is prepended to all conversations.
SystemMessage string `yaml:"system_message"`
// Timeout is the maximum duration for a query (e.g., "5m").
// Defaults to 5 minutes if empty.
// Provider is returned unchanged by Client.Provider. It is not sent to the
// inference endpoint.
Provider string `yaml:"provider"`
// Timeout limits each request, including reading a streaming response. It
// uses Go duration syntax and defaults to five minutes when empty.
Timeout string `yaml:"timeout"`
// URL is the base URL of the OpenAI-compatible API endpoint.
// URL is the endpoint root. Client appends /chat/completions or /models.
URL string `yaml:"url"`
}
// Validate checks that required configuration values are present and valid.
// Validate reports whether Timeout is a Go duration, when set, and URL is
// non-empty. It does not contact the endpoint.
func (cfg Config) Validate() error {
if cfg.Timeout != "" {
if _, err := time.ParseDuration(cfg.Timeout); err != nil {
@@ -33,30 +47,27 @@ func (cfg Config) Validate() error {
return nil
}
// Client wraps an OpenAI-compatible client with tool execution support.
// Client is an OpenAI-compatible inference client.
type Client struct {
baseURL string
httpc *http.Client
key string
log *slog.Logger
registry *Registry
systemMessage string
timeout time.Duration
tools []APITool
baseURL string
httpc *http.Client
key string
log *slog.Logger
provider string
timeout time.Duration
}
// NewClient creates a new LLM client with the provided configuration.
// The registry is optional; if nil, tool calling is disabled.
func NewClient(
cfg Config,
registry *Registry,
log *slog.Logger,
) (*Client, error) {
// NewClient creates a client from cfg. If cfg.Timeout is empty, requests have a
// five-minute timeout. log must be non-nil.
func NewClient(cfg Config, log *slog.Logger) (*Client, error) {
if err := cfg.Validate(); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
}
if log == nil {
return nil, fmt.Errorf("missing logger")
}
timeout := 5 * time.Minute
timeout := defaultTimeout
if cfg.Timeout != "" {
d, err := time.ParseDuration(cfg.Timeout)
if err != nil {
@@ -65,18 +76,17 @@ func NewClient(
timeout = d
}
llm := &Client{
baseURL: cfg.URL,
key: cfg.Key,
log: log,
systemMessage: cfg.SystemMessage,
registry: registry,
httpc: &http.Client{},
timeout: timeout,
}
return &Client{
baseURL: cfg.URL,
key: cfg.Key,
log: log,
provider: cfg.Provider,
httpc: &http.Client{},
timeout: timeout,
}, nil
}
// Convert tools from registry.
llm.tools = registry.ToAPI()
return llm, nil
// Provider returns the application-defined provider name supplied in Config.
func (c *Client) Provider() string {
return c.provider
}