-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
61 lines (43 loc) · 1.73 KB
/
Copy pathserver.py
File metadata and controls
61 lines (43 loc) · 1.73 KB
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
import argparse
import logging
from dotenv import load_dotenv
from flask import Flask, request
from flask import jsonify, render_template, make_response
from llmhelper import LangchainHelper
app = Flask(__name__)
load_dotenv()
logging.basicConfig(level=logging.CRITICAL, format='%(asctime)s %(levelname)s %(message)s')
parser = argparse.ArgumentParser(description="A Flask app for handling chat and QA tasks")
parser.add_argument('--module_name', type=str, default="local",
help='The name of the module to import and use')
parser.add_argument('--reload', action='store_true', help='Reload data')
args = parser.parse_args()
module_name = args.module_name
langchain_helper = LangchainHelper(module_name=module_name, reload=args.reload)
@app.route('/health', methods=['GET'])
def health_check():
return jsonify({"status": "healthy"}), 200
@app.route('/chat', methods=['POST'])
def chat():
data = request.get_json()
chat_history = data.get('chat_history', [])
query = data.get('question', '')
result = langchain_helper.initialize_chat_bot()({"question": query, "chat_history": chat_history})
response = {
"answer": result["answer"],
"chat_history": chat_history + [(query, result["answer"])]
}
response = make_response(jsonify({"result": response}), 200)
return response
@app.route('/qa', methods=['POST'])
def qa():
data = request.get_json()
question = data.get('question', '')
result = langchain_helper.answer_simple_question(query=question)
response = make_response(jsonify({"result": result}), 200)
return response
@app.route('/')
def index():
return render_template('index.html', module_name=module_name)
if __name__ == '__main__':
app.run(debug=False)