drewdan commited on
Commit
09ee545
1 Parent(s): 450df15

Create QA_Chatbot_BM

Browse files
Files changed (1) hide show
  1. QA_Chatbot_BM +122 -0
QA_Chatbot_BM ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # install packages
2
+ !pip install langchain openai chromadb tiktoken pypdf panel
3
+
4
+ # import packages
5
+ import os
6
+ from langchain.chains import RetrievalQA
7
+ from langchain.llms import OpenAI
8
+ from langchain.document_loaders import TextLoader
9
+ from langchain.document_loaders import PyPDFLoader
10
+ from langchain.indexes import VectorstoreIndexCreator
11
+ from langchain.text_splitter import CharacterTextSplitter
12
+ from langchain.embeddings import OpenAIEmbeddings
13
+ from langchain.vectorstores import Chroma
14
+ import panel as pn
15
+ import tempfile
16
+
17
+ # sets Panel Framework
18
+ pn.extension('texteditor', template="bootstap", sizing_mode='stretch_width')
19
+ pn.state.template.param.update(
20
+ main_max_width="690px",
21
+ header_background="#F08080",
22
+ )
23
+
24
+ # set widgets
25
+ file_input = pn.widgets.FileInput(width=300)
26
+
27
+ openaikey = pn.widgets.PasswordInput(
28
+ value="", placeholder="Enter your OpenAI API key here...", width=300
29
+ )
30
+ prompt = pn.widgets.TextEditor(
31
+ value="", placeholder="Enter your questions here...", height=160, toolbar=False
32
+ )
33
+ run_button = pn.widgets.Button(name="Run!")
34
+
35
+ select_k = pn.widgets.IntSlider(
36
+ name="Number of relevant chunks", start=1, end=5, step=1, value=2
37
+ )
38
+ select_chain_type = pn.widgets.RadioButtonGroup(
39
+ name='Chain type',
40
+ options =['stuff', 'map_reduce', "refine", "map_rerank"]
41
+ )
42
+ widgets = pn.Row(
43
+ pn.Column(prompt, run_button, margin=5),
44
+ pn.Card(
45
+ "Chain type:",
46
+ pn.Column(select_chain_type, select_k),
47
+ title="Advanced settings", margin=10
48
+ ), width=600
49
+ )
50
+
51
+ # define the question answering function
52
+ def qa(file, query, chain_type, k):
53
+ # load document
54
+ loader = PyPDFLoader(file)
55
+ documents = loader.load()
56
+ # split the documents into chunks
57
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
58
+ texts = text_splitter.split_documents(documents)
59
+ # select which embeddings we want to use
60
+ embeddings = OpenAIEmbeddings()
61
+ # create the vectorstore to use as the index
62
+ db = Chroma.from_documents(texts, embeddings)
63
+ # expose this index in a retriever interface
64
+ retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": k})
65
+ # to create a chain to answer questions
66
+ qa = RetreivalQA.from_chain_type(
67
+ llm = OPenAI(), chain_type=chain_type, retriever=retriever, return_source_documents=True)
68
+ result = qa({"query": query})
69
+ print(result['result'])
70
+ return result
71
+
72
+ # store all Panel objects in a list
73
+ convos = []
74
+
75
+ def qa_result(_):
76
+ os.environ["OPENAI_API_KEY"] = openaikey.value
77
+
78
+ # save pdf file to a temp file
79
+ if file_input.value is not None:
80
+ file_input.save(temp.pdf)
81
+
82
+ prompt_text = prompt.value
83
+ if prompt_text:
84
+ result = qa(file="temp.pdf", query=prompt_text, chain_type=select_chain_type.value, k=select_k.value)
85
+ convos.extend([
86
+ pn.Row(
87
+ pn.panel("\U0001F60A", width=10),
88
+ prompt_text,
89
+ width=600
90
+ ),
91
+ pn.Row(
92
+ pn.panel(\"U0001F916", width=10),
93
+ pn.Column(
94
+ result["result"],
95
+ "Relevant source text:",
96
+ pn.pane.Markdown('\n--------------------------------------------------------------------\n'.join(doc.page_content for docu in result["source_documents"]))
97
+ )
98
+ )
99
+ ])
100
+ return pn.Column(*convos, margin=15, width=575, min_height=400)
101
+
102
+ # bind run button with the qa_result function
103
+
104
+ qa_interactive = pn.panel(
105
+ pn.bind(qa_result, run_button),
106
+ loading_indicator=True,
107
+ )
108
+
109
+ output pn.WidgetBox('*Output will show up here:*', qa_interactive, width=630, scroll=True)
110
+
111
+ # define the layout
112
+
113
+ pn.Column(
114
+ pn.pane.Markdown("""
115
+ ## \U0001F60A! Question Answering with your PDF file
116
+
117
+ 1) Upload a PDF. 2) Enter OpenAI API key. This costs $. Set up billing at [OpenAI](https://platform.openai.com/account). 3) Type a question and click "Run".
118
+ """),
119
+ pn.Row(file_input, openaikey)
120
+ output,
121
+ widgets
122
+ ).servable()