gollm provides a variety of tools for creating sophisticated prompts to guide Language Models (LLMs) in producing more accurate and tailored responses. Here are examples of different prompt engineering techniques available in gollm, based on the provided code:
1. Basic Prompt with Structured Output
basicPrompt := gollm.NewPrompt("List the top 3 benefits of exercise", gollm.WithOutput("JSON array of benefits, each with a 'title' and 'description'"),)
This example demonstrates how to create a basic prompt that requests a structured output in JSON format.
2. Prompt with Directives, Output, and Context
directivePrompt := gollm.NewPrompt("Propose a solution to reduce urban traffic congestion", gollm.WithDirectives("Consider both technological and policy-based approaches","Address environmental concerns","Consider cost-effectiveness", ), gollm.WithOutput("Solution proposal in markdown format with headings"), gollm.WithContext("The city has a population of 2 million and limited public transportation."),)
This example shows how to combine directives, output specifications, and context in a single prompt.
3. Prompt with Examples and Max Length
examplesPrompt := gollm.NewPrompt("Write a short, engaging tweet about climate change", gollm.WithExamples("🌍 Small actions, big impact! Reduce, reuse, recycle to fight climate change. #ClimateAction","🌡️ Climate change is real, and it's happening now. Let's act before it's too late! #ClimateEmergency", ), gollm.WithMaxLength(30),)
This example demonstrates how to provide examples and set a maximum length for the response.
4. Prompt Template with Dynamic Content
templatePrompt := gollm.NewPromptTemplate("ProductDescription","Generate a product description","Create an engaging product description for a {{.ProductType}} named '{{.ProductName}}'. "+"Target audience: {{.TargetAudience}}. Highlight {{.NumFeatures}} key features.", gollm.WithPromptOptions( gollm.WithDirectives("Use persuasive language","Include a call-to-action", ), gollm.WithOutput("Product description in HTML format"), ),)prompt, err := templatePrompt.Execute(map[string]interface{}{"ProductType": "smartwatch","ProductName": "TimeWise X1","TargetAudience": "fitness enthusiasts","NumFeatures": 3,})
This example shows how to create a reusable prompt template with dynamic content and additional options.
5. Prompt with JSON Schema Validation
jsonSchemaPrompt := gollm.NewPrompt("Generate a user profile", gollm.WithOutput(`JSON object with the following schema: { "type": "object", "properties": { "name": {"type": "string"}, "age": {"type": "integer", "minimum": 18}, "interests": {"type": "array", "items": {"type": "string"}} }, "required": ["name", "age", "interests"] }`),)jsonSchemaResponse, err := llm.Generate(ctx, jsonSchemaPrompt, gollm.WithJSONSchemaValidation())
This example demonstrates how to specify a JSON schema for the output and use it for validation.
6. Advanced Analysis Prompt Template
balancedAnalysisTemplate := gollm.NewPromptTemplate("BalancedAnalysis","Analyze a topic from multiple perspectives",`Analyze the following topic from multiple perspectives: {{.Topic}} Please structure your response as a JSON object with the following format: { "perspectives": [ { "name": "Perspective Name", "implications": [ "Positive implication", "Negative implication" ] } ], "summary": "A brief, balanced summary of the analysis" }`, gollm.WithPromptOptions( gollm.WithDirectives("Consider technological, economic, social, and ethical implications","Provide at least one potential positive and one potential negative outcome for each perspective","Ensure the analysis is balanced and objective", ), gollm.WithMaxLength(500), ),)prompt, err := balancedAnalysisTemplate.Execute(map[string]interface{}{"Topic": "The impact of artificial intelligence on job markets",})
This example shows a sophisticated prompt template for balanced analysis, combining structured output, directives, and dynamic content.
7. Structured Output with JSON Schema
typePersonInfostruct { Name string`json:"name" validate:"required"` Age int`json:"age" validate:"required,gte=0,lte=150"` Occupation string`json:"occupation" validate:"required"` Hobbies []string`json:"hobbies" validate:"required,min=1,max=5"`}schemaJSON, err := gollm.GenerateJSONSchema(&PersonInfo{}, gollm.WithExpandedStruct(true))if err !=nil { log.Fatalf("Failed to generate JSON schema: %v", err)}prompt := gollm.NewPrompt("Generate information about a fictional person", gollm.WithDirectives("Create a fictional person with a name, age, occupation, and hobbies","Ensure the age is realistic for the occupation","Include 1 to 5 hobbies","Return ONLY the JSON data for the person, not the schema", ), gollm.WithOutput(fmt.Sprintf("Generate a JSON object that adheres to this schema:\n%s\nDo not include the schema in your response, only the generated data.", string(schemaJSON))),
)response, err := llm.Generate(ctx, prompt, gollm.WithJSONSchemaValidation())
This example demonstrates how to generate a JSON schema from a struct and use it in a prompt to ensure structured output.
These prompt engineering techniques showcase the flexibility and power of gollm in creating sophisticated prompts. By leveraging these features, you can guide LLMs to produce more accurate, structured, and contextually relevant responses.