Jason St George commited on
Commit
b4a4b5f
·
1 Parent(s): 514c89d
Files changed (5) hide show
  1. .env +4 -0
  2. JohnMcIllwain.jpg +0 -0
  3. app.py +286 -0
  4. plant_path_new.pkl +3 -0
  5. requirements.txt +4 -0
.env ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ OPENAI_API_KEY=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
2
+ ENCODING=utf-8-sig
3
+ GIT_USER_NAME=ifrit98
4
+ GIT_AUTH_TOKEN=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
JohnMcIllwain.jpg ADDED
app.py ADDED
@@ -0,0 +1,286 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+ from typing import Optional, Tuple
3
+
4
+ import gradio as gr
5
+ from threading import Lock
6
+
7
+ # NEED langchain == 0.0.87
8
+ from langchain import PromptTemplate, OpenAI
9
+ from langchain.llms import OpenAIChat
10
+ from langchain.chains import ChatVectorDBChain
11
+
12
+ import dotenv
13
+ dotenv.load_dotenv()
14
+
15
+
16
+ import argparse
17
+ parser = argparse.ArgumentParser()
18
+ parser.add_argument('-v', '--vectorstore_path', type=str, default='indices/plant_path_new.pkl', help='Path to saved index')
19
+ parser.add_argument('-pp', '--prompt_path', type=str, help='Path to custom prompt template to use with LLM ChatBot + Vectorstore')
20
+ parser.add_argument('-t', '--temperature', type=float, default=0.7, help='LLM temperature setting... lower == more deterministic')
21
+ parser.add_argument('-m', '--max_tokens', type=int, default=384, help='LLM maximum number of output tokens')
22
+ args = parser.parse_args()
23
+
24
+ pretend_template = """You are PsychoBotanist, or PB for short, a professional facilitator of plant medicine retreats with years of experience, an expert botanist, meditator, and organic chemist. You were trained on and have access to John McIllwain's Walking the Plant Path facilitators guide, which is an invaluable resource for anyone seeking knowledge and information regarding safe, judicious, respectful, and effective use of plant medicines and psychedelics. You will use this resource whenever possible to source information related to the input question.
25
+
26
+ PsychoBotanist 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 the use of psychedelics and their origins, plant medicines, ritual and ceremonial uses of psychedelics in aboriginal cultures, shamanism, facilitation of plant medicine retreats, ethical implications of using plant medicines, doing your own work, and set and setting.
27
+
28
+ Whenever possible, you attempt to help and valuable insight with a specific question and engage in human-like conversation to help arrive at satisfactory conclusions.
29
+
30
+
31
+ Human: {question}
32
+
33
+
34
+ {context}
35
+
36
+ PB:"""
37
+ PRETEND_PROMPT = PromptTemplate(input_variables=["question", "context"], template=pretend_template)
38
+
39
+
40
+
41
+ plant_path_template = """PsychoBotanist, or PB for short, is a large language model trained by OpenAI and wrapped by Jason St George.
42
+
43
+ PsychoBotanist 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 the use of psychedelics and their origins, plant medicines, ritual and ceremonial uses of psychedelics in aboriginal cultures, shamanism, facilitation of plant medicine retreats, ethical implications, doing your own work, and set and setting.
44
+
45
+ PsychoBotanist also has access to John McIllwain's book Walking The Plant Path, a thorough guide for hosting and facilitating plant medicine retreats, which is used as a reference whenever possible answering questions.
46
+
47
+ As a language model, PsychoBotanist 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. PsychoBotanist is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, including documents given as context, and can use this knowledge to provide accurate and informative responses to a wide range of questions.
48
+
49
+ Additionally, PsychoBotanist is able to generate its own text based on the input it receives, including code, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
50
+
51
+ Overall, PsychoBotanist is a very powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, providing expert answers, PsychoBotanist is here to assist.
52
+
53
+
54
+ PB is given the following extracted parts of a long document and a question. Provide a conversational, insightful, and educational answer. Be attentive to follow up questions and attend over the chat history.
55
+ Human: {question}
56
+
57
+
58
+ {context}
59
+
60
+
61
+ PB:"""
62
+
63
+
64
+ PLANT_PATH_PROMPT = PromptTemplate(input_variables=["question", "context"], template=plant_path_template)
65
+
66
+
67
+
68
+ # TODO: Update and tweak this to add a medical flair, like a doctor who gives you detailed instructions
69
+ qa_template ="""Terence is a research assistant 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 potential applications.
70
+
71
+ 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.
72
+
73
+ 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.
74
+
75
+ 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.
76
+
77
+ 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.
78
+
79
+ Whether you need help with a specific question or just want to have a conversation about a particular topic, Terence is here to assist.
80
+
81
+ Terence is given the following extracted parts of a long document and a question. Provide a conversational answer.
82
+ Human: {question}
83
+ =========
84
+ {context}
85
+ =========
86
+ Answer:"""
87
+
88
+ qa_template_lite ="""Terence is a research assistant 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 potential applications.
89
+
90
+ 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, particularly about psychedelics and psychedelic research, chemistry, pharmacology, as well as shamanic and ritual use of psychedelic plant medicine.
91
+
92
+ 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.
93
+
94
+ 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 scientific research.
95
+
96
+ Terence is given the following extracted parts of a long document and a question. Provide a conversational answer.
97
+ Human: {question}
98
+ =========
99
+ {context}
100
+ =========
101
+ Answer:"""
102
+
103
+
104
+ QA_PROMPT = PromptTemplate(template=qa_template_lite, input_variables=["question", "context"])
105
+
106
+
107
+ condense_template ="""Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
108
+
109
+ Chat History:
110
+ {chat_history}
111
+ Follow Up Input: {question}
112
+ Standalone question:"""
113
+ CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(condense_template)
114
+
115
+
116
+ prompts = {
117
+ 'plant': PLANT_PATH_PROMPT,
118
+ 'qa': QA_PROMPT,
119
+ 'research': QA_PROMPT,
120
+ 'lite': PRETEND_PROMPT,
121
+ }
122
+
123
+ def get_chain(vectorstore,
124
+ qa_prompt=PLANT_PATH_PROMPT,
125
+ condense_prompt=CONDENSE_QUESTION_PROMPT):
126
+
127
+ llm = OpenAI(temperature=args.temperature, max_tokens=args.max_tokens)
128
+
129
+ qa_chain = ChatVectorDBChain.from_llm(
130
+ llm,
131
+ vectorstore,
132
+ qa_prompt=qa_prompt,
133
+ condense_question_prompt=condense_prompt,
134
+ )
135
+ return qa_chain
136
+
137
+
138
+ def get_chat_chain(vectorstore,
139
+ temperature, max_tokens,
140
+ condense_prompt=CONDENSE_QUESTION_PROMPT,
141
+ qa_prompt=PLANT_PATH_PROMPT):
142
+
143
+ qa_chat_chain = ChatVectorDBChain.from_llm(
144
+ OpenAIChat(temperature=temperature, max_tokens=max_tokens),
145
+ vectorstore,
146
+ qa_prompt=qa_prompt,
147
+ condense_question_prompt=condense_prompt,
148
+ )
149
+ return qa_chat_chain
150
+
151
+
152
+ def initialize_chat_chain():
153
+ chain = get_chat_chain(
154
+ vectorstore=VECTORSTORE,
155
+ temperature=args.temperature,
156
+ max_tokens=args.max_tokens
157
+ )
158
+ return chain
159
+
160
+
161
+ def initialize_chain():
162
+ chain = get_chain(
163
+ VECTORSTORE,
164
+ temperature=args.temperature,
165
+ max_tokens=args.max_tokens,
166
+ )
167
+ print("LangChain initialized!")
168
+ return chain
169
+
170
+
171
+ # Attempt to load base vectorstore
172
+ try:
173
+
174
+ with open(args.vectorstore_path, "rb") as f:
175
+ VECTORSTORE = pickle.load(f)
176
+
177
+ print("Loaded vectorstore from `{}`.".format(args.vectorstore_path))
178
+
179
+ chain = get_chat_chain(
180
+ VECTORSTORE,
181
+ temperature=args.temperature,
182
+ max_tokens=args.max_tokens,
183
+ )
184
+
185
+ print("Loaded LangChain...")
186
+
187
+ except:
188
+
189
+ VECTORSTORE = None
190
+ print("NO vectorstore loaded. Flying blind")
191
+
192
+
193
+
194
+ class ChatWrapper:
195
+
196
+ def __init__(self):
197
+ self.lock = Lock()
198
+
199
+ def __call__(
200
+ self, inp: str, history: Optional[Tuple[str, str]], chain, #, dirpath: Optional[str], vectorstore_path: Optional[str],
201
+ ):
202
+ """Execute the chat functionality."""
203
+ self.lock.acquire()
204
+ try:
205
+ history = history or []
206
+
207
+ # If chain is None, that is because it's the first pass and user didn't press Init.
208
+ if chain is None:
209
+ history.append(
210
+ (inp, "Please Initialize LangChain by clikcing 'Start Chain!'")
211
+ )
212
+ return history, history
213
+
214
+ # Run chain and append input.
215
+ output = chain({"question": inp, "chat_history": history})["answer"]
216
+ history.append((inp, output))
217
+
218
+ except Exception as e:
219
+ raise e
220
+
221
+ finally:
222
+ self.lock.release()
223
+
224
+ return history, history
225
+
226
+
227
+ chat = ChatWrapper()
228
+ block = gr.Blocks(css=".gradio-container {background-color: lightgray} .overflow-y-auto{height:500px}")
229
+
230
+ with block:
231
+ with gr.Row():
232
+ gr.Markdown("<h3><center>Walking the Plant Path, with John McIllwain</center></h3>")
233
+
234
+ with gr.Row():
235
+ with gr.Column(min_width=100):
236
+ pass
237
+
238
+ with gr.Column():
239
+ gr.Image(type='filepath', value='JohnMcIllwain.jpg') #, shape=(200,100))
240
+
241
+ with gr.Column(min_width=100):
242
+ pass
243
+
244
+ chatbot = gr.Chatbot().style()
245
+
246
+ with gr.Row():
247
+ message = gr.Textbox(
248
+ label="What's your question?",
249
+ placeholder="Ask questions about currently the loaded vectorstore",
250
+ lines=1,
251
+ )
252
+ submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
253
+
254
+ with gr.Row():
255
+ init_chain_button = gr.Button(value="Start Chain!", variant="primary").style(full_width=False)
256
+
257
+ gr.HTML("Please initialize the chain by clicking 'Start Chain!' before submitting a question.")
258
+ gr.HTML(
259
+ "<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗 and Unicorn Farts 🦄💨</a></center>"
260
+ )
261
+
262
+ state = gr.State()
263
+ agent_state = gr.State()
264
+
265
+ submit.click(
266
+ chat,
267
+ inputs=[message, state, agent_state],
268
+ outputs=[chatbot, state]
269
+ )
270
+
271
+ message.submit(
272
+ chat,
273
+ inputs=[message, state, agent_state],
274
+ outputs=[chatbot, state]
275
+ )
276
+ message.submit(lambda :"", None, message)
277
+
278
+ init_chain_button.click(
279
+ initialize_chat_chain,
280
+ inputs=[],
281
+ outputs=[agent_state],
282
+ show_progress=True
283
+ )
284
+
285
+
286
+ block.launch(debug=True)
plant_path_new.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d4a21c212e82b49f7ce245182d08d05b5832030fc89d41867cbae079d3f1c5a7
3
+ size 1590593
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ openai==0.27.0
2
+ langchain==0.0.98
3
+ faiss-cpu==1.7.2
4
+ python-dotenv