3.6 KiB
API
Generated from the package comment and every exported declaration. Do not edit.
Regenerate with UPDATE_API_DOCS=1 go test -run TestAPIDoc -count=1.
Package gollm
Package gollm is a chat LLM wire client: Chat, Stream, and List Models through an Ollama-native or OpenAI-compatible Adapter.
Types
Adapter
type Adapter string
Adapter selects the wire protocol a Client speaks. AdapterOpenAI uses POST /v1/chat/completions and GET /v1/models. AdapterOllama uses POST /api/chat and GET /api/tags.
AdapterOpenAI
AdapterOllama
const (
// AdapterOpenAI selects the OpenAI-compatible protocol.
AdapterOpenAI Adapter = "openai"
// AdapterOllama selects the Ollama native protocol.
AdapterOllama Adapter = "ollama"
)
Client
type Client struct {
// contains filtered or unexported fields
}
Client calls one Adapter at one base URL. An empty API key sends no Authorization header. A non-empty key is sent as a Bearer token on every request.
NewClient
func NewClient(adapter Adapter, baseURL, apiKey string) (*Client, error)
NewClient checks adapter and baseURL. It returns an error when adapter is not AdapterOpenAI or AdapterOllama, or when baseURL is empty or only whitespace. A trailing slash on baseURL is removed. apiKey may be empty.
Client.Chat
func (c *Client) Chat(ctx context.Context, messages []Message, opt Options) (string, error)
Chat sends messages and returns the assistant text. It returns an error when ctx is canceled, the HTTP status is not 2xx, the body cannot be decoded, or an OpenAI response contains no choices. An HTTP failure includes a short prefix of the response body.
Client.ListModels
func (c *Client) ListModels(ctx context.Context) ([]string, error)
ListModels returns model names reported by the server. AdapterOpenAI reads data[].id from GET /v1/models. AdapterOllama reads models[].name from GET /api/tags. It returns an error when ctx is canceled, the HTTP status is not 2xx, or the body cannot be decoded. Empty names are omitted.
Client.Stream
func (c *Client) Stream(ctx context.Context, messages []Message, opt Options, yield func(chunk string) error) error
Stream sends the same request as Chat with streaming enabled and calls yield for each non-empty text chunk, in order. It returns the error from yield. It also returns an error when ctx is canceled, the HTTP status is not 2xx, or a stream line cannot be decoded. AdapterOpenAI ends the stream when a data line is the token DONE. AdapterOllama reads one JSON object per line and yields message content.
Message
type Message struct {
Role Role
Content string
}
Message is one chat turn. Role and Content are sent unchanged.
Options
type Options struct {
Model string
Temperature *float64
MaxTokens *int
}
Options configures one Chat or Stream call. Model is the server-side model name. The Client does not check that it is set; the server rejects an empty model. A nil Temperature or MaxTokens is left out of the request. On AdapterOllama, MaxTokens is sent as num_predict.
Role
type Role string
Role is the speaker of one Message.
RoleSystem
RoleUser
RoleAssistant
const (
// RoleSystem is a system instruction.
RoleSystem Role = "system"
// RoleUser is the caller's turn.
RoleUser Role = "user"
// RoleAssistant is a previous model turn.
RoleAssistant Role = "assistant"
)