Issue with AGI Script: Combining OpenAI and Stream File Operations

Hello everyone,

I’m encountering an issue with my AGI script in Asterisk. When I initiate a call, the script fails to execute the playback part of the AGI script if it includes any OpenAI operations. Specifically, if I comment out the entire OpenAI part of the script, the playback works fine. Similarly, If I comment AGI part of the script it is able to run OpenAI commands. I uncomment even just the import openai line, the playback fails.

My script includes both transcription and text-to-speech operations using OpenAI, and then it tries to play back the resulting audio file. Here’s a simplified version of the script that shows the problem:

#!/usr/bin/env python3

import sys
import openai
import warnings
import subprocess

warnings.filterwarnings("ignore", category=DeprecationWarning)

openai.api_key = "..."

# Transcription audio
audio_file = open("/var/lib/asterisk/sounds/gpt/gpt6_incoming.wav", "rb")
transcription = openai.audio.transcriptions.create(
  model="whisper-1",
  file=audio_file
)
print(transcription.text)
transcripted_text = transcription.text

# Chat completion
completion = openai.ChatCompletion.create(
  model="gpt-4o",
  messages=[
    {"role": "system", "content": "You are a helpful assistant. Answer creatively."},
    {"role": "user", "content": transcripted_text}
  ]
)
completion_response = completion.choices[0].message.content
print(completion_response)

# Text to speech
speech_file_path = "/var/lib/asterisk/sounds/gpt/tts_mp3.mp3"
response = openai.audio.speech.create(
  model="tts-1",
  voice="alloy",
  input=completion_response
)
response.stream_to_file(speech_file_path)
print(response)

# Convert tts_mp3.mp3 into wav
speech_file_wav = "/var/lib/asterisk/sounds/gpt/tts_wav.wav"
subprocess.run([
    "sox", speech_file_path, "-r", "8000", "-c", "1", "-b", "16", speech_file_wav
])
print("convert finished")

# ***Playback (This part doesn't work when OpenAI part is uncommented***)
def main():
    sys.stderr.write("Starting AGI script for playback\n")
    sys.stderr.flush()
    env = {}
    while True:
        line = sys.stdin.readline().strip()
        if line == '':
            break
        sys.stderr.write(f"AGI input: {line}\n")
        key, value = line.split(':', 1)
        env[key.strip()] = value.strip()

    sys.stderr.write(f"Environment: {env}\n")
    sys.stderr.flush()

    # Play the generated wav file
    filename = "/var/lib/asterisk/sounds/gpt/tts_wav"
    sys.stderr.write(f"Attempting playback of {filename}\n")
    sys.stdout.write(f'STREAM FILE {filename} ""\n')
    sys.stdout.flush()
    result = sys.stdin.readline().strip()
    sys.stderr.write(f"Playback result: {result}\n")
    sys.stderr.flush()

if __name__ == '__main__':
    main()

Dialplan Configuration

I’ve also tried separating the tasks in the dialplan to isolate the problem:

[incoming]
exten => xxxxxx,1,NoOp(Incoming call from ${CALLERID(num)})
same => n,Answer()
same => n,Playback(/var/lib/asterisk/sounds/gpt/welcome_wav)
same => n,AGI(/var/lib/asterisk/agi-bin/record_and_hangup.py).   ;I made separate script for recording
same => n,Playback(/var/lib/asterisk/sounds/gpt/wait_wav)
same => n,AGI(/var/lib/asterisk/agi-bin/gpt6.py)  ; current script, without agi part
same => n,Playback(/var/lib/asterisk/sounds/gpt/tts6_wav)  ; playback of the gpt6.py output

Questions

  1. Why does the AGI script fail to play the audio file when the OpenAI part is included?
  2. Is it possible to handle both OpenAI operations and the STREAM FILE command in the same AGI script?
  3. Should I split the tasks into separate scripts and manage them through the dialplan, or is there a better approach?

Thank you for your help!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.