6. Structured output

  1. Defining the Structure: The example starts by defining a struct that represents the desired output structure:

    type PersonInfo struct {
        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"`
    }

    This struct includes validation tags to ensure the generated data meets specific criteria.

  2. Creating an LLM Instance: The example creates an LLM instance with specific configuration:

    llm, err := gollm.NewLLM(
        gollm.SetProvider("openai"),
        gollm.SetModel("gpt-3.5-turbo"),
        gollm.SetAPIKey(apiKey),
        gollm.SetMaxTokens(300),
        gollm.SetMaxRetries(3),
        gollm.SetRetryDelay(time.Second*2),
        gollm.SetDebugLevel(gollm.LogLevelWarn),
    )
  3. Generating JSON Schema: The example generates a JSON schema based on the PersonInfo struct:

    schemaJSON, err := gollm.GenerateJSONSchema(&PersonInfo{}, gollm.WithExpandedStruct(true))

    The WithExpandedStruct(true) option ensures that nested structs are fully expanded in the schema.

  4. Creating a Prompt with Schema: A prompt is created that includes the generated JSON schema:

    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))),
    )

    This prompt includes directives for generating the person's information and specifies that the output should adhere to the provided schema.

  5. Generating Structured Output: The example generates the structured output using the LLM:

    response, err := llm.Generate(ctx, prompt, gollm.WithJSONSchemaValidation())

    The WithJSONSchemaValidation() option ensures that the generated output adheres to the specified JSON schema.

  6. Parsing and Validating the Response: The generated response is parsed into the PersonInfo struct:

    var person PersonInfo
    err = json.Unmarshal([]byte(response), &person)

    Then, the parsed data is validated against the struct's validation rules:

    if err := gollm.Validate(&person); err != nil {
        log.Fatalf("Generated data does not match schema: %v", err)
    }
  7. Displaying the Result: Finally, the example prints the validated PersonInfo:

    fmt.Println("\nValidated PersonInfo:")
    fmt.Printf("Name: %s\n", person.Name)
    fmt.Printf("Age: %d\n", person.Age)
    fmt.Printf("Occupation: %s\n", person.Occupation)
    fmt.Printf("Hobbies: %v\n", person.Hobbies)

In summary, this example demonstrates:

  • Defining a struct with validation tags for structured output

  • Generating a JSON schema from a Go struct

  • Creating a prompt that includes the JSON schema

  • Using the LLM to generate data that adheres to the schema

  • Parsing and validating the generated data against the original struct

This example is particularly useful for developers who need to generate structured data using LLMs, ensuring that the output adheres to a specific format and validation rules. It showcases how gollm can be used to create reliable, schema-compliant outputs from language models.

Last updated