Structured Extraction
Run the dev server and navigate to the /extraction
route:
You should see an input field where you can enter unstructured text about an appointment. Let's build a system to extract structured information from this input using the generateObject
function.
Create a new file called actions.ts
in the extraction
directory. In it, define a new server action called extractAppointment
. This action will take in one argument, input
, which is a string. Import and call generateObject
and return the resulting object generation.
Pass in a model and a prompt. In this case, we want the model to extract appointment information from the input text. We'll use the gpt-4o-mini
model for this task.
Now, let's define a schema for the exact information we want to extract.
Import the CalendarAppointment
type and create a new state variable to store the extracted appointment details.
Import the newly created extractAppointment
action and call it. Pass in the input value from the form and update the extracted appointment state with the awaited resulting value.
Pass the appointment to the CalendarAppointment
component as props.
Test the extraction functionality:
- Run the dev server if it's not already running:
- Navigate to the
/extraction
route in your browser. - Enter an unstructured appointment text in the input field, such as:
- Click the "Submit" button and observe the structured appointment details being displayed.
You should now see the extracted appointment information rendered in a structured format using the CalendarAppointment
component.
But notice that the date and time format isn't great. And the appointment name isn't perfect either. Let's add some Zod descriptions to clarify the expected format of the extracted fields.
Update the action.ts
file with descriptions for the extracted fields:
Try the following input again and see the improvement in the output.