LyrGen2 / scripts /test_openai_connection.py
James Edmunds
added test envrionment to sapce app
4f8d0ad
from langchain_openai import OpenAIEmbeddings
from config.settings import Settings
import requests
def test_connection():
"""Test OpenAI API connection"""
print("Testing OpenAI API connection...")
# First test general internet connectivity
try:
response = requests.get("https://api.openai.com")
print(f"OpenAI API reachable: {response.status_code}")
except Exception as e:
print(f"Cannot reach OpenAI API: {e}")
# Test with embeddings
try:
embeddings = OpenAIEmbeddings(
openai_api_key=Settings.OPENAI_API_KEY
)
result = embeddings.embed_query("test")
print(f"Embeddings working! Vector size: {len(result)}")
except Exception as e:
print(f"Embeddings error: {e}")
if __name__ == "__main__":
test_connection()