Skip to content

Commit eddad27

Browse files
authored
Merge pull request #30 from DefangLabs/linda-segment-api
Add analytics tracking (Segment API) to server
2 parents f321695 + 00667dc commit eddad27

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

app/app.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
from flask import Flask, request, jsonify, render_template, Response, stream_with_context
1+
from flask import Flask, request, jsonify, render_template, Response, stream_with_context, session
22
from flask_wtf.csrf import CSRFProtect
33
from rag_system import rag_system
44
import hashlib
55
import subprocess
66
import os
7+
import segment.analytics as analytics
8+
import uuid
9+
10+
analytics.write_key = os.getenv('SEGMENT_WRITE_KEY')
711

812
app = Flask(__name__, static_folder='templates/images')
913
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')
@@ -33,17 +37,35 @@ def ask():
3337

3438
data = request.get_json()
3539
query = data.get('query')
40+
3641
if not query:
3742
return jsonify({"error": "No query provided"}), 400
43+
44+
# For analytics tracking, generates an anonymous id and uses it for the session
45+
if 'anonymous_id' not in session:
46+
session['anonymous_id'] = str(uuid.uuid4())
47+
anonymous_id = session['anonymous_id']
3848

3949
def generate():
50+
full_response = ""
4051
try:
4152
for token in rag_system.answer_query_stream(query):
4253
yield token
54+
full_response += token
4355
except Exception as e:
4456
print(f"Error in /ask endpoint: {e}")
4557
yield "Internal Server Error"
4658

59+
if not full_response:
60+
full_response = "No response generated"
61+
62+
# Track the query and response
63+
analytics.track(
64+
anonymous_id=anonymous_id,
65+
event='Chatbot Question submitted',
66+
properties={'query': query, 'response': full_response, 'source': 'Ask Defang'}
67+
)
68+
4769
return Response(stream_with_context(generate()), content_type='text/markdown')
4870

4971
@app.route('/trigger-rebuild', methods=['POST'])
@@ -75,4 +97,4 @@ def trigger_rebuild():
7597
return jsonify({"error": "Internal Server Error"}), 500
7698

7799
if __name__ == '__main__':
78-
app.run(host='0.0.0.0', port=5000)
100+
app.run(host='0.0.0.0', port=5000)

app/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Flask==2.0.1
22
Flask-WTF==1.2.2
33
Werkzeug==2.0.3
44
scikit-learn==0.24.2
5+
segment-analytics-python==2.3.3
56
numpy==1.22.0
67
sentence-transformers==2.1.0
78
torch==1.10.0

compose.dev.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ services:
1111
environment:
1212
FLASK_APP: app.py
1313
SECRET_KEY: supersecret
14+
SEGMENT_WRITE_KEY: ${SEGMENT_WRITE_KEY} # Set your Segment write key here or in the .env file
1415
SESSION_COOKIE_SECURE: 0
1516
OPENAI_API_KEY: ${OPENAI_API_KEY} # Set your OpenAI API key here or in the .env file
1617
command: flask run --host=0.0.0.0

compose.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ services:
1414
environment:
1515
FLASK_APP: app.py
1616
SECRET_KEY:
17+
SEGMENT_WRITE_KEY:
1718
SESSION_COOKIE_SECURE: 1
1819
OPENAI_API_KEY: ${OPENAI_API_KEY} # Set your OpenAI API key here or in the .env file
1920
command: uwsgi --http 0.0.0.0:5000 --wsgi-file app.py --callable app --processes 4 --threads 2

0 commit comments

Comments
 (0)