Get startedGet started for free

Creating a speech response for customers

1. Creating a speech response for customers

Hello and welcome back! Now we have a tailored and safe response for our customers, there are just a few steps remaining to complete our chatbot.

2. Case study plan

In this video, we will focus on delivering the response back to the customer. First, translating the response into the customer's language. Then, converting that text into audio for a spoken reply.

3. Variables to use

To do this, we'll use the language variable we created earlier when detecting the customer's language. And, of course, we'll use the generated response stored in the chatbot_reply variable. Let's start with translation.

4. Response translation

We handle translation using the chat completions endpoint. We send a request to the model, passing in both system and user messages, along with a token limit. In the system message, we describe the task and pass the target language using an f-string. We also instruct the model to return only the translated text - so we get a clean output with no extra clarifications or explanations. The user message contains the original chatbot reply we want to translate.

5. Response translation

After extracting the content from the response, we now have our reply generated in the customer's language. Notice that the model didn't touch the content links.

6. Text-to-speech

Next, we convert that text into audio. We send a request to the audio speech endpoint, passing the model name, a voice option, and the translated reply as input. Then, we save the output using the .stream_to_file() method, creating an audio file that can be sent back to the customer. Here's the first few seconds: "??? ????? ????????? ???????? ?????????, ??????? ???????????? ?? ?? OpenAI".

7. Case study recap

And that's it for our case study! Let's take a moment to recap everything we achieved using just the OpenAI API and a few lines of code. We received a voice message from the customer, transcribed and cleaned it up, powered our model with context to generate a relevant response, and finally, returned that response as audio - in the customer's own language. That's pretty amazing!

8. Next steps

Of course, there's more we could build and improve. For example, we could track the conversation by storing all questions and responses - allowing the chatbot to remember previous interactions and responding more naturally over time.

9. Let's practice!

Time to finish building your chatbot!