Classification
In this project, we want to pass the model a number of support requests and ask it to classify those requests into categories.
Open app/(2-classification)/classification.ts
, you should see the following code:
Import and call generateText
like before. Prompt the model to classify the support requests (which are passed in as a JSON object).
Run the script
Response:
Notice how the model is able to classify the support requests into the correct categories. This is great, but generating a big plain text chunk isn't super useful. Notice, the model also generates extraneous information like "Here are the classified support requests:" and "(Note: The request about API integration (id: 3) was not classified as it does not fit neatly into the provided categories.)". We can solve this with the generateObject
function.
Update to use generateObject
instead of generateText
Define a schema for the output and set the output mode to "array".
Set the output mode to "array". This will instruct the model to generate an array of objects that match the schema we defined. Define a schema for the output and set the output mode to "array".
Now the output is nicely constrained to the exact format we specified.
Update the schema to estimate the urgency of the request
This is super powerful. A lot of these models are multi-lingual too, so we could pass in support requests in different languages and the model would still be able to classify them. This is a great example of how we can use AI to automate a task that would otherwise be very time-consuming.
Update the import to use the multi-lingual support requests. Add a language field to the schema too.
Note that the model is identifying the language correctly, but it's returning the language as a country code rather than the full name. Note: you can also use the describe
method to give the model more information about what you want it to generate.