Assistance Needed: OpenAI API Connection Error in Voice-Cloned GPT-4 Personal Assistant Project

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.

:wrench: Project Overview :wrench:

  • 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

:hammer_and_wrench: Setup and Configuration :hammer_and_wrench:

  1. **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.
  1. **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.

:rotating_light: Issue Description :rotating_light:

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

:mag: Troubleshooting Steps Taken :mag:

  1. Verified .env Configuration:
  • Ensured correct placement and formatting.
  • Confirmed that environment variables are loaded without exposing API keys.
  1. **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}")
  1. **Tested PlayHT API Separately:**Executed a cURL command to synthesize speech, which successfully created an output.mp3 file with the synthesized speech.
  2. 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.
  1. Verified Network Connectivity:
  • Confirmed that my internet connection is stable.
  • Ensured no firewall or proxy settings are blocking outbound requests to OpenAI’s servers.
  1. Reinstalled and Updated Python Packages:
  • Upgraded openai, aiohttp, pygame, and python-dotenv to the latest versions.
  1. Recreated Virtual Environment:
  • Deleted the existing venv folder.
  • Created a new virtual environment and installed dependencies afresh.

:page_facing_up: Relevant Log Snippets :page_facing_up:

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.

:question: Specific Questions :question:

  1. Why am I receiving the OpenAI API connection error: Error communicating with OpenAI despite successfully testing API connectivity separately?
  2. 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?
  3. Can anyone provide insights or solutions to resolve this persistent connection error?

:memo: Additional Information :memo:

  • 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.

:open_file_folder: Attachments :open_file_folder:

  • assistant.log: [Attached, with all sensitive information redacted.]

Thank you in advance for your assistance!

Best regards,
Greg Jackson

The information you provided makes no sense.

You haven’t shown any of your actual code, and hardly any actual useful log information. You’re including mostly redundant information twice.

I would safely wager that this whole project, including your post is all AI-generated. I apologize for the coarseness but with this information the sky is the limit when it comes to issues.

You should be building these things as modules that connect together. Not as a holistic project.

Because you are most likely wrapping all your code in try catches, which is a common indicator of AI-generated code. Remove your try/catch wrappers and then maybe you can paste the stacktrace to ChatGPT and solve your issue.