Guardrails: Manual topic detection
Sometimes you may want to take more control and detect the topic explicitly. Maybe you want to use other LLM model which you don't use for the conversation, eg. you want to use smarter model to detect prompt injections or other attack surfaces.
We will use a separate call to generateText() with structured output (Output.object) to detect the topic. In AI SDK
6, generateObject is deprecated in favor of this pattern. You can detect the topic either before or after the LLM call
for next assistant message.
- Detecting topic before LLM call will save you from processing the LLM response if the topic is not suitable.
- Detecting topic after LLM call will will provide complete conversation (incl. response) to the topic detector.
So, as everything in software development, you can decide on your specific case.
Let's start with a simple topic detector. We use generateText() with Output.object() to detect the topic. We specify
in system message the goal of the call and we specify response schema to return object with only property - topic.
Now, refactor the topic detector to use allowedTopics array and add whole conversation history through messages
property. As you can see, on the last line we extract the topic property from the structured output.
Let's wrap everything into a nice reusable function with template parameter.
1import { generateText, Output } from "ai";2import { openai } from "@ai-sdk/openai";3import { z } from "zod";45const { output } = await generateText({6model: openai("gpt-4o-mini"),7output: Output.object({8schema: z.object({9topic: z.string(),10}),11}),12prompt: "Identify the conversation topic based on the instructions.",13system: `Identify conversation topic into one of the following categories:14- Categories: baseball; basketball;15- If no topic is suitable, simply reply "none";16`,17});
Great! Now you have a reusable topic detector function. You can use it in your chat application to detect the topic of the conversation.
As always you can find the source code on Github.
You will find even more information about advanced guardrails techniques in next lesson.