Spaces:
Sleeping
Sleeping
minor updates for hf multiple max_tokens fix
Browse files- .chainlit/config.toml +6 -1
- .gitignore +6 -2
- app.py +58 -40
.chainlit/config.toml
CHANGED
@@ -7,7 +7,7 @@ enable_telemetry = true
|
|
7 |
user_env = []
|
8 |
|
9 |
# Duration (in seconds) during which the session is saved when the connection is lost
|
10 |
-
session_timeout =
|
11 |
|
12 |
# Duration (in seconds) of the user session expiry
|
13 |
user_session_timeout = 1296000 # 15 days
|
@@ -18,6 +18,11 @@ cache = false
|
|
18 |
# Authorized origins
|
19 |
allow_origins = ["*"]
|
20 |
|
|
|
|
|
|
|
|
|
|
|
21 |
[features]
|
22 |
# Process and display HTML in messages. This can be a security risk (see https://stackoverflow.com/questions/19603097/why-is-it-dangerous-to-render-user-generated-html-or-javascript)
|
23 |
unsafe_allow_html = false
|
|
|
7 |
user_env = []
|
8 |
|
9 |
# Duration (in seconds) during which the session is saved when the connection is lost
|
10 |
+
session_timeout = 7200 # 2 hours
|
11 |
|
12 |
# Duration (in seconds) of the user session expiry
|
13 |
user_session_timeout = 1296000 # 15 days
|
|
|
18 |
# Authorized origins
|
19 |
allow_origins = ["*"]
|
20 |
|
21 |
+
# Enable WebSocket heartbeat to maintain stable connections
|
22 |
+
[websocket]
|
23 |
+
ping_interval = 25 # Send ping every 25 seconds
|
24 |
+
ping_timeout = 120 # Longer timeout for better stability
|
25 |
+
|
26 |
[features]
|
27 |
# Process and display HTML in messages. This can be a security risk (see https://stackoverflow.com/questions/19603097/why-is-it-dangerous-to-render-user-generated-html-or-javascript)
|
28 |
unsafe_allow_html = false
|
.gitignore
CHANGED
@@ -1,4 +1,8 @@
|
|
1 |
.env
|
2 |
.venv
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
1 |
.env
|
2 |
.venv
|
3 |
+
__pycache__
|
4 |
+
# Keep chainlit config
|
5 |
+
!.chainlit/
|
6 |
+
!.chainlit/config.toml
|
7 |
+
# Ignore translations
|
8 |
+
/app/.chainlit/translations/
|
app.py
CHANGED
@@ -2,6 +2,7 @@ import os
|
|
2 |
from dotenv import load_dotenv
|
3 |
import chainlit as cl
|
4 |
from openai import AsyncOpenAI
|
|
|
5 |
|
6 |
# Load environment variables from .env file
|
7 |
load_dotenv()
|
@@ -10,7 +11,7 @@ load_dotenv()
|
|
10 |
DEFAULT_SETTINGS = {
|
11 |
"model": "gpt-3.5-turbo",
|
12 |
"temperature": 0.7,
|
13 |
-
"max_tokens":
|
14 |
"top_p": 1,
|
15 |
"frequency_penalty": 0,
|
16 |
"presence_penalty": 0,
|
@@ -28,15 +29,8 @@ async def start():
|
|
28 |
if not api_key:
|
29 |
raise ValueError("OPENAI_API_KEY environment variable is not set")
|
30 |
|
31 |
-
# Initialize OpenAI client
|
32 |
client = AsyncOpenAI(api_key=api_key)
|
33 |
-
# Test the API key with a simple request
|
34 |
-
await client.chat.completions.create(
|
35 |
-
model="gpt-3.5-turbo",
|
36 |
-
messages=[{"role": "system", "content": "Test"}],
|
37 |
-
max_tokens=5
|
38 |
-
)
|
39 |
-
|
40 |
cl.user_session.set("client", client)
|
41 |
|
42 |
# Initialize message history with system prompt
|
@@ -55,36 +49,46 @@ async def start():
|
|
55 |
content=f"⚠️ Configuration Error: {str(e)}\nPlease make sure OPENAI_API_KEY is set in the environment variables."
|
56 |
).send()
|
57 |
except Exception as e:
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
@cl.on_message
|
64 |
async def main(user_message: cl.Message):
|
65 |
"""
|
66 |
-
Process user messages and generate AI responses
|
67 |
-
- Update message history with user input
|
68 |
-
- Call OpenAI API with current conversation context
|
69 |
-
- Stream the response back to the user
|
70 |
-
- Update message history with AI response
|
71 |
-
|
72 |
-
Args:
|
73 |
-
user_message: The message sent by the user
|
74 |
"""
|
75 |
-
# Retrieve session data
|
76 |
-
client = cl.user_session.get("client")
|
77 |
-
message_history = cl.user_session.get("message_history")
|
78 |
-
settings = cl.user_session.get("settings")
|
79 |
-
|
80 |
-
# Add user message to history
|
81 |
-
message_history.append({"role": "user", "content": user_message.content})
|
82 |
-
|
83 |
-
# Prepare response message with loading state
|
84 |
-
response_message = cl.Message(content="")
|
85 |
-
await response_message.send()
|
86 |
-
|
87 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
# Call OpenAI API to get response
|
89 |
stream = await client.chat.completions.create(
|
90 |
messages=message_history,
|
@@ -92,22 +96,36 @@ async def main(user_message: cl.Message):
|
|
92 |
**settings
|
93 |
)
|
94 |
|
95 |
-
# Stream the response
|
96 |
full_response = ""
|
|
|
|
|
|
|
|
|
97 |
async for chunk in stream:
|
98 |
if chunk.choices[0].delta.content:
|
99 |
-
|
100 |
-
|
101 |
|
102 |
-
# Update
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
# Add AI response to message history
|
107 |
message_history.append({"role": "assistant", "content": full_response})
|
108 |
cl.user_session.set("message_history", message_history)
|
109 |
|
110 |
except Exception as e:
|
111 |
-
|
112 |
-
response_message.content =
|
113 |
await response_message.update()
|
|
|
2 |
from dotenv import load_dotenv
|
3 |
import chainlit as cl
|
4 |
from openai import AsyncOpenAI
|
5 |
+
import time
|
6 |
|
7 |
# Load environment variables from .env file
|
8 |
load_dotenv()
|
|
|
11 |
DEFAULT_SETTINGS = {
|
12 |
"model": "gpt-3.5-turbo",
|
13 |
"temperature": 0.7,
|
14 |
+
"max_tokens": 1000,
|
15 |
"top_p": 1,
|
16 |
"frequency_penalty": 0,
|
17 |
"presence_penalty": 0,
|
|
|
29 |
if not api_key:
|
30 |
raise ValueError("OPENAI_API_KEY environment variable is not set")
|
31 |
|
32 |
+
# Initialize OpenAI client (without test request)
|
33 |
client = AsyncOpenAI(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
cl.user_session.set("client", client)
|
35 |
|
36 |
# Initialize message history with system prompt
|
|
|
49 |
content=f"⚠️ Configuration Error: {str(e)}\nPlease make sure OPENAI_API_KEY is set in the environment variables."
|
50 |
).send()
|
51 |
except Exception as e:
|
52 |
+
error_msg = f"⚠️ Error: {str(e)}"
|
53 |
+
if "session" in str(e).lower():
|
54 |
+
error_msg = "⚠️ Session error. Please refresh the page and try again."
|
55 |
+
await cl.Message(content=error_msg).send()
|
56 |
+
|
57 |
+
@cl.on_stop
|
58 |
+
async def on_stop():
|
59 |
+
"""Cleanup when the chat session ends"""
|
60 |
+
try:
|
61 |
+
cl.user_session.clear()
|
62 |
+
except Exception:
|
63 |
+
pass
|
64 |
|
65 |
+
async def handle_error(error: Exception) -> str:
|
66 |
+
"""Helper function to format error messages"""
|
67 |
+
if "session" in str(error).lower():
|
68 |
+
return "⚠️ Session error occurred. Please refresh the page and try again."
|
69 |
+
return f"⚠️ An error occurred: {str(error)}"
|
70 |
|
71 |
@cl.on_message
|
72 |
async def main(user_message: cl.Message):
|
73 |
"""
|
74 |
+
Process user messages and generate AI responses
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
try:
|
77 |
+
# Retrieve session data
|
78 |
+
client = cl.user_session.get("client")
|
79 |
+
message_history = cl.user_session.get("message_history")
|
80 |
+
settings = cl.user_session.get("settings")
|
81 |
+
|
82 |
+
if not client or not message_history or not settings:
|
83 |
+
raise ValueError("Session data not found. Please refresh the page.")
|
84 |
+
|
85 |
+
# Add user message to history
|
86 |
+
message_history.append({"role": "user", "content": user_message.content})
|
87 |
+
|
88 |
+
# Prepare response message with loading state
|
89 |
+
response_message = cl.Message(content="")
|
90 |
+
await response_message.send()
|
91 |
+
|
92 |
# Call OpenAI API to get response
|
93 |
stream = await client.chat.completions.create(
|
94 |
messages=message_history,
|
|
|
96 |
**settings
|
97 |
)
|
98 |
|
99 |
+
# Stream the response with buffering
|
100 |
full_response = ""
|
101 |
+
buffer = ""
|
102 |
+
update_interval = 0.1 # Update every 100ms
|
103 |
+
last_update_time = 0
|
104 |
+
|
105 |
async for chunk in stream:
|
106 |
if chunk.choices[0].delta.content:
|
107 |
+
buffer += chunk.choices[0].delta.content
|
108 |
+
current_time = time.time()
|
109 |
|
110 |
+
# Update UI when buffer reaches certain size or time has passed
|
111 |
+
if len(buffer) >= 50 or (current_time - last_update_time) >= update_interval:
|
112 |
+
full_response += buffer
|
113 |
+
response_message.content = full_response
|
114 |
+
await response_message.update()
|
115 |
+
buffer = "" # Clear buffer
|
116 |
+
last_update_time = current_time
|
117 |
+
|
118 |
+
# Send any remaining buffer content
|
119 |
+
if buffer:
|
120 |
+
full_response += buffer
|
121 |
+
response_message.content = full_response
|
122 |
+
await response_message.update()
|
123 |
|
124 |
# Add AI response to message history
|
125 |
message_history.append({"role": "assistant", "content": full_response})
|
126 |
cl.user_session.set("message_history", message_history)
|
127 |
|
128 |
except Exception as e:
|
129 |
+
error_message = await handle_error(e)
|
130 |
+
response_message.content = error_message
|
131 |
await response_message.update()
|