Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,6 @@ GLOBAL_BOT_ID = None
|
|
| 12 |
|
| 13 |
# Cookie value used in requests (should be updated with a valid cookie)
|
| 14 |
AUTH_COOKIE = "pscd=try.botpress.com; _hjSessionUser_2931810=eyJpZCI6ImQ2MGMzYjhkLTlkMjQtNTA0OS1hMzlmLWEzNmI0NzA0NzUxNCIsImNyZWF0ZWQiOjE3MzU3MTg0MDcwNTAsImV4aXN0aW5nIjp0cnVlfQ==; hubspotutk=75739411a4d011b2164c4f3d944ecb94; intercom-device-id-bjzkw2xf=afd0a36b-b229-44e3-828e-60483c80c10c; _hjSessionUser_3339867=eyJpZCI6IjU4ODlmMTY4LWRkNGEtNTJhZS1hZTUzLWZlYWQwM2ZmMTVjNyIsImNyZWF0ZWQiOjE3MzU3MTg1ODM4MDgsImV4aXN0aW5nIjp0cnVlfQ==; __hstc=59821234.75739411a4d011b2164c4f3d944ecb94.1735718442141.1746194848836.1746538539919.88; mp_1195923e954ce61d822842b5832047cd_mixpanel=%7B%22distinct_id%22%3A%20%22d403ad7b-ea73-4d29-b977-5fd95afd585c%22%2C%22%24device_id%22%3A%20%22d403ad7b-ea73-4d29-b977-5fd95afd585c%22%2C%22%24initial_referrer%22%3A%20%22https%3A%2F%2Fapp.botpress.cloud%2F%22%2C%22%24initial_referring_domain%22%3A%20%22app.botpress.cloud%22%2C%22__mps%22%3A%20%7B%7D%2C%22__mpso%22%3A%20%7B%22%24initial_referrer%22%3A%20%22https%3A%2F%2Fapp.botpress.cloud%2F%22%2C%22%24initial_referring_domain%22%3A%20%22app.botpress.cloud%22%7D%2C%22__mpus%22%3A%20%7B%7D%2C%22__mpa%22%3A%20%7B%7D%2C%22__mpu%22%3A%20%7B%7D%2C%22__mpr%22%3A%20%5B%5D%2C%22__mpap%22%3A%20%5B%5D%2C%22%24user_id%22%3A%20%22d403ad7b-ea73-4d29-b977-5fd95afd585c%22%7D; intercom-session-bjzkw2xf=TTdnZGNWUC9xNXMreE80NXhRZFNZS0pyUEdsbkJRc2JMcXdGZmcveVRPYkxZTmVnVnhqMUhJTWlDcEpVcWljeDZVYVVSblN4YnV5S0xBdWxDd2swQjZiaUZTeWl5M1psRmtoUWJwUU9FSFE9LS1Bay9zNldJTmVhUFdwMFNReFRmcXB3PT0=--68abc5394d7aab99748f3e451637cab5d9152a4c; _ga=GA1.2.1726154447.1735718383; _gid=GA1.2.1619749406.1746821934; _gat_UA-226900660-1=1; _gat_UA-226900660-2=1; _ga_W6YT9YSNLH=GS2.2.s1746821948$o89$g0$t1746821948$j0$l0$h0; _ga_CYSS87Q508=GS2.2.s1746821949$o89$g0$t1746821949$j0$l0$h0; _hjSession_2931810=eyJpZCI6IjA1YTlkMzY3LWNiZWEtNGQ3OC04YzNiLTEzNTFjNjkxYzViZSIsImMiOjE3NDY4MjE5NTU2NjYsInMiOjAsInIiOjAsInNiIjowLCJzciI6MCwic2UiOjAsImZzIjowLCJzcCI6MH0=; _ga_PCC6TBWJY6=GS2.1.s1746821926$o120$g1$t1746821960$j0$l0$h0; _ga_HKHSWES9V9=GS2.1.s1746821929$o120$g1$t1746821960$j29$l0$h193244206; csrf_token_bd9ac21c34b9f0915e733c3e5305d737d0722c1168be7376b889426b5ec2a298=pIDxu4npODUGEpo7JHVQrKZ4GFaa3U+3BpgxaV5hcVw=; ory_kratos_session=MTc0NjgyMjAyOHxfUmdMaVlTQXVfSmxlT1lJSGpyU2FhbjVUTHg0R0ZsQWgtVm00M3pHcXZwVG9yNW1qRDJheUFGaFZvNmFEUVdBOThQR014RjJJbmhUMmhIV1I1ME5UVHZkTDNpMUMtQlRjZ1ZTbE55M19Pb2dHTF9vQlJoSGlBQnRRWUp0M1ZUdnVvcENLeVhOTllWNk1zMk11bFVPOWFrTzJMTTdxMmVteUozVVRDMWE5TVIxbDgzU3dUY2VQaDBRWDN4bDJUVm8yUkZQa19sb09GbzlFZHF2MDFQcVR6bVVWVVpDLXVoQ1lXMEh2LV9Sd2VNZXM1cjM4TGZPVTJqdW5xNTBETTBDYkppU0xNU2xicUk3Z2EyMnFkVmdyQT09fEYiv7pXcfXVnpIFi4JLGgDObQAchyJCoAwGDSkFkoX7; ajs_user_id=d403ad7b-ea73-4d29-b977-5fd95afd585c; ajs_anonymous_id=cda6139d-cb82-4906-bfac-adaea115b097"
|
| 15 |
-
|
| 16 |
# -------------------------------------------------------------------
|
| 17 |
# Helper functions for random bot/workspace names
|
| 18 |
# -------------------------------------------------------------------
|
|
@@ -179,7 +178,7 @@ def delete_workspace(workspace_id):
|
|
| 179 |
# -------------------------------------------------------------------
|
| 180 |
# Main function that calls the Botpress API endpoint
|
| 181 |
# -------------------------------------------------------------------
|
| 182 |
-
def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
|
| 183 |
"""
|
| 184 |
Sends the user input and chat history to the Botpress API endpoint,
|
| 185 |
returns the assistant's response and (possibly updated) bot/workspace IDs.
|
|
@@ -222,8 +221,8 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
|
|
| 222 |
},
|
| 223 |
"systemPrompt": system_prompt,
|
| 224 |
"messages": messages,
|
| 225 |
-
"temperature":
|
| 226 |
-
"topP":
|
| 227 |
"debug": False,
|
| 228 |
}
|
| 229 |
}
|
|
@@ -365,7 +364,9 @@ def chat_endpoint():
|
|
| 365 |
{"role": "system", "content": "..."},
|
| 366 |
{"role": "user", "content": "..."},
|
| 367 |
...
|
| 368 |
-
]
|
|
|
|
|
|
|
| 369 |
}
|
| 370 |
Returns JSON with:
|
| 371 |
{
|
|
@@ -379,6 +380,29 @@ def chat_endpoint():
|
|
| 379 |
user_input = data.get("user_input", "")
|
| 380 |
chat_history = data.get("chat_history", [])
|
| 381 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
# If we don't yet have a workspace or bot, create them
|
| 383 |
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
|
| 384 |
print("No existing IDs found. Creating new workspace and bot...")
|
|
@@ -392,11 +416,15 @@ def chat_endpoint():
|
|
| 392 |
|
| 393 |
# Call our function that interacts with Botpress API
|
| 394 |
print(f"Sending chat request with existing bot_id={GLOBAL_BOT_ID}, workspace_id={GLOBAL_WORKSPACE_ID}")
|
|
|
|
|
|
|
| 395 |
assistant_response, updated_bot_id, updated_workspace_id = chat_with_assistant(
|
| 396 |
user_input,
|
| 397 |
chat_history,
|
| 398 |
GLOBAL_BOT_ID,
|
| 399 |
-
GLOBAL_WORKSPACE_ID
|
|
|
|
|
|
|
| 400 |
)
|
| 401 |
|
| 402 |
# Update global IDs if they changed
|
|
|
|
| 12 |
|
| 13 |
# Cookie value used in requests (should be updated with a valid cookie)
|
| 14 |
AUTH_COOKIE = "pscd=try.botpress.com; _hjSessionUser_2931810=eyJpZCI6ImQ2MGMzYjhkLTlkMjQtNTA0OS1hMzlmLWEzNmI0NzA0NzUxNCIsImNyZWF0ZWQiOjE3MzU3MTg0MDcwNTAsImV4aXN0aW5nIjp0cnVlfQ==; hubspotutk=75739411a4d011b2164c4f3d944ecb94; intercom-device-id-bjzkw2xf=afd0a36b-b229-44e3-828e-60483c80c10c; _hjSessionUser_3339867=eyJpZCI6IjU4ODlmMTY4LWRkNGEtNTJhZS1hZTUzLWZlYWQwM2ZmMTVjNyIsImNyZWF0ZWQiOjE3MzU3MTg1ODM4MDgsImV4aXN0aW5nIjp0cnVlfQ==; __hstc=59821234.75739411a4d011b2164c4f3d944ecb94.1735718442141.1746194848836.1746538539919.88; mp_1195923e954ce61d822842b5832047cd_mixpanel=%7B%22distinct_id%22%3A%20%22d403ad7b-ea73-4d29-b977-5fd95afd585c%22%2C%22%24device_id%22%3A%20%22d403ad7b-ea73-4d29-b977-5fd95afd585c%22%2C%22%24initial_referrer%22%3A%20%22https%3A%2F%2Fapp.botpress.cloud%2F%22%2C%22%24initial_referring_domain%22%3A%20%22app.botpress.cloud%22%2C%22__mps%22%3A%20%7B%7D%2C%22__mpso%22%3A%20%7B%22%24initial_referrer%22%3A%20%22https%3A%2F%2Fapp.botpress.cloud%2F%22%2C%22%24initial_referring_domain%22%3A%20%22app.botpress.cloud%22%7D%2C%22__mpus%22%3A%20%7B%7D%2C%22__mpa%22%3A%20%7B%7D%2C%22__mpu%22%3A%20%7B%7D%2C%22__mpr%22%3A%20%5B%5D%2C%22__mpap%22%3A%20%5B%5D%2C%22%24user_id%22%3A%20%22d403ad7b-ea73-4d29-b977-5fd95afd585c%22%7D; intercom-session-bjzkw2xf=TTdnZGNWUC9xNXMreE80NXhRZFNZS0pyUEdsbkJRc2JMcXdGZmcveVRPYkxZTmVnVnhqMUhJTWlDcEpVcWljeDZVYVVSblN4YnV5S0xBdWxDd2swQjZiaUZTeWl5M1psRmtoUWJwUU9FSFE9LS1Bay9zNldJTmVhUFdwMFNReFRmcXB3PT0=--68abc5394d7aab99748f3e451637cab5d9152a4c; _ga=GA1.2.1726154447.1735718383; _gid=GA1.2.1619749406.1746821934; _gat_UA-226900660-1=1; _gat_UA-226900660-2=1; _ga_W6YT9YSNLH=GS2.2.s1746821948$o89$g0$t1746821948$j0$l0$h0; _ga_CYSS87Q508=GS2.2.s1746821949$o89$g0$t1746821949$j0$l0$h0; _hjSession_2931810=eyJpZCI6IjA1YTlkMzY3LWNiZWEtNGQ3OC04YzNiLTEzNTFjNjkxYzViZSIsImMiOjE3NDY4MjE5NTU2NjYsInMiOjAsInIiOjAsInNiIjowLCJzciI6MCwic2UiOjAsImZzIjowLCJzcCI6MH0=; _ga_PCC6TBWJY6=GS2.1.s1746821926$o120$g1$t1746821960$j0$l0$h0; _ga_HKHSWES9V9=GS2.1.s1746821929$o120$g1$t1746821960$j29$l0$h193244206; csrf_token_bd9ac21c34b9f0915e733c3e5305d737d0722c1168be7376b889426b5ec2a298=pIDxu4npODUGEpo7JHVQrKZ4GFaa3U+3BpgxaV5hcVw=; ory_kratos_session=MTc0NjgyMjAyOHxfUmdMaVlTQXVfSmxlT1lJSGpyU2FhbjVUTHg0R0ZsQWgtVm00M3pHcXZwVG9yNW1qRDJheUFGaFZvNmFEUVdBOThQR014RjJJbmhUMmhIV1I1ME5UVHZkTDNpMUMtQlRjZ1ZTbE55M19Pb2dHTF9vQlJoSGlBQnRRWUp0M1ZUdnVvcENLeVhOTllWNk1zMk11bFVPOWFrTzJMTTdxMmVteUozVVRDMWE5TVIxbDgzU3dUY2VQaDBRWDN4bDJUVm8yUkZQa19sb09GbzlFZHF2MDFQcVR6bVVWVVpDLXVoQ1lXMEh2LV9Sd2VNZXM1cjM4TGZPVTJqdW5xNTBETTBDYkppU0xNU2xicUk3Z2EyMnFkVmdyQT09fEYiv7pXcfXVnpIFi4JLGgDObQAchyJCoAwGDSkFkoX7; ajs_user_id=d403ad7b-ea73-4d29-b977-5fd95afd585c; ajs_anonymous_id=cda6139d-cb82-4906-bfac-adaea115b097"
|
|
|
|
| 15 |
# -------------------------------------------------------------------
|
| 16 |
# Helper functions for random bot/workspace names
|
| 17 |
# -------------------------------------------------------------------
|
|
|
|
| 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.
|
|
|
|
| 221 |
},
|
| 222 |
"systemPrompt": system_prompt,
|
| 223 |
"messages": messages,
|
| 224 |
+
"temperature": temperature,
|
| 225 |
+
"topP": top_p,
|
| 226 |
"debug": False,
|
| 227 |
}
|
| 228 |
}
|
|
|
|
| 364 |
{"role": "system", "content": "..."},
|
| 365 |
{"role": "user", "content": "..."},
|
| 366 |
...
|
| 367 |
+
],
|
| 368 |
+
"temperature": 0.9, // Optional, defaults to 0.9
|
| 369 |
+
"top_p": 0.95 // Optional, defaults to 0.95
|
| 370 |
}
|
| 371 |
Returns JSON with:
|
| 372 |
{
|
|
|
|
| 380 |
user_input = data.get("user_input", "")
|
| 381 |
chat_history = data.get("chat_history", [])
|
| 382 |
|
| 383 |
+
# Get temperature and top_p from request, or use defaults
|
| 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:
|
| 389 |
+
temperature = float(temperature)
|
| 390 |
+
if not 0 <= temperature <= 2:
|
| 391 |
+
temperature = 0.9
|
| 392 |
+
print(f"Invalid temperature value. Using default: {temperature}")
|
| 393 |
+
except (ValueError, TypeError):
|
| 394 |
+
temperature = 0.9
|
| 395 |
+
print(f"Invalid temperature format. Using default: {temperature}")
|
| 396 |
+
|
| 397 |
+
try:
|
| 398 |
+
top_p = float(top_p)
|
| 399 |
+
if not 0 <= top_p <= 1:
|
| 400 |
+
top_p = 0.95
|
| 401 |
+
print(f"Invalid top_p value. Using default: {top_p}")
|
| 402 |
+
except (ValueError, TypeError):
|
| 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 |
|
| 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,
|
| 423 |
chat_history,
|
| 424 |
GLOBAL_BOT_ID,
|
| 425 |
+
GLOBAL_WORKSPACE_ID,
|
| 426 |
+
temperature,
|
| 427 |
+
top_p
|
| 428 |
)
|
| 429 |
|
| 430 |
# Update global IDs if they changed
|