Example
Overview of the demo, its purpose, functionality, and how it works.
Overview
The demo integrates OpenAI’s API with the Avaturn.Live SDK, allowing users to interact with an AI-powered avatar through a chat interface. The demo allows users to submit text inputs that either trigger an echo response from the avatar or connect to OpenAI’s GPT-4 model for more advanced interactions.
The demo consists of a few key components:
- OpenAI Hook: A custom hook that manages the connection and communication with OpenAI’s API.
- TokenPopup: A popup modal for entering and submitting the OpenAI API token.
- SettingsForm: A form that allows users to select a session mode (Echo/OpenAI) and provide the necessary API keys.
- Home Component: The main interface that displays the video feed and input form for interacting with the avatar.
Details
1. OpenAI Hook
The useOpenAI
hook manages the initialization and communication with OpenAI’s API.
- The hook initializes the OpenAI client using the provided token.
- It streams the response from OpenAI, breaking it into segments for real-time updates.
- The response is passed back to the caller via a callback function.
2. TokenPopup Component
The TokenPopup
component displays a modal where users can enter their OpenAI API token. This token is used to authenticate requests to the OpenAI API.
- The
TokenPopup
component maintains a local state to track whether the popup is open or closed. - Users input their token in a text field.
- On form submission, the token is validated and sent to the parent component via the
onSubmit
prop.
3. SettingsForm Component
The SettingsForm
component allows users to configure the session mode and provide necessary API keys.
- The component manages local state for
apiKey
,gptKey
, andmode
. - Users select a mode and input the necessary keys.
- The form validates the input, ensuring that all required fields are filled.
- Upon submission, the settings are passed back to the parent component.
4. Home Component
The Home
component is the main user interface, displaying the video feed of the avatar and an input form for user interaction.
- The component initializes the avatar using the Avaturn.Live SDK.
- It displays a video feed and a form for user input.
- Users type a message, which is either echoed by the avatar or sent to OpenAI for processing.
- The response is displayed in real-time through the avatar.