Compare commits
13 Commits
feat/add-t
...
realtime-a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3c12ead6e2 | ||
|
|
8e49e4c807 | ||
|
|
ba8fbf7be4 | ||
|
|
bcaea9916d | ||
|
|
0fa4f0385e | ||
|
|
6fca8b4d42 | ||
|
|
431281d47c | ||
|
|
274a307e83 | ||
|
|
03ef24b031 | ||
|
|
21f6c6d468 | ||
|
|
d7a7e3174c | ||
|
|
e653fa74c4 | ||
|
|
5dd784d63e |
4
.env
4
.env
@@ -1,3 +1 @@
|
||||
HEYGEN_API_KEY=your Heygen API key
|
||||
OPENAI_API_KEY=your OpenAI API key
|
||||
NEXT_PUBLIC_OPENAI_API_KEY=your OpenAI API key
|
||||
HEYGEN_API_KEY=your Heygen API key
|
||||
26
README.md
26
README.md
@@ -23,13 +23,13 @@ Feel free to play around with the existing code and please leave any feedback fo
|
||||
|
||||
6. Run `npm run dev`
|
||||
|
||||
### Difference between Trial Token and Enterprise API Token
|
||||
### Using the API Key
|
||||
|
||||
The HeyGen Trial Token is available to all users, not just Enterprise users, and allows for testing of the Interactive Avatar API, as well as other HeyGen API endpoints.
|
||||
Every HeyGen user receives 10 free API Credits when they sign up for HeyGen. You can access your HeyGen API Key at https://app.heygen.com/settings?nav=API
|
||||
|
||||
Each Trial Token is limited to 3 concurrent interactive sessions. However, every interactive session you create with the Trial Token is free of charge, no matter how many tasks are sent to the avatar. Please note that interactive sessions will automatically close after 10 minutes of no tasks sent.
|
||||
To see the different amounts of credits that are consumed by different endpoints of the API, visit the Limits page of the API documentation: https://docs.heygen.com/reference/limits
|
||||
|
||||
If you do not 'close' the interactive sessions and try to open more than 3, you will encounter errors including stuttering and freezing of the Interactive Avatar. Please endeavor to only have 3 sessions open at any time while you are testing the Interactive Avatar API with your Trial Token.
|
||||
To add more API credits, subscribe to one of HeyGen's API Plans. Pricing and details can be found at https://www.heygen.com/api-pricing
|
||||
|
||||
### Starting sessions
|
||||
|
||||
@@ -41,24 +41,6 @@ After you see Monica appear on the screen, you can enter text into the input lab
|
||||
|
||||
If you want to see a different Avatar or try a different voice, you can close the session and enter the IDs and then 'start' the session again. Please see below for information on where to retrieve different Avatar and voice IDs that you can use.
|
||||
|
||||
### Connecting to OpenAI
|
||||
|
||||
A common use case for a Interactive Avatar is to use it as the 'face' of an LLM that users can interact with. In this demo we have included functionality to showcase this by both accepting user input via voice (using OpenAI's Whisper library) and also sending that input to an OpenAI LLM model (using their Chat Completions endpoint).
|
||||
|
||||
Both of these features of this demo require an OpenAI API Key. If you do not have a paid OpenAI account, you can learn more on their website: [https://openai.com/index/openai-api/]
|
||||
|
||||
Without an OpenAI API Key, this functionality will not work, and the Interactive Avatar will only be able to repeat text input that you provide, and not demonstrate being the 'face' of an LLM. Regardless, this demo is meant to demonstrate what kinds of apps and experiences you can build with our Interactive Avatar SDK, so you can code your own connection to a different LLM if you so choose.
|
||||
|
||||
To add your Open AI API Key, fill copy it to the `OPENAI_API_KEY` and `NEXT_PUBLIC_OPENAI_API_KEY` variables in the `.env` file.
|
||||
|
||||
### How does the integration with OpenAI / ChatGPT work?
|
||||
|
||||
In this demo, we are calling the Chat Completions API from OpenAI in order to come up with some response to user input. You can see the relevant code in components/InteractiveAvatar.tsx.
|
||||
|
||||
In the initialMessages parameter, you can replace the content of the 'system' message with whatever 'knowledge base' or context that you would like the GPT-4o model to reply to the user's input with.
|
||||
|
||||
You can explore this API and the different parameters and models available here: [https://platform.openai.com/docs/guides/text-generation/chat-completions-api]
|
||||
|
||||
### Which Avatars can I use with this project?
|
||||
|
||||
By default, there are several Public Avatars that can be used in Interactive Avatar. (AKA Interactive Avatars.) You can find the Avatar IDs for these Public Avatars by navigating to [app.heygen.com/interactive-avatar](https://app.heygen.com/interactive-avatar) and clicking 'Select Avatar' and copying the avatar id.
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import { openai } from "@ai-sdk/openai";
|
||||
import { streamText } from "ai";
|
||||
|
||||
// Allow streaming responses up to 30 seconds
|
||||
export const maxDuration = 30;
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const { messages } = await req.json();
|
||||
|
||||
const result = await streamText({
|
||||
model: openai("gpt-4-turbo"),
|
||||
messages,
|
||||
});
|
||||
|
||||
return result.toAIStreamResponse();
|
||||
}
|
||||
@@ -20,3 +20,34 @@ export const AVATARS = [
|
||||
name: "Joshua Heygen CEO",
|
||||
},
|
||||
];
|
||||
|
||||
export const STT_LANGUAGE_LIST = [
|
||||
{ label: 'Bulgarian', value: 'bg', key: 'bg' },
|
||||
{ label: 'Chinese', value: 'zh', key: 'zh' },
|
||||
{ label: 'Czech', value: 'cs', key: 'cs' },
|
||||
{ label: 'Danish', value: 'da', key: 'da' },
|
||||
{ label: 'Dutch', value: 'nl', key: 'nl' },
|
||||
{ label: 'English', value: 'en', key: 'en' },
|
||||
{ label: 'Finnish', value: 'fi', key: 'fi' },
|
||||
{ label: 'French', value: 'fr', key: 'fr' },
|
||||
{ label: 'German', value: 'de', key: 'de' },
|
||||
{ label: 'Greek', value: 'el', key: 'el' },
|
||||
{ label: 'Hindi', value: 'hi', key: 'hi' },
|
||||
{ label: 'Hungarian', value: 'hu', key: 'hu' },
|
||||
{ label: 'Indonesian', value: 'id', key: 'id' },
|
||||
{ label: 'Italian', value: 'it', key: 'it' },
|
||||
{ label: 'Japanese', value: 'ja', key: 'ja' },
|
||||
{ label: 'Korean', value: 'ko', key: 'ko' },
|
||||
{ label: 'Malay', value: 'ms', key: 'ms' },
|
||||
{ label: 'Norwegian', value: 'no', key: 'no' },
|
||||
{ label: 'Polish', value: 'pl', key: 'pl' },
|
||||
{ label: 'Portuguese', value: 'pt', key: 'pt' },
|
||||
{ label: 'Romanian', value: 'ro', key: 'ro' },
|
||||
{ label: 'Russian', value: 'ru', key: 'ru' },
|
||||
{ label: 'Slovak', value: 'sk', key: 'sk' },
|
||||
{ label: 'Spanish', value: 'es', key: 'es' },
|
||||
{ label: 'Swedish', value: 'sv', key: 'sv' },
|
||||
{ label: 'Turkish', value: 'tr', key: 'tr' },
|
||||
{ label: 'Ukrainian', value: 'uk', key: 'uk' },
|
||||
{ label: 'Vietnamese', value: 'vi', key: 'vi' },
|
||||
];
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { StartAvatarResponse } from "@heygen/streaming-avatar";
|
||||
|
||||
import StreamingAvatar, {
|
||||
AvatarQuality,
|
||||
StreamingEvents,
|
||||
StreamingEvents, TaskMode, TaskType, VoiceEmotion,
|
||||
} from "@heygen/streaming-avatar";
|
||||
import {
|
||||
Button,
|
||||
@@ -23,7 +23,7 @@ import { useMemoizedFn, usePrevious } from "ahooks";
|
||||
|
||||
import InteractiveAvatarTextInput from "./InteractiveAvatarTextInput";
|
||||
|
||||
import { AVATARS } from "@/app/lib/constants";
|
||||
import {AVATARS, STT_LANGUAGE_LIST} from "@/app/lib/constants";
|
||||
|
||||
export default function InteractiveAvatar() {
|
||||
const [isLoadingSession, setIsLoadingSession] = useState(false);
|
||||
@@ -31,7 +31,9 @@ export default function InteractiveAvatar() {
|
||||
const [stream, setStream] = useState<MediaStream>();
|
||||
const [debug, setDebug] = useState<string>();
|
||||
const [knowledgeId, setKnowledgeId] = useState<string>("");
|
||||
const [avatarId, setAvatarId] = useState<string>("");
|
||||
const [avatarId, setAvatarId] = useState<string>("Wayne_20240711");
|
||||
const [language, setLanguage] = useState<string>('en');
|
||||
|
||||
const [data, setData] = useState<StartAvatarResponse>();
|
||||
const [text, setText] = useState<string>("");
|
||||
const mediaStream = useRef<HTMLVideoElement>(null);
|
||||
@@ -62,6 +64,8 @@ export default function InteractiveAvatar() {
|
||||
|
||||
avatar.current = new StreamingAvatar({
|
||||
token: newToken,
|
||||
basePath: "https://api.heygen.com",
|
||||
userAudioWebsocketPath: "ws://localhost:3001/user-audio-input",
|
||||
});
|
||||
avatar.current.on(StreamingEvents.AVATAR_START_TALKING, (e) => {
|
||||
console.log("Avatar started talking", e);
|
||||
@@ -89,12 +93,14 @@ export default function InteractiveAvatar() {
|
||||
const res = await avatar.current.createStartAvatar({
|
||||
quality: AvatarQuality.Low,
|
||||
avatarName: avatarId,
|
||||
knowledgeId: knowledgeId,
|
||||
disableIdleTimeout: true,
|
||||
});
|
||||
|
||||
setData(res);
|
||||
// default to voice mode
|
||||
await avatar.current?.startVoiceChat();
|
||||
await avatar.current?.startVoiceChat({
|
||||
useSilencePrompt: false
|
||||
});
|
||||
setChatMode("voice_mode");
|
||||
} catch (error) {
|
||||
console.error("Error starting avatar session:", error);
|
||||
@@ -109,13 +115,13 @@ export default function InteractiveAvatar() {
|
||||
|
||||
return;
|
||||
}
|
||||
await avatar.current
|
||||
.speak({ text: text })
|
||||
.catch((e) => {
|
||||
setDebug(e.message);
|
||||
});
|
||||
// speak({ text: text, task_type: TaskType.REPEAT })
|
||||
await avatar.current.speak({ text: text, taskType: TaskType.REPEAT, taskMode: TaskMode.SYNC }).catch((e) => {
|
||||
setDebug(e.message);
|
||||
});
|
||||
setIsLoadingRepeat(false);
|
||||
}
|
||||
|
||||
async function handleInterrupt() {
|
||||
if (!avatar.current) {
|
||||
setDebug("Avatar API not initialized");
|
||||
@@ -129,12 +135,7 @@ export default function InteractiveAvatar() {
|
||||
});
|
||||
}
|
||||
async function endSession() {
|
||||
if (!avatar.current) {
|
||||
setDebug("Avatar API not initialized");
|
||||
|
||||
return;
|
||||
}
|
||||
await avatar.current.stopAvatar();
|
||||
await avatar.current?.stopAvatar();
|
||||
setStream(undefined);
|
||||
}
|
||||
|
||||
@@ -247,6 +248,21 @@ export default function InteractiveAvatar() {
|
||||
</SelectItem>
|
||||
))}
|
||||
</Select>
|
||||
<Select
|
||||
label="Select language"
|
||||
placeholder="Select language"
|
||||
className="max-w-xs"
|
||||
selectedKeys={[language]}
|
||||
onChange={(e) => {
|
||||
setLanguage(e.target.value);
|
||||
}}
|
||||
>
|
||||
{STT_LANGUAGE_LIST.map((lang) => (
|
||||
<SelectItem key={lang.key}>
|
||||
{lang.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</Select>
|
||||
</div>
|
||||
<Button
|
||||
className="bg-gradient-to-tr from-indigo-500 to-indigo-300 w-full text-white"
|
||||
|
||||
@@ -28,7 +28,7 @@ export default function NavBar() {
|
||||
<Link
|
||||
isExternal
|
||||
color="foreground"
|
||||
href="https://app.heygen.com/interactive-avatar"
|
||||
href="https://labs.heygen.com/interactive-avatar"
|
||||
>
|
||||
Avatars
|
||||
</Link>
|
||||
|
||||
26
package.json
26
package.json
@@ -10,20 +10,20 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@ai-sdk/openai": "^0.0.34",
|
||||
"@heygen/streaming-avatar": "^2.0.0-beta.1",
|
||||
"@nextui-org/button": "2.0.34",
|
||||
"@heygen/streaming-avatar": "github:HeyGen-Official/StreamingAvatarSDK#realtime-alpha",
|
||||
"@nextui-org/button": "^2.0.34",
|
||||
"@nextui-org/chip": "^2.0.32",
|
||||
"@nextui-org/code": "2.0.29",
|
||||
"@nextui-org/input": "2.2.2",
|
||||
"@nextui-org/kbd": "2.0.30",
|
||||
"@nextui-org/link": "2.0.32",
|
||||
"@nextui-org/listbox": "2.1.21",
|
||||
"@nextui-org/navbar": "2.0.33",
|
||||
"@nextui-org/code": "^2.0.29",
|
||||
"@nextui-org/input": "^2.2.2",
|
||||
"@nextui-org/kbd": "^2.0.30",
|
||||
"@nextui-org/link": "^2.0.32",
|
||||
"@nextui-org/listbox": "^2.1.21",
|
||||
"@nextui-org/navbar": "^2.0.33",
|
||||
"@nextui-org/react": "^2.4.2",
|
||||
"@nextui-org/snippet": "2.0.38",
|
||||
"@nextui-org/switch": "2.0.31",
|
||||
"@nextui-org/system": "2.2.1",
|
||||
"@nextui-org/theme": "2.2.5",
|
||||
"@nextui-org/snippet": "^2.0.38",
|
||||
"@nextui-org/switch": "^2.0.31",
|
||||
"@nextui-org/system": "^2.2.1",
|
||||
"@nextui-org/theme": "^2.2.5",
|
||||
"@phosphor-icons/react": "^2.1.5",
|
||||
"@react-aria/ssr": "3.9.4",
|
||||
"@react-aria/visually-hidden": "3.8.12",
|
||||
@@ -32,7 +32,7 @@
|
||||
"ahooks": "^3.8.1",
|
||||
"ai": "^3.2.15",
|
||||
"clsx": "2.1.1",
|
||||
"framer-motion": "~11.1.1",
|
||||
"framer-motion": "^11.5.6",
|
||||
"intl-messageformat": "^10.5.0",
|
||||
"next": "14.2.4",
|
||||
"next-themes": "^0.2.1",
|
||||
|
||||
Reference in New Issue
Block a user