4.5 KiB
go-llm guide
How to use the library. Symbol signatures and doc comments are in API. Words for the domain are in CONTEXT.md.
Adapters
Purpose
An adapter is the wire protocol a client speaks. There are two: Ollama native and OpenAI-compatible. One client is bound to one adapter, one base URL, and an optional API key.
When to use it
Choose the adapter when constructing the client. Use the OpenAI adapter for any server that implements /v1/chat/completions and /v1/models. Use the Ollama adapter for Ollama's /api/chat and /api/tags.
Call sequence
- Pick
AdapterOpenAIorAdapterOllama. - Call
NewClientwith the adapter, the base URL, and the API key. - Use that client for Chat, Stream, and List Models. The client adds the path for the adapter.
A trailing slash on the base URL is removed. An empty API key sends no Authorization header. A non-empty key is sent as Bearer on every request.
OpenAI paths are POST /v1/chat/completions and GET /v1/models. Ollama paths are POST /api/chat and GET /api/tags. On Ollama, max tokens are sent as num_predict.
Errors
NewClient returns an error when the adapter is not one of the two values, or when the base URL is empty or only whitespace. It does not contact the server.
Example
client, err := gollm.NewClient(gollm.AdapterOpenAI, "http://localhost:8000", "")
if err != nil {
return err
}
_ = client
Chat
Purpose
Chat sends a list of messages and returns the assistant's text in one response.
When to use it
Use Chat when the caller wants the full reply before continuing. Use Stream when the caller wants to show text as it arrives.
Call sequence
- Build a
[]Message. Each message has aRole(RoleSystem,RoleUser, orRoleAssistant) andContent. - Set
Options.Modelto the server-side model name. - Set
Options.TemperatureorOptions.MaxTokensonly when the call should send them. A nil pointer is omitted. On the Ollama adapter,MaxTokensis sent asnum_predict. - Call
Client.Chatwith a context, the messages, and the options. - Use the returned string as the assistant text.
The client does not check that Model is set. The server rejects an empty model.
Errors
Chat returns an error when the context is canceled, the HTTP status is not 2xx, or the body cannot be decoded. An OpenAI response with no choices is also an error. An HTTP failure includes a short prefix of the response body.
Example
temp := 0.2
text, err := client.Chat(ctx, []gollm.Message{{
Role: gollm.RoleUser,
Content: "Hello",
}}, gollm.Options{Model: "llama", Temperature: &temp})
if err != nil {
return err
}
_ = text
Stream
Purpose
Stream sends the same request as Chat and delivers the assistant text as non-empty chunks, in order.
When to use it
Use Stream when the caller wants incremental text. The request body matches Chat, with streaming turned on.
Call sequence
- Build messages and options the same way as Chat.
- Call
Client.Streamwith ayieldfunction.yieldreceives each non-empty chunk. - Return nil from
yieldto keep reading. Return an error fromyieldto stop the stream.
The OpenAI adapter ends the stream when a data line is the token DONE. The Ollama adapter reads one JSON object per line and yields message.content.
Errors
Stream returns the error from yield. It also returns an error when the context is canceled, the HTTP status is not 2xx, or a stream line cannot be decoded.
Example
var reply strings.Builder
err := client.Stream(ctx, []gollm.Message{{
Role: gollm.RoleUser,
Content: "Hello",
}}, gollm.Options{Model: "llama"}, func(chunk string) error {
reply.WriteString(chunk)
return nil
})
if err != nil {
return err
}
List Models
Purpose
List Models returns the model names the server reports.
When to use it
Use it to discover names to put in Options.Model. The client does not cache the list.
Call sequence
- Use the same client as Chat.
- Call
Client.ListModelswith a context. - Use the returned names. Empty names from the server are omitted.
The OpenAI adapter reads data[].id from GET /v1/models. The Ollama adapter reads models[].name from GET /api/tags.
Errors
ListModels returns an error when the context is canceled, the HTTP status is not 2xx, or the body cannot be decoded.
Example
names, err := client.ListModels(ctx)
if err != nil {
return err
}
_ = names