React library
Use RΞASON with React with ease
RΞASON has a React package called tryreason-react
that is the recommend way to integrate with your backend RΞASON app.
Installation
tryreason-react
tryreason-react
provides two hooks:
useReason()
: calls RΞASON and returns the results for you (be that streaming or not);useChat()
: this is made for chat-like agents that use the default agent streaming behaviour. When interacting chat-like agents, this is the recommend hook to use.
useReason()
hook
data
: The data your app returns;reason
: A function that actually makes the request to your app;onFinish
: A callback to handle when RΞASON stop streaming;onError
: A callback that’ll get called if RΞASON errors;isLoading
: Whenever a request is made but has no streaming response has arrived yet. This will befalse
as soon as the first response comes;isStreaming
: This becomestrue
when the first streaming byte comes. It remainstrue
while your app continues to stream more data.
Options
The reason()
function that useReason()
returns accepts a parameter that can contain some options:
useChat()
hook
If your entrypoint calls a chat-like agent with the default agent streaming behaviour this hook is your best-friend.
Here’s how you can use the useChat()
hook:
By the way, if the above looks similar to Vercel’s AI SDK to you, its because we took major inspiration from their React hooks. We believe its the best syntax for developers and we congralute Vercel for being the ones who created it.
And we’ll be testing it in the MathAgent
we created in the Agents page.
Showcase
useChat() in action
Properties
messages
: Array containing all the chat messages;input
: The input for the user to type its message;handleInputChange
: The function to pass to<input type="text" onChange={handleInputChange} value={input} />
;handleSubmit
: The function to call when the user wants to send its message;memoryID
: The memory ID of the chat;clear
: A function that when called will clear all messages in order for the user to start a new chat;onFinish
: A callback to handle when RΞASON stop streaming;onError
: A callback that’ll get called if RΞASON errors;isLoading
: Whenever a request is made but has no streaming response has arrived yet. This will befalse
as soon as the first response comes;isStreaming
: This becomestrue
when the first streaming byte comes. It remainstrue
while your app continues to stream more data.
Options
Similar to useReason()
you can also pass some request options to useChat()
:
useChat()
only support using POST
requests.
Was this page helpful?