gollm
  • Introduction
  • Getting started
    • Getting started
    • Basic usage
    • Configuration
    • Memory
    • Examples inside Prompts
  • Prompt-template
    • Sumarize
    • Question Answering
    • Chain of Thought
    • Structured Output
  • advanced features
    • Advanced features
    • Prompt engineering
    • Comparison
    • JSON output validation
    • Prompt Optimization
  • Examples
    • Example
    • 1. Basic usage
    • 2. Prompt types
    • 3. Compare providers
    • 4. Custom config
    • 5. Advanced Prompt
    • 6. Structured output
    • 7. Structured output comparison
    • 8. Content creation workflo
    • Ollama Example
    • Prompt Optimizer
    • Batch Prompt Optimizer
  • API reference
  • FAQ
Powered by GitBook
On this page
  • Key Components
  • Code Structure
  • Key Code Snippets
  • Usage
  • Customization

Was this helpful?

  1. Examples

Prompt Optimizer

This example demonstrates how to use the prompt optimization features of the gollm package for individual prompt optimization.

Key Components

  1. LLM Setup: Initializes an LLM client using the Groq provider.

  2. PromptExamples: Defines prompts to be optimized with associated metrics and thresholds.

  3. Optimization Process: Iterates through examples, optimizing each prompt.

  4. Response Generation: Generates content using optimized prompts.

  5. 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

  1. Install and configure the gollm package.

  2. Set the GROQ_API_KEY environment variable.

  3. Run: go run prompt_optimizer_example.go

Customization

  • Modify PromptExample structs in the examples slice.

  • Adjust optimization parameters (threshold, metrics, rating system).

  • Change LLM configuration (provider, model).

PreviousOllama ExampleNextBatch Prompt Optimizer

Last updated 9 months ago

Was this helpful?