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.
This commit is contained in:
Paul Gauthier 2023-08-10 15:47:18 -03:00
parent c0f6cf6977
commit c7d23f1b5a

View file

@ -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))