@@ -81,9 +81,24 @@ async def connect(self) -> None:
81
81
self .ws = await websockets .connect (url , extra_headers = headers )
82
82
await self .initialize_session ()
83
83
84
+ async def initialize_session (self ):
85
+ """Control initial session with OpenAI."""
86
+ session_update = {
87
+ "type" : "session.update" ,
88
+ "session" : {
89
+ "turn_detection" : {"type" : "server_vad" },
90
+ "input_audio_format" : "g711_ulaw" ,
91
+ "output_audio_format" : "g711_ulaw" ,
92
+ "voice" : VOICE ,
93
+ "instructions" : SYSTEM_MESSAGE ,
94
+ "modalities" : ["text" , "audio" ],
95
+ "temperature" : 0.8 ,
96
+ }
97
+ }
98
+ print ('Sending session update:' , json .dumps (session_update ))
99
+ await self .ws .send (json .dumps (session_update ))
84
100
85
- async def send_initial_conversation_item (self ):
86
- """Send initial conversation item if AI talks first."""
101
+ # Send initial conversation item if AI talks first
87
102
initial_conversation_item = {
88
103
"type" : "conversation.item.create" ,
89
104
"item" : {
@@ -99,59 +114,6 @@ async def send_initial_conversation_item(self):
99
114
}
100
115
await self .ws .send (json .dumps (initial_conversation_item ))
101
116
102
- async def initialize_session (self ) -> None :
103
- """Control initial session with OpenAI and register tools."""
104
- tools = [
105
- {
106
- "type" : "function" ,
107
- "name" : "handoff_to_agent" ,
108
- "description" : "Request human intervention." ,
109
- "parameters" : {
110
- "type" : "object" ,
111
- "properties" : {
112
- "reason_for_handoff" : {
113
- "type" : "string" ,
114
- "description" : "Reason for requesting an agent."
115
- }
116
- },
117
- "required" : ["reason_for_handoff" ]
118
- }
119
- },
120
- {
121
- "type" : "function" ,
122
- "name" : "get_weather" ,
123
- "description" : "Retrieves weather info for a given lat/lng." ,
124
- "parameters" : {
125
- "type" : "object" ,
126
- "properties" : {
127
- "lat" : {"type" : "number" , "description" : "Latitude." },
128
- "lng" : {"type" : "number" , "description" : "Longitude." },
129
- "location" : {"type" : "string" , "description" : "Location name." }
130
- },
131
- "required" : ["lat" , "lng" , "location" ]
132
- }
133
- }
134
- ]
135
-
136
- session_update = {
137
- "type" : "session.update" ,
138
- "session" : {
139
- "turn_detection" : {"type" : self .turn_detection_mode .value },
140
- "input_audio_format" : "g711_ulaw" ,
141
- "output_audio_format" : "g711_ulaw" ,
142
- "voice" : self .voice ,
143
- "instructions" : self .instructions ,
144
- "modalities" : ["text" , "audio" ],
145
- "temperature" : self .temperature ,
146
- "tools" : tools ,
147
- "tool_choice" : "auto" ,
148
- }
149
- }
150
- print ('Sending session update:' , json .dumps (session_update ))
151
- await self .ws .send (json .dumps (session_update ))
152
-
153
- await self .send_initial_conversation_item (self .ws )
154
-
155
117
async def send_text (self , text : str ) -> None :
156
118
"""Send text message to the API."""
157
119
event = {
0 commit comments