Skip to content

Commit 1d05af0

Browse files
committed
updated the Python Speech sample
1 parent eb6bbc9 commit 1d05af0

File tree

1 file changed

+35
-32
lines changed
  • articles/ai-services/speech-service/includes/quickstarts/openai-speech

1 file changed

+35
-32
lines changed

articles/ai-services/speech-service/includes/quickstarts/openai-speech/python.md

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Install the following Python libraries: `os`, `requests`, `json`
2626

2727
### Set environment variables
2828

29-
This example requires environment variables named `OPEN_AI_KEY`, `OPEN_AI_ENDPOINT`, `SPEECH_KEY`, and `SPEECH_REGION`.
29+
This example requires environment variables named `OPEN_AI_KEY`, `OPEN_AI_ENDPOINT`, `OPEN_AI_DEPLOYMENT_NAME`, `SPEECH_KEY`, and `SPEECH_REGION`.
3030

3131
[!INCLUDE [Environment variables](../../common/environment-variables-openai.md)]
3232

@@ -41,7 +41,7 @@ Follow these steps to create a new console application.
4141
```
4242
1. Run this command to install the OpenAI SDK:
4343
```console
44-
pip install openai==0.28.1
44+
pip install openai
4545
```
4646
> [!NOTE]
4747
> This library is maintained by OpenAI (not Microsoft Azure). Refer to the [release history](https://github.com/openai/openai-python/releases) or the [version.py commit history](https://github.com/openai/openai-python/commits/main/openai/version.py) to track the latest updates to the library.
@@ -51,63 +51,66 @@ Follow these steps to create a new console application.
5151
```Python
5252
import os
5353
import azure.cognitiveservices.speech as speechsdk
54-
import openai
55-
56-
# This example requires environment variables named "OPEN_AI_KEY" and "OPEN_AI_ENDPOINT"
54+
from openai import AzureOpenAI
55+
56+
# This example requires environment variables named "OPEN_AI_KEY", "OPEN_AI_ENDPOINT" and "OPEN_AI_DEPLOYMENT_NAME"
5757
# Your endpoint should look like the following https://YOUR_OPEN_AI_RESOURCE_NAME.openai.azure.com/
58-
openai.api_key = os.environ.get('OPEN_AI_KEY')
59-
openai.api_base = os.environ.get('OPEN_AI_ENDPOINT')
60-
openai.api_type = 'azure'
61-
openai.api_version = '2022-12-01'
62-
58+
client = AzureOpenAI(
59+
azure_endpoint=os.environ.get('OPEN_AI_ENDPOINT'),
60+
api_key=os.environ.get('OPEN_AI_KEY'),
61+
api_version="2023-05-15"
62+
)
63+
6364
# This will correspond to the custom name you chose for your deployment when you deployed a model.
64-
deployment_id='gpt-35-turbo-instruct'
65-
65+
deployment_id=os.environ.get('OPEN_AI_DEPLOYMENT_NAME')
66+
6667
# This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
6768
speech_config = speechsdk.SpeechConfig(subscription=os.environ.get('SPEECH_KEY'), region=os.environ.get('SPEECH_REGION'))
6869
audio_output_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)
6970
audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
70-
71+
7172
# Should be the locale for the speaker's language.
7273
speech_config.speech_recognition_language="en-US"
7374
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
74-
75+
7576
# The language of the voice that responds on behalf of Azure OpenAI.
7677
speech_config.speech_synthesis_voice_name='en-US-JennyMultilingualNeural'
7778
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_output_config)
78-
7979
# tts sentence end mark
8080
tts_sentence_end = [ ".", "!", "?", ";", "。", "!", "?", ";", "\n" ]
81-
81+
8282
# Prompts Azure OpenAI with a request and synthesizes the response.
8383
def ask_openai(prompt):
8484
# Ask Azure OpenAI in streaming way
85-
response = openai.Completion.create(engine=deployment_id, prompt=prompt, max_tokens=200, stream=True)
85+
response = client.chat.completions.create(model=deployment_id, max_tokens=200, stream=True, messages=[
86+
{"role": "user", "content": prompt}
87+
])
8688
collected_messages = []
8789
last_tts_request = None
88-
90+
8991
# iterate through the stream response stream
9092
for chunk in response:
91-
if len(chunk['choices']) > 0:
92-
chunk_message = chunk['choices'][0]['text'] # extract the message
93-
collected_messages.append(chunk_message) # save the message
94-
if chunk_message in tts_sentence_end: # sentence end found
95-
text = ''.join(collected_messages).strip() # join the recieved message together to build a sentence
96-
if text != '': # if sentence only have \n or space, we could skip
97-
print(f"Speech synthesized to speaker for: {text}")
98-
last_tts_request = speech_synthesizer.speak_text_async(text)
99-
collected_messages.clear()
93+
if len(chunk.choices) > 0:
94+
chunk_message = chunk.choices[0].delta.content # extract the message
95+
if chunk_message is not None:
96+
collected_messages.append(chunk_message) # save the message
97+
if chunk_message in tts_sentence_end: # sentence end found
98+
text = ''.join(collected_messages).strip() # join the recieved message together to build a sentence
99+
if text != '': # if sentence only have \n or space, we could skip
100+
print(f"Speech synthesized to speaker for: {text}")
101+
last_tts_request = speech_synthesizer.speak_text_async(text)
102+
collected_messages.clear()
100103
if last_tts_request:
101104
last_tts_request.get()
102-
105+
103106
# Continuously listens for speech input to recognize and send as text to Azure OpenAI
104107
def chat_with_open_ai():
105108
while True:
106109
print("Azure OpenAI is listening. Say 'Stop' or press Ctrl-Z to end the conversation.")
107110
try:
108111
# Get audio from the microphone and then send it to the TTS service.
109112
speech_recognition_result = speech_recognizer.recognize_once_async().get()
110-
113+
111114
# If speech is recognized, send it to Azure OpenAI and listen for the response.
112115
if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
113116
if speech_recognition_result.text == "Stop.":
@@ -125,9 +128,9 @@ Follow these steps to create a new console application.
125128
print("Error details: {}".format(cancellation_details.error_details))
126129
except EOFError:
127130
break
128-
131+
129132
# Main
130-
133+
131134
try:
132135
chat_with_open_ai()
133136
except Exception as err:
@@ -143,7 +146,7 @@ python openai-speech.py
143146
```
144147

145148
> [!IMPORTANT]
146-
> Make sure that you set the `OPEN_AI_KEY`, `OPEN_AI_ENDPOINT`, `SPEECH_KEY` and `SPEECH_REGION` environment variables as described [previously](#set-environment-variables). If you don't set these variables, the sample will fail with an error message.
149+
> Make sure that you set the `OPEN_AI_KEY`, `OPEN_AI_ENDPOINT`, `OPEN_AI_DEPLOYMENT_NAME`, `SPEECH_KEY` and `SPEECH_REGION` environment variables as described [previously](#set-environment-variables). If you don't set these variables, the sample will fail with an error message.
147150
148151
Speak into your microphone when prompted. The console output includes the prompt for you to begin speaking, then your request as text, and then the response from Azure OpenAI as text. The response from Azure OpenAI should be converted from text to speech and then output to the default speaker.
149152

0 commit comments

Comments
 (0)