Hello OpenAI Community,
I’m currently working on developing a Voice-Cloned GPT-4 Personal Assistant that leverages OpenAI’s GPT-4 API for generating responses and PlayHT’s API for text-to-speech synthesis. Despite following the setup instructions meticulously, I’m encountering persistent issues when attempting to connect to OpenAI’s API. I’m seeking guidance and assistance to resolve these challenges.
Project Overview
- Objective: Create a voice-cloned personal assistant that can understand user inputs, generate textual responses using GPT-4, and convert these responses into speech using PlayHT’s text-to-speech API.
- Technologies Used:
- Programming Language: Python 3.11.0
- APIs:
- OpenAI GPT-4 API for text generation.
- PlayHT API v2 for speech synthesis.
- Libraries:
openai
aiohttp
pygame
python-dotenv
Setup and Configuration
- **Environment Variables:**I’ve securely stored my API keys in a
.env
file with the following structure:
env
OPENAI_API_KEY=sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
PLAYHT_API_KEY=playht-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
PLAYHT_USER_ID=your_playht_user_id_here
# Optional
OPENAI_ORG_ID=your_openai_org_id_here
- Note: All API keys have been revoked and regenerated to ensure security.
- **Python Script (
assistant.py
):**The script is designed to:
- Capture user input.
- Send prompts to OpenAI’s GPT-4 API and stream responses.
- Send the generated text to PlayHT for speech synthesis.
- Play the synthesized audio.
Issue Description
When running the assistant.py
script, I consistently receive the following error after providing any input:
vbnet
OpenAI API connection error: Error communicating with OpenAI
Sample Terminal Output:
vbnet
Welcome to the GPT-4 Voice Assistant!
Type 'exit' or 'quit' to end the session.
hello
Assistant: Generating response...
OpenAI API connection error: Error communicating with OpenAI
Troubleshooting Steps Taken
- Verified
.env
Configuration:
- Ensured correct placement and formatting.
- Confirmed that environment variables are loaded without exposing API keys.
- **Tested OpenAI API Connectivity Separately:**Created a
test_openai.py
script to list available models, which successfully outputs the list, indicating that the API key is valid and connectivity is functional.
python
import openai
import os
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.organization = os.getenv("OPENAI_ORG_ID") # Optional
try:
response = openai.Model.list()
print("OpenAI API Key is valid. Available models:")
for model in response['data']:
print(model['id'])
except openai.error.AuthenticationError:
print("Authentication failed: Check your OpenAI API Key and Organization ID.")
except openai.error.RateLimitError:
print("Rate limit exceeded: You've hit OpenAI's rate limits.")
except openai.error.OpenAIError as e:
print(f"OpenAI API error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
- **Tested PlayHT API Separately:**Executed a cURL command to synthesize speech, which successfully created an
output.mp3
file with the synthesized speech. - Updated and Recreated
assistant.py
:
- Removed all debugging print statements to ensure API keys remain secure.
- Ensured that asynchronous operations are correctly handled.
- Verified that API endpoints and headers are correctly set.
- Verified Network Connectivity:
- Confirmed that my internet connection is stable.
- Ensured no firewall or proxy settings are blocking outbound requests to OpenAI’s servers.
- Reinstalled and Updated Python Packages:
- Upgraded
openai
,aiohttp
,pygame
, andpython-dotenv
to the latest versions.
- Recreated Virtual Environment:
- Deleted the existing
venv
folder. - Created a new virtual environment and installed dependencies afresh.
Relevant Log Snippets
Attached below are sanitized excerpts from assistant.log
capturing the error:
yaml
2024-04-27 12:34:56,789 - INFO - Initiating synthesis request with data: {'voice': 's3://voice-cloning-zero-shot/45dc2e61-a143-4b28-8458-ef7e92179eaa/original/manifest.json', 'voiceEngine': 'PlayHT2.0', 'title': 'Assistant Response'}
2024-04-27 12:34:58,123 - INFO - PlayHT Synthesis Response: {'status': 'CREATED', 'transcriptionId': '-OEyP_ojgPIapgHpD0An', 'contentLength': 1, 'wordCount': 7}
2024-04-27 12:34:58,124 - INFO - Transcription ID received: -OEyP_ojgPIapgHpD0An
2024-04-27 12:34:58,125 - INFO - Polling transcription ID: -OEyP_ojgPIapgHpD0An | Attempt 1
2024-04-27 12:35:13,234 - ERROR - PlayHT API error: 404 - {"status":"FAILED","message":"Transcription ID not found."}
2024-04-27 12:35:13,235 - ERROR - PlayHT Client error: 404 Client Error: Not Found for url: https://api.play.ht/api/v2/stream
2024-04-27 12:35:13,236 - ERROR - Speech synthesis failed: Failed to retrieve audio URL after polling.
Specific Questions
- Why am I receiving the
OpenAI API connection error: Error communicating with OpenAI
despite successfully testing API connectivity separately? - Are there any known issues or additional configurations required when integrating OpenAI’s GPT-4 API with PlayHT’s text-to-speech API in a Python environment?
- Can anyone provide insights or solutions to resolve this persistent connection error?
Additional Information
- Operating System: macOS
- Python Version: 3.11.0
- Virtual Environment: Activated (
venv
) - Security Measures: API keys are secured in the
.env
file; no keys are exposed in logs or terminal outputs.
Attachments
assistant.log
: [Attached, with all sensitive information redacted.]
Thank you in advance for your assistance!
Best regards,
Greg Jackson