UI2 Docs
UI2 API Reference

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 NameTypeDescription
modelLanguageModel or Model ConfigurationConfigure the model to use with UI2.
systemPromptstringAdditional instructions for the AI.
contextobjectContext for intent identification
onLoadStart() => voidCalled when AI loading startsonintent-and-onsubmit-options).
onLoadEnd() => voidCalled when AI loading ends
onPartialIntent(partialIntents: IntentCall[]) => void;Called on each stream part for the intent.
onIntent(intentCall: IntentCall, input?: string) => voidCalled when any full intent is detected
onCleanup(intentCall: IntentCall, input?: string) => voidCalled 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:

{
    baseURL: string,
    apiKey: string,
    modelId: string
}

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 IntentCalls (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

async identifyIntent(text: string, config?: IdentifyIntentConfig): IntentCall[]

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

addIntent<T extends z.ZodType>(intentName: string, intent: IntentPartialOptional<T>): this

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 NameTypeDescription
parametersz.ZodType (default: z.ZodObject<any>)Zod schema defining the parameters for this intent.
descriptionstringDescription of what the intent does.
onIntent(intentCall: IntentCall<T>, input?: string) => voidCalled when this intent is identified.
onCleanup(intentCall: IntentCall<T>, input?: string) => voidCalled 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

addOther(intent: OtherIntent): this

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 NameTypeDescription
descriptionstringCustomize when the other intent can be called.
onIntent(intentCall: IntentCall<T>, input?: string) => voidCalled when this intent is identified.
onCleanup(intentCall: IntentCall<T>, input?: string) => voidCalled when this intent is no longer detected.

setContext

public setContext(context: object): this

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

public removeIntent(intentName: string): this

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.

On this page