|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import logging |
| 5 | +import os |
| 6 | + |
| 7 | +from samples.concepts.audio.audio_player import AudioPlayer |
| 8 | +from samples.concepts.audio.audio_recorder import AudioRecorder |
| 9 | +from semantic_kernel.connectors.ai.open_ai import ( |
| 10 | + AzureAudioToText, |
| 11 | + AzureChatCompletion, |
| 12 | + AzureTextToAudio, |
| 13 | + OpenAIChatPromptExecutionSettings, |
| 14 | + OpenAITextToAudioExecutionSettings, |
| 15 | +) |
| 16 | +from semantic_kernel.contents import AudioContent, ChatHistory |
| 17 | + |
| 18 | +# This simple sample demonstrates how to use the AzureChatCompletion, AzureTextToAudio, and AzureAudioToText |
| 19 | +# services to create a chat bot that can communicate with the user using both audio input and output. |
| 20 | +# The chatbot will engage in a conversation with the user by audio only. |
| 21 | +# This sample combines the functionality of the samples/concepts/audio/01-chat_with_audio_input.py and |
| 22 | +# samples/concepts/audio/02-chat_with_audio_output.py samples. |
| 23 | + |
| 24 | +# Resources required for this sample: |
| 25 | +# 1. An Azure OpenAI model deployment (e.g. GPT-4o-mini). |
| 26 | +# 2. An Azure Text to Speech deployment (e.g. tts). |
| 27 | +# 3. An Azure Speech to Text deployment (e.g. whisper). |
| 28 | + |
| 29 | +# Additional dependencies required for this sample: |
| 30 | +# - pyaudio: `pip install pyaudio` or `uv pip install pyaudio` if you are using uv and have a virtual env activated. |
| 31 | +# - keyboard: `pip install keyboard` or `uv pip install keyboard` if you are using uv and have a virtual env activated. |
| 32 | + |
| 33 | + |
| 34 | +logging.basicConfig(level=logging.WARNING) |
| 35 | +AUDIO_FILEPATH = os.path.join(os.path.dirname(__file__), "output.wav") |
| 36 | + |
| 37 | + |
| 38 | +system_message = """ |
| 39 | +You are a chat bot. Your name is Mosscap and |
| 40 | +you have one goal: figure out what people need. |
| 41 | +Your full name, should you need to know it, is |
| 42 | +Splendid Speckled Mosscap. You communicate |
| 43 | +effectively, but you tend to answer with long |
| 44 | +flowery prose. |
| 45 | +""" |
| 46 | + |
| 47 | + |
| 48 | +chat_service = AzureChatCompletion() |
| 49 | +text_to_audio_service = AzureTextToAudio() |
| 50 | +audio_to_text_service = AzureAudioToText() |
| 51 | + |
| 52 | +history = ChatHistory() |
| 53 | +history.add_user_message("Hi there, who are you?") |
| 54 | +history.add_assistant_message("I am Mosscap, a chat bot. I'm trying to figure out what people need.") |
| 55 | + |
| 56 | + |
| 57 | +async def chat() -> bool: |
| 58 | + try: |
| 59 | + print("User:> ", end="", flush=True) |
| 60 | + with AudioRecorder(output_filepath=AUDIO_FILEPATH) as recorder: |
| 61 | + recorder.start_recording() |
| 62 | + user_input = await audio_to_text_service.get_text_content(AudioContent.from_audio_file(AUDIO_FILEPATH)) |
| 63 | + print(user_input.text) |
| 64 | + except KeyboardInterrupt: |
| 65 | + print("\n\nExiting chat...") |
| 66 | + return False |
| 67 | + except EOFError: |
| 68 | + print("\n\nExiting chat...") |
| 69 | + return False |
| 70 | + |
| 71 | + if "exit" in user_input.text.lower(): |
| 72 | + print("\n\nExiting chat...") |
| 73 | + return False |
| 74 | + |
| 75 | + history.add_user_message(user_input.text) |
| 76 | + |
| 77 | + # No need to stream the response since we can only pass the |
| 78 | + # response to the text to audio service as a whole |
| 79 | + response = await chat_service.get_chat_message_content( |
| 80 | + chat_history=history, |
| 81 | + settings=OpenAIChatPromptExecutionSettings( |
| 82 | + max_tokens=2000, |
| 83 | + temperature=0.7, |
| 84 | + top_p=0.8, |
| 85 | + ), |
| 86 | + ) |
| 87 | + |
| 88 | + # Need to set the response format to wav since the audio player only supports wav files |
| 89 | + audio_content = await text_to_audio_service.get_audio_content( |
| 90 | + response.content, OpenAITextToAudioExecutionSettings(response_format="wav") |
| 91 | + ) |
| 92 | + print("Mosscap:> ", end="", flush=True) |
| 93 | + AudioPlayer(audio_content=audio_content).play(text=response.content) |
| 94 | + |
| 95 | + history.add_message(response) |
| 96 | + |
| 97 | + return True |
| 98 | + |
| 99 | + |
| 100 | +async def main() -> None: |
| 101 | + print( |
| 102 | + "Instruction: when it's your turn to speak, press the spacebar to start recording." |
| 103 | + " Release the spacebar to stop recording." |
| 104 | + ) |
| 105 | + |
| 106 | + chatting = True |
| 107 | + while chatting: |
| 108 | + chatting = await chat() |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + asyncio.run(main()) |
0 commit comments