Batch Prompt Optimizer

This example demonstrates the usage of the BatchPromptOptimizer in gollm. Here's a detailed breakdown of the code:

  1. Setting up the environment: The example starts by creating an LLM client using the Groq provider, similar to the previous example:

    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),
    )
  2. Creating the BatchPromptOptimizer: The code creates a new BatchPromptOptimizer instance:

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

    This optimizer is set to verbose mode, which will provide detailed output during the optimization process.

  3. Defining prompt examples: The code defines a slice of PromptExample structs, each representing a different prompt to be optimized:

    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"},
            },
        },
        // ... (another example for Technical Documentation)
    }

    Each PromptExample includes a name, prompt, description, threshold, and custom metrics.

  4. Running batch optimization: The code runs the batch optimization process:

    results := optimizer.OptimizePrompts(ctx, examples)

    This optimizes all the prompts in the examples slice simultaneously.

  5. Processing and displaying results: The code then iterates through the optimization results, displaying information for each optimized prompt:

    for i, result := range results {
        fmt.Printf("\nResults for: %s (Example %d)\n", result.Name, i+1)
        fmt.Printf("Original Prompt: %s\n", result.OriginalPrompt)
        if result.Error != nil {
            fmt.Printf("Error: %v\n", result.Error)
        } else {
            fmt.Printf("Optimized Prompt: %s\n", result.OptimizedPrompt)
            fmt.Printf("Generated Content:\n%s\n", result.GeneratedContent)
        }
        fmt.Println(strings.Repeat("=", 100))
    }

    For each result, it prints:

    • The name of the example

    • The original prompt

    • Either an error message (if optimization failed) or:

      • The optimized prompt

      • The content generated using the optimized prompt

This example showcases how to use the BatchPromptOptimizer to optimize multiple prompts simultaneously. It demonstrates setting up the optimizer, defining prompt examples with custom metrics, running the batch optimization process, and handling the results.

Last updated