Module: batteries/natural-language-router
Type Aliases
RouteProps
Ƭ RouteProps: { children
: Node
} & MergeExclusive
<{ when
: string
}, { unmatched
: true
}>
Properties to pass to the Route component.
Defined in
packages/ai-jsx/src/batteries/natural-language-router.tsx:122
Functions
NaturalLanguageRouter
▸ NaturalLanguageRouter(props
, «destructured»
): AsyncGenerator
<string
| PartiallyRendered
[], PartiallyRendered
[], unknown
>
Use a Large Language Model to steer the control flow of your application.
You give this component two pieces: children
, which contains Routes, and query
, which is used to steer. The
model will pick the Route that's the best match for the query
.
<NaturalLanguageRouter query="I'd like to cancel my account.">
<Route when='the user would like to cancel'>
<CancelRoute />
</Route>
<Route when='the user would like to upgrade'>
<Upgrade />
</Route>
<Route unmatched>
I'm sorry, but I can't help with that.
</Route>
</NaturalLanguageRouter>
Typically, instead of hardcoding query
as in the example above, you'd be receiving it from user input.
The Routes do not need to be the direct descendents of the NaturalLanguageRouter:
<NaturalLanguageRouter query="I'd like to cancel my account.">
<><Route when='the user would like to cancel'></>
{() => <Route when='the user would like to upgrade'></>}
<SomeOtherComponentThatReturnsARoute />
</NaturalLanguageRouter>
However, you can't use nested Route components. If a Route contains another Route,
the inner Route's when
will not be considered. The inner Route will always show
its children. For example:
<NaturalLanguageRouter>
<Route when='first option'>...</Route>
<Route when='second option'>
<Route when='third option'>This will always be shown</Route>
</Route>
The router will find the first two routes, and ask the model to pick between "first option" and "second option". "third option" will not be presented as a choice, and its children will always be rendered.
See
Route
Parameters
Name | Type |
---|---|
props | Object |
props.children | Node |
props.query | Node |
«destructured» | RenderContext |
Returns
AsyncGenerator
<string
| PartiallyRendered
[], PartiallyRendered
[], unknown
>
Defined in
packages/ai-jsx/src/batteries/natural-language-router.tsx:65
Route
▸ Route(props
): Node
Use with NaturalLanguageRouter to define a route.
See
NaturalLanguageRouter
Parameters
Name | Type |
---|---|
props | RouteProps |
Returns
Defined in
packages/ai-jsx/src/batteries/natural-language-router.tsx:142