Embedding an Agent on your Website
Introduction
The play.ai web embed allows you to integrate AI-powered interactions into your web application. This guide will walk you through the process of setting up and using the play.ai web embed in your React application.
Installation
First, install the @play-ai/web-embed
package:
Basic Usage
Here’s a step-by-step guide to implement the play.ai web embed in your React application:
Create an agent
If you haven’t already, create an agent on https://play.ai.
Import the necessary dependencies
Define your web embed ID
Replace YOUR_WEB_EMBED_ID
with the actual ID provided by play.ai, which can be found in the Agent editor.
Find your web embed ID on the last page of the Agent editor under 'Deploy Web'
Define custom events (optional)
Custom events are optional, but they allow you to define custom behavior for your agent, to allow it to execute javascript and interact with the page. Learn more about custom events here.
This example defines a single event called “change-text” that will be triggered when the user specifies the text they want to display.
Implement the custom event handler (optional)
If you define custom events, you must implement an event handler to handle the events. Learn more about the custom event handler here.
This handler logs the event and updates the text state when a “change-text” event is received.
Initialize the web embed
This useEffect
hook initializes the web embed when the component mounts. See all the parameters that can be passed to openEmbed()
here.
Render the component
This example renders the current text in the center of the page.
Full Example
Here’s the complete example of a React component using the play.ai web embed.
View the live example here.
View the code here.
Customization
You can customize the behavior of your AI agent by modifying the agent greeting and prompt. In this example, the agent is instructed to change the text on the page and end the call immediately after doing so.
The greeting and prompt for the Change Text demo agent
Other examples
Conclusion
This guide demonstrates how to integrate the play.ai web embed into your React application. You can extend this functionality by adding more events and customizing the agent’s behavior to suit your specific needs.