IntentCreator
The backbone of UI2 intent creation, wrapped by createUI2
The IntentCreator
class is what powers all instances of UI2. We will discuss all properties and methods on this class.
Note that this class is not meant to be directly, but it is exposed to help extend UI2.
Constructor
The IntentCreator constructor takes a configuration with the following properties:
Value Name | Type | Description |
---|---|---|
model | LanguageModel or Model Configuration | Configure the model to use with UI2. |
systemPrompt | string | Additional instructions for the AI. |
context | object | Context for intent identification |
onLoadStart | () => void | Called when AI loading startsonintent-and-onsubmit-options). |
onLoadEnd | () => void | Called when AI loading ends |
onPartialIntent | (partialIntents: IntentCall[]) => void; | Called on each stream part for the intent. |
onIntent | (intentCall: IntentCall, input?: string) => void | Called when any full intent is detected |
onCleanup | (intentCall: IntentCall, input?: string) => void | Called when any full Intent is cleaned up |
Let's go through each of these parameters:
model
You have two options to supply the model. First, you can use a Vercel AI SDK compatible LanguageModel
, based off a provider.
It is recommended to use Cerebras for its speed.
Structured Output
UI2 uses Structured Output to guarentee that your Intent parameter schemas are
satisfied. Your model has to support this feature. llama-3.3-70b
from
Cerebras works great.
However, if you do not want to use Vercel AI SDK, you can also directly use a object of the following schema:
This helps specify any provider that is compliant with OpenAI schema.
systemPrompt
These instructions are given to AI to guide its intent identification.
You should tell it what app you are building, and generally when to identify each intent. Remember that you are able to specify instructions per intent as well, so don't get too specific.
context
This is an object that supplies context to the AI. Remember that this object should have a stable reference, since whenever the reference is updated, the context will also be updated.
This context can have any schema, and will be directly provided after having JSON.stringify()
called on it to the AI.
onLoadStart
and onLoadEnd
These are called when the AI starts and stops loading respectively. Takes a callback with no arguments.
onPartialIntent
This is a low-level callback that is called when the Intent object stream is coming in.
This is a risky event listener to use, since the object may not be complete.
However, if in theory you wished to make an integration to livetime stream incomplete toolcalls, this could be useful.
Note that it returns what is seemingly a normal array of IntentCall
s (based
off the type), but its properties may be incomplete.
onIntent
This onIntent returns any intent that is identified in the form of an IntentCall
.
It also provides the current input as its second parameter.
This is a way to globally process intents, if you do not want to specify intent callbacks with addIntent
, or want to do something on top of that.
onCleanup
This onCleanup returns any intent that is cleaned up (in other words, no longer identified) in the form of an IntentCall
.
It also provides the current input as its second parameter.
This is a way to globally process intents, if you do not want to specify intent callbacks with addIntent
, or want to do something on top of that.
Methods
The IntentCreator
class also has a few significant methods to be aware of.
These methods may be better documented in the later sections specifically on these methods (as accessed through the Builder Function).
identifyIntent
This takes two parameters: The current text
to identify as well as an optional configuration object.
First, the text to identify with is just the input.
The config takes one property, which is the currentIntents
, an array of IntentCalls[]
.
Based off this array, we will determine what intents to trigger, which to do nothing, and which to cleanup.
This is more so an internal API, so wrappers like StatefulIntentCreator
don't require this anymore.
addIntent
This function adds an intent, to be able to later be identified.
This takes two parameters: One for the name of the intent, and the other for intent options, as listed below:
Value Name | Type | Description |
---|---|---|
parameters | z.ZodType (default: z.ZodObject<any> ) | Zod schema defining the parameters for this intent. |
description | string | Description of what the intent does. |
onIntent | (intentCall: IntentCall<T>, input?: string) => void | Called when this intent is identified. |
onCleanup | (intentCall: IntentCall<T>, input?: string) => void | Called when this intent is no longer detected. |
These intents are later able to be identified.
Note that you cannot create an intent named other
. Use the following
addOther
method to achieve this.
addOther
The other
intent is unique, since it acts as a catch-all for any intent that isn't listed. It always exists even if you don't define it, but you can customize it's behavior. The configuration is very similar to addIntent
, but it is missing various things that you cannot customize.
Value Name | Type | Description |
---|---|---|
description | string | Customize when the other intent can be called. |
onIntent | (intentCall: IntentCall<T>, input?: string) => void | Called when this intent is identified. |
onCleanup | (intentCall: IntentCall<T>, input?: string) => void | Called when this intent is no longer detected. |
setContext
Simply sets this.config.context
to the parameter.
Helps if you wish manually reset context to a certain value, or if you don't want to deal with object references.
removeIntent
Removes the intent with the name intentName
. Throws an error if there is no such intent.
Conclusion
Again, recall that IntentCreator
is an internal API.
If you are creating extensions for UI2, it is highly recommended that you create a Builder Function to abstract away some of the implementation details.