Spaces:
Runtime error
Runtime error
Commit
·
98d627f
1
Parent(s):
46427c3
Update main.py
Browse files
main.py
CHANGED
@@ -230,6 +230,60 @@ def chat():
|
|
230 |
return jsonify({'error': str(e)}), 500
|
231 |
|
232 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
@app.route('/generateQuestions', methods=['OPTIONS'])
|
234 |
def options_generate():
|
235 |
response = make_response()
|
|
|
230 |
return jsonify({'error': str(e)}), 500
|
231 |
|
232 |
|
233 |
+
@app.route('/chatgrouped', methods=['OPTIONS'])
|
234 |
+
def options_grouped():
|
235 |
+
response = make_response()
|
236 |
+
response.headers.add("Access-Control-Allow-Origin", "*")
|
237 |
+
response.headers.add("Access-Control-Allow-Methods", "POST")
|
238 |
+
response.headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
239 |
+
response.headers.add("Access-Control-Allow-Credentials", "true")
|
240 |
+
return response
|
241 |
+
|
242 |
+
@app.route('/chatgrouped', methods=['POST'])
|
243 |
+
def chat_grouped():
|
244 |
+
try:
|
245 |
+
data = request.get_json()
|
246 |
+
messages = data.get('messages', [])
|
247 |
+
|
248 |
+
if messages:
|
249 |
+
results = []
|
250 |
+
# Update the model name to "text-davinci-003" (Ada)
|
251 |
+
prompt = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages])
|
252 |
+
response = openai.completions.create(
|
253 |
+
model="gpt-3.5-turbo-instruct",
|
254 |
+
prompt=start_message +'\n'+ context + question ,
|
255 |
+
max_tokens=500,
|
256 |
+
temperature=0
|
257 |
+
)
|
258 |
+
date = time.ctime(time.time())
|
259 |
+
texte = prompt
|
260 |
+
data = Question(date, texte)
|
261 |
+
db.session.add(data)
|
262 |
+
db.session.commit()
|
263 |
+
question_id = data.id
|
264 |
+
resulta = response.choices[0].text
|
265 |
+
chat_references = filtergpt(resulta)
|
266 |
+
for law, article in chat_references:
|
267 |
+
search_results = perform_search_and_get_results_with_filter(collection_names[0], prompt, reference_filter=article)
|
268 |
+
results.extend(search_results)
|
269 |
+
for collection_name in collection_names:
|
270 |
+
search_results = perform_search_and_get_results(collection_name, prompt)
|
271 |
+
results.extend(search_results)
|
272 |
+
grouped_hits = {}
|
273 |
+
for i, hit in enumerate(results, 1):
|
274 |
+
second_number = hit.payload['numero_article']
|
275 |
+
if second_number not in grouped_hits:
|
276 |
+
grouped_hits[second_number] = []
|
277 |
+
grouped_hits[second_number].append(hit)
|
278 |
+
return jsonify({'question': {'id': question_id, 'date': date, 'texte': texte},'result_qdrant':grouped_hits})
|
279 |
+
else:
|
280 |
+
return jsonify({'error': 'Invalid request'}), 400
|
281 |
+
except Exception as e:
|
282 |
+
return jsonify({'error': str(e)}), 500
|
283 |
+
|
284 |
+
|
285 |
+
|
286 |
+
|
287 |
@app.route('/generateQuestions', methods=['OPTIONS'])
|
288 |
def options_generate():
|
289 |
response = make_response()
|