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

Was this helpful?

  1. Examples

Example

gollm Examples

This section provides practical examples to help you understand how to use gollm in various scenarios.

Basic Question Answering

llm, _ := gollm.NewLLM(gollm.SetProvider("openai"), gollm.SetModel("gpt-4o-mini"))
prompt := gollm.NewPrompt("What is the capital of France?")
response, _ := llm.Generate(context.Background(), prompt)
fmt.Println(response)

Summarization with Context

prompt := gollm.NewPrompt("Summarize the main points in 3 sentences.",
	gollm.WithContext("The text is about the history of the Internet."),
	gollm.WithMaxLength(100),
)
response, _ := llm.Generate(context.Background(), prompt)
fmt.Println(response)

Comparing Models for a Specific Task

configs := []*gollm.Config{
	{Provider: "openai", Model: "gpt-4o-mini"},
	{Provider: "anthropic", Model: "claude-3-5-sonnet-20240620"},
}
prompt := "Explain how a computer works to a 5-year-old."
results, _ := gollm.CompareModels(context.Background(), prompt, nil, configs...)
fmt.Println(gollm.AnalyzeComparisonResults(results))

JSON Output with Validation

type Recipe struct {
	Name        string   `json:"name"`
	Ingredients []string `json:"ingredients"`
	Steps       []string `json:"steps"`
}

prompt := gollm.NewPrompt("Create a recipe for chocolate chip cookies.",
	gollm.WithOutput("Respond in JSON format with 'name', 'ingredients', and 'steps' fields."),
)

response, _ := llm.Generate(context.Background(), prompt, gollm.WithJSONSchemaValidation())

var recipe Recipe
json.Unmarshal([]byte(response), &recipe)
fmt.Printf("Recipe: %+v\n", recipe)

These examples demonstrate various features of gollm, from basic usage to more advanced scenarios. Experiment with these examples and adapt them to your specific needs.

PreviousPrompt OptimizationNext1. Basic usage

Last updated 9 months ago

Was this helpful?