-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchat_app.py
601 lines (472 loc) · 23 KB
/
chat_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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
import os, time, shutil, subprocess
import params
if params.set_visible_devices:
os.environ["CUDA_VISIBLE_DEVICES"] = params.visible_devices
import llms, prompts, bot_tools
import torch
from langchain_core.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain_community.vectorstores import Chroma
from langchain.agents import Tool, AgentType, initialize_agent
from langchain_community.document_loaders import OnlinePDFLoader
from langchain_community.llms import HuggingFacePipeline
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain import hub
from langchain.agents import AgentExecutor, create_json_chat_agent
from langchain_core.messages import AIMessage, HumanMessage
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import START, MessagesState, StateGraph
from langchain_community.document_loaders.generic import GenericLoader
from langchain_community.document_loaders.parsers import LanguageParser
from langchain_text_splitters import Language
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain.chains import create_history_aware_retriever, create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
import gradio as gr
from gradio import ChatMessage
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
#Setup device
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Device:", device)
print("Using %d GPUs" %torch.cuda.device_count())
#Cleanups
gr.close_all() #Close any existing open ports'
def clean_pdf_paths():
if os.path.exists(params.pdf_path): #Remove any PDF embeddings
shutil.rmtree(params.pdf_path, ignore_errors=True)
if os.path.exists(params.pdf_text_path): #Remove any raw PDF text
shutil.rmtree(params.pdf_text_path, ignore_errors=True)
os.mkdir(params.pdf_text_path)
def init_local_llm(params):
#Create a local tokenizer copy the first time
if os.path.isdir(params.tokenizer_path):
tokenizer = AutoTokenizer.from_pretrained(params.tokenizer_path)
else:
tokenizer = AutoTokenizer.from_pretrained(params.model_name)
os.mkdir(params.tokenizer_path)
tokenizer.save_pretrained(params.tokenizer_path)
#Setup pipeline
model = AutoModelForCausalLM.from_pretrained(params.model_name,
device_map="auto",
torch_dtype=torch.bfloat16)#, load_in_8bit=True)
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_length=params.seq_length,
temperature=0,
top_p=0.95,
repetition_penalty=1.2
)
#Setup LLM chain with memory and context
return HuggingFacePipeline(pipeline=pipe)
#Setup embedding model
def init_local_embeddings(params):
return HuggingFaceEmbeddings(model_name=params.embedding_model_name)
"""
===========================
Chat Functionality
===========================
"""
def get_model():
return params.anl_llm_model
def change_model(model_id):
params.anl_llm_model = model_id
class Chat():
def __init__(self, llm, embedding, doc_store):
self.llm = llm
self.embedding = embedding
self.doc_store = doc_store
self.is_PDF = False #Flag to use NER over right set of docs. Changed in update_pdf_docstore
def _init_chain(self):
template = """The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
Context:
{context}
Current conversation:
{history}
Human: {input}
AI:"""
PROMPT = PromptTemplate(
input_variables=["history", "input", "context"], template=template
)
memory = ConversationBufferWindowMemory(memory_key="history",
input_key = "input",
k=6)
conversation = LLMChain(
prompt=PROMPT,
llm=self.llm,
verbose=True,
memory=memory
)
return conversation
#Method to find text with highest likely context
def _get_context(self, query, doc_store):
# Context retrieval from embeddings
docs = doc_store.similarity_search_with_score(query, k=params.N_hits)
#Get context strings
context=""
print ("Context hits found", len(docs))
for i in range(min(params.N_hits, len(docs))):
if docs[i][1]<params.similarity_cutoff:
context += docs[i][0].page_content +"\n"
print (i+1, len(docs[i][0].page_content), docs[i][1], docs[i][0].page_content)
else:
print ("\n\nIGNORING CONTENT of score %.2f" %docs[i][1],len(docs[i][0].page_content), docs[i][0].page_content)
#Context retrieval from NER
ners = llms.ner_hits(query) #Get unique named entities of > some length from query
ner_hits = []
#Set path from where to get NER context hits
if self.is_PDF:
doc_path = params.pdf_text_path
print("Getting NER hits from PDF context")
else:
doc_path = params.doc_path_root
clean_pdf_paths() #Make sure PDF folders are clean to avoid context leak
print("Getting NER hits from facility context")
for ner in ners: #Grep NEs from raw text
try:
hit = subprocess.check_output("grep -r -i -h '%s' %s/" %(ner, doc_path), shell=True).decode()
hits = hit.split("\n") #split all the grep results into indiv strings
ner_hits.extend(hits)
except subprocess.CalledProcessError as err:
if err.returncode > 1:
print ("No hits found for: ", ner)
continue
#Exit values: 0 One or more lines were selected. 1 No lines were selected. >1 An error occurred.
#print ("NERs", ner_hits)
ner_hits.sort(key=len, reverse=True) #Sort by length of hits
#print ("Sorted NERs", ner_hits)
for i in range(min(params.N_NER_hits, len(ner_hits))):
print ("Selected NER hit %d : " %i, ner_hits[i])
context += ner_hits[i]
return context
def generate_response(self, history, debug_output, convo_state, doc_state = None):
user_message = history[-1]['content'] #History is list of tuple list. E.g. : [['Hi', 'Test'], ['Hello again', '']]
all_user_messages = [x['content'] for x in history]
if convo_state is None:
convo_state = self._init_chain()
if self.doc_store is not None:
context = ""
for message in all_user_messages:
context += self._get_context(message, self.doc_store)
elif doc_state is not None:
context = ""
for message in all_user_messages:
context += self._get_context(message, doc_state)
else:
context = ""
if debug_output:
inputs = convo_state.prep_inputs({'input': user_message, 'context':context})
prompt = convo_state.prep_prompts([inputs])[0][0].text
bot_message = convo_state.predict(input=user_message, context=context)
if debug_output:
bot_message = f'---Prompt---\n\n {prompt} \n\n---Response---\n\n {bot_message}'
print(history)
print(convo_state)
history.append(
ChatMessage(role='assistant', content=bot_message)
)
return history, convo_state
def add_message(self, user_message, history):
history.append(
ChatMessage(role='user', content=user_message)
)
return "", history
def clear_memory(self, convo_state):
if convo_state is not None:
convo_state.memory.clear()
return convo_state, None
else:
return None, None
class PDFChat(Chat):
def update_pdf_docstore(self, pdf_docs, pdf_state):
all_pdfs = []
for pdf_doc in pdf_docs:
loader = OnlinePDFLoader(pdf_doc.name)
documents = loader.load()
text_splitter = llms.init_text_splitter()
texts = text_splitter.split_documents(documents)
all_pdfs += texts
llms.write_list(all_pdfs) #Write raw split text to file
embed_path = params.pdf_path
db = Chroma.from_documents(all_pdfs, self.embedding, #metadatas=[{"source": str(i)} for i in range(len(all_pdfs))],
persist_directory=embed_path) #Compute embeddings over pdf using embedding model specified in params file
return "PDF Ready", db
class ToolChat(Chat):
"""
Implements an agentexector in a chat context. The agentexecutor is called in a fundimentally
differnet way than the other chains, so custom implementaiton for much of the class.
"""
def _init_chain(self):
"""
tools = [
dfrac_tools.DiffractometerAIO(params.spec_init)
]
"""
# TODO: CHANGE CREATION TYPE
tools = [bot_tools.lattice_tool, bot_tools.diffractometer_tool]
memory = ConversationBufferWindowMemory(memory_key="chat_history", k=6)
agent = create_json_chat_agent(
tools=tools,
llm=self.llm,
prompt=prompts.json_tool_prompt)
agent_executor = AgentExecutor(
agent=agent, tools=tools, handle_parsing_errors=True,
max_iterations = 15,
verbose=True
)
self.memory = memory
self.conversation = agent_executor
return memory, agent_executor
def generate_response(self, history, debug_output):
user_message = history[-1]['content'] #History is list of tuple list. E.g. : [['Hi', 'Test'], ['Hello again', '']]
# Convert to langchain history
lang_hist = []
for message in history:
if message['role'] == 'user':
lang_hist.append(HumanMessage(content=message['content']))
elif message['role'] == 'assistant':
lang_hist.append(AIMessage(content=message['content']))
else:
raise ValueError(f"Unknown role in history {history}, {message['role']}. Add way to resolve.")
#raise ValueError(f'Unknown role in history {history}, {message['role']}. Add way to resolve.')
# TODO: Implement debug output for langchain agents. Might have to use a callback?
print(f'User input: {user_message}')
response = self.conversation.invoke(
{
"input": user_message,
"chat_history": lang_hist,
}
)
bot_message = response['output']
#Pass user message and get context and pass to model
history.append(
ChatMessage(role='assistant', content=bot_message)
)
return history
class S26ExecChat(ToolChat):
"""
Implements an agentexector in a chat context. The agentexecutor is called in a fundimentally
differnet way than the other chains, so custom implementaiton for much of the class.
"""
def _init_chain(self):
"""
tools = [
dfrac_tools.DiffractometerAIO(params.spec_init)
]
"""
tools = [bot_tools.exec_cmd_tool] #, bot_tools.wolfram_tool
memory = ConversationBufferWindowMemory(memory_key="chat_history", k=6)
agent = create_json_chat_agent(
tools=tools,
llm=self.llm,
prompt=prompts.json_tool_prompt)
agent_executor = AgentExecutor(
agent=agent, tools=tools, handle_parsing_errors=True,
max_iterations = 15,
verbose=True
)
self.memory = memory
self.conversation = agent_executor
return memory, agent_executor
class PolybotExecChat(ToolChat):
def _init_chain(self):
tools = [bot_tools.exec_polybot_tool, bot_tools.exec_polybot_lint_tool]
memory = ConversationBufferWindowMemory(memory_key="chat_history", k=7)
agent = create_json_chat_agent(
tools=tools,
llm=self.llm,
prompt=prompts.json_tool_prompt)
agent_executor = AgentExecutor(
agent=agent, tools=tools, handle_parsing_errors=True,
max_iterations = 15,
verbose=True
)
self.memory = memory
self.conversation = agent_executor
return memory, agent_executor
"""
===========================
UI/Frontend
===========================
"""
def init_chat_layout():
chatbot = gr.Chatbot(show_label=False, elem_id="chatbot", type='messages',
show_copy_button=True)#.style(height="500")
with gr.Row():
with gr.Column(scale=8):
msg = gr.Textbox(show_label = False,
placeholder="Send a message with Enter")
with gr.Column(scale=2, min_width=0):
submit_btn = gr.Button("Send")
clear = gr.Button("Clear")
disp_prompt = gr.Checkbox(label='Debug: Display Prompt')
return chatbot, msg, clear, disp_prompt, submit_btn
def main_interface(params, llm, embeddings):
#Page layout
with gr.Blocks(css="footer {visibility: hidden}", title="APS ChatBot") as demo:
#Header
gr.Markdown("""
## Hi! I am CALMS, a Scientific AI Assistant
"""
)
gr.Markdown("""
* Use the General Chat to AMA. E.g. write some code for you, create a recipe from ingredients etc.
* Use the Facility Q&A to ask me questions specific to the DOE facilities, I will look up answers using the documentation my trainers have provided me.
* Use the Document Q&A to ask me questions about a document you provide.
""")
if llm_type.huggingface:
model_descr = f"local model: {params.model_name}"
elif llm_type.openai:
model_descr = f"ANL Hosted Model [{params.anl_llm_model}] (OpenAI)"
else:
model_descr = "Error! Unknown model"
if embed_type.huggingface:
embed_descr = f"Local model: {params.embedding_model_name}"
elif embed_type.openai:
embed_descr = f"ANL Hosted Model (OpenAI)"
else:
embed_descr = "Error! Unknown model"
with gr.Row():
openai_model_dd = gr.Dropdown(
choices=['gpt35', 'gpt35large', 'gpt4', 'gpt4large', 'gpt4turbo', 'gpto1preview'],
label='openai_model',
value=get_model,
interactive=True,
scale=1
)
openai_model_dd.change(change_model, inputs=[openai_model_dd])
gr.Markdown('')#, scale=5)
gr.Markdown(f"Context hits: {params.N_hits}\nNER hits: {params.N_NER_hits}")
#General chat tab
with gr.Tab("General Chat"):
chatbot, msg, clear, disp_prompt, submit_btn = init_chat_layout() #Init layout
chat_general = Chat(llm, embeddings, doc_store=None)
chat_general_state = gr.State(None)
msg.submit(chat_general.add_message, [msg, chatbot], [msg, chatbot], queue=False).then(
chat_general.generate_response, [chatbot, disp_prompt, chat_general_state], [chatbot, chat_general_state] #Use bot without context
)
submit_btn.click(chat_general.add_message, [msg, chatbot], [msg, chatbot], queue=False).then(
chat_general.generate_response, [chatbot, disp_prompt, chat_general_state], [chatbot, chat_general_state] #Use bot without context
)
clear.click(chat_general.clear_memory, [chat_general_state], [chat_general_state, chatbot])
#APS Q&A tab
with gr.Tab("Facility Q&A"):
chatbot, msg, clear, disp_prompt, submit_btn = init_chat_layout() #Init layout
facility_qa_docstore = llms.init_facility_qa(embeddings, params)
chat_qa = Chat(llm, embeddings, doc_store=facility_qa_docstore)
chat_qa_state = gr.State(None)
#Pass an empty string to context when don't want domain specific context
msg.submit(chat_qa.add_message, [msg, chatbot], [msg, chatbot], queue=False).then(
chat_qa.generate_response, [chatbot, disp_prompt, chat_qa_state], [chatbot, chat_qa_state] #Use bot with context
)
submit_btn.click(chat_qa.add_message, [msg, chatbot], [msg, chatbot], queue=False).then(
chat_qa.generate_response, [chatbot, disp_prompt, chat_qa_state], [chatbot, chat_qa_state] #Use bot with context
)
clear.click(chat_qa.clear_memory, [chat_qa_state], [chat_qa_state, chatbot])
#Document Q&A tab
with gr.Tab("Document Q&A"):
gr.Markdown("""
"""
)
title = """
<div style="text-align: center;max-width: 700px;">
<h1>Chat with PDF</h1>
<p style="text-align: center;">Upload one or more PDFs from your computer, click the "Load PDFs" button, <br />
when everything is ready, you can start asking questions about the pdf</p>
<a style="display:inline-block; margin-left: 1em"></a>
</div>
"""
with gr.Column(elem_id="col-container"):
gr.HTML(title)
with gr.Column():
pdf_doc = gr.File(label="Load PDFs", file_types=['.pdf'], type="filepath", file_count = 'multiple')
with gr.Row():
langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
load_pdf = gr.Button("Load PDF")
chatbot, msg, clear, disp_prompt, submit_btn = init_chat_layout() #Init layout
chat_pdf = PDFChat(llm, embeddings, doc_store=None)
chat_pdf_state = gr.State(None)
pdf_store_state = gr.State(None)
load_pdf.click(chat_pdf.update_pdf_docstore, inputs=[pdf_doc, pdf_store_state], outputs=[langchain_status, pdf_store_state], queue=False)
msg.submit(chat_pdf.add_message, [msg, chatbot], [msg, chatbot], queue=False).then(
chat_pdf.generate_response, [chatbot, disp_prompt, chat_pdf_state, pdf_store_state], [chatbot, chat_pdf_state] #Use bot with context
)
submit_btn.click(chat_pdf.add_message, [msg, chatbot], [msg, chatbot], queue=False).then(
chat_pdf.generate_response, [chatbot, disp_prompt, chat_pdf_state, pdf_store_state], [chatbot, chat_pdf_state] #Use bot with context
)
clear.click(chat_general.clear_memory, [chat_pdf_state], [chat_pdf_state, chatbot])
with gr.Tab("S26 Agent"):
chatbot, msg, clear, disp_prompt_tool, submit_btn = init_chat_layout() #Init layout
tool_qa = S26ExecChat(llm, embeddings, None)
tool_qa._init_chain()
#Pass an empty string to context when don't want domain specific context
msg.submit(tool_qa.add_message, [msg, chatbot], [msg, chatbot], queue=False).then(
tool_qa.generate_response, [chatbot, disp_prompt_tool], [chatbot] #Use bot with context
)
submit_btn.click(tool_qa.add_message, [msg, chatbot], [msg, chatbot], queue=False).then(
tool_qa.generate_response, [chatbot, disp_prompt_tool], [chatbot] #Use bot with context
)
clear.click(lambda: tool_qa.memory.clear(), None, chatbot, queue=False)
with gr.Tab("Polybot Exec"):
chatbot, msg, clear, disp_prompt_tool, submit_btn = init_chat_layout() #Init layout
polybot_exec = PolybotExecChat(llm, embeddings, None)
polybot_exec._init_chain()
#Pass an empty string to context when don't want domain specific context
msg.submit(polybot_exec.add_message, [msg, chatbot], [msg, chatbot], queue=False).then(
polybot_exec.generate_response, [chatbot, disp_prompt_tool], chatbot #Use bot with context
)
submit_btn.click(polybot_exec.add_message, [msg, chatbot], [msg, chatbot], queue=False).then(
polybot_exec.generate_response, [chatbot, disp_prompt_tool], chatbot #Use bot with context
)
clear.click(lambda: polybot_exec.memory.clear(), None, chatbot, queue=False)
with gr.Tab("Tips & Tricks"):
gr.Markdown("""
1. If I don't give a satisfactory answer, try rephrasing your question. For e.g. 'Can I do high energy diffraction at the APS?' instead of 'Where can I do high energy diffraction at the APS?
2. Avoid using acronyms, e.g. say coherent diffraction imaging instead of CDI.
3. CALMS is an acronym for Context-Aware Language Model for Science.
"""
)
#Footer
gr.Markdown("""
##### Made with ❤️ for 🧑🔬 by:
Mathew J. Cherukara, Michael Prince @ APS<br>
Henry Chan, Aikaterini Vriza, Tao Zhou @ CNM<br>
Varuni K. Sastry @ DSL/ALCF
"""
)
demo.queue()
demo.launch(server_name="0.0.0.0", server_port=params.port)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True) #One of mutuallu exclusive args is required
group.add_argument('-hf', '--huggingface', action='store_true', help='Open-source model')
group.add_argument('-o', '--openai', action='store_true', help='OpenAI Model')
llm_type = parser.parse_args()
print(llm_type)
if llm_type.openai:
llm = llms.AnlLLM(params)
if params.anl_user == "":
print("The ANL OpenAI API user parameter (anl_user) must be set in params.py! Exiting.")
exit()
elif llm_type.huggingface:
llm = init_local_llm(params)
else:
raise AssertionError("LLM type must be huggingface or openai")
#Embedding model parameters
embed_type = llm_type # Can be different from llm_type
#Embedding paths
if embed_type.huggingface:
params.embed_path = '%s/%s' %(params.base_path, params.embedding_model_name)
embeddings = init_local_embeddings(params)
elif embed_type.openai:
if params.init_docs:
input('WARNING: WILL INIT ALL DOCS WITH OPENAI EMBEDS. Press enter to continue')
params.embed_path = f"{params.base_path}/anl_openai"
embeddings = llms.ANLEmbeddingModel(params)
params.pdf_path = '%s/pdf' %params.embed_path
clean_pdf_paths() #Clear any PDF embeds and NER text
main_interface(params, llm, embeddings)