-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separated client out from RealtimeAgent
- Loading branch information
1 parent
96cc3bd
commit 6145e62
Showing
5 changed files
with
91 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import asyncio | ||
import json | ||
from abc import ABC, abstractmethod | ||
|
||
import websockets | ||
|
||
from .function_observer import FunctionObserver | ||
|
||
|
||
class Client(ABC): | ||
def __init__(self, agent, audio_adapter, function_observer: FunctionObserver): | ||
self._agent = agent | ||
self._observers = [] | ||
self._openai_ws = None # todo factor out to OpenAIClient | ||
self.register(audio_adapter) | ||
self.register(function_observer) | ||
|
||
def register(self, observer): | ||
observer.register_client(self) | ||
self._observers.append(observer) | ||
|
||
async def notify_observers(self, message): | ||
for observer in self._observers: | ||
await observer.update(message) | ||
|
||
async def function_result(self, call_id, result): | ||
result_item = { | ||
"type": "conversation.item.create", | ||
"item": { | ||
"type": "function_call_output", | ||
"call_id": call_id, | ||
"output": result, | ||
}, | ||
} | ||
await self._openai_ws.send(json.dumps(result_item)) | ||
await self._openai_ws.send(json.dumps({"type": "response.create"})) | ||
|
||
# todo override in specific clients | ||
async def initialize_session(self): | ||
"""Control initial session with OpenAI.""" | ||
session_update = { | ||
"turn_detection": {"type": "server_vad"}, | ||
"voice": self._agent.voice, | ||
"instructions": self._agent.system_message, | ||
"modalities": ["text", "audio"], | ||
"temperature": 0.8, | ||
} | ||
await self.session_update(session_update) | ||
|
||
# todo override in specific clients | ||
async def session_update(self, session_options): | ||
update = {"type": "session.update", "session": session_options} | ||
print("Sending session update:", json.dumps(update)) | ||
await self._openai_ws.send(json.dumps(update)) | ||
|
||
async def _read_from_client(self): | ||
try: | ||
async for openai_message in self._openai_ws: | ||
response = json.loads(openai_message) | ||
await self.notify_observers(response) | ||
except Exception as e: | ||
print(f"Error in _read_from_client: {e}") | ||
|
||
async def run(self): | ||
|
||
async with websockets.connect( | ||
"wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01", | ||
additional_headers={ | ||
"Authorization": f"Bearer {self._agent.llm_config['config_list'][0]['api_key']}", | ||
"OpenAI-Beta": "realtime=v1", | ||
}, | ||
) as openai_ws: | ||
self._openai_ws = openai_ws | ||
await self.initialize_session() | ||
await asyncio.gather(self._read_from_client(), *[observer.run() for observer in self._observers]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters