A community-driven registry for Claude, Cursor, Windsurf, Cline & more. Not affiliated with Anthropic.
Are you the author? Sign in to claim
Unified Go interface for Language Model (LLM) providers. Simplifies LLM integration with flexible prompt management and
gollm is a Go package designed to help you build your own AI golems. Just as the mystical golem of legend was brought to life with sacred words, gollm empowers you to breathe life into your AI creations using the power of Large Language Models (LLMs). This package simplifies and streamlines interactions with various LLM providers, offering a unified, flexible, and powerful interface for AI engineers and developers to craft their own digital servants.
ChainOfThought for complex reasoning tasks.gollm works with a variety of LLM providers:
gollm can handle a wide range of AI-powered tasks, including:
ChainOfThought function to analyze complex problems step-by-step.go get github.com/teilomillet/gollm
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/teilomillet/gollm"
)
func main() {
// Load API key from environment variable
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
log.Fatalf("OPENAI_API_KEY environment variable is not set")
}
// Create a new LLM instance with custom configuration
llm, err := gollm.NewLLM(
gollm.SetProvider("openai"),
gollm.SetModel("gpt-4o-mini"),
gollm.SetAPIKey(apiKey),
gollm.SetMaxTokens(200),
gollm.SetMaxRetries(3),
gollm.SetRetryDelay(time.Second*2),
gollm.SetLogLevel(gollm.LogLevelInfo),
)
if err != nil {
log.Fatalf("Failed to create LLM: %v", err)
}
ctx := context.Background()
// Create a basic prompt
prompt := gollm.NewPrompt("Explain the concept of 'recursion' in programming.")
// Generate a response
response, err := llm.Generate(ctx, prompt)
if err != nil {
log.Fatalf("Failed to generate text: %v", err)
}
fmt.Printf("Response:\n%s\n", response)
}
Here's a quick reference guide for the most commonly used functions and options in the gollm package:
// Using OpenAI
llm, err := gollm.NewLLM(
gollm.SetProvider("openai"),
gollm.SetModel("gpt-4"),
gollm.SetAPIKey("your-api-key"),
gollm.SetMaxTokens(100),
gollm.SetTemperature(0.7),
gollm.SetMemory(4096),
)
// Using OpenRouter (with model fallback)
llm, err := gollm.NewLLM(
gollm.SetProvider("openrouter"),
gollm.SetModel("anthropic/claude-3-5-sonnet"),
gollm.SetAPIKey("your-openrouter-api-key"),
gollm.SetMaxTokens(500),
)
// Enable fallback models if primary model is unavailable
llm.SetOption("fallback_models", []string{"openai/gpt-4o", "mistral/mistral-large"})
// Or use OpenRouter's auto-routing capability
// llm.SetOption("auto_route", true)
prompt := gollm.NewPrompt("Your prompt text here",
gollm.WithContext("Additional context"),
gollm.WithDirectives("Be concise", "Use examples"),
gollm.WithOutput("Expected output format"),
gollm.WithMaxLength(300),
)
response, err := llm.Generate(ctx, prompt)
response, err := tools.ChainOfThought(ctx, llm, "Your question here")
optimizer := optimizer.NewPromptOptimizer(llm, initialPrompt, taskDescription,
optimizer.WithCustomMetrics(/* custom metrics */),
optimizer.WithRatingSystem("numerical"),
optimizer.WithThreshold(0.8),
)
optimizedPrompt, err := optimizer.OptimizePrompt(ctx)
results, err := tools.CompareModels(ctx, promptText, validateFunc, configs...)
The gollm package offers a range of advanced features to enhance your AI applications:
Create sophisticated prompts to guide the AI's responses:
prompt := gollm.NewPrompt(
"Explain the concept of recursion in programming",
gollm.WithDirectives(
"Be concise and clear",
"Include code examples in multiple languages",
"Provide a practical example.",
),
gollm.WithContext("This is for a beginner programmer who is just starting to learn."),
gollm.WithOutput("Structure your response with sections: Definition, Example, Pitfalls, Best Practices."),
gollm.WithMaxLength(1000),
)
OpenRouter provides access to multiple LLM providers with additional capabilities:
// Create OpenRouter client
llm, err := gollm.NewLLM(
gollm.SetProvider("openrouter"),
gollm.SetModel("anthropic/claude-3-5-sonnet"),
gollm.SetAPIKey(apiKey),
)
// Enable model fallbacks (tried in order if primary model fails)
llm.SetOption("fallback_models", []string{"openai/gpt-4o", "mistral/mistral-large"})
// Use auto-routing (automatically select best model)
llm.SetOption("auto_route", true)
// Enable prompt caching (improves performance and reduces costs)
llm.SetOption("enable_prompt_caching", true)
// Enable reasoning tokens (step-by-step thinking)
llm.SetOption("enable_reasoning", true)
// Specify provider routing preferences
llm.SetOption("provider_preferences", map[string]interface{}{
"openai": map[string]interface{}{
"weight": 1.0,
},
})
Use the ChainOfThought function for step-by-step reasoning:
question := "What is the result of 15 * 7 + 22?"
response, err := tools.ChainOfThought(ctx, llm, question)
if err != nil {
log.Fatalf("Failed to perform chain of thought: %v", err)
}
fmt.Printf("Chain of Thought:\n%s\n", response)
Load examples directly from files:
examples, err := utils.ReadExamplesFromFile("examples.txt")
if err != nil {
log.Fatalf("Failed to read examples: %v", err)
}
prompt := gollm.NewPrompt("Generate a similar example:",
gollm.WithExamples(examples...),
)
response, err := llm.Generate(ctx, prompt)
if err != nil {
log.Fatalf("Failed to generate example: %v", err)
}
fmt.Printf("Generated Example:\n%s\n", response)
Create reusable prompt templates for consistent prompt generation:
// Create a new prompt template
template := gollm.NewPromptTemplate(
"AnalysisTemplate",
"A template for analyzing topics",
"Provide a comprehensive analysis of {{.Topic}}. Consider the following aspects:\n" +
"1. Historical context\n" +
"2. Current relevance\n" +
"3. Future implications",
gollm.WithPromptOptions(
gollm.WithDirectives(
"Use clear and concise language",
"Provide specific examples where appropriate",
),
gollm.WithOutput("Structure your analysis with clear headings for each aspect."),
),
)
// Use the template to create a prompt
data := map[string]interface{}{
"Topic": "artificial intelligence in healthcare",
}
prompt, err := template.Execute(data)
if err != nil {
log.Fatalf("Failed to execute template: %v", err)
}
// Generate a response using the created prompt
response, err := llm.Generate(ctx, prompt)
if err != nil {
log.Fatalf("Failed to generate response: %v", err)
}
fmt.Printf("Analysis:\n%s\n", response)
Ensure your LLM outputs are in a valid JSON format:
prompt := gollm.NewPrompt("Analyze the pros and cons of remote work.",
gollm.WithOutput("Respond in JSON format with 'topic', 'pros', 'cons', and 'conclusion' fields."),
)
response, err := llm.Generate(ctx, prompt, gollm.WithJSONSchemaValidation())
if err != nil {
log.Fatalf("Failed to generate valid analysis: %v", err)
}
var result AnalysisResult
if err := json.Unmarshal([]byte(response), &result); err != nil {
log.Fatalf("Failed to parse response: %v", err)
}
fmt.Printf("Analysis: %+v\n", result)
Use the PromptOptimizer to automatically refine and improve your prompts:
initialPrompt := gollm.NewPrompt("Write a short story about a robot learning to love.")
taskDescription := "Generate a compelling short story that explores the theme of artificial intelligence developing emotions."
optimizerInstance := optimizer.NewPromptOptimizer(
llm,
initialPrompt,
taskDescription,
optimizer.WithCustomMetrics(
optimizer.Metric{Name: "Creativity", Description: "How original and imaginative the story is"},
optimizer.Metric{Name: "Emotional Impact", Description: "How well the story evokes feelings in the reader"},
),
optimizer.WithRatingSystem("numerical"),
optimizer.WithThreshold(0.8),
optimizer.WithVerbose(),
)
optimizedPrompt, err := optimizerInstance.OptimizePrompt(ctx)
if err != nil {
log.Fatalf("Optimization failed: %v", err)
}
fmt.Printf("Optimized Prompt: %s\n", optimizedPrompt.Input)
Compare responses from different LLM providers or models:
configs := []*gollm.Config{
{
Provider: "openai",
Model: "gpt-4o-mini",
APIKey: os.Getenv("OPENAI_API_KEY"),
MaxTokens: 500,
},
{
Provider: "anthropic",
Model: "claude-3-5-sonnet-20240620",
APIKey: os.Getenv("ANTHROPIC_API_KEY"),
MaxTokens: 500,
},
{
Provider: "groq",
Model: "llama-3.1-70b-versatile",
APIKey: os.Getenv("GROQ_API_KEY"),
MaxTokens: 500,
},
}
promptText := "Tell me a joke about programming. Respond in JSON format with 'setup' and 'punchline' fields."
validateJoke := func(joke map[string]interface{}) error {
if joke["setup"] == "" || joke["punchline"] == "" {
return fmt.Errorf("joke must have both a setup and a punchline")
}
return nil
}
results, err := tools.CompareModels(context.Background(), promptText, validateJoke, configs...)
if err != nil {
log.Fatalf("Error comparing models: %v", err)
}
fmt.Println(tools.AnalyzeComparisonResults(results))
Enable memory to maintain context across multiple interactions:
llm, err := gollm.NewLLM(
gollm.SetProvider("openai"),
gollm.SetModel("gpt-3.5-turbo"),
gollm.SetAPIKey(os.Getenv("OPENAI_API_KEY")),
gollm.SetMemory(4096), // Enable memory with a 4096 token limit
)
if err != nil {
log.Fatalf("Failed to create LLM: %v", err)
}
ctx := context.Background()
// First interaction
prompt1 := gollm.NewPrompt("What's the capital of France?")
response1, err := llm.Generate(ctx, prompt1)
if err != nil {
log.Fatalf("Failed to generate response: %v", err)
}
fmt.Printf("Response 1: %s\n", response1)
// Second interaction, referencing the first
prompt2 := gollm.NewPrompt("What's the population of that city?")
response2, err := llm.Generate(ctx, prompt2)
if err != nil {
log.Fatalf("Failed to generate response: %v", err)
}
fmt.Printf("Response 2: %s\n", response2)
When using memory with LLMs, gollm preserves each message as a separate structured object (enabled by default). This approach:
Structured messages are enabled by default when you use SetMemory():
llm, err := gollm.NewLLM(
gollm.SetProvider("anthropic"),
gollm.SetModel("claude-3-5-sonnet-20241022"),
gollm.SetAPIKey(os.Getenv("ANTHROPIC_API_KEY")),
gollm.SetMemory(4096), // Structured messages enabled by default
)
For fine-grained control over caching, you can add messages with explicit cache control:
// Add a message with cache control
// "ephemeral" caching is recommended for most use cases
llm.AddStructuredMessage("user", "Here is a long document to analyze...", "ephemeral")
// Generate - subsequent requests with the same context will use cached tokens
response, err := llm.Generate(ctx, gollm.NewPrompt("Summarize the key points"))
// Check if memory is enabled
if llm.HasMemory() {
// Get current conversation history
messages := llm.GetMemory()
// Add a message manually
llm.AddToMemory("user", "Remember this context")
// Clear conversation history
llm.ClearMemory()
}
You can toggle between structured and flattened message modes:
// Use structured messages (default, recommended for caching)
llm.SetUseStructuredMessages(true)
// Or use flattened prompt (legacy mode, combines all messages into one)
llm.SetUseStructuredMessages(false)
See the examples/caching_structured/ directory for a complete working example.
Prompt Engineering:
NewPrompt() with options like WithContext(), WithDirectives(), and WithOutput() to create well-structured prompts.prompt := gollm.NewPrompt("Your main prompt here",
gollm.WithContext("Provide relevant context"),
gollm.WithDirectives("Be concise", "Use examples"),
gollm.WithOutput("Specify expected output format"),
)
Utilize Prompt Templates:
Native macOS app to monitor Claude AI usage limits and watch your coding sessions live
干净、强大、属于你的 AI Agent 平台 --AI agents, without the clutter.
An AI-powered custom node for ComfyUI designed to enhance workflow automation and provide intelligent assistance
npx CLI installing 100+ agents, commands, hooks, and integrations in one command