forked from samelhousseini/km-openai
-
Notifications
You must be signed in to change notification settings - Fork 62
/
app.py
178 lines (127 loc) · 5.01 KB
/
app.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import logging
import os
from flask import Flask, redirect, url_for, request, jsonify
from flask_socketio import SocketIO
from flask_socketio import send, emit
import urllib
from utils import bot_helpers
from utils import langchain_helpers
from utils import km_agents
from utils import redis_helpers
from utils import language
global_params_dict = {
'enable_unified_search': False,
'enable_redis_search': False,
'enable_cognitive_search': True,
'evaluate_step': False,
'check_adequacy': False,
'check_intent': False
}
# redis_conn = redis_helpers.get_new_conn()
from utils.env_vars import *
app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins='*')
app.config['SECRET_KEY'] = 'secret!'
redis_conn = redis_helpers.get_new_conn()
##############################################################
##############################################################
# IMPORTANT
# To run this web server, use the following command:
# flask --app app.py --debug run
# To be able to run this, activate the venv first using the
# following command on Windows:
# .\.venv\Scripts\activate
# Then install the required packages using the following command:
# pip install -r requirements.txt
##############################################################
##############################################################
agents_sid = {}
@app.route("/", defaults={"path": "index.html"})
@app.route("/<path:path>")
def static_file(path):
print("path", path)
return app.send_static_file(path)
@socketio.on('connect')
def on_connect():
print(f"connected {request.sid}")
@socketio.on('config')
def on_config(agent_type):
print(f"config {request.sid} - {agent_type}")
connection = {'socketio': socketio, 'connection_id':request.sid}
agent = km_agents.KMOAI_Agent(agent_name = agent_type, params_dict=global_params_dict, stream = True, connection=connection)
agents_sid[request.sid] = agent
@socketio.on('disconnect')
def on_disconnect():
try:
del agents_sid[request.sid]
except Exception as e:
print(f"Client not found: {e}")
@socketio.on('message')
def handle_message(q):
print(f'received message: {q} from {request.sid}')
emit('new_message', "Query: " + q + '\n')
lang = language.detect_content_language(q)
if lang != 'en': q = language.translate(q, lang, 'en')
print(f'language detected: {lang}')
answer, sources, likely_sources, s_id = agents_sid[request.sid].run(q, request.sid, redis_conn=redis_conn)
sources_str = ''
if lang != 'en': answer = language.translate(answer, 'en', lang)
answer = answer.replace('\n', ' <br> ')
send(answer)
if len(sources) > 0:
for s in set(sources):
try:
linkname = urllib.parse.unquote(os.path.basename(s.split('?')[0]))
except:
linkname = 'Link'
sources_str += '[<a href="' + s + f'" target="_blank">{linkname}</a>]'
send('Links:'+ sources_str)
##### IMPORTANT
##### INCLUDE IN THE POST HEADER --> Content-Type: application/json
##### IMPORTANT
@app.route('/kmoai_request', methods=['POST'])
def kmoai_request():
data = request.get_json()
return process_kmoai_request(data)
def check_param(param):
if param == 'true':
param = True
else:
param = False
return param
def get_param(req, param_name):
param = req.get(param_name, None)
return param
##### IMPORTANT
##### INCLUDE IN THE POST HEADER --> Content-Type: application/json
##### IMPORTANT
def process_kmoai_request(req):
logging.info('Python HTTP trigger function processed a request.')
query = get_param(req, 'query')
session_id = get_param(req, 'session_id')
filter_param = get_param(req, 'filter')
search_method = get_param(req, 'search_method')
enable_unified_search = get_param(req, 'enable_unified_search')
enable_redis_search = get_param(req, 'enable_redis_search')
enable_cognitive_search = get_param(req, 'enable_cognitive_search')
evaluate_step = get_param(req, 'evaluate_step')
check_adequacy = get_param(req, 'check_adequacy')
check_intent = get_param(req, 'check_intent')
use_calendar = get_param(req, 'use_calendar')
use_bing = get_param(req, 'use_bing')
params_dict = {
'enable_unified_search': check_param(enable_unified_search),
'enable_redis_search': check_param(enable_redis_search),
'enable_cognitive_search': check_param(enable_cognitive_search),
'evaluate_step': check_param(evaluate_step),
'check_adequacy': check_param(check_adequacy),
'check_intent': check_param(check_intent),
'use_calendar': check_param(use_calendar),
'use_bing': check_param(use_bing)
}
if filter_param is None: filter_param = '*'
return bot_helpers.openai_interrogate_text(query, session_id=session_id, filter_param=filter_param, agent_name=search_method, params_dict=params_dict)
if __name__ == '__main__':
app.run()
socketio.run(app, allow_unsafe_werkzeug=True)
print('socket io start')