Spaces:
Running
Running
Commit
Β·
3ee7b82
1
Parent(s):
008dadb
feat: add OpenAI connection debug script and update requirements for improved package management
Browse files- backend/app/api/v1/endpoints/chat.py +3 -3
- backend/requirements.txt +6 -6
- test_openai.py +71 -0
backend/app/api/v1/endpoints/chat.py
CHANGED
|
@@ -3,7 +3,7 @@ import json
|
|
| 3 |
|
| 4 |
from fastapi import APIRouter, HTTPException
|
| 5 |
from fastapi.responses import StreamingResponse
|
| 6 |
-
|
| 7 |
from typing import List
|
| 8 |
|
| 9 |
from ....schemas.chat import Message, ChatRequest
|
|
@@ -11,8 +11,8 @@ from ....core.config import settings
|
|
| 11 |
|
| 12 |
router = APIRouter(prefix="/chat", tags=["chat"])
|
| 13 |
|
| 14 |
-
# Initialize OpenAI client
|
| 15 |
-
client = OpenAI(api_key=settings.OPENAI_API_KEY)
|
| 16 |
|
| 17 |
|
| 18 |
async def stream_text(messages: List[Message]):
|
|
|
|
| 3 |
|
| 4 |
from fastapi import APIRouter, HTTPException
|
| 5 |
from fastapi.responses import StreamingResponse
|
| 6 |
+
import openai
|
| 7 |
from typing import List
|
| 8 |
|
| 9 |
from ....schemas.chat import Message, ChatRequest
|
|
|
|
| 11 |
|
| 12 |
router = APIRouter(prefix="/chat", tags=["chat"])
|
| 13 |
|
| 14 |
+
# Initialize OpenAI client using the same settings as other working endpoints
|
| 15 |
+
client = openai.OpenAI(api_key=settings.OPENAI_API_KEY)
|
| 16 |
|
| 17 |
|
| 18 |
async def stream_text(messages: List[Message]):
|
backend/requirements.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
fastapi
|
| 2 |
-
uvicorn
|
| 3 |
-
pydantic
|
| 4 |
-
|
| 5 |
-
openai
|
| 6 |
-
python-dotenv
|
|
|
|
| 1 |
+
fastapi[standard]
|
| 2 |
+
uvicorn>=0.15.0
|
| 3 |
+
pydantic>=1.8.0
|
| 4 |
+
python-dotenv>=0.19.0
|
| 5 |
+
openai>=1.0.0
|
| 6 |
+
python-dotenv>=0.19.0
|
test_openai.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
print("π OpenAI Connection Debug Script")
|
| 8 |
+
print("=" * 40)
|
| 9 |
+
|
| 10 |
+
# Load environment
|
| 11 |
+
load_dotenv()
|
| 12 |
+
api_key = os.getenv('OPENAI_API_KEY')
|
| 13 |
+
|
| 14 |
+
print(f"1. API Key configured: {'β
Yes' if api_key else 'β No'}")
|
| 15 |
+
if api_key:
|
| 16 |
+
print(f" Preview: {api_key[:10]}...")
|
| 17 |
+
|
| 18 |
+
# Test basic HTTP connectivity
|
| 19 |
+
print("\n2. Testing basic HTTP connectivity...")
|
| 20 |
+
try:
|
| 21 |
+
import requests
|
| 22 |
+
response = requests.get('https://api.openai.com/v1/models',
|
| 23 |
+
headers={'Authorization': f'Bearer {api_key}'},
|
| 24 |
+
timeout=30)
|
| 25 |
+
print(f" Status: {response.status_code}")
|
| 26 |
+
if response.status_code == 200:
|
| 27 |
+
print(" β
Direct HTTP request works!")
|
| 28 |
+
else:
|
| 29 |
+
print(f" β HTTP error: {response.text[:200]}")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f" β Network error: {str(e)}")
|
| 32 |
+
|
| 33 |
+
# Test OpenAI library - simple client
|
| 34 |
+
print("\n3. Testing OpenAI library (simple config)...")
|
| 35 |
+
try:
|
| 36 |
+
from openai import OpenAI
|
| 37 |
+
client = OpenAI(api_key=api_key)
|
| 38 |
+
|
| 39 |
+
response = client.chat.completions.create(
|
| 40 |
+
model="gpt-3.5-turbo",
|
| 41 |
+
messages=[{"role": "user", "content": "Say 'test'"}],
|
| 42 |
+
max_tokens=5
|
| 43 |
+
)
|
| 44 |
+
print(" β
OpenAI library works!")
|
| 45 |
+
print(f" Response: {response.choices[0].message.content}")
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f" β OpenAI library error: {str(e)}")
|
| 48 |
+
print(f" Error type: {type(e).__name__}")
|
| 49 |
+
|
| 50 |
+
# Test OpenAI library - with timeout config
|
| 51 |
+
print("\n4. Testing OpenAI library (with timeout)...")
|
| 52 |
+
try:
|
| 53 |
+
from openai import OpenAI
|
| 54 |
+
client = OpenAI(
|
| 55 |
+
api_key=api_key,
|
| 56 |
+
timeout=60.0,
|
| 57 |
+
max_retries=2
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
response = client.chat.completions.create(
|
| 61 |
+
model="gpt-3.5-turbo",
|
| 62 |
+
messages=[{"role": "user", "content": "Say 'test'"}],
|
| 63 |
+
max_tokens=5
|
| 64 |
+
)
|
| 65 |
+
print(" β
OpenAI library with timeout works!")
|
| 66 |
+
print(f" Response: {response.choices[0].message.content}")
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f" β OpenAI library with timeout error: {str(e)}")
|
| 69 |
+
print(f" Error type: {type(e).__name__}")
|
| 70 |
+
|
| 71 |
+
print("\nπ Debug complete!")
|