Skip to content

Commit 2228864

Browse files
authored
Merge pull request pipecat-ai#3210 from pipecat-ai/filipi/heygen_liveavatar
Adding support for the HeyGen LiveAvatar API
2 parents 1e98094 + a6ee040 commit 2228864

10 files changed

Lines changed: 536 additions & 42 deletions

File tree

changelog/3210.added.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Added support for the HeyGen LiveAvatar API
2+
(see https://www.liveavatar.com/).

changelog/3210.changed.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
- Updated `HeyGenVideoService` and `HeyGenTransport` to support both HeyGen APIs (Interactive Avatar and Live Avatar).
2+
Using them is as simple as specifying the `service_type` when creating the `HeyGenVideoService` and the `HeyGenTransport`:
3+
```python
4+
heyGen = HeyGenVideoService(
5+
api_key=os.getenv("HEYGEN_LIVE_AVATAR_API_KEY"),
6+
service_type=ServiceType.LIVE_AVATAR,
7+
session=session,
8+
)
9+
```
10+

env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ GROQ_API_KEY=...
8484

8585
# Heygen
8686
HEYGEN_API_KEY=...
87+
HEYGEN_LIVE_AVATAR_API_KEY=...
8788

8889
# Hume
8990
HUME_API_KEY=...

examples/foundational/43a-heygen-video-service.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from pipecat.services.cartesia.tts import CartesiaTTSService
2626
from pipecat.services.deepgram.stt import DeepgramSTTService
2727
from pipecat.services.google.llm import GoogleLLMService
28-
from pipecat.services.heygen.api import AvatarQuality, NewSessionRequest
28+
from pipecat.services.heygen.client import ServiceType
2929
from pipecat.services.heygen.video import HeyGenVideoService
3030
from pipecat.transports.base_transport import BaseTransport, TransportParams
3131
from pipecat.transports.daily.transport import DailyParams, DailyTransport
@@ -73,11 +73,9 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments):
7373
llm = GoogleLLMService(api_key=os.getenv("GOOGLE_API_KEY"))
7474

7575
heyGen = HeyGenVideoService(
76-
api_key=os.getenv("HEYGEN_API_KEY"),
76+
api_key=os.getenv("HEYGEN_LIVE_AVATAR_API_KEY"),
77+
service_type=ServiceType.LIVE_AVATAR,
7778
session=session,
78-
session_request=NewSessionRequest(
79-
avatar_id="Shawn_Therapist_public", version="v2", quality=AvatarQuality.high
80-
),
8179
)
8280

8381
messages = [

src/pipecat/services/heygen/api.py renamed to src/pipecat/services/heygen/api_interactive_avatar.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from loguru import logger
1717
from pydantic import BaseModel, Field
1818

19+
from pipecat.services.heygen.base_api import BaseAvatarApi, StandardSessionResponse
20+
1921

2022
class AvatarQuality(str, Enum):
2123
"""Enum representing different avatar quality levels."""
@@ -136,7 +138,7 @@ def __init__(self, message: str, status: int, response_text: str) -> None:
136138
self.response_text = response_text
137139

138140

139-
class HeyGenApi:
141+
class HeyGenApi(BaseAvatarApi):
140142
"""HeyGen Streaming API client."""
141143

142144
BASE_URL = "https://api.heygen.com/v1"
@@ -193,16 +195,16 @@ async def _request(self, path: str, params: Dict[str, Any], expect_data: bool =
193195
logger.error(f"Network error while calling HeyGen API: {str(e)}")
194196
raise
195197

196-
async def new_session(self, request_data: NewSessionRequest) -> HeyGenSession:
197-
"""Create a new streaming session.
198+
async def new_session(self, request_data: NewSessionRequest) -> StandardSessionResponse:
199+
"""Create a new streaming session and start it immediately.
198200
199201
https://docs.heygen.com/reference/new-session
200202
201203
Args:
202204
request_data: Session configuration parameters.
203205
204206
Returns:
205-
Session information, including ID and access token.
207+
StandardSessionResponse: Standardized session information with HeyGen raw response.
206208
"""
207209
params = {
208210
"quality": request_data.quality,
@@ -225,9 +227,21 @@ async def new_session(self, request_data: NewSessionRequest) -> HeyGenSession:
225227
session_info = await self._request("/streaming.new", params)
226228
print("heygen session info", session_info)
227229

228-
return HeyGenSession.model_validate(session_info)
230+
heygen_session = HeyGenSession.model_validate(session_info)
231+
232+
await self._start_session(heygen_session.session_id)
233+
234+
# Convert to standardized response
235+
return StandardSessionResponse(
236+
session_id=heygen_session.session_id,
237+
access_token=heygen_session.access_token,
238+
livekit_url=heygen_session.url,
239+
livekit_agent_token=heygen_session.livekit_agent_token,
240+
ws_url=heygen_session.realtime_endpoint,
241+
raw_response=heygen_session,
242+
)
229243

230-
async def start_session(self, session_id: str) -> Any:
244+
async def _start_session(self, session_id: str) -> Any:
231245
"""Start the streaming session.
232246
233247
https://docs.heygen.com/reference/start-session

0 commit comments

Comments
 (0)