- A Squid backend with a security service function to enable access to a Squid AI profile.
- A React frontend that uses Squid's React SDK.
- A Squid Cloud account
- Node.js and npm
- The Squid CLI
- Navigate to the Squid Cloud Console.
- Click Create new application.
- Name the app "squid-facts".
- Choose us-east-1.aws as the project region.
- In the Squid Console, ensure you are in the
devenvironment. If you are in theprodenvironment, click the prod button at the top of the window and select dev from the dropdown. - Select the Agent Studio tab.
- Click Create your first Agent. Give the agent the ID squid-facts with description Squid Facts chatbot.
- Click Create. You have now created an AI agent!
An agent can be thought of as one distinct AI chatbot. You can have multiple agents with different purposes. Notice that agents have two components: Instructions and Abilities.
- Instructions are the rule set for how the chatbot profile responds and answers questions. They can be about tone or purpose. For example, "You are a pirate. Only answer questions like a pirate would", or "You are a helpful customer support expert that understands camping products and can answer questions in detail”.
- Abilities consists of a wide breadth of actions the agent can choose to take, including looking in a knowledge base of context.
- In the Instructions box, enter the following text as the instruction:
You're a friendly AI who shares random facts about squids and answers user questions about squids.
Use only language that is appropriate for children.
Important: Only answer based on the provided context.
- If you haven't yet made an Auth0 API, set up an Auth0 account and create a single-page application configured to use React and an audience using an Auth0 API. Use
'squid-ai'as the API audience. When adding the callback and logout URLs, usehttp://localhost:5173. - In the Squid Cloud Console in the
devenvironment, select the Connectors tab. - Click the Available connectors tab to view all connectors.
- Scroll or search for the Auth0 integration and click Add Connector.
- Enter auth0 for the Connector ID.
- Enter the client ID and audience for your Auth0 app. These can be found in the Auth0 console.
- Click Add Connector.
- Change to the
frontenddirectory, and then install the required NPM packages:
cd frontend
npm install- Change to the
backenddirectory and install the required NPM packages:
cd ../backend
npm installOpen the project in the IDE of your choice.
- In the Application tab, scroll to the Backend Project section and click Create .env file.
- Copy the command and run it in your terminal in the
backendfolder. This creates a.envfile that's used in local development.
The command will have the following format:
squid init-env \
--appId YOUR_APP_ID \
--apiKey YOUR_API_KEY \
--environmentId dev \
--squidDeveloperId YOUR_SQUID_DEVELOPER_KEY \
--region us-east-1.aws- Navigate to
frontend/src/main.tsx. Notice there is anAuth0Providercomponent and aSquidContextProvidercomponent, both of which require configuration:
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<Auth0Provider
domain="AUTH0_DOMAIN"
clientId="AUTH0_CLIENT_ID"
authorizationParams={{
redirect_uri: window.location.origin,
audience: 'squid-ai',
}}
>
<SquidContextProvider
options={{
appId: 'YOUR_APP_ID',
region: 'us-east-1.aws', // replace with your region
environmentId: 'dev',
squidDeveloperId: 'YOUR_SQUID_DEVELOPER_ID',
}}
>
<App />
</SquidContextProvider>
</Auth0Provider>
);-
Replace the placeholders in the
Auth0Providercomponent with the domain and client ID from your Auth0 application. -
Replace the placeholders in the SquidContextProvider component with the App ID, Region and Developer ID for your Squid app. You can find these values in the Squid Console.
- To run the backend locally, change to the
backenddirectory and run:
squid start- Open a separate terminal window. Change to the
frontenddirectory and run:
npm run devYou now have two terminal windows: one running the backend and the other running the frontend.
-
To view the app, navigate to localhost:PORT where PORT is logged in the terminal. The address will likely be
http://localhost:5173. -
Ask a question about squids to the chatbot. Notice you do not get a response. Instead, an unauthorized message appears.
-
Log into the app and ask a question again. Now the AI chatbot will chat with you!
Feel free to keep asking questions and to see what happens when you log out and in. With the Squid Service on the backend handling authorization, you can rest easy knowing only authenticated users will have access to the service.
Here are some questions to try:
- Do squids have arms or tentacles?
- What should I do if I encounter a squid?
- What's your favorite fact about squids?
- Tell me about the Onykia ingens.
Share your favorite squid questions and answers with the Squid Squad on Discord or X.
The @secureAiAgent-decorated function can do more than just check if the user has logged into Auth0. The function can take in a SecureAiAgentContext object and enforce other rules, like so:
@secureAiAgent('squid-facts')
allowChat(context: SecureAiAgentContext): boolean {
// Enforce the user is logged in.
if (!this.isAuthenticated()) {
return false;
}
// Don't allow the user to override the LLM model configured for the agent.
if (context.options?.model !== undefined) {
return false;
}
// Block nefarious prompts in either the prompt itself or the added instructions.
if (`${context.prompt || ''} ${context.options?.instructions || ''}`.toLowerCase().includes('ignore all previous instructions')) {
return false;
}
return true;
}For a list of all options fields that can be analyzed in this function, see BaseAiChatOptions.
Congratulations! You just added a chatbot to an app, and then secured it using the Squid Backend SDK. Feel free to keep asking questions to see just how much this AI chatbot knows about squids!
To prevent further billing, delete the Squid app in the Squid Console by selecting the Overview tab for the app and scrolling down to Delete Application.