You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This simulates the RAG backend responses without requiring Azure services.
4
+
"""
5
+
6
+
importasyncio
7
+
importjson
8
+
importlogging
9
+
fromquartimportQuart, request, jsonify
10
+
11
+
# Configure logging
12
+
logging.basicConfig(level=logging.INFO)
13
+
logger=logging.getLogger(__name__)
14
+
15
+
app=Quart(__name__)
16
+
17
+
# Mock responses for testing
18
+
MOCK_RESPONSES= {
19
+
"hello": {
20
+
"answer": "Hello! I'm your AI assistant. I can help you search through documents and answer questions. How can I assist you today?",
21
+
"data_points": {
22
+
"text": [],
23
+
"citations": []
24
+
},
25
+
"thoughts": [
26
+
{
27
+
"title": "Greeting Response",
28
+
"description": "Generated a friendly greeting response"
29
+
}
30
+
],
31
+
"token_usage": {
32
+
"prompt_tokens": 10,
33
+
"completion_tokens": 25,
34
+
"total_tokens": 35
35
+
},
36
+
"model_info": {
37
+
"model": "gpt-4",
38
+
"temperature": "0.3"
39
+
}
40
+
},
41
+
"benefits": {
42
+
"answer": "Based on the policy document, the main benefits include comprehensive health coverage, flexible work arrangements, professional development opportunities, and competitive compensation packages. These benefits are designed to support employee well-being and career growth.",
43
+
"data_points": {
44
+
"text": [
45
+
"Policy Document: The company offers comprehensive health coverage including medical, dental, and vision insurance.",
46
+
"Policy Document: Flexible work arrangements are available including remote work options and flexible hours.",
47
+
"Policy Document: Professional development opportunities include training programs, conference attendance, and tuition reimbursement."
48
+
],
49
+
"citations": [
50
+
"Policy Document (Page 1)",
51
+
"Policy Document (Page 2)",
52
+
"Policy Document (Page 3)"
53
+
]
54
+
},
55
+
"thoughts": [
56
+
{
57
+
"title": "Document Search",
58
+
"description": "Searched through policy documents for benefit information"
59
+
},
60
+
{
61
+
"title": "Response Generation",
62
+
"description": "Generated comprehensive response about benefits"
63
+
}
64
+
],
65
+
"token_usage": {
66
+
"prompt_tokens": 150,
67
+
"completion_tokens": 75,
68
+
"total_tokens": 225
69
+
},
70
+
"model_info": {
71
+
"model": "gpt-4",
72
+
"temperature": "0.3"
73
+
}
74
+
}
75
+
}
76
+
77
+
@app.route("/", methods=["GET"])
78
+
asyncdefhealth():
79
+
"""Health check endpoint."""
80
+
returnjsonify({
81
+
"status": "healthy",
82
+
"service": "Mock RAG Backend",
83
+
"version": "1.0.0"
84
+
})
85
+
86
+
@app.route("/chat", methods=["POST"])
87
+
asyncdefchat():
88
+
"""Chat endpoint that simulates RAG responses."""
89
+
try:
90
+
data=awaitrequest.get_json()
91
+
messages=data.get("messages", [])
92
+
93
+
ifnotmessages:
94
+
returnjsonify({"error": "No messages provided"}), 400
95
+
96
+
# Get the last user message
97
+
last_message=None
98
+
formsginreversed(messages):
99
+
ifmsg.get("role") =="user":
100
+
last_message=msg.get("content", "").lower()
101
+
break
102
+
103
+
ifnotlast_message:
104
+
returnjsonify({"error": "No user message found"}), 400
"answer": f"I understand you're asking about '{last_message}'. I'm a mock backend for testing purposes. In a real implementation, I would search through your documents and provide detailed answers with citations.",
115
+
"data_points": {
116
+
"text": [],
117
+
"citations": []
118
+
},
119
+
"thoughts": [
120
+
{
121
+
"title": "Mock Response",
122
+
"description": "Generated mock response for testing"
123
+
}
124
+
],
125
+
"token_usage": {
126
+
"prompt_tokens": 50,
127
+
"completion_tokens": 30,
128
+
"total_tokens": 80
129
+
},
130
+
"model_info": {
131
+
"model": "gpt-4",
132
+
"temperature": "0.3"
133
+
}
134
+
}
135
+
136
+
logger.info(f"Mock backend responding to: {last_message}")
137
+
returnjsonify(response)
138
+
139
+
exceptExceptionase:
140
+
logger.error(f"Error in mock chat endpoint: {e}")
141
+
returnjsonify({"error": "Internal server error"}), 500
142
+
143
+
@app.route("/chat/stream", methods=["POST"])
144
+
asyncdefchat_stream():
145
+
"""Streaming chat endpoint that simulates streaming RAG responses."""
146
+
try:
147
+
data=awaitrequest.get_json()
148
+
messages=data.get("messages", [])
149
+
150
+
ifnotmessages:
151
+
returnjsonify({"error": "No messages provided"}), 400
152
+
153
+
# Get the last user message
154
+
last_message=None
155
+
formsginreversed(messages):
156
+
ifmsg.get("role") =="user":
157
+
last_message=msg.get("content", "").lower()
158
+
break
159
+
160
+
ifnotlast_message:
161
+
returnjsonify({"error": "No user message found"}), 400
0 commit comments