Prompt Optimizer
This example demonstrates how to use the prompt optimization features of the gollm package for individual prompt optimization.
Key Components
LLM Setup: Initializes an LLM client using the Groq provider.
PromptExamples: Defines prompts to be optimized with associated metrics and thresholds.
Optimization Process: Iterates through examples, optimizing each prompt.
Response Generation: Generates content using optimized prompts.
Result Display: Shows original prompts, optimized versions, and generated content.
Code Structure
func main() {
// LLM setup
// Define PromptExamples
// For each example:
// Create and configure optimizer
// Optimize prompt
// Generate response
// Store result
// Display results
}
Key Code Snippets
LLM Setup
llm, err := gollm.NewLLM(
gollm.SetProvider("groq"),
gollm.SetModel("llama-3.1-70b-versatile"),
gollm.SetAPIKey(os.Getenv("GROQ_API_KEY")),
gollm.SetMaxTokens(1024),
gollm.SetDebugLevel(gollm.LogLevelWarn),
)
Defining a PromptExample
{
Name: "Creative Writing",
Prompt: "Write the opening paragraph of a mystery novel set in a small coastal town.",
Description: "Create an engaging and atmospheric opening that hooks the reader",
Threshold: 0.9,
Metrics: []gollm.Metric{
{Name: "Atmosphere", Description: "How well the writing evokes the setting"},
{Name: "Intrigue", Description: "How effectively it sets up the mystery"},
{Name: "Character Introduction", Description: "How well it introduces key characters"},
},
}
Optimization Process
optimizer := gollm.NewPromptOptimizer(llm, ex.Prompt, ex.Description,
gollm.WithCustomMetrics(ex.Metrics...),
gollm.WithRatingSystem("numerical"),
gollm.WithThreshold(ex.Threshold),
gollm.WithMaxRetries(3),
gollm.WithRetryDelay(time.Second * 2),
gollm.WithVerbose(),
)
optimizedPrompt, err := optimizer.OptimizePrompt(ctx)
Usage
Install and configure the gollm package.
Set the
GROQ_API_KEY
environment variable.Run:
go run prompt_optimizer_example.go
Customization
Modify
PromptExample
structs in theexamples
slice.Adjust optimization parameters (threshold, metrics, rating system).
Change LLM configuration (provider, model).
Last updated
Was this helpful?