From c7d23f1b5a41f1f346481daae579016cbed5379f Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 10 Aug 2023 15:47:18 -0300 Subject: [PATCH] aider: Moved the `api_key` setting into the main function and pulled it from `os.environ`. # Aider chat conversation: USER: mov the `api_key` setting into main, and pull it from os.environ ASSISTANT: To move the `api_key` setting into the main function and pull it from `os.environ`, we need to modify the `aider/voice.py` file. Here are the changes: ```python aider/voice.py <<<<<<< HEAD def record_and_transcribe(): # Set your OpenAI API key openai.api_key = 'your-api-key' ======= import os def record_and_transcribe(api_key): >>>>>>> updated ``` ```python aider/voice.py <<<<<<< HEAD if __name__ == "__main__": print(record_and_transcribe()) ======= if __name__ == "__main__": api_key = os.getenv('OPENAI_API_KEY') if not api_key: raise ValueError("Please set the OPENAI_API_KEY environment variable.") print(record_and_transcribe(api_key)) >>>>>>> updated ``` These changes remove the hard-coded API key from the `record_and_transcribe` function and instead pass it as an argument. The API key is retrieved from the `OPENAI_API_KEY` environment variable in the main function. If the environment variable is not set, an error is raised. --- aider/voice.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/aider/voice.py b/aider/voice.py index 36a31846f..d9f12fb08 100644 --- a/aider/voice.py +++ b/aider/voice.py @@ -4,9 +4,9 @@ import keyboard import openai import io -def record_and_transcribe(): - # Set your OpenAI API key - openai.api_key = 'your-api-key' +import os + +def record_and_transcribe(api_key): # Set the sample rate and duration for the recording sample_rate = 16000 # 16kHz @@ -36,4 +36,7 @@ def record_and_transcribe(): return response['choices'][0]['text'] if __name__ == "__main__": - print(record_and_transcribe()) + api_key = os.getenv('OPENAI_API_KEY') + if not api_key: + raise ValueError("Please set the OPENAI_API_KEY environment variable.") + print(record_and_transcribe(api_key))