mckplus commited on
Commit
75b116b
1 Parent(s): f20077c

Update DocuChat.py

Browse files
Files changed (1) hide show
  1. DocuChat.py +51 -52
DocuChat.py CHANGED
@@ -8,60 +8,59 @@ from langchain.embeddings import OpenAIEmbeddings
8
  from langchain.vectorstores import Chroma
9
  import panel as pn
10
 
11
- # Set global sizing mode
12
- pn.config.sizing_mode = 'stretch_width'
13
-
14
- # Panel extension
15
- pn.extension()
16
-
17
- file_input = pn.widgets.FileInput(height=45)
18
- openaikey = pn.widgets.PasswordInput(value="", placeholder="Enter your OpenAI API Key here...", height=45)
19
- chatbox = pn.widgets.ChatBox(height=300, primary_name="User")
20
 
21
- def remove_empty_lines(text):
22
- lines = re.split(r'\r\n|\r|\n', text)
23
- return '\n'.join([line.strip() for line in lines if line.strip()])
24
 
25
- def qa(file, query):
26
- loader = PyPDFLoader(file)
27
- documents = loader.load()
28
- text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
29
- texts = text_splitter.split_documents(documents)
30
- embeddings = OpenAIEmbeddings()
31
- db = Chroma.from_documents(texts, embeddings)
32
- retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 3})
33
- qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="stuff", retriever=retriever, return_source_documents=True)
34
- result = qa({"query": query})
35
- return result['result']
36
 
37
- def qa_result(event):
38
- print("Debugging: Event triggered") # Debugging Line 0
39
- if len(event.new) > len(event.old):
40
- os.environ["OPENAI_API_KEY"] = openaikey.value
41
- if file_input.value is not None:
42
- file_input.save("/.cache/temp.pdf")
43
- prompt_text = remove_empty_lines(event.new[-1][chatbox.primary_name])
44
- print("Debugging: Prompt text:", prompt_text) # Debugging Line 1
45
- print("Debugging: Chat input from user:", event.new[-1][chatbox.primary_name]) # Debugging Line 1.1
46
- if prompt_text:
47
- print("Debugging: Calling Langchain") # Debugging Line 2
48
- result = qa(file="/.cache/temp.pdf", query=prompt_text)
49
- print("Debugging: Result from Langchain:", result) # Debugging Line 3
50
- # Append the AI's response to the chatbox
51
- new_value = chatbox.value.copy() # Copy the existing value
52
- new_value.append({"AI": result}) # Append the AI's response
53
- chatbox.value = new_value # Assign the new value
54
- print("Debugging: Appended result to chatbox") # Debugging Line 4
55
 
 
 
 
 
 
 
 
 
 
 
56
 
57
- chatbox.param.watch(qa_result, 'value')
58
-
59
- layout = pn.Column(
60
- pn.pane.Markdown("""
61
- # DocuChat
62
- AI-Powered Query Engine for Document Insights (powered by LangChain & OpenAI)
63
- ...
64
- """),
65
- pn.Row(file_input, openaikey), chatbox
66
- ).servable()
67
- print("Debugging: Application is ready") # Debugging Line 4
 
8
  from langchain.vectorstores import Chroma
9
  import panel as pn
10
 
11
+ class LangChainConversation:
12
+ def __init__(self):
13
+ self.file_input = pn.widgets.FileInput(height=45)
14
+ self.openaikey = pn.widgets.PasswordInput(value="", placeholder="Enter your OpenAI API Key here...", height=45)
15
+ self.chatbox = pn.widgets.ChatBox(height=300, primary_name="User")
16
+ self.chatbox.param.watch(self.qa_result, 'value')
 
 
 
17
 
18
+ def remove_empty_lines(self, text):
19
+ lines = re.split(r'\r\n|\r|\n', text)
20
+ return '\n'.join([line.strip() for line in lines if line.strip()])
21
 
22
+ def qa(self, file, query):
23
+ loader = PyPDFLoader(file)
24
+ documents = loader.load()
25
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
26
+ texts = text_splitter.split_documents(documents)
27
+ embeddings = OpenAIEmbeddings()
28
+ db = Chroma.from_documents(texts, embeddings)
29
+ retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 3})
30
+ qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="stuff", retriever=retriever, return_source_documents=True)
31
+ result = qa({"query": query})
32
+ return result['result']
33
 
34
+ def qa_result(self, event):
35
+ print("Debugging: Event triggered")
36
+ if len(event.new) > len(event.old):
37
+ os.environ["OPENAI_API_KEY"] = self.openaikey.value
38
+ if self.file_input.value is not None:
39
+ self.file_input.save("/.cache/temp.pdf")
40
+ prompt_text = self.remove_empty_lines(event.new[-1][self.chatbox.primary_name])
41
+ print("Debugging: Prompt text:", prompt_text)
42
+ if prompt_text:
43
+ print("Debugging: Calling Langchain")
44
+ result = self.qa(file="/.cache/temp.pdf", query=prompt_text)
45
+ print("Debugging: Result from Langchain:", result)
46
+ # Append the AI's response to the chatbox
47
+ new_value = self.chatbox.value.copy()
48
+ new_value.append({"AI": result})
49
+ self.chatbox.value = new_value
50
+ print("Debugging: Appended result to chatbox")
 
51
 
52
+ def view(self):
53
+ layout = pn.Column(
54
+ pn.pane.Markdown("""
55
+ # DocuChat
56
+ AI-Powered Query Engine for Document Insights (powered by LangChain & OpenAI)
57
+ ...
58
+ """),
59
+ pn.Row(self.file_input, self.openaikey), self.chatbox
60
+ )
61
+ return layout.servable()
62
 
63
+ pn.config.sizing_mode = 'stretch_width'
64
+ pn.extension()
65
+ chat = LangChainConversation()
66
+ chat.view()