Structured Output
It demonstrates how gollm can be used to extract structured information from unstructured text, with options for validation and concurrent processing. This is particularly useful for tasks like information extraction, data parsing, and automated data entry from natural language inputs.
Let's start with the structured_data_extractor.go
file:
ExtractStructuredData Function: This is the main function for extracting structured data:
It's a generic function that can extract data into any struct type
T
.JSON Schema Generation: The function generates a JSON schema based on the provided struct type:
Prompt Creation: It creates a prompt that includes the input text and the generated JSON schema:
LLM Generation and Parsing: The function generates a response using the LLM and parses it into the struct:
Validation: The extracted data is validated against the struct's validation rules:
Now, let's look at the example usage in the main
function:
Struct Definitions: Two struct types are defined:
MovieReview
(without validation tags) andMovieReviewValidated
(with validation tags).LLM Client Initialization: An LLM client is created using the Groq provider:
Text Input: A sample movie review text is defined.
Concurrent Extraction: The example demonstrates concurrent extraction of structured data with and without validation:
Result Handling: The results are collected and printed using channels and a select statement.
Pretty Printing: The
printReview
function is used to display the extracted data in a readable format.
This example showcases several advanced features of gollm:
Generic structured data extraction based on Go struct definitions
Automatic JSON schema generation from struct types
Concurrent processing of multiple extraction tasks
Handling of validation rules in struct tags
Use of channels for asynchronous result collection
Pretty printing of structured data
Last updated