My code can make an exchange like :
1 question (by the user) –> 1 answer by my llm.
How can I make the conversation continue ?
1 question (by the user) –> 1 answer by my llm.-> 1 question (by the user) -> 1 answer by the llm etc..
@sock.route(WEBSOCKET_ROUTE)
def transcription_websocket(ws):
exchange_count = 0
max_exchanges = 5
has_seen_media = False
while exchange_count < max_exchanges:
data = json.loads(ws.receive())
match data['event']:
case "connected":
transcriber = TwilioTranscriber()
transcriber.connect()
print('transcriber connected')
case "start":
print('twilio started')
has_seen_media = False
case "media":
if not has_seen_media:
payload_b64 = data['media']['payload']
payload_mulaw = base64.b64decode(payload_b64)
transcriber.stream(payload_mulaw)
twilio = transcriber.llm
ngrok = listener.url()
if transcriber.llm_call_in_progress and twilio :
send_tts_message(current_calls["call_sid"],twilio,ngrok)
twilio = None
transcriber.llm_call_in_progress = False
exchange_count += 1
has_seen_media = True
if exchange_count >= max_exchanges:
ws.send(json.dumps({"continue": True}))
exchange_count = 0
def send_tts_message(call_sid, message, ngrok):
client = Client(api_key, api_secret, account_sid)
# TwiML with a Gather that sends a callback to your server endpoint
twiml = f'<Response><Say>{message}</Say><Gather numDigits="0" timeout="10"><Pause length="10"/></Gather></Response>'
call = client.calls(call_sid).update(twiml=twiml)
print(f"Message sent to call {call_sid}")
The transcription_websocket handles the transcription (of what is said through the phone) and the response of the llm.
We tried to change the twML using a Gather (Without result) we also tried a “for” loop, that didn’t work.
We would be really grateful of any response !!
cyril PETRIS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.