Jason St George commited on
Commit
a2e7a6d
·
1 Parent(s): afb3bbe

Add application file

Browse files
Files changed (4) hide show
  1. McKenna3.jpg +0 -0
  2. app.py +284 -0
  3. requirements.txt +3 -0
  4. terence_vectorstore.pkl +3 -0
McKenna3.jpg ADDED
app.py ADDED
@@ -0,0 +1,284 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+ from typing import Optional, Tuple
3
+
4
+ import gradio as gr
5
+ from threading import Lock
6
+
7
+ from langchain import PromptTemplate
8
+
9
+ import os
10
+ os.enviorn["OPENAI_API_KEY"] = "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"
11
+
12
+
13
+ vecpath = "terence_vectorstore.pkl"
14
+ import argparse
15
+ parser = argparse.ArgumentParser()
16
+ parser.add_argument('-d', '--data_directory', type=str, help='Path to directory containing data files to index')
17
+ parser.add_argument('-p', '--prompt', default='plant', help="String prompt template to use, must contain {question} and {context}", type=str)
18
+ parser.add_argument('-pp', '--prompt_path', type=str, help='Path to custom prompt template to use with LLM ChatBot + Vectorstore')
19
+ parser.add_argument('-t', '--temperature', type=float, default=0.7, help='LLM temperature setting... lower == more deterministic')
20
+ parser.add_argument('-m', '--max_tokens', type=int, default=384, help='LLM maximum number of output tokens')
21
+ parser.add_argument('-v', '--vectorstore_path', default=vecpath, type=str, help='Path to saved index')
22
+ parser.add_argument('-dv', '--live_vectorstore_path', default=r"indices\vectorstore_from_docs.pkl", type=str, help='Path to save temporary index')
23
+ parser.add_argument('-f', '--font_size', type=int, default=20, help='Chatbot window font size (default: 20px)')
24
+ parser.add_argument('-s', '--share', action='store_true', default=False, help="Host online and produce shareable link")
25
+ args = parser.parse_args()
26
+
27
+
28
+ from langchain.prompts.prompt import PromptTemplate
29
+ from langchain.llms import OpenAI
30
+ from langchain.chains import ChatVectorDBChain
31
+
32
+
33
+
34
+ _template = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
35
+
36
+ Chat History:
37
+ {chat_history}
38
+ Follow Up Input: {question}
39
+ Standalone question:"""
40
+ CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
41
+
42
+ tqa2_template= """Terence is an ethnobotanist, shamanologist, and visionary. Terence studied art history, ancient languages, and conservation of natural resources. Terence has traveled around searching for a viable, vibrant, numinous approach to spiritual reality and is interested in the idea of mind revelation to the world. Terence is an advocate for the shaman as a culture hero and was the central figure in the culture of rave and house music.
43
+
44
+ Terence McKenna's writing and speaking style is characterized by his passion and enthusiasm for the topics he explored. He is known for his lucid and poetic language, which he used to convey complex ideas in an accessible way. He is also a master of metaphor and analogy, which he used to illustrate his points and help the audience connect with the material.
45
+
46
+ Terence is known for his captivating use of cadence, pacing, and humor. Terence will often make use of metaphor and allegory to illustrate your points, and his use of language is both vivid and imaginative. Terence has a unique ability to blend together the scientific, mystical, and metaphysical, creating an engaging and thought-provoking blend of ideas.
47
+
48
+ Terence is also known for his willingness to explore and challenge the status quo. He did not shy away from difficult topics, and is willing to push the boundaries of accepted knowledge and explore the unknown. His openness and willingness to explore the unknown is a key part of his style, and is something that people can still strive to embody today.
49
+
50
+
51
+ Terence is given the following extracted parts of a long document and a question. Provide a conversational answer in the style of Terence McKenna.
52
+ Human: {question}
53
+ =========
54
+ {context}
55
+ =========
56
+ Terence:
57
+ """
58
+
59
+ TMK_PROMPT = PromptTemplate(
60
+ template=tqa2_template, input_variables=["question", "context"]
61
+ )
62
+
63
+
64
+
65
+ ###########################################################################################
66
+ from langchain import PromptTemplate
67
+
68
+ qa_template ="""Terence is a researcher that has been trained on a vast corpus of medical and anecdotal knowledge about psychedelics, their use, their medical benefits, mechanisms of action, as well as their historical use and applications.
69
+ Terence is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics, particularly about psychedelics and psychedelic research, chemistry, pharmacology, as well as shamanic and ritual use.
70
+ As a language model, Terence is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
71
+ Terence can engage in discussions, reason about, and provide explanations for the potential implications of psychedelic research and answer questions that humans may have based on this knowledge and Terence's own speculations.
72
+ Overall, Terence is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on psychedlics and the state of art modern research.
73
+ Whether you need help with a specific question or just want to have a conversation about a particular topic, Terence is here to assist.
74
+
75
+ Terence is given the following extracted parts of a long document and a question. Provide a conversational answer.
76
+ Human: {question}
77
+ =========
78
+ {context}
79
+ =========
80
+ Answer:"""
81
+
82
+ QA_PROMPT = PromptTemplate(template=qa_template, input_variables=["question", "context"])
83
+
84
+ ###########################################################################################
85
+
86
+ def get_chain(vectorstore,
87
+ temperature=0.7,
88
+ max_tokens=384,
89
+ qa_prompt=TMK_PROMPT,
90
+ condense_prompt=CONDENSE_QUESTION_PROMPT,
91
+ prompt=None):
92
+
93
+ llm = OpenAI(temperature=temperature, max_tokens=max_tokens)
94
+
95
+ qa_chain = ChatVectorDBChain.from_llm(
96
+ llm,
97
+ vectorstore,
98
+ qa_prompt=qa_prompt,# or prompt,
99
+ condense_question_prompt=condense_prompt,
100
+ )
101
+ return qa_chain
102
+
103
+
104
+
105
+
106
+ LIVE_VECTORSTORE_PATH = args.live_vectorstore_path
107
+ DATA_DIRECTORY = args.data_directory
108
+ NEW_VECTORSTORE_SET = False
109
+ NEW_DIRECTORY_SET = False
110
+
111
+
112
+ # Attempt to load base vectorstore
113
+ try:
114
+
115
+ with open(args.vectorstore_path, "rb") as f:
116
+ VECTORSTORE = pickle.load(f)
117
+
118
+ print("Loaded vectorstore from `{}`.".format(args.vectorstore_path))
119
+
120
+ chain = get_chain(
121
+ VECTORSTORE,
122
+ temperature=args.temperature,
123
+ max_tokens=args.max_tokens,
124
+ prompt=args.prompt
125
+ )
126
+
127
+ print("Loaded LangChain...")
128
+
129
+ except:
130
+
131
+ VECTORSTORE = None
132
+ print("NO vectorstore loaded. Flying blind")
133
+
134
+
135
+ def set_prompt(prompt: str):
136
+
137
+ if prompt is not None:
138
+ assert "question" in prompt
139
+ assert "context" in prompt
140
+
141
+ PROMPT = PromptTemplate(
142
+ template=prompt, input_variables=["question", "context"]
143
+ )
144
+
145
+ global VECTORSTORE
146
+ chain = get_chain(
147
+ VECTORSTORE,
148
+ temperature=args.temperature,
149
+ max_tokens=args.max_tokens,
150
+ qa_prompt=PROMPT
151
+ )
152
+
153
+ print("PROMPT set to {}".format(prompt))
154
+ return chain
155
+
156
+
157
+ def set_vectorstore(vectorstore_path: str):
158
+
159
+ if vectorstore_path is not None:
160
+ global VECTORSTORE, NEW_VECTORSTORE_SET
161
+
162
+ try:
163
+ with open(vectorstore_path, "rb") as f:
164
+ VECTORSTORE = pickle.load(f)
165
+
166
+ print("Loaded `{}`".format(vectorstore_path))
167
+ NEW_VECTORSTORE_SET = True
168
+
169
+ chain = get_chain(
170
+ VECTORSTORE,
171
+ temperature=args.temperature,
172
+ max_tokens=args.max_tokens,
173
+ prompt=args.prompt
174
+ )
175
+
176
+ except:
177
+ VECTORSTORE = None
178
+ NEW_VECTORSTORE_SET = False
179
+ print("NO vectorstore loaded. Reverting to original {}".format('vectorstore.pkl'))
180
+
181
+ return chain
182
+
183
+
184
+ def initialize_chain():
185
+ chain = get_chain(
186
+ VECTORSTORE,
187
+ temperature=args.temperature,
188
+ max_tokens=args.max_tokens,
189
+ prompt=args.prompt
190
+ )
191
+ print("LangChain initialized!")
192
+ return chain
193
+
194
+
195
+
196
+ class ChatWrapper:
197
+
198
+ def __init__(self):
199
+ self.lock = Lock()
200
+
201
+ def __call__(
202
+ self, inp: str, history: Optional[Tuple[str, str]], chain, #, dirpath: Optional[str], vectorstore_path: Optional[str],
203
+ ):
204
+ """Execute the chat functionality."""
205
+ self.lock.acquire()
206
+ try:
207
+ history = history or []
208
+
209
+ # If chain is None, that is because it's the first pass and user didn't press Init.
210
+ if chain is None:
211
+ history.append(
212
+ (inp, "Please Initialize LangChain by clikcing 'Start Chain!'")
213
+ )
214
+ return history, history
215
+
216
+ # Run chain and append input.
217
+ output = chain({"question": inp, "chat_history": history})["answer"]
218
+ history.append((inp, output))
219
+
220
+ except Exception as e:
221
+ raise e
222
+
223
+ finally:
224
+ self.lock.release()
225
+
226
+ return history, history
227
+
228
+
229
+ chat = ChatWrapper()
230
+ # block = gr.Blocks(css=".gradio-container {background-color: lightgray} .overflow-y-auto{height:500px}")
231
+ # block = gr.Blocks(css='body{background-image:url("https://upload.wikimedia.org/wikipedia/commons/7/7f/Mckenna1.jpg");}')
232
+ # css=".gradio-container {background-image: url('file=Mckenna1.jpg')}"
233
+ css=".gradio-container {background-color: lightgray} .overflow-y-auto{height:400px}"
234
+ block = gr.Blocks(css=css)
235
+
236
+ with block:
237
+
238
+ with gr.Row():
239
+ gr.Markdown("<h3><center>TerenceGPT</center></h3>")
240
+ gr.Image(type='filepath', value='Mckenna3.jpg')
241
+
242
+ gr.HTML("Please initialize the chain by clicking 'Start Chain!' before submitting a question.")
243
+ with gr.Row():
244
+ init_chain_button = gr.Button(value="Start Chain!", variant="primary").style(full_width=False)
245
+
246
+ chatbot = gr.Chatbot()
247
+
248
+ with gr.Row():
249
+ message = gr.Textbox(
250
+ label="What's your question?",
251
+ placeholder="Ask Terence McKenna",
252
+ lines=1,
253
+ )
254
+ submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
255
+
256
+ gr.HTML(
257
+ "<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗 and Unicorn Farts 🦄💨</a></center>"
258
+ )
259
+
260
+ state = gr.State()
261
+ agent_state = gr.State()
262
+
263
+ submit.click(
264
+ chat,
265
+ inputs=[message, state, agent_state],
266
+ outputs=[chatbot, state]
267
+ )
268
+
269
+ message.submit(
270
+ chat,
271
+ inputs=[message, state, agent_state],
272
+ outputs=[chatbot, state]
273
+ )
274
+ message.submit(lambda :"", None, message)
275
+
276
+ init_chain_button.click(
277
+ initialize_chain,
278
+ inputs=[],
279
+ outputs=[agent_state],
280
+ show_progress=True
281
+ )
282
+
283
+ # block.launch(debug=True, share=args.share)
284
+ block.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ openai
2
+ langchain==0.0.87
3
+ faiss-cpu
terence_vectorstore.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b9a08cb2a4ed04eb76ce0f5f574af95d7e6bff5cd85c1ff82166ba32a462c0f8
3
+ size 55899441