React
Add Siri like Native AI Assistants to React App
-
Prerequisites
Before using this package, ensure you have the following:
- An existing React application
- Sugar AI Account or Self Hosted
-
Install Pacakge
Install the latest package in you react project
-
Create Copilot
Login/Sigup Account or Self Hosted
Create a Copilot and get the
copilotId
andtoken
-
Add to your main file
src/App.tsx
-
Wrap your App
src/App.tsx
Take this example of
Todo app
-
Add Assistants
Add the Voice assistant to the pages you want to. If you want to enable it on all the pages, add it to common section.
This will show a mic button on the bottom center of the page. Test it out if you talk with it. This Assistant have no registered actions
Add the Text assistant to the pages you want to. If you want to enable it on all the pages, add it to common section.
This will show a Chat button on the bottom center of the page. Test it out if you talk with it. This Assistant have no registered actions
-
Register Actions
To invoke actions by AI Assistant, register methods using
registerAction
with its name, definition and callback.After this, ask Assistant to perform the action by saying
Remind to get a haircut
. In you app you can use equivalent instruction. -
Active Screen Context (Optional)
To make AI Assistant aware of the current screen content, use
useStateEmbedding
to track the current screen content.useStateEmbedding
is build on too ofuseState
hook. This automatically create an embedding in db for the current user with a scope.you can easily replace your
useState
hook withuseStateEmbedding
and pass the scope as the second argument. This will define the scope for created embedding.Check out the todo App exmaple below
useStateEmbedding
Guides lead a user through a specific task they want to accomplish, often with a sequence of steps. Writing a good guide requires thinking about what your users are trying to do.
-
Test The AI Assistant on your App
Go ahead and press the Mic button shown in the Voice Assistant component to interact with the AI Assistant.
You should be able to invoke registered actions like “Add a todo item”.
-
Refine Prompt
Go to Copilot > Select Copilot > Linked Packages
Update, Version, Test & Deploy the prompt