Here is a little guide to : the Official Ollama API package, the community go-ollama-sdk, and the OpenAI Go client configured for Ollama. For each, we describe its features and include practical code examples, concluding with a comparison to help developers choose the right fit.
1. The Official Ollama Go SDK (github.com/ollama/ollama/api)
This package is maintained by Ollama and provides a fully typed, comprehensive client for all Ollama REST API features. It includes methods for text generation, chat, model management, offers an OpenAI-compatible API endpoint that can be accessed using the official OpenAI Go SDK. This approach leverages existing OpenAI tooling and ecosystems but requires configuring the base URL.
Features
- Compatibility with OpenAI's client libraries
- Supports chat, completion, and embeddings
- Easy to integrate if familiar with OpenAI tools
Code Example
Using the OpenAI SDK to query Ollama:
package main
import (
"context"
"log"
"github.com/openai/openai-go"
oaioption "github.com/openai/openai-go/option"
)
func main() {
ctx := context.Background()
client := openai.NewClient(
oaioption.WithBaseURL("http://localhost:11434/v1"),
oaioption.WithAPIKey("ollama"),
)
resp, err := client.Chat.Completions.New(ctx, openai.ChatCompletionNewParams{
Model: "llama3",
Messages: []openai.ChatCompletionMessageParamUnion{
openai.UserMessage("Tell me a joke about Go."),
},
})
if err != nil {
log.Fatalf("Failed to generate: %v", err)
}
log.Println("Response:", resp.Choices[0].Message.Content)
}
Strengths
- Leverages the mature OpenAI ecosystem
- Easy if already using OpenAI SDKs
- Compatible with existing codebases
Comparison Summary
| Aspect | needed.
|
|---|
SOCIAL SHARE CARD GENERATOR