Prompt Optimization

gollm provides a powerful Prompt Optimizer feature that allows you to automatically refine and improve your prompts for better results from Language Models (LLMs). Here are examples of different prompt optimization techniques and configurations available in gollm:

1. Basic Prompt Optimizer

optimizer := gollm.NewPromptOptimizer(llm, "Write a tagline for a new smartphone", "Create a catchy and memorable tagline")
optimizedPrompt, err := optimizer.OptimizePrompt(ctx)

This example demonstrates how to create a basic prompt optimizer with a simple prompt and task description.

2. Prompt Optimizer with Custom Metrics

optimizer := gollm.NewPromptOptimizer(llm, "Explain quantum computing to a 10-year-old",
    "Create an easy-to-understand explanation of quantum computing",
    gollm.WithCustomMetrics(
        gollm.Metric{Name: "Simplicity", Description: "How easy it is for a 10-year-old to understand"},
        gollm.Metric{Name: "Accuracy", Description: "How scientifically accurate the explanation is"},
        gollm.Metric{Name: "Engagement", Description: "How interesting and captivating the explanation is"},
    ),
)

This example shows how to add custom metrics to guide the optimization process.

3. Prompt Optimizer with Different Rating Systems

// Numerical rating system
numericOptimizer := gollm.NewPromptOptimizer(llm, "Write a news article about recent advancements in renewable energy",
    "Create an informative and engaging news article",
    gollm.WithRatingSystem("numerical"),
    gollm.WithThreshold(0.8),
)

// Letter grade rating system
letterGradeOptimizer := gollm.NewPromptOptimizer(llm, "Compose a persuasive speech on the importance of education",
    "Create a compelling and motivational speech",
    gollm.WithRatingSystem("letter"),
)

These examples demonstrate how to use different rating systems (numerical or letter grades) for evaluation.

4. Prompt Optimizer with Iteration Callback

callback := func(iteration int, entry gollm.OptimizationEntry) {
    fmt.Printf("Iteration %d:\n", iteration)
    fmt.Printf("  Prompt: %s\n", entry.Prompt.Input)
    fmt.Printf("  Overall Score: %.2f\n", entry.Assessment.OverallScore)
    fmt.Printf("  Overall Grade: %s\n", entry.Assessment.OverallGrade)
}

optimizer := gollm.NewPromptOptimizer(llm, "Write a product description for a new eco-friendly water bottle",
    "Create an appealing and informative product description",
    gollm.WithIterationCallback(callback),
)

This example shows how to use an iteration callback to monitor the optimization process.

5. Prompt Optimizer with Advanced Configuration

optimizer := gollm.NewPromptOptimizer(llm, "Generate a business plan for a startup in the AI industry",
    "Create a comprehensive and realistic business plan",
    gollm.WithCustomMetrics(
        gollm.Metric{Name: "Feasibility", Description: "How realistic and achievable the plan is"},
        gollm.Metric{Name: "Innovation", Description: "How innovative and unique the business idea is"},
        gollm.Metric{Name: "Market Analysis", Description: "How well the plan analyzes the target market"},
    ),
    gollm.WithRatingSystem("numerical"),
    gollm.WithThreshold(0.9),
    gollm.WithMaxRetries(5),
    gollm.WithRetryDelay(time.Second * 3),
    gollm.WithMemorySize(3),
    gollm.WithIterations(10),
)

This example demonstrates a fully configured prompt optimizer with custom metrics, numerical rating, threshold, retry settings, memory size, and iteration limit.

Key Components in the Process

  • Custom Metrics: Guide the LLM in assessing specific aspects of the prompt.

  • Rating System: Can be numerical (e.g., 0-20 scale) or letter grades (A, B, C, etc.).

  • Threshold: The target score for a satisfactory prompt. (i.e. 18/20 for 0.9)

  • Retry Mechanism: Handles transient errors during API calls.

  • Memory: Keeps track of previous iterations to inform future refinements.

6. Batch Prompt Optimization

batchOptimizer := gollm.NewBatchPromptOptimizer(llm)
batchOptimizer.Verbose = true

examples := []gollm.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"},
        },
    },
    {
        Name:        "Technical Documentation",
        Prompt:      "Explain how blockchain technology works to a non-technical audience.",
        Description: "Create a clear, accessible explanation without sacrificing accuracy",
        Threshold:   0.85,
        Metrics: []gollm.Metric{
            {Name: "Clarity", Description: "How easy it is for a layperson to understand"},
            {Name: "Accuracy", Description: "How technically correct the explanation is"},
            {Name: "Engagement", Description: "How interesting and relevant the explanation is"},
        },
    },
}

results := batchOptimizer.OptimizePrompts(ctx, examples)

This example shows how to use batch optimization to improve multiple prompts simultaneously.

7. Prompt Optimizer with Verbose Output

optimizer := gollm.NewPromptOptimizer(llm, "Write a poem about the changing seasons",
    "Create a vivid and emotive poem about seasonal transitions",
    gollm.WithVerbose(),
)

This example demonstrates how to enable verbose output for detailed optimization progress.

8. Prompt Optimizer with Custom Goal

optimizer := gollm.NewPromptOptimizer(llm, "Describe the process of photosynthesis",
    "Create a clear and accurate explanation of photosynthesis",
    gollm.WithOptimizationGoal("Optimize the prompt to generate a response that is both scientifically accurate and easy for a high school student to understand"),
)

This example shows how to set a custom optimization goal to guide the improvement process.

These prompt optimization techniques showcase the flexibility and power of gollm in automatically refining prompts. By leveraging these features, you can improve the quality and effectiveness of your prompts, leading to better responses from Language Models.

Last updated