Update app.py
Browse files
app.py
CHANGED
|
@@ -178,7 +178,7 @@ def delete_workspace(workspace_id):
|
|
| 178 |
# -------------------------------------------------------------------
|
| 179 |
# Main function that calls the Botpress API endpoint
|
| 180 |
# -------------------------------------------------------------------
|
| 181 |
-
def chat_with_assistant(user_input, chat_history, bot_id, workspace_id, temperature=0.9, top_p=0.95):
|
| 182 |
"""
|
| 183 |
Sends the user input and chat history to the Botpress API endpoint,
|
| 184 |
returns the assistant's response and (possibly updated) bot/workspace IDs.
|
|
@@ -227,6 +227,10 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id, temperat
|
|
| 227 |
}
|
| 228 |
}
|
| 229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
botpress_url = "https://api.botpress.cloud/v1/chat/actions"
|
| 231 |
max_retries = 3
|
| 232 |
timeout = 120 # Increased timeout for long messages
|
|
@@ -309,6 +313,10 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id, temperat
|
|
| 309 |
else:
|
| 310 |
print(f"Failed to delete bot {bot_id}")
|
| 311 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
print(f"Now deleting workspace {workspace_id}...")
|
| 313 |
ws_delete_success = delete_workspace(workspace_id)
|
| 314 |
if ws_delete_success:
|
|
@@ -366,7 +374,8 @@ def chat_endpoint():
|
|
| 366 |
...
|
| 367 |
],
|
| 368 |
"temperature": 0.9, // Optional, defaults to 0.9
|
| 369 |
-
"top_p": 0.95
|
|
|
|
| 370 |
}
|
| 371 |
Returns JSON with:
|
| 372 |
{
|
|
@@ -380,9 +389,10 @@ def chat_endpoint():
|
|
| 380 |
user_input = data.get("user_input", "")
|
| 381 |
chat_history = data.get("chat_history", [])
|
| 382 |
|
| 383 |
-
# Get temperature and
|
| 384 |
temperature = data.get("temperature", 0.9)
|
| 385 |
top_p = data.get("top_p", 0.95)
|
|
|
|
| 386 |
|
| 387 |
# Validate temperature and top_p values
|
| 388 |
try:
|
|
@@ -403,6 +413,17 @@ def chat_endpoint():
|
|
| 403 |
top_p = 0.95
|
| 404 |
print(f"Invalid top_p format. Using default: {top_p}")
|
| 405 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
# If we don't yet have a workspace or bot, create them
|
| 407 |
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
|
| 408 |
print("No existing IDs found. Creating new workspace and bot...")
|
|
@@ -416,7 +437,7 @@ def chat_endpoint():
|
|
| 416 |
|
| 417 |
# Call our function that interacts with Botpress API
|
| 418 |
print(f"Sending chat request with existing bot_id={GLOBAL_BOT_ID}, workspace_id={GLOBAL_WORKSPACE_ID}")
|
| 419 |
-
print(f"Using temperature={temperature}, top_p={top_p}")
|
| 420 |
|
| 421 |
assistant_response, updated_bot_id, updated_workspace_id = chat_with_assistant(
|
| 422 |
user_input,
|
|
@@ -424,7 +445,8 @@ def chat_endpoint():
|
|
| 424 |
GLOBAL_BOT_ID,
|
| 425 |
GLOBAL_WORKSPACE_ID,
|
| 426 |
temperature,
|
| 427 |
-
top_p
|
|
|
|
| 428 |
)
|
| 429 |
|
| 430 |
# Update global IDs if they changed
|
|
|
|
| 178 |
# -------------------------------------------------------------------
|
| 179 |
# Main function that calls the Botpress API endpoint
|
| 180 |
# -------------------------------------------------------------------
|
| 181 |
+
def chat_with_assistant(user_input, chat_history, bot_id, workspace_id, temperature=0.9, top_p=0.95, max_tokens=None):
|
| 182 |
"""
|
| 183 |
Sends the user input and chat history to the Botpress API endpoint,
|
| 184 |
returns the assistant's response and (possibly updated) bot/workspace IDs.
|
|
|
|
| 227 |
}
|
| 228 |
}
|
| 229 |
|
| 230 |
+
# Add maxTokens to the payload if provided
|
| 231 |
+
if max_tokens is not None:
|
| 232 |
+
payload["input"]["maxTokens"] = max_tokens
|
| 233 |
+
|
| 234 |
botpress_url = "https://api.botpress.cloud/v1/chat/actions"
|
| 235 |
max_retries = 3
|
| 236 |
timeout = 120 # Increased timeout for long messages
|
|
|
|
| 313 |
else:
|
| 314 |
print(f"Failed to delete bot {bot_id}")
|
| 315 |
|
| 316 |
+
# Wait 3 seconds before deleting the workspace
|
| 317 |
+
print("Waiting 3 seconds before deleting workspace...")
|
| 318 |
+
time.sleep(3)
|
| 319 |
+
|
| 320 |
print(f"Now deleting workspace {workspace_id}...")
|
| 321 |
ws_delete_success = delete_workspace(workspace_id)
|
| 322 |
if ws_delete_success:
|
|
|
|
| 374 |
...
|
| 375 |
],
|
| 376 |
"temperature": 0.9, // Optional, defaults to 0.9
|
| 377 |
+
"top_p": 0.95, // Optional, defaults to 0.95
|
| 378 |
+
"max_tokens": 1000 // Optional, defaults to null (no limit)
|
| 379 |
}
|
| 380 |
Returns JSON with:
|
| 381 |
{
|
|
|
|
| 389 |
user_input = data.get("user_input", "")
|
| 390 |
chat_history = data.get("chat_history", [])
|
| 391 |
|
| 392 |
+
# Get temperature, top_p, and max_tokens from request, or use defaults
|
| 393 |
temperature = data.get("temperature", 0.9)
|
| 394 |
top_p = data.get("top_p", 0.95)
|
| 395 |
+
max_tokens = data.get("max_tokens", None)
|
| 396 |
|
| 397 |
# Validate temperature and top_p values
|
| 398 |
try:
|
|
|
|
| 413 |
top_p = 0.95
|
| 414 |
print(f"Invalid top_p format. Using default: {top_p}")
|
| 415 |
|
| 416 |
+
# Validate max_tokens if provided
|
| 417 |
+
if max_tokens is not None:
|
| 418 |
+
try:
|
| 419 |
+
max_tokens = int(max_tokens)
|
| 420 |
+
if max_tokens <= 0:
|
| 421 |
+
print("Invalid max_tokens value (must be positive). Not using max_tokens.")
|
| 422 |
+
max_tokens = None
|
| 423 |
+
except (ValueError, TypeError):
|
| 424 |
+
print("Invalid max_tokens format. Not using max_tokens.")
|
| 425 |
+
max_tokens = None
|
| 426 |
+
|
| 427 |
# If we don't yet have a workspace or bot, create them
|
| 428 |
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
|
| 429 |
print("No existing IDs found. Creating new workspace and bot...")
|
|
|
|
| 437 |
|
| 438 |
# Call our function that interacts with Botpress API
|
| 439 |
print(f"Sending chat request with existing bot_id={GLOBAL_BOT_ID}, workspace_id={GLOBAL_WORKSPACE_ID}")
|
| 440 |
+
print(f"Using temperature={temperature}, top_p={top_p}, max_tokens={max_tokens}")
|
| 441 |
|
| 442 |
assistant_response, updated_bot_id, updated_workspace_id = chat_with_assistant(
|
| 443 |
user_input,
|
|
|
|
| 445 |
GLOBAL_BOT_ID,
|
| 446 |
GLOBAL_WORKSPACE_ID,
|
| 447 |
temperature,
|
| 448 |
+
top_p,
|
| 449 |
+
max_tokens
|
| 450 |
)
|
| 451 |
|
| 452 |
# Update global IDs if they changed
|