Creating the Prompt: A prompt is created that includes the generated JSON schema:
promptText := fmt.Sprintf(`Generate information about a fictional person.Create a fictional person with the following attributes: name, age, occupation, city, country, favorite color, hobbies (1-5), education, pet name, and lucky number (1-100).
Ensure all fields are filled and adhere to the specified constraints.Return the data as a JSON object that adheres to this schema:%s`, string(schema))
Defining a Validation Function: A custom validation function is defined for the ComplexPerson struct:
validateComplexPerson :=func(person ComplexPerson) error {// Add any additional validation logic hereif person.Age <0|| person.Age >150 {return fmt.Errorf("age must be between 0 and 150") }// ... (other validation checks)returnnil}
Comparing Model Outputs: The CompareModels function is used to generate and compare outputs from all configured models:
Final Analysis: The example concludes with an analysis of the comparison results:
fmt.Println("\nFinal Analysis of Results:")analysis := gollm.AnalyzeComparisonResults(results)fmt.Println(analysis)
In summary, this example demonstrates:
Defining a complex struct for structured output
Configuring multiple LLM models from different providers
Generating a JSON schema from a Go struct
Creating a prompt that includes the JSON schema
Implementing custom validation for the structured output
Using gollm's CompareModels function to generate and compare outputs from multiple models
Processing and displaying the results, including error handling and debug logging
Analyzing the comparison results
This example is particularly useful for developers who need to compare the performance and output quality of different LLM models when generating complex, structured data. It showcases gollm's capabilities in handling multiple providers, structured output validation, and model comparison.