Easy Ollama package for Golang
go get -u github.com/jonathanhecl/gollama
package main
import (
"context"
"fmt"
"github.com/jonathanhecl/gollama"
)
func main() {
ctx := context.Background()
g := gollama.New("llama3.2") // Create a new Gollama with the default model
g.Verbose = true // Enable verbose mode
if err := g.PullIfMissing(ctx); err != nil { // Pull the model if it is not available
fmt.Println("Error:", err)
return
}
prompt := "what is the capital of Argentina?" // The prompt to send to the model
type Capital struct {
Capital string `required:"true"`
}
option := gollama.StructToStructuredFormat(Capital{}) // Convert the struct to a structured format
fmt.Printf("Option: %+v\n", option)
output, err := g.Chat(ctx, prompt, option) // Generate a response
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Printf("\n%s\n", output.Content) // Print the response
}
- Support Vision models
- Support Tools models
- Support Structured format
- Downloads model if missing
- Chat with model
- Generates embeddings with model
- Get model details
- Get list of available models
New(model string) *Gollama
- Create a new GollamaNewWithConfig(config Gollama) *Gollama
- Create a new Gollama with a pre-populated configChat(prompt string, ...ChatOption) (*gollama.ChatOutput, error)
- Generate a responseEmbedding(prompt string) ([]float64, error)
- Generate embeddingsListModels() ([]ModelInfo, error)
- List models available on ollamaHasModel(model string) (bool, error)
- Check if model is availableModelSize(model string) (int, error)
- Get model size from ollamaPullModel(model string) error
- Pull modelPullIfMissing(model ...string) error
- Pull model if missingGetModels() ([]string, error)
- Get list of available modelsGetDetails(model ...string) ([]ModelDetails, error)
- Get model details from ollamaVersion() (string, error)
- Get ollama versionStructToStructuredFormat(interface{}) StructuredFormat
- Converts a Go struct to a Gollama structured formatCosenoSimilarity(vector1, vector2 []float64) float64
- Calculates the cosine similarity between two vectors- output.
DecodeContent(output interface{}) error
- Decodes the content of a Gollama response