Spaces:
Sleeping
Sleeping
Commit
·
d47dfbe
1
Parent(s):
03cac6f
Add debug logging to troubleshoot connection error
Browse files
app.py
CHANGED
@@ -1,26 +1,74 @@
|
|
1 |
import os
|
|
|
|
|
2 |
import chainlit as cl
|
3 |
from openai import OpenAI
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
# Initialize the OpenAI client
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
@cl.on_message
|
9 |
async def main(message: cl.Message):
|
10 |
-
|
11 |
-
user_message = message.content
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
{"role": "user", "content": user_message}
|
19 |
-
],
|
20 |
-
temperature=0.7,
|
21 |
-
)
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import sys
|
3 |
+
import traceback
|
4 |
import chainlit as cl
|
5 |
from openai import OpenAI
|
6 |
|
7 |
+
# Set up basic logging to stdout
|
8 |
+
def log_message(message):
|
9 |
+
print(f"DEBUG: {message}", file=sys.stdout)
|
10 |
+
sys.stdout.flush() # Ensure output is flushed immediately
|
11 |
+
|
12 |
+
# Log startup information
|
13 |
+
log_message("Application starting up")
|
14 |
+
log_message(f"Python version: {sys.version}")
|
15 |
+
|
16 |
+
# Check for API key
|
17 |
+
api_key = os.environ.get("OPENAI_API_KEY")
|
18 |
+
if api_key:
|
19 |
+
log_message("API key found (first few chars): " + api_key[:4] + "...")
|
20 |
+
else:
|
21 |
+
log_message("WARNING: No API key found in environment")
|
22 |
+
|
23 |
# Initialize the OpenAI client
|
24 |
+
try:
|
25 |
+
client = OpenAI(api_key=api_key)
|
26 |
+
log_message("OpenAI client initialized successfully")
|
27 |
+
except Exception as e:
|
28 |
+
log_message(f"Error initializing OpenAI client: {str(e)}")
|
29 |
+
traceback.print_exc()
|
30 |
+
|
31 |
+
@cl.on_chat_start
|
32 |
+
async def start():
|
33 |
+
log_message("New chat session started")
|
34 |
+
if not api_key:
|
35 |
+
await cl.Message(content="⚠️ API key not found. Please add OPENAI_API_KEY secret.").send()
|
36 |
|
37 |
@cl.on_message
|
38 |
async def main(message: cl.Message):
|
39 |
+
log_message(f"Received message: {message.content[:50]}...")
|
|
|
40 |
|
41 |
+
# Simple test response without using OpenAI
|
42 |
+
if message.content.lower() == "test":
|
43 |
+
log_message("Sending test response")
|
44 |
+
await cl.Message(content="Test successful! This is a direct response without using the OpenAI API.").send()
|
45 |
+
return
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
try:
|
48 |
+
log_message("Attempting to call OpenAI API")
|
49 |
+
|
50 |
+
# Call OpenAI API
|
51 |
+
response = client.chat.completions.create(
|
52 |
+
model="gpt-3.5-turbo",
|
53 |
+
messages=[
|
54 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
55 |
+
{"role": "user", "content": message.content}
|
56 |
+
],
|
57 |
+
temperature=0.7,
|
58 |
+
)
|
59 |
+
|
60 |
+
log_message("OpenAI API call successful")
|
61 |
+
|
62 |
+
# Send response back to user
|
63 |
+
await cl.Message(
|
64 |
+
content=response.choices[0].message.content,
|
65 |
+
).send()
|
66 |
+
|
67 |
+
except Exception as e:
|
68 |
+
log_message(f"Error during API call: {str(e)}")
|
69 |
+
traceback.print_exc()
|
70 |
+
|
71 |
+
# Send error message to user
|
72 |
+
await cl.Message(
|
73 |
+
content=f"Sorry, I encountered an error: {str(e)}",
|
74 |
+
).send()
|