File size: 18,154 Bytes
5492f08 20a3b47 909eff0 e1d36ec 33a2564 305d870 5492f08 ed64977 1f75665 7fb4ed3 e1d36ec f82c2c9 16ebf14 a9b5cfe 909eff0 1f75665 909eff0 1f75665 909eff0 16ebf14 909eff0 1f75665 e1d36ec 1f75665 e1d36ec 909eff0 1f75665 909eff0 e1d36ec 909eff0 16ebf14 e1d36ec 909eff0 1f75665 e1d36ec 1f75665 e1d36ec 1f75665 e1d36ec 909eff0 e1d36ec 1f75665 e1d36ec 16ebf14 e1d36ec d138ec4 e1d36ec 1f75665 e1d36ec c96e847 1f75665 909eff0 e1d36ec 909eff0 16ebf14 909eff0 1f75665 e1d36ec 1f75665 e1d36ec 1f75665 909eff0 c96e847 1f75665 909eff0 e1d36ec 16ebf14 909eff0 1f75665 e1d36ec 1f75665 e1d36ec 1f75665 909eff0 e1d36ec 909eff0 cc82d6a 909eff0 e1d36ec 909eff0 e1d36ec 909eff0 16ebf14 909eff0 1f75665 e1d36ec 8d5313c e1d36ec 90c8756 8d5313c 90c8756 8d5313c 90c8756 d91811c 90c8756 e1d36ec 8d5313c e1d36ec d91811c e1d36ec 1f75665 909eff0 d138ec4 e1d36ec a2578f6 e1d36ec fec291d e1d36ec 909eff0 cc82d6a e1d36ec 1f75665 d91811c 909eff0 e1d36ec 1f75665 e1d36ec 909eff0 e1d36ec 1f75665 e1d36ec 2f62045 c96e847 ed64977 c96e847 ed64977 c96e847 1f75665 ed64977 1f75665 587f9e6 909eff0 1f75665 e1d36ec 1f75665 758e870 1f75665 758e870 1f75665 e1d36ec 909eff0 90c8756 909eff0 90c8756 909eff0 fec291d cc82d6a 909eff0 cc82d6a fec291d cc82d6a fec291d cc82d6a 909eff0 1f75665 909eff0 e1d36ec 1f75665 909eff0 e1d36ec 909eff0 e1d36ec 1f75665 cc82d6a fec291d 909eff0 fec291d cc82d6a 909eff0 1f75665 e1d36ec 909eff0 5492f08 909eff0 e1d36ec 909eff0 1c5cb1e 5492f08 909eff0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | from flask import Flask, request, jsonify
import requests
import random
import string
import time
import json
app = Flask(__name__)
# Global variables to store workspace and bot IDs
GLOBAL_WORKSPACE_ID = None
GLOBAL_BOT_ID = None
# Authorization value used in requests (should be updated with a valid Authorization),
TOKEN = "Bearer bp_pat_vTuxol25N0ymBpYaWqtWpFfGPKt260IfT784"
# -------------------------------------------------------------------
# Helper functions for random bot/workspace names
# -------------------------------------------------------------------
def generate_random_name(length=5):
"""Generate a random name for workspace or bot"""
return ''.join(random.choices(string.ascii_letters, k=length))
# -------------------------------------------------------------------
# Functions to create/delete workspaces and bots
# -------------------------------------------------------------------
def create_workspace():
"""Create a new workspace and return its ID"""
ws_url = "https://api.botpress.cloud/v1/admin/workspaces"
headers = {
"User-Agent": "Mozilla/5.0",
"Authorization": TOKEN
}
payload = {"name": generate_random_name()}
try:
response = requests.post(ws_url, headers=headers, json=payload)
if response.status_code == 200:
response_json = response.json()
workspace_id = response_json.get('id')
print(f"Successfully created workspace: {workspace_id}")
return workspace_id
else:
print(f"Workspace creation failed with: {response.status_code}, {response.text}")
return None
except Exception as e:
print(f"Error creating workspace: {str(e)}")
return None
def create_bot(workspace_id):
"""Create a new bot in the specified workspace and return its ID"""
if not workspace_id:
print("Cannot create bot: No workspace ID provided")
return None
bot_url = "https://api.botpress.cloud/v1/admin/bots"
headers = {
"User-Agent": "Mozilla/5.0",
"x-workspace-id": workspace_id,
"Authorization": TOKEN,
"Content-Type": "application/json"
}
payload = {"name": generate_random_name()}
try:
response = requests.post(bot_url, headers=headers, json=payload)
if response.status_code == 200:
response_json = response.json()
bot_id = response_json.get("bot", {}).get("id")
if not bot_id:
print("Bot ID not found in the response.")
return None
print(f"Successfully created bot: {bot_id} in workspace: {workspace_id}")
# Install integration for the new bot
integration_success = install_bot_integration(bot_id, workspace_id)
if integration_success:
print(f"Successfully installed integration for bot {bot_id}")
return bot_id
else:
print(f"Failed to install integration for bot {bot_id}")
return bot_id # Still return the bot ID even if integration fails
else:
print(f"Bot creation failed with: {response.status_code}, {response.text}")
return None
except Exception as e:
print(f"Error creating bot: {str(e)}")
return None
def install_bot_integration(bot_id, workspace_id):
"""Install required integration for the bot to function properly"""
if not bot_id or not workspace_id:
print("Cannot install integration: Missing bot ID or workspace ID")
return False
url = f"https://api.botpress.cloud/v1/admin/bots/{bot_id}"
headers = {
"User-Agent": "Mozilla/5.0",
"Authorization": TOKEN,
"Content-Type": "application/json",
"x-bot-id": bot_id,
"x-workspace-id": workspace_id
}
# Integration payload
payload = {
"integrations": {
"intver_01JZTWCQ87B3HS0GVF9JNNYR1W": {
"enabled": True
}
}
}
try:
response = requests.put(url, headers=headers, json=payload)
if response.status_code == 200:
print(f"Successfully installed integration for bot {bot_id}")
return True
else:
print(f"Failed to install integration: {response.status_code}, {response.text}")
return False
except Exception as e:
print(f"Error installing integration: {str(e)}")
return False
def try_delete_bot(bot_id, workspace_id):
"""Attempt to delete a bot from the specified workspace but continue if it fails"""
if not bot_id or not workspace_id:
print("Cannot delete bot: Missing bot ID or workspace ID")
return False
url = f"https://api.botpress.cloud/v1/admin/bots/{bot_id}"
headers = {
"User-Agent": "Mozilla/5.0",
"x-workspace-id": workspace_id,
"Authorization": TOKEN
}
try:
response = requests.delete(url, headers=headers)
if response.status_code in [200, 204]:
print(f"Successfully deleted bot: {bot_id}")
return True
else:
print(f"Failed to delete bot: {response.status_code}, {response.text}")
return False
except Exception as e:
print(f"Error deleting bot: {str(e)}")
return False
def try_delete_workspace(workspace_id):
"""Attempt to delete a workspace but continue if it fails"""
if not workspace_id:
print("Cannot delete workspace: No workspace ID provided")
return False
url = f"https://api.botpress.cloud/v1/admin/workspaces/{workspace_id}"
headers = {
"User-Agent": "Mozilla/5.0",
"Authorization": TOKEN
}
try:
response = requests.delete(url, headers=headers)
if response.status_code in [200, 204]:
print(f"Successfully deleted workspace: {workspace_id}")
return True
else:
print(f"Failed to delete workspace: {response.status_code}, {response.text}")
return False
except Exception as e:
print(f"Error deleting workspace: {str(e)}")
return False
# -------------------------------------------------------------------
# Main function that calls the Botpress API endpoint
# -------------------------------------------------------------------
def chat_with_assistant(user_input, chat_history, bot_id, workspace_id, temperature=0.9, top_p=0.95, max_tokens=None):
"""
Sends the user input and chat history to the Botpress API endpoint,
returns the assistant's response and (possibly updated) bot/workspace IDs.
"""
# Prepare the headers
headers = {
"User-Agent": "Mozilla/5.0",
"x-bot-id": bot_id,
"Content-Type": "application/json",
"Authorization": TOKEN
}
# Process chat history into the format expected by the API
messages = []
system_prompt = ""
for msg in chat_history:
if msg["role"] == "system":
system_prompt = msg["content"]
elif msg["role"] in ["user", "assistant"]:
# Pass multipart messages directly without modifying their structure
if "type" in msg and msg["type"] == "multipart" and "content" in msg:
messages.append(msg) # Keep the original multipart structure
# Handle regular text messages
else:
messages.append({
"role": msg["role"],
"content": msg["content"]
})
# Add the latest user input if not already in chat history
if user_input and isinstance(user_input, str) and (not messages or messages[-1]["role"] != "user" or messages[-1]["content"] != user_input):
messages.append({
"role": "user",
"content": user_input
})
# Prepare the payload for the API
payload = {
"type": "openai:generateContent",
"input": {
"model": {
"id": "o1-2024-12-17"
},
"systemPrompt": system_prompt,
"messages": messages,
"temperature": temperature,
"debug": False,
}
}
# Add maxTokens to the payload if provided
if max_tokens is not None:
payload["input"]["maxTokens"] = max_tokens
botpress_url = "https://api.botpress.cloud/v1/chat/actions"
max_retries = 3
timeout = 120 # Increased timeout for long messages
# For debugging
print("Payload being sent to Botpress:")
print(json.dumps(payload, indent=2))
# Attempt to send the request
for attempt in range(max_retries):
try:
print(f"Attempt {attempt+1}: Sending request to Botpress API with bot_id={bot_id}, workspace_id={workspace_id}")
response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
# If successful (200)
if response.status_code == 200:
data = response.json()
assistant_content = data.get('output', {}).get('choices', [{}])[0].get('content', '')
print(f"Successfully received response from Botpress API")
return assistant_content, bot_id, workspace_id
# Check for authentication or permission errors (401, 403)
elif response.status_code in [401, 403]:
error_message = "Authentication error"
try:
error_data = response.json()
error_message = error_data.get('message', 'Authentication error')
except:
pass
print(f"Authentication error detected: {error_message}")
# We need to create new resources immediately
print("Creating new workspace and bot...")
new_workspace_id = create_workspace()
if not new_workspace_id:
print("Failed to create a new workspace")
if attempt < max_retries - 1:
time.sleep(3)
continue
else:
return "Unable to create new resources. Please try again later.", bot_id, workspace_id
new_bot_id = create_bot(new_workspace_id)
if not new_bot_id:
print("Failed to create a new bot")
if attempt < max_retries - 1:
time.sleep(3)
continue
else:
return "Unable to create new bot. Please try again later.", new_workspace_id, workspace_id
print(f"Created new workspace: {new_workspace_id} and bot: {new_bot_id}")
# Try again with new IDs
headers["x-bot-id"] = new_bot_id
try:
print(f"Retrying with new bot_id={new_bot_id}")
retry_response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
if retry_response.status_code == 200:
data = retry_response.json()
assistant_content = data.get('output', {}).get('choices', [{}])[0].get('content', '')
print(f"Successfully received response with new IDs")
# Try to clean up old resources in the background, but don't wait for result
if bot_id and workspace_id:
print(f"Attempting to clean up old resources in the background")
try_delete_bot(bot_id, workspace_id)
try_delete_workspace(workspace_id)
return assistant_content, new_bot_id, new_workspace_id
else:
print(f"Failed with new IDs: {retry_response.status_code}")
if attempt < max_retries - 1:
time.sleep(2)
continue
else:
return f"Unable to get a response even with new credentials.", new_bot_id, new_workspace_id
except Exception as e:
print(f"Error with new IDs: {str(e)}")
if attempt < max_retries - 1:
time.sleep(2)
continue
else:
return f"Error with new credentials: {str(e)}", new_bot_id, new_workspace_id
# Handle network errors or timeouts (just retry)
elif response.status_code in [404, 408, 502, 503, 504]:
print(f"Received error {response.status_code}. Retrying...")
time.sleep(3) # Wait before retrying
continue
# Any other error status code
else:
print(f"Received unexpected error: {response.status_code}, {response.text}")
if attempt < max_retries - 1:
time.sleep(2)
continue
else:
return f"Unable to get a response from the assistant (Error {response.status_code}).", bot_id, workspace_id
except requests.exceptions.Timeout:
print(f"Request timed out. Retrying...")
if attempt < max_retries - 1:
time.sleep(2)
continue
else:
return "The assistant is taking too long to respond. Please try again with a shorter message.", bot_id, workspace_id
except Exception as e:
print(f"Error during request: {str(e)}")
if attempt < max_retries - 1:
time.sleep(2)
continue
else:
return f"Unable to get a response from the assistant: {str(e)}", bot_id, workspace_id
# Should not reach here due to the handling in the loop
return "Unable to get a response from the assistant.", bot_id, workspace_id
# -------------------------------------------------------------------
# Flask Endpoint
# -------------------------------------------------------------------
@app.route("/chat", methods=["POST"])
def chat_endpoint():
"""
Expects JSON with:
{
"user_input": "string", // Can be null if multipart message is in chat_history
"chat_history": [
{"role": "system", "content": "..."},
{"role": "user", "content": "..."},
// Or for images:
{"role": "user", "type": "multipart", "content": [
{"type": "image", "url": "https://example.com/image.jpg"},
{"type": "text", "text": "What's in this image?"}
]},
...
],
"temperature": 0.9, // Optional, defaults to 0.9
"top_p": 0.95, // Optional, defaults to 0.95
"max_tokens": 1000 // Optional, defaults to null (no limit)
}
Returns JSON with:
{
"assistant_response": "string"
}
"""
global GLOBAL_WORKSPACE_ID, GLOBAL_BOT_ID
# Parse JSON from request
data = request.get_json(force=True)
user_input = data.get("user_input", "")
chat_history = data.get("chat_history", [])
# Get temperature, top_p, and max_tokens from request, or use defaults
temperature = data.get("temperature", 0.9)
top_p = data.get("top_p", 0.95)
max_tokens = data.get("max_tokens", None)
# Validate temperature and top_p values
try:
temperature = float(temperature)
if not 0 <= temperature <= 2:
temperature = 0.9
print(f"Invalid temperature value. Using default: {temperature}")
except (ValueError, TypeError):
temperature = 0.9
print(f"Invalid temperature format. Using default: {temperature}")
try:
top_p = float(top_p)
if not 0 <= top_p <= 1:
top_p = 0.95
print(f"Invalid top_p value. Using default: {top_p}")
except (ValueError, TypeError):
top_p = 0.95
print(f"Invalid top_p format. Using default: {top_p}")
# Validate max_tokens if provided
if max_tokens is not None:
try:
max_tokens = int(max_tokens)
if max_tokens <= 0:
print("Invalid max_tokens value (must be positive). Not using max_tokens.")
max_tokens = None
except (ValueError, TypeError):
print("Invalid max_tokens format. Not using max_tokens.")
max_tokens = None
# If we don't yet have a workspace or bot, create them
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
print("No existing IDs found. Creating new workspace and bot...")
GLOBAL_WORKSPACE_ID = create_workspace()
if GLOBAL_WORKSPACE_ID:
GLOBAL_BOT_ID = create_bot(GLOBAL_WORKSPACE_ID)
# If creation failed
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
return jsonify({"assistant_response": "I'm currently unavailable. Please try again later."}), 500
# Call our function that interacts with Botpress API
print(f"Sending chat request with existing bot_id={GLOBAL_BOT_ID}, workspace_id={GLOBAL_WORKSPACE_ID}")
print(f"Using temperature={temperature}, top_p={top_p}, max_tokens={max_tokens}")
assistant_response, updated_bot_id, updated_workspace_id = chat_with_assistant(
user_input,
chat_history,
GLOBAL_BOT_ID,
GLOBAL_WORKSPACE_ID,
temperature,
top_p,
max_tokens
)
# Update global IDs if they changed
if updated_bot_id != GLOBAL_BOT_ID or updated_workspace_id != GLOBAL_WORKSPACE_ID:
print(f"Updating global IDs: bot_id={updated_bot_id}, workspace_id={updated_workspace_id}")
GLOBAL_BOT_ID = updated_bot_id
GLOBAL_WORKSPACE_ID = updated_workspace_id
return jsonify({"assistant_response": assistant_response})
# -------------------------------------------------------------------
# Run the Flask app
# -------------------------------------------------------------------
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=True) |