Skip to content

Commit defb23a

Browse files
committed
openai sample
1 parent 82b5982 commit defb23a

File tree

1 file changed

+22
-19
lines changed
  • articles/cognitive-services/Speech-Service/includes/quickstarts/openai-speech

1 file changed

+22
-19
lines changed

articles/cognitive-services/Speech-Service/includes/quickstarts/openai-speech/python.md

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Follow these steps to create a new console application.
4444
pip install openai
4545
```
4646
> [!NOTE]
47-
> This library is maintained by OpenAI and is currently in preview. 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.
47+
> 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.
4848

4949
1. Copy the following code into `openai-speech.py`:
5050

@@ -72,18 +72,17 @@ Follow these steps to create a new console application.
7272
speech_config.speech_recognition_language="en-US"
7373
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_output_config)
7474
75-
# The language of the voice that responds on behalf of OpenAI.
75+
# The language of the voice that responds on behalf of Azure OpenAI.
7676
speech_config.speech_synthesis_voice_name='en-US-JennyMultilingualNeural'
7777
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
7878
79-
# Prompts OpenAI with a request and synthesizes the response.
79+
# Prompts Azure OpenAI with a request and synthesizes the response.
8080
def ask_openai(prompt):
8181
82-
# Ask OpenAI
83-
print('OpenAI prompt:' + prompt)
84-
response = openai.Completion.create(engine=deployment_id, prompt=prompt, max_tokens=10)
82+
# Ask Azure OpenAI
83+
response = openai.Completion.create(engine=deployment_id, prompt=prompt, max_tokens=50)
8584
text = response['choices'][0]['text'].replace('\n', '').replace(' .', '.').strip()
86-
print('OpenAI response:' + text)
85+
print('Azure OpenAI response:' + text)
8786
8887
# Azure text-to-speech output
8988
speech_synthesis_result = speech_synthesizer.speak_text_async(text).get()
@@ -100,13 +99,17 @@ Follow these steps to create a new console application.
10099
# Continuously listens for speech input to recognize and send as text to Azure OpenAI
101100
def chat_with_open_ai():
102101
while True:
103-
print("OpenAI is listening. Ctrl-Z to exit")
102+
print("Azure OpenAI is listening. Say 'Stop' or press Ctrl-Z to end the conversation.")
104103
try:
105104
# Get audio from the microphone and then send it to the TTS service.
106105
speech_recognition_result = speech_recognizer.recognize_once_async().get()
107106
108-
# If speech is recognized, send it to OpenAI and listen for the response.
107+
# If speech is recognized, send it to Azure OpenAI and listen for the response.
109108
if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
109+
if speech_recognition_result.text == "Stop.":
110+
print("Conversation ended.")
111+
break
112+
print("Recognized speech: {}".format(speech_recognition_result.text))
110113
ask_openai(speech_recognition_result.text)
111114
elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch:
112115
print("No speech could be recognized: {}".format(speech_recognition_result.no_match_details))
@@ -141,16 +144,16 @@ Speak into your microphone when prompted. The console output will include the pr
141144

142145
```console
143146
PS C:\dev\openai\python> python.exe .\openai-speech.py
144-
OpenAI is listening. Ctrl-Z to exit
145-
OpenAI prompt:How can artificial intelligence help technical writers?
146-
OpenAI response:Artificial intelligence (AI) can help improve the efficiency of a technical writer's workflow in several ways. For example, AI can be used to generate information maps of documentation sets, analyze customer feedback to identify areas that need improvement, or suggest new topics for documentation based on customer queries. Additionally, AI-powered search engines can help technical writers find relevant information more quickly and easily.
147-
Speech synthesized to speaker for text [Artificial intelligence (AI) can help improve the efficiency of a technical writer's workflow in several ways. For example, AI can be used to generate information maps of documentation sets, analyze customer feedback to identify areas that need improvement, or suggest new topics for documentation based on customer queries. Additionally, AI-powered search engines can help technical writers find relevant information more quickly and easily.]
148-
OpenAI is listening. Ctrl-Z to exit
149-
OpenAI prompt:Write a tagline for an ice cream shop.
150-
OpenAI response:Your favorite ice cream, made fresh!
151-
Speech synthesized to speaker for text [Your favorite ice cream, made fresh!]
152-
OpenAI is listening. Ctrl-Z to exit
153-
No speech could be recognized: NoMatchDetails(reason=NoMatchReason.InitialSilenceTimeout)
147+
Azure OpenAI is listening. Say 'Stop' or press Ctrl-Z to end the conversation.
148+
Recognized speech:Make a comma separated list of all continents.
149+
Azure OpenAI response:Africa, Antarctica, Asia, Australia, Europe, North America, South America
150+
Speech synthesized to speaker for text [Africa, Antarctica, Asia, Australia, Europe, North America, South America]
151+
Azure OpenAI is listening. Say 'Stop' or press Ctrl-Z to end the conversation.
152+
Recognized speech:Make a comma separated list of 1 Astronomical observatory for each continent. The list should include each continent name in parentheses.
153+
Azure OpenAI response:Mauna Kea Observatory (North America), European Southern Observatory (Europe), Atacama Large Millimeter Array (South America), Siding Spring Observatory (Australia), and South African Astronomical Observatory (Africa)
154+
Speech synthesized to speaker for text [Mauna Kea Observatory (North America), European Southern Observatory (Europe), Atacama Large Millimeter Array (South America), Siding Spring Observatory (Australia), and South African Astronomical Observatory (Africa)]
155+
Azure OpenAI is listening. Say 'Stop' or press Ctrl-Z to end the conversation.
156+
Conversation ended.
154157
PS C:\dev\openai\python>
155158
```
156159

0 commit comments

Comments
 (0)