-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
73 lines (59 loc) · 2.1 KB
/
app.py
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
from flask import Flask, request, jsonify
from flask_cors import CORS
from flask import Flask, render_template
import requests
import os
app = Flask(__name__)
CORS(app) # Allow frontend to call API
# Define account-specific configuration
account_config = {
"ranveer": {
"BASE_API_URL": "https://api.langflow.astra.datastax.com",
"LANGFLOW_ID": "50f59f86-16d5-46db-abe2-c0995d97f2b0",
"FLOW_ID": "3030aad7-cea9-4049-956c-bc5c7486ad0a",
"APPLICATION_TOKEN": os.getenv('TOKEN1')
},
"hitesh": {
"BASE_API_URL": "https://api.langflow.astra.datastax.com",
"LANGFLOW_ID": "50f59f86-16d5-46db-abe2-c0995d97f2b0",
"FLOW_ID": "ed7e98d8-933c-4cf8-811b-86d56ddaf262",
"APPLICATION_TOKEN": os.getenv('TOKEN2')
}
}
@app.route('/api/message', methods=['POST'])
def get_message():
"""Handle chat messages."""
message = request.json.get('message')
account = request.json.get('account')
if not message or not account:
return jsonify({"error": "Message and account are required"}), 400
if account not in account_config:
return jsonify({"error": "Invalid account selected"}), 400
response = run_flow(message, account)
return jsonify(response)
def run_flow(message, account):
"""Send message to the appropriate Langflow API."""
config = account_config[account]
api_url = f"{config['BASE_API_URL']}/lf/{config['LANGFLOW_ID']}/api/v1/run/{config['FLOW_ID']}"
payload = {
"input_value": f"{account}: {message}", # Pass account context
"output_type": "chat",
"input_type": "chat",
}
headers = {
"Authorization": f"Bearer {config['APPLICATION_TOKEN']}",
"Content-Type": "application/json"
}
response = requests.post(api_url, json=payload, headers=headers)
return response.json()
@app.route('/')
def home():
return render_template('index.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/contact')
def contact():
return render_template('contact.html')
if __name__ == "__main__":
app.run(debug=True)