|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from poet_chat.agent import poet_runner |
| 4 | +from poet_chat.config import ( |
| 5 | + PEER_OPTIONS, |
| 6 | + fishjam_client, |
| 7 | +) |
| 8 | +from poet_chat.notifier import make_notifier |
| 9 | + |
| 10 | +from fishjam.agent import AudioTrackOptions |
| 11 | + |
| 12 | +CHUNK_SIZE = 12000 |
| 13 | + |
| 14 | + |
| 15 | +async def main(): |
| 16 | + room = fishjam_client.create_room() |
| 17 | + _, token = fishjam_client.create_peer(room.id, PEER_OPTIONS) |
| 18 | + print(f"Join the chat with the following token: {token}") |
| 19 | + |
| 20 | + agent = fishjam_client.create_agent(room.id) |
| 21 | + async with ( |
| 22 | + agent.connect() as fishjam_session, |
| 23 | + await poet_runner.run() as openai_session, |
| 24 | + ): |
| 25 | + track = await fishjam_session.add_track( |
| 26 | + AudioTrackOptions(sample_rate=24000, metadata={"type": "microphone"}) |
| 27 | + ) |
| 28 | + |
| 29 | + async def _openai_recv(): |
| 30 | + msg = "" |
| 31 | + async for event in openai_session: |
| 32 | + if event.type == "audio": |
| 33 | + audio = event.audio.data |
| 34 | + await track.send_chunk(audio) |
| 35 | + elif event.type == "raw_model_event": |
| 36 | + if event.data.type == "input_audio_transcription_completed": |
| 37 | + print(f"Peer said:\n{event.data.transcript}\n") |
| 38 | + elif event.data.type == "transcript_delta": |
| 39 | + msg += event.data.delta |
| 40 | + elif event.data.type == "turn_ended": |
| 41 | + print(f"Agent said:\n{msg}\n") |
| 42 | + msg = "" |
| 43 | + |
| 44 | + async def _fishjam_recv(): |
| 45 | + async for event in fishjam_session.receive(): |
| 46 | + await openai_session.send_audio(event.data) |
| 47 | + |
| 48 | + async with asyncio.TaskGroup() as tg: |
| 49 | + tg.create_task(make_notifier(openai_session).connect()) |
| 50 | + tg.create_task(_openai_recv()) |
| 51 | + tg.create_task(_fishjam_recv()) |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + asyncio.run(main()) |
0 commit comments