ollama is a Go client for a local or remote Ollama
server. It implements the github.com/goloop/ai interface, so it looks and
works like every other goloop AI provider, and exposes Ollama's native
endpoints on top.
- Chat:
Generatefor a single response,Streamfor token-by-token output throughiter.Seq2. - Tool use (function calling) and image input.
- Native
ChatCompletionandChatStreamover/api/chat. - Embeddings, the installed-model list and per-model details (
Show). - Retries on 429 and 5xx with backoff; normalized, typed API errors.
- Depends only on
github.com/goloop/aiand the standard library. - Structured output:
ai.Formatmaps onto the server'sformatfield ("json"or a schema); read the reply withresp.JSON(&v). - Hosted capabilities:
ai.Request.Hostedis refused withai.ErrNoHosted, because a local model server has no search to run.
go get github.com/goloop/ollamapackage main
import (
"context"
"fmt"
"github.com/goloop/ai"
"github.com/goloop/ollama"
)
func main() {
c := ollama.New("") // local server on http://localhost:11434
resp, err := c.Generate(context.Background(), &ai.Request{
Model: ollama.ModelLlama32,
Messages: []ai.Message{ai.UserText("Say hello in one word.")},
})
if err != nil {
panic(err)
}
fmt.Println(resp.Text())
}c := ollama.New(apiKey, ollama.WithBaseURL("https://ollama.example.com"))The API key is optional; it is sent as a bearer token when set, for authenticated proxies in front of a server.
Ollama streams newline-delimited JSON rather than Server-Sent Events; Stream
hides that difference and yields the same ai.Chunk values as every other
provider.
for chunk, err := range c.Stream(ctx, req) {
if err != nil {
break
}
fmt.Print(chunk.Text)
}vecs, err := c.Embed(ctx, ollama.ModelLlama32, "hello", "world")
models, err := c.Models(ctx) // what is installed locally
info, err := c.Show(ctx, ollama.ModelLlama32) // template, params, capabilitiesFull reference: DOC.md (Ukrainian: DOC.UK.md).
See CONTRIBUTING.md.
MIT - see LICENSE.
Report malformed stream events and reject nil requests.