-
Notifications
You must be signed in to change notification settings - Fork 0
/
rag-server.py
124 lines (91 loc) · 3.77 KB
/
rag-server.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# ! pip install langchain_community tiktoken langchain-openai langchainhub chromadb langchain flask
# =========== SERVER ===========
import os
from flask import Flask, request, jsonify
from flask_cors import CORS
from core import LOG_DIR, send_prompt_rag_plain, send_prompt_llm, send_prompt_experimental, get_follow_up_questions, transcribe, summarize, send_rag_chat
# from glossary import get_dictionary_tw
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
default_system_prompt = "You are an evangelical Christian with traditional beliefs about God and the Bible. However, do not preface your responses with your persona."
@app.route('/rag-system-prompt', methods=['GET'])
def get_prompt():
prompt = request.args.get('user-prompt', default='', type=str)
# system_prompt = request.args.get('system-prompt', default='', type=str)
response = {
'rag-response' : send_prompt_experimental(prompt, default_system_prompt),
}
return jsonify(response)
@app.route('/rag-compare', methods=['GET'])
def rag_compare():
prompt = request.args.get('prompt', default='', type=str)
response = {
'rag-response' : send_prompt_experimental(prompt, system_prompt=default_system_prompt),
'llm-response' : send_prompt_llm(prompt),
}
return jsonify(response)
@app.route('/rag', methods=['GET'])
def rag():
prompt = request.args.get('prompt', default='', type=str)
response = {
'rag-response' : send_prompt_experimental(prompt, system_prompt=default_system_prompt)
}
return jsonify(response)
@app.route('/llm', methods=['GET'])
def llm_endpoint():
prompt = request.args.get('prompt', default='', type=str)
response = {
'llm-response' : send_prompt_llm(prompt)
}
return jsonify(response)
@app.route('/follow-up-questions', methods=['POST'])
def follow_up_questions():
request_json = request.json
question = request_json["question"]
answer = request_json["answer"]
response = get_follow_up_questions(question, answer)
return response
@app.route('/message', methods=['POST'])
def message():
request_json = request.json
user_query = request_json['userQuery']
lastResponse = request_json['lastResponse']
new_response = send_rag_chat(user_query, lastResponse)
if request.args.get('logging', '') != '':
log_message(user=user_query, system=new_response)
return jsonify({
'chat-summary': [],
'rag-response': new_response
})
@app.route('/upload-audio-command', methods=['POST'])
def upload_audio():
if 'audio' not in request.files:
return jsonify({"error": "No audio file provided"}), 400
audio_file = request.files['audio']
if audio_file.filename == '':
return jsonify({"error": "No selected file"}), 400
# Save the file to the uploads folder
file_path = os.path.join(r"/tmp", audio_file.filename)
audio_file.save(file_path)
prompt = transcribe(file_path)
print(prompt)
return jsonify({"prompt": prompt }), 200
import threading
import datetime;
file_lock = threading.Lock()
def log_message(user: str, system: str):
with file_lock:
try:
# Check if the file exists, and read the current logs
log_file = os.path.join(LOG_DIR, "chat-log.txt")
if not os.path.exists(log_file):
with open(log_file, 'w') as _:
pass
with open(log_file, 'a') as file:
message = f'Timestamp:{datetime.datetime.now()}\n<User>: {user}\n<System>: {system}\n\n==================================\n\n'
file.write(message)
except Exception as e:
print(f"Error logging message: {e}")
if __name__ == '__main__':
# app.run(debug=True)
app.run(host='0.0.0.0', port=80, debug=True)