-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
342 lines (260 loc) · 10.1 KB
/
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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
from langchain.schema import AgentAction, AgentFinish, LLMResult
from langchain.callbacks.base import BaseCallbackHandler
from typing import Any, Dict, List, Union
import sys
import time
from flask import Flask, Response, request
from flask_cors import CORS
from llama_index import Document, GPTSimpleVectorIndex, LLMPredictor, QueryMode, QuestionAnswerPrompt, ServiceContext, PromptHelper
from langchain.chat_models import ChatOpenAI
from langchain.callbacks.base import CallbackManager, BaseCallbackHandler
from langchain.schema import LLMResult
from typing import Any, Union
from queue import Queue
import threading
from pathlib import Path
import os
# enable logging of llama_index
import logging
logging.getLogger().setLevel(logging.DEBUG)
"""Callback Handler streams to stdout on new llm token."""
class CustomCallBackHandler(BaseCallbackHandler):
queue: Queue
def __init__(self, queue: Queue):
self.queue = queue
"""Callback handler for streaming. Only works with LLMs that support streaming."""
def on_llm_start(
self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any
) -> None:
"""Run when LLM starts running."""
def on_llm_new_token(self, token: str, **kwargs: Any) -> None:
"""Run on new LLM token. Only available when streaming is enabled."""
self.queue.put(token)
# print(token)
def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
"""Run when LLM ends running."""
self.queue.put("[END]")
def on_llm_error(
self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any
) -> None:
"""Run when LLM errors."""
self.queue.put("[END]")
def on_chain_start(
self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any
) -> None:
"""Run when chain starts running."""
def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> None:
"""Run when chain ends running."""
self.queue.put("[END]")
def on_chain_error(
self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any
) -> None:
"""Run when chain errors."""
self.queue.put("[END]")
def on_tool_start(
self, serialized: Dict[str, Any], input_str: str, **kwargs: Any
) -> None:
"""Run when tool starts running."""
def on_agent_action(self, action: AgentAction, **kwargs: Any) -> Any:
"""Run on agent action."""
pass
def on_tool_end(self, output: str, **kwargs: Any) -> None:
"""Run when tool ends running."""
def on_tool_error(
self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any
) -> None:
"""Run when tool errors."""
def on_text(self, text: str, **kwargs: Any) -> None:
"""Run on arbitrary text."""
def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> None:
"""Run on agent end."""
app = Flask(__name__)
CORS(app)
@app.route('/', methods=["POST"])
def home():
return 'Hello, World!'
openai_key = "YOUR_KEY_HERE"
print("key")
print(openai_key)
chatModel = ChatOpenAI(
streaming=True,
openai_api_key=openai_key,
temperature=0)
llm_predictor = LLMPredictor(llm=chatModel)
# define prompt helper
# set maximum input size
max_input_size = 4096
# set number of output tokens
num_output = 512
# set maximum chunk overlap
max_chunk_overlap = 20
prompt_helper = PromptHelper(
max_input_size, num_output, max_chunk_overlap)
service_context = ServiceContext.from_defaults(
llm_predictor=llm_predictor, prompt_helper=prompt_helper, chunk_size_limit=2000)
def gen_index(document, sc):
# Document array according every 100 characters
documents = []
for i in range(0, len(document), 1000):
documents.append(Document(document[i:i+1000]))
index = GPTSimpleVectorIndex([], service_context=sc)
for doc in documents:
# adding space every 100 characters
doc.text = '\n'.join([doc.text[i:i+10]
for i in range(0, len(doc.text), 10)])
index.insert(doc)
# doc = Document(document)
# index = GPTSimpleVectorIndex.from_documents(
# [doc], service_context=service_context)
return index
def getQAPrompt2():
QUESTION_ANSWER_PROMPT_TMPL = (
"Context information is below. This is a meeting transcript.\n"
"---------------------\n"
"{context_str}"
"\n---------------------\n"
"According to context above to answer following questions\n"
"{query_str}\n")
QUESTION_ANSWER_PROMPT = QuestionAnswerPrompt(QUESTION_ANSWER_PROMPT_TMPL)
return QUESTION_ANSWER_PROMPT
def getQAPrompt():
QUESTION_ANSWER_PROMPT_TMPL = (
"The text below is a meeting transcript, {query_str}\n\n"
"Text: \"\"\""
"{context_str}"
"\n"
"\"\"\""
)
QUESTION_ANSWER_PROMPT = QuestionAnswerPrompt(QUESTION_ANSWER_PROMPT_TMPL)
return QUESTION_ANSWER_PROMPT
@ app.route("/query", methods=["POST"])
def query():
context = request.json.get('context')
prompt = request.json.get('prompt')
start_time = time.time()
print("index start @ {}".format(start_time))
index = gen_index(context, service_context)
end_time = time.time()
print("index done, Total time elapsed: {}".format(end_time-start_time))
start_time = time.time()
print("query start @ {}".format(start_time))
response = index.query(prompt + " respond in Chinese.",
text_qa_template=getQAPrompt(), response_mode="tree_summarize", mode=QueryMode.EMBEDDING, service_context=service_context, use_async=True)
end_time = time.time()
print("index done, Total time elapsed: {}".format(end_time-start_time))
print(response)
return str(response), 200
@ app.route("/stream", methods=["POST"])
def stream():
q = Queue()
context = request.json.get('context')
prompt = request.json.get('prompt')
cm = ChatOpenAI(
streaming=True,
openai_api_key=openai_key,
temperature=0,
callback_manager=CallbackManager([CustomCallBackHandler(queue=q)]), verbose=True)
lp = LLMPredictor(llm=cm)
sc = ServiceContext.from_defaults(
llm_predictor=lp, prompt_helper=prompt_helper)
index = gen_index(context, sc)
def query():
resp = index.query(prompt + " respond in Chinese.",
text_qa_template=getQAPrompt(), response_mode="tree_summarize", mode=QueryMode.EMBEDDING, service_context=sc, use_async=True)
q.put("[END]")
print(resp)
t = threading.Thread(target=query)
t.start()
def generate():
while True:
token = q.get()
if token == "[END]":
print("stream end!")
break
yield token
return Response(generate(), mimetype='text/event-stream')
mutex = threading.Lock()
@ app.route("/index/add", methods=["POST"])
def add_index():
id = request.json.get('id')
context = request.json.get('context')
# adding space every 100 characters for context
context = '\n'.join([context[i:i+10]
for i in range(0, len(context), 10)])
app.logger.info(id)
app.logger.info(context)
index_file = os.path.join(Path('./data'), Path(id))
if os.path.exists(index_file):
mutex.acquire(timeout=10)
index = GPTSimpleVectorIndex.load_from_disk(
index_file, service_context=service_context)
mutex.release()
index.insert(Document(context))
mutex.acquire(timeout=10)
index.save_to_disk(index_file)
mutex.release()
else:
mutex.acquire(timeout=10)
index = GPTSimpleVectorIndex.from_documents(
[Document(context)], service_context=service_context)
index.save_to_disk(index_file)
mutex.release()
return "success", 200
@ app.route("/index/query", methods=["POST"])
def query_index():
id = request.json.get('id')
prompt = request.json.get('prompt')
app.logger.info(id)
app.logger.info(prompt)
index_file = os.path.join(Path('./data'), Path(id))
if os.path.exists(index_file):
mutex.acquire(timeout=10)
index = GPTSimpleVectorIndex.load_from_disk(
index_file, service_context=service_context)
mutex.release()
response = index.query(prompt + " respond in Chinese.",
text_qa_template=getQAPrompt(), similarity_top_k=15, response_mode="tree_summarize", mode=QueryMode.EMBEDDING, service_context=service_context, use_async=True)
print(response)
return str(response), 200
else:
return "index not found", 404
@ app.route("/index/stream", methods=["POST"])
def stream_index():
q = Queue()
id = request.json.get('id')
prompt = request.json.get('prompt')
app.logger.info(id)
app.logger.info(prompt)
cm = ChatOpenAI(
streaming=True,
openai_api_key=openai_key,
temperature=0,
callback_manager=CallbackManager([CustomCallBackHandler(queue=q)]), verbose=True)
lp = LLMPredictor(llm=cm)
sc = ServiceContext.from_defaults(
llm_predictor=lp, prompt_helper=prompt_helper)
index_file = os.path.join(Path('./data'), Path(id))
if os.path.exists(index_file):
mutex.acquire(timeout=10)
index = GPTSimpleVectorIndex.load_from_disk(
index_file, service_context=service_context)
mutex.release()
def query():
resp = index.query(prompt + " respond in Chinese.",
text_qa_template=getQAPrompt(), similarity_top_k=10, response_mode="tree_summarize", mode=QueryMode.EMBEDDING, service_context=sc, use_async=True)
q.put("[END]")
print(resp)
t = threading.Thread(target=query)
t.start()
def generate():
while True:
token = q.get()
if token == "[END]":
print("stream end!")
break
yield token
return Response(generate(), mimetype='text/event-stream')
else:
return "index not found", 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5601)