Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,226 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import requests
|
|
|
|
|
|
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
history = data.get('history', [])
|
| 22 |
-
|
| 23 |
-
# Construct the payload for the external API
|
| 24 |
-
payload = {
|
| 25 |
-
"prompt": {
|
| 26 |
-
"messages": history + [
|
| 27 |
-
{"role": "user", "content": question}
|
| 28 |
-
],
|
| 29 |
-
"model": "gpt-4o",
|
| 30 |
-
"temperature": 0.9,
|
| 31 |
-
"signatureVersion": "Jan-2024",
|
| 32 |
-
},
|
| 33 |
-
"variables": {
|
| 34 |
-
"TASK_INPUT": ""
|
| 35 |
-
},
|
| 36 |
-
"options": {
|
| 37 |
-
"origin": "cards/ai-task",
|
| 38 |
-
},
|
| 39 |
-
"origin": "cards/ai-task",
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
# Send the POST request to the external API
|
| 43 |
-
response = requests.post(EXTERNAL_API_URL, json=payload, headers=HEADERS)
|
| 44 |
-
|
| 45 |
-
# Check the response status
|
| 46 |
if response.status_code == 200:
|
| 47 |
-
|
| 48 |
-
assistant_content =
|
| 49 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
else:
|
| 51 |
-
return
|
| 52 |
|
| 53 |
except Exception as e:
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
|
|
|
|
|
|
|
|
|
| 56 |
if __name__ == "__main__":
|
| 57 |
-
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import requests
|
| 3 |
+
import random
|
| 4 |
+
import string
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
+
# -------------------------------------------------------------------
|
| 9 |
+
# Global variables to hold workspace and bot IDs across requests
|
| 10 |
+
# -------------------------------------------------------------------
|
| 11 |
+
GLOBAL_WORKSPACE_ID = None
|
| 12 |
+
GLOBAL_BOT_ID = None
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# -------------------------------------------------------------------
|
| 16 |
+
# Helper functions for random bot/workspace names
|
| 17 |
+
# -------------------------------------------------------------------
|
| 18 |
+
def generate_random_name(length=5):
|
| 19 |
+
return ''.join(random.choices(string.ascii_letters, k=length))
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# -------------------------------------------------------------------
|
| 23 |
+
# Functions to create/delete workspaces and bots
|
| 24 |
+
# -------------------------------------------------------------------
|
| 25 |
+
def create_workspace():
|
| 26 |
+
ws_url = "https://api.botpress.cloud/v1/admin/workspaces"
|
| 27 |
+
headers = {
|
| 28 |
+
"User-Agent": "Mozilla/5.0",
|
| 29 |
+
# Replace with your valid cookie below
|
| 30 |
+
"cookie": "pscd=try.botpress.com; ..."
|
| 31 |
+
}
|
| 32 |
+
payload = {"name": generate_random_name()}
|
| 33 |
+
response = requests.post(ws_url, headers=headers, json=payload)
|
| 34 |
+
|
| 35 |
+
if response.status_code == 200:
|
| 36 |
+
response_json = response.json()
|
| 37 |
+
return response_json.get('id')
|
| 38 |
+
else:
|
| 39 |
+
print(f"Workspace creation failed with: {response.status_code}, {response.text}")
|
| 40 |
+
return None
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def create_bot(workspace_id):
|
| 44 |
+
bot_url = "https://api.botpress.cloud/v1/admin/bots"
|
| 45 |
+
headers = {
|
| 46 |
+
"User-Agent": "Mozilla/5.0",
|
| 47 |
+
"x-workspace-id": workspace_id,
|
| 48 |
+
# Replace with your valid cookie below
|
| 49 |
+
"cookie": "pscd=try.botpress.com; ..."
|
| 50 |
+
}
|
| 51 |
+
payload = {"name": generate_random_name()}
|
| 52 |
+
response = requests.post(bot_url, headers=headers, json=payload)
|
| 53 |
+
|
| 54 |
+
if response.status_code == 200:
|
| 55 |
+
response_json = response.json()
|
| 56 |
+
bot_id = response_json.get("bot", {}).get("id")
|
| 57 |
+
if not bot_id:
|
| 58 |
+
print("Bot ID not found in the response.")
|
| 59 |
+
return bot_id
|
| 60 |
+
else:
|
| 61 |
+
print(f"Bot creation failed with: {response.status_code}, {response.text}")
|
| 62 |
+
return None
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def delete_bot(bot_id, workspace_id):
|
| 66 |
+
url = f"https://api.botpress.cloud/v1/admin/bots/{bot_id}"
|
| 67 |
+
headers = {
|
| 68 |
+
"User-Agent": "Mozilla/5.0",
|
| 69 |
+
"x-workspace-id": workspace_id,
|
| 70 |
+
# Replace with your valid cookie below
|
| 71 |
+
"cookie": "pscd=try.botpress.com; ..."
|
| 72 |
+
}
|
| 73 |
+
return requests.delete(url, headers=headers)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def delete_workspace(workspace_id):
|
| 77 |
+
url = f"https://api.botpress.cloud/v1/admin/workspaces/{workspace_id}"
|
| 78 |
+
headers = {
|
| 79 |
+
"User-Agent": "Mozilla/5.0",
|
| 80 |
+
# Replace with your valid cookie below
|
| 81 |
+
"cookie": "pscd=try.botpress.com; ..."
|
| 82 |
+
}
|
| 83 |
+
return requests.delete(url, headers=headers)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# -------------------------------------------------------------------
|
| 87 |
+
# Main function that calls the Botpress GPT-4 endpoint
|
| 88 |
+
# -------------------------------------------------------------------
|
| 89 |
+
def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
|
| 90 |
+
"""
|
| 91 |
+
Sends the user input and chat history to the Botpress GPT-4 endpoint,
|
| 92 |
+
returns the assistant's response and (possibly updated) bot/workspace IDs.
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
+
# Prepare the headers
|
| 96 |
+
headers = {
|
| 97 |
+
"User-Agent": "Mozilla/5.0",
|
| 98 |
+
"x-bot-id": bot_id, # existing bot ID (could be None on first try)
|
| 99 |
+
"Content-Type": "application/json",
|
| 100 |
+
# Replace with your valid cookie below
|
| 101 |
+
"Cookie": "pscd=try.botpress.com; ..."
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
# Prepare the payload
|
| 105 |
+
payload = {
|
| 106 |
+
"prompt": {
|
| 107 |
+
"messages": chat_history, # from user request
|
| 108 |
+
"model": "gpt-4o",
|
| 109 |
+
"temperature": 0.9,
|
| 110 |
+
"signatureVersion": "Jan-2024"
|
| 111 |
+
},
|
| 112 |
+
"variables": {
|
| 113 |
+
"TASK_INPUT": ""
|
| 114 |
+
},
|
| 115 |
+
"options": {
|
| 116 |
+
"origin": "cards/ai-task"
|
| 117 |
+
},
|
| 118 |
+
"origin": "cards/ai-task"
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
botpress_url = "https://api.botpress.cloud/v1/cognitive/chat-gpt/query"
|
| 122 |
+
|
| 123 |
+
# Attempt to send the request
|
| 124 |
try:
|
| 125 |
+
response = requests.post(botpress_url, json=payload, headers=headers)
|
| 126 |
+
|
| 127 |
+
# If successful (200)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
if response.status_code == 200:
|
| 129 |
+
data = response.json()
|
| 130 |
+
assistant_content = data.get('choices', [{}])[0].get('message', {}).get('content', '')
|
| 131 |
+
return assistant_content, bot_id, workspace_id
|
| 132 |
+
|
| 133 |
+
# If we get a 403, it could be because the bot/workspace IDs are invalid/expired
|
| 134 |
+
elif response.status_code == 403:
|
| 135 |
+
raise Exception("Invalid or expired bot ID.")
|
| 136 |
+
|
| 137 |
+
# Other errors
|
| 138 |
else:
|
| 139 |
+
return f"Error {response.status_code}: {response.text}", bot_id, workspace_id
|
| 140 |
|
| 141 |
except Exception as e:
|
| 142 |
+
# If invalid/expired bot, create new workspace/bot and retry
|
| 143 |
+
if "Invalid or expired bot ID" in str(e):
|
| 144 |
+
# Attempt to delete old IDs if they exist
|
| 145 |
+
if bot_id and workspace_id:
|
| 146 |
+
delete_bot(bot_id, workspace_id)
|
| 147 |
+
delete_workspace(workspace_id)
|
| 148 |
+
|
| 149 |
+
# Create fresh IDs
|
| 150 |
+
new_workspace = create_workspace()
|
| 151 |
+
new_bot = create_bot(new_workspace)
|
| 152 |
+
if not new_workspace or not new_bot:
|
| 153 |
+
return "Failed to regenerate workspace or bot IDs.", None, None
|
| 154 |
+
|
| 155 |
+
# Update headers with the new bot ID
|
| 156 |
+
headers["x-bot-id"] = new_bot
|
| 157 |
+
|
| 158 |
+
# Retry
|
| 159 |
+
retry_response = requests.post(botpress_url, json=payload, headers=headers)
|
| 160 |
+
if retry_response.status_code == 200:
|
| 161 |
+
data = retry_response.json()
|
| 162 |
+
assistant_content = data.get('choices', [{}])[0].get('message', {}).get('content', '')
|
| 163 |
+
return assistant_content, new_bot, new_workspace
|
| 164 |
+
else:
|
| 165 |
+
return f"Error {retry_response.status_code}: {retry_response.text}", new_bot, new_workspace
|
| 166 |
+
else:
|
| 167 |
+
# Other exceptions
|
| 168 |
+
return f"Unexpected error: {str(e)}", bot_id, workspace_id
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
# -------------------------------------------------------------------
|
| 172 |
+
# Flask Endpoint
|
| 173 |
+
# -------------------------------------------------------------------
|
| 174 |
+
@app.route("/chat", methods=["POST"])
|
| 175 |
+
def chat_endpoint():
|
| 176 |
+
"""
|
| 177 |
+
Expects JSON with:
|
| 178 |
+
{
|
| 179 |
+
"user_input": "string",
|
| 180 |
+
"chat_history": [
|
| 181 |
+
{"role": "system", "content": "..."},
|
| 182 |
+
{"role": "user", "content": "..."},
|
| 183 |
+
...
|
| 184 |
+
]
|
| 185 |
+
}
|
| 186 |
+
Returns JSON with:
|
| 187 |
+
{
|
| 188 |
+
"assistant_response": "string"
|
| 189 |
+
}
|
| 190 |
+
"""
|
| 191 |
+
global GLOBAL_WORKSPACE_ID, GLOBAL_BOT_ID
|
| 192 |
+
|
| 193 |
+
# Parse JSON from request
|
| 194 |
+
data = request.get_json(force=True)
|
| 195 |
+
user_input = data.get("user_input", "")
|
| 196 |
+
chat_history = data.get("chat_history", [])
|
| 197 |
+
|
| 198 |
+
# If we don't yet have a workspace or bot, create them
|
| 199 |
+
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
|
| 200 |
+
GLOBAL_WORKSPACE_ID = create_workspace()
|
| 201 |
+
GLOBAL_BOT_ID = create_bot(GLOBAL_WORKSPACE_ID)
|
| 202 |
+
|
| 203 |
+
# If creation failed
|
| 204 |
+
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
|
| 205 |
+
return jsonify({"assistant_response": "Could not create workspace or bot."}), 500
|
| 206 |
+
|
| 207 |
+
# Call our function that interacts with Botpress GPT-4
|
| 208 |
+
assistant_response, updated_bot_id, updated_workspace_id = chat_with_assistant(
|
| 209 |
+
user_input,
|
| 210 |
+
chat_history,
|
| 211 |
+
GLOBAL_BOT_ID,
|
| 212 |
+
GLOBAL_WORKSPACE_ID
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
# Update global IDs if they changed
|
| 216 |
+
GLOBAL_BOT_ID = updated_bot_id
|
| 217 |
+
GLOBAL_WORKSPACE_ID = updated_workspace_id
|
| 218 |
+
|
| 219 |
+
return jsonify({"assistant_response": assistant_response})
|
| 220 |
+
|
| 221 |
|
| 222 |
+
# -------------------------------------------------------------------
|
| 223 |
+
# Run the Flask app (example)
|
| 224 |
+
# -------------------------------------------------------------------
|
| 225 |
if __name__ == "__main__":
|
| 226 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|