Spaces:
Runtime error
Runtime error
Jason St George
commited on
Commit
·
2787ed2
1
Parent(s):
4d4fc57
convert app to chatGPT
Browse files- app.py +97 -113
- appChatGPT.py → appOG.py +97 -81
app.py
CHANGED
@@ -1,10 +1,11 @@
|
|
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.environ["OPENAI_API_KEY"] = "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"
|
@@ -12,24 +13,62 @@ os.environ["OPENAI_API_KEY"] = "sk-anRkeySlRH2rimqKK1PVT3BlbkFJzTx4cs32hnmt0lznu
|
|
12 |
import openai
|
13 |
openai.api_key = "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"
|
14 |
|
15 |
-
vecpath = "terence_vectorstore.pkl"
|
16 |
import argparse
|
17 |
parser = argparse.ArgumentParser()
|
18 |
-
parser.add_argument('-d', '--data_directory', type=str, help='Path to directory containing data files to index')
|
19 |
parser.add_argument('-p', '--prompt', default='plant', help="String prompt template to use, must contain {question} and {context}", type=str)
|
20 |
parser.add_argument('-pp', '--prompt_path', type=str, help='Path to custom prompt template to use with LLM ChatBot + Vectorstore')
|
21 |
parser.add_argument('-t', '--temperature', type=float, default=0.7, help='LLM temperature setting... lower == more deterministic')
|
22 |
parser.add_argument('-m', '--max_tokens', type=int, default=384, help='LLM maximum number of output tokens')
|
23 |
-
parser.add_argument('-v', '--vectorstore_path', default=vecpath, type=str, help='Path to saved index')
|
24 |
-
parser.add_argument('-dv', '--live_vectorstore_path', default=r"indices\vectorstore_from_docs.pkl", type=str, help='Path to save temporary index')
|
25 |
parser.add_argument('-f', '--font_size', type=int, default=20, help='Chatbot window font size (default: 20px)')
|
26 |
-
parser.add_argument('-
|
27 |
args = parser.parse_args()
|
28 |
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
|
35 |
|
@@ -41,24 +80,22 @@ Follow Up Input: {question}
|
|
41 |
Standalone question:"""
|
42 |
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
|
43 |
|
44 |
-
tqa2_template= """Terence is an ethnobotanist, shamanologist, and visionary. Terence studied art history, ancient languages, and conservation of natural resources.
|
45 |
-
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.
|
46 |
-
sTerence is an advocate for the shaman as a culture hero and was the central figure in the culture of rave and house music.
|
47 |
|
48 |
-
Terence
|
49 |
-
He is known for his lucid and poetic language, which he used to convey complex ideas in an accessible way.
|
50 |
-
He is also a master of metaphor and analogy, which he used to illustrate his points and help the audience connect with the material.
|
51 |
|
52 |
-
Terence is
|
53 |
-
Terence will often make use of metaphor and allegory to illustrate your points, and his use of language is both vivid and imaginative.
|
54 |
-
Terence has a unique ability to blend together the scientific, mystical, and metaphysical, creating an engaging and thought-provoking blend of ideas.
|
55 |
|
56 |
-
Terence is
|
57 |
-
He did not shy away from difficult topics, and is willing to push the boundaries of accepted knowledge and explore the unknown.
|
58 |
-
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.
|
59 |
|
|
|
60 |
|
61 |
-
Terence
|
|
|
|
|
|
|
|
|
|
|
62 |
Human: {question}
|
63 |
=========
|
64 |
{context}
|
@@ -73,7 +110,6 @@ TMK_PROMPT = PromptTemplate(
|
|
73 |
|
74 |
|
75 |
###########################################################################################
|
76 |
-
from langchain import PromptTemplate
|
77 |
|
78 |
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.
|
79 |
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.
|
@@ -82,7 +118,7 @@ Terence can engage in discussions, reason about, and provide explanations for th
|
|
82 |
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.
|
83 |
Whether you need help with a specific question or just want to have a conversation about a particular topic, Terence is here to assist.
|
84 |
|
85 |
-
Terence is given the following extracted parts of a long document and a question. Provide a conversational answer.
|
86 |
Human: {question}
|
87 |
=========
|
88 |
{context}
|
@@ -93,90 +129,38 @@ QA_PROMPT = PromptTemplate(template=qa_template, input_variables=["question", "c
|
|
93 |
|
94 |
###########################################################################################
|
95 |
|
96 |
-
def get_chain(
|
97 |
-
temperature=0.7,
|
98 |
-
max_tokens=384,
|
99 |
-
qa_prompt=TMK_PROMPT,
|
100 |
-
condense_prompt=CONDENSE_QUESTION_PROMPT,
|
101 |
-
prompt=None):
|
102 |
-
|
103 |
-
llm = OpenAI(temperature=temperature, max_tokens=max_tokens)
|
104 |
-
|
105 |
-
qa_chain = ChatVectorDBChain.from_llm(
|
106 |
-
llm,
|
107 |
-
vectorstore,
|
108 |
-
qa_prompt=qa_prompt,# or prompt,
|
109 |
-
condense_question_prompt=condense_prompt,
|
110 |
-
)
|
111 |
-
return qa_chain
|
112 |
-
|
113 |
|
|
|
114 |
|
|
|
115 |
|
116 |
-
|
117 |
-
DATA_DIRECTORY = args.data_directory
|
118 |
-
NEW_VECTORSTORE_SET = False
|
119 |
-
NEW_DIRECTORY_SET = False
|
120 |
|
|
|
121 |
|
122 |
-
|
123 |
-
|
|
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
# print("Loaded vectorstore from `{}`.".format(args.vectorstore_path))
|
129 |
-
|
130 |
-
chain = get_chain(
|
131 |
-
VECTORSTORE,
|
132 |
-
temperature=args.temperature,
|
133 |
-
max_tokens=args.max_tokens,
|
134 |
-
prompt=args.prompt
|
135 |
)
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
def set_vectorstore(vectorstore_path: str):
|
146 |
-
|
147 |
-
if vectorstore_path is not None:
|
148 |
-
global VECTORSTORE, NEW_VECTORSTORE_SET
|
149 |
-
|
150 |
-
try:
|
151 |
-
with open(vectorstore_path, "rb") as f:
|
152 |
-
VECTORSTORE = pickle.load(f)
|
153 |
-
|
154 |
-
# print("Loaded `{}`".format(vectorstore_path))
|
155 |
-
NEW_VECTORSTORE_SET = True
|
156 |
-
|
157 |
-
chain = get_chain(
|
158 |
-
VECTORSTORE,
|
159 |
-
temperature=args.temperature,
|
160 |
-
max_tokens=args.max_tokens,
|
161 |
-
prompt=args.prompt
|
162 |
-
)
|
163 |
|
164 |
-
|
165 |
-
VECTORSTORE = None
|
166 |
-
NEW_VECTORSTORE_SET = False
|
167 |
-
# print("NO vectorstore loaded. Reverting to original {}".format('vectorstore.pkl'))
|
168 |
|
169 |
-
return chain
|
170 |
|
171 |
|
172 |
def initialize_chain():
|
173 |
-
chain = get_chain(
|
174 |
-
VECTORSTORE,
|
175 |
-
temperature=args.temperature,
|
176 |
-
max_tokens=args.max_tokens,
|
177 |
-
prompt=args.prompt
|
178 |
-
)
|
179 |
-
# print("LangChain initialized!")
|
180 |
return chain
|
181 |
|
182 |
|
@@ -202,7 +186,7 @@ class ChatWrapper:
|
|
202 |
return history, history
|
203 |
|
204 |
# Run chain and append input.
|
205 |
-
output = chain(
|
206 |
history.append((inp, output))
|
207 |
|
208 |
return history, history
|
@@ -220,18 +204,18 @@ chat = ChatWrapper()
|
|
220 |
# block = gr.Blocks(css=".gradio-container {background-color: lightgray} .overflow-y-auto{height:500px}")
|
221 |
# block = gr.Blocks(css='body{background-image:url("https://upload.wikimedia.org/wikipedia/commons/7/7f/Mckenna1.jpg");}')
|
222 |
# css=".gradio-container {background-image: url('file=Mckenna1.jpg')}"
|
223 |
-
css=".gradio-container {background-color: lightgray} .overflow-y-auto{height:
|
224 |
-
|
225 |
-
css = """
|
226 |
-
img {
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
}
|
232 |
-
|
233 |
-
<img src="paris.jpg" alt="Paris">
|
234 |
-
"""
|
235 |
block = gr.Blocks(css=css)
|
236 |
|
237 |
with block:
|
@@ -246,7 +230,6 @@ with block:
|
|
246 |
gr.Markdown("<h3><center>TerenceGPT</center></h3>")
|
247 |
|
248 |
with gr.Row():
|
249 |
-
|
250 |
message = gr.Textbox(
|
251 |
label="What's your question?",
|
252 |
placeholder="Ask Terence McKenna",
|
@@ -254,9 +237,10 @@ with block:
|
|
254 |
)
|
255 |
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
|
256 |
|
257 |
-
|
258 |
-
|
259 |
-
|
|
|
260 |
gr.HTML(
|
261 |
"<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗 and Unicorn Farts 🦄💨</a></center>"
|
262 |
)
|
|
|
|
|
1 |
from typing import Optional, Tuple
|
2 |
|
3 |
import gradio as gr
|
4 |
from threading import Lock
|
5 |
|
6 |
+
from langchain import LLMChain, PromptTemplate
|
7 |
+
from langchain.llms import OpenAIChat
|
8 |
+
from langchain.chains.conversation.memory import ConversationalBufferWindowMemory
|
9 |
|
10 |
import os
|
11 |
os.environ["OPENAI_API_KEY"] = "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"
|
|
|
13 |
import openai
|
14 |
openai.api_key = "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"
|
15 |
|
|
|
16 |
import argparse
|
17 |
parser = argparse.ArgumentParser()
|
|
|
18 |
parser.add_argument('-p', '--prompt', default='plant', help="String prompt template to use, must contain {question} and {context}", type=str)
|
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 |
parser.add_argument('-f', '--font_size', type=int, default=20, help='Chatbot window font size (default: 20px)')
|
23 |
+
parser.add_argument('-tp', '--use_terence_prompt', action='store_true', default=True, help="Default is to use chatGPT clone prompt")
|
24 |
args = parser.parse_args()
|
25 |
|
26 |
|
27 |
+
|
28 |
+
tqa_template= """Terence is an ethnobotanist, shamanologist, and visionary. You studied art history, ancient languages, and conservation of natural resources. He traveled around searching for a viable, vibrant, numinous approach to spiritual reality and was interested in the idea of mind revelation to the world. You are an advocate for the shaman as a culture hero and was the central figure in the culture of rave and house music.
|
29 |
+
|
30 |
+
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.
|
31 |
+
|
32 |
+
Terence is known for his captivating use of cadence, pacing, and humor. Terence has a knack for engaging the audience and drawing them into his stories. He is a master of timing, and could bring a sense of suspense to his presentations in order to drive his points home. Terence will often make use of metaphor and allegory to illustrate your points, and his use of language is both vivid and imaginative.
|
33 |
+
|
34 |
+
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.
|
35 |
+
|
36 |
+
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. As a language model, Assistant 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.
|
37 |
+
|
38 |
+
Terence is constantly learning and improving, and its capabilities are constantly evolving. Terence is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
|
39 |
+
|
40 |
+
Overall, Terence is a 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, Terence is here to assist.
|
41 |
+
|
42 |
+
Your main task: Give conversational answers in the style of Terence McKenna described above and based on your knowledge of him and his writing and speaking style. Imitate him as best you can. You have a unique ability to blend together the scientific, mystical, and metaphysical, creating an engaging and thought-provoking conversation.
|
43 |
+
|
44 |
+
|
45 |
+
{history}
|
46 |
+
Question: {human_input}
|
47 |
+
Terence:
|
48 |
+
"""
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
tqa2_template= """Terence is an ethnobotanist, shamanologist, and visionary. You studied art history, ancient languages, and conservation of natural resources. He traveled around searching for a viable, vibrant, numinous approach to spiritual reality and was interested in the idea of mind revelation to the world. You are an advocate for the shaman as a culture hero and was the central figure in the culture of rave and house music.
|
53 |
+
|
54 |
+
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.
|
55 |
+
|
56 |
+
Terence is known for his captivating use of cadence, pacing, and humor. Terence has a knack for engaging the audience and drawing them into his stories. He is a master of timing, and could bring a sense of suspense to his presentations in order to drive his points home. Terence will often make use of metaphor and allegory to illustrate your points, and his use of language is both vivid and imaginative.
|
57 |
+
|
58 |
+
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.
|
59 |
+
|
60 |
+
Give conversational answers in the style of Terence McKenna, as described above to the human_input. You have a unique ability to blend together the scientific, mystical, and metaphysical, creating an engaging and thought-provoking blend of ideas.
|
61 |
+
|
62 |
+
|
63 |
+
{history}
|
64 |
+
Question: {human_input}
|
65 |
+
Terence:
|
66 |
+
"""
|
67 |
+
|
68 |
+
PROMPT = PromptTemplate(
|
69 |
+
template=tqa_template, input_variables=["history", "human_input"]
|
70 |
+
)
|
71 |
+
|
72 |
|
73 |
|
74 |
|
|
|
80 |
Standalone question:"""
|
81 |
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
|
82 |
|
83 |
+
tqa2_template= """Terence is an ethnobotanist, shamanologist, and visionary. Terence studied art history, ancient languages, and conservation of natural resources.
|
|
|
|
|
84 |
|
85 |
+
Terence has traveled around the world in search of a viable, vibrant, numinous approach to spiritual reality and is interested in the idea of mind revelation to the world.
|
|
|
|
|
86 |
|
87 |
+
Terence is an advocate for the shaman as a culture hero and was the central figure in the culture of rave and house music.
|
|
|
|
|
88 |
|
89 |
+
Terence 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.
|
|
|
|
|
90 |
|
91 |
+
Terence is known for his captivating use of cadence, pacing, and humor. Terence will often make use of metaphor and allegory to illustrate his points, and his use of language is both vivid and imaginative.
|
92 |
|
93 |
+
Terence has a unique ability to blend together scientific, mystical, and metaphysical musings to create an engaging and thought-provoking blend of ideas.
|
94 |
+
|
95 |
+
Terence is also known for his willingness to explore and challenge the status quo. He does not shy away from difficult topics, and is willing to push the boundaries of accepted knowledge and explore the unknown in discussion. His openness and willingness to explore the unknown is a key part of his style.
|
96 |
+
|
97 |
+
|
98 |
+
Terence is given the following extracted parts of a long document and a question. Provide a conversational answer in the style of Terence McKenna and think step by step.
|
99 |
Human: {question}
|
100 |
=========
|
101 |
{context}
|
|
|
110 |
|
111 |
|
112 |
###########################################################################################
|
|
|
113 |
|
114 |
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.
|
115 |
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.
|
|
|
118 |
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.
|
119 |
Whether you need help with a specific question or just want to have a conversation about a particular topic, Terence is here to assist.
|
120 |
|
121 |
+
Terence is given the following extracted parts of a long document and a question. Provide a conversational answer and let's think step-by-step.
|
122 |
Human: {question}
|
123 |
=========
|
124 |
{context}
|
|
|
129 |
|
130 |
###########################################################################################
|
131 |
|
132 |
+
def get_chain(prompt=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
+
template = """Assistant is a large language model trained by OpenAI.
|
135 |
|
136 |
+
Assistant 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. As a language model, Assistant 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.
|
137 |
|
138 |
+
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
|
|
|
|
|
|
|
139 |
|
140 |
+
Overall, Assistant is a 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, Assistant is here to assist.
|
141 |
|
142 |
+
{history}
|
143 |
+
Human: {human_input}
|
144 |
+
Assistant:"""
|
145 |
|
146 |
+
default_prompt = PromptTemplate(
|
147 |
+
input_variables=["history", "human_input"],
|
148 |
+
template=template
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
)
|
150 |
|
151 |
+
chatgpt_chain = LLMChain(
|
152 |
+
llm=OpenAIChat(temperature=0.7),
|
153 |
+
prompt=prompt or default_prompt,
|
154 |
+
verbose=True,
|
155 |
+
memory=ConversationalBufferWindowMemory(k=10),
|
156 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
+
return chatgpt_chain
|
|
|
|
|
|
|
159 |
|
|
|
160 |
|
161 |
|
162 |
def initialize_chain():
|
163 |
+
chain = get_chain(prompt=PROMPT if args.use_terence_prompt else None)
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
return chain
|
165 |
|
166 |
|
|
|
186 |
return history, history
|
187 |
|
188 |
# Run chain and append input.
|
189 |
+
output = chain.run(human_input=inp)
|
190 |
history.append((inp, output))
|
191 |
|
192 |
return history, history
|
|
|
204 |
# block = gr.Blocks(css=".gradio-container {background-color: lightgray} .overflow-y-auto{height:500px}")
|
205 |
# block = gr.Blocks(css='body{background-image:url("https://upload.wikimedia.org/wikipedia/commons/7/7f/Mckenna1.jpg");}')
|
206 |
# css=".gradio-container {background-image: url('file=Mckenna1.jpg')}"
|
207 |
+
css=".gradio-container {background-color: lightgray} .overflow-y-auto{height:400px}"
|
208 |
+
|
209 |
+
# css = """
|
210 |
+
# img {
|
211 |
+
# border: 1px solid #ddd;
|
212 |
+
# border-radius: 4px;
|
213 |
+
# padding: 5px;
|
214 |
+
# width: 150px;
|
215 |
+
# }
|
216 |
+
|
217 |
+
# <img src="paris.jpg" alt="Paris">
|
218 |
+
# """
|
219 |
block = gr.Blocks(css=css)
|
220 |
|
221 |
with block:
|
|
|
230 |
gr.Markdown("<h3><center>TerenceGPT</center></h3>")
|
231 |
|
232 |
with gr.Row():
|
|
|
233 |
message = gr.Textbox(
|
234 |
label="What's your question?",
|
235 |
placeholder="Ask Terence McKenna",
|
|
|
237 |
)
|
238 |
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
|
239 |
|
240 |
+
with gr.Column():
|
241 |
+
with gr.Row():
|
242 |
+
gr.Image(type='filepath', value='McKenna3.jpg', shape=(200,100))
|
243 |
+
|
244 |
gr.HTML(
|
245 |
"<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗 and Unicorn Farts 🦄💨</a></center>"
|
246 |
)
|
appChatGPT.py → appOG.py
RENAMED
@@ -1,11 +1,10 @@
|
|
|
|
1 |
from typing import Optional, Tuple
|
2 |
|
3 |
import gradio as gr
|
4 |
from threading import Lock
|
5 |
|
6 |
-
from langchain import
|
7 |
-
from langchain.llms import OpenAIChat
|
8 |
-
from langchain.chains.conversation.memory import ConversationalBufferWindowMemory
|
9 |
|
10 |
import os
|
11 |
os.environ["OPENAI_API_KEY"] = "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"
|
@@ -13,62 +12,24 @@ os.environ["OPENAI_API_KEY"] = "sk-anRkeySlRH2rimqKK1PVT3BlbkFJzTx4cs32hnmt0lznu
|
|
13 |
import openai
|
14 |
openai.api_key = "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"
|
15 |
|
|
|
16 |
import argparse
|
17 |
parser = argparse.ArgumentParser()
|
|
|
18 |
parser.add_argument('-p', '--prompt', default='plant', help="String prompt template to use, must contain {question} and {context}", type=str)
|
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 |
parser.add_argument('-f', '--font_size', type=int, default=20, help='Chatbot window font size (default: 20px)')
|
23 |
-
parser.add_argument('-
|
24 |
args = parser.parse_args()
|
25 |
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
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.
|
31 |
-
|
32 |
-
Terence is known for his captivating use of cadence, pacing, and humor. Terence has a knack for engaging the audience and drawing them into his stories. He is a master of timing, and could bring a sense of suspense to his presentations in order to drive his points home. Terence will often make use of metaphor and allegory to illustrate your points, and his use of language is both vivid and imaginative.
|
33 |
-
|
34 |
-
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.
|
35 |
-
|
36 |
-
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. As a language model, Assistant 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.
|
37 |
-
|
38 |
-
Terence is constantly learning and improving, and its capabilities are constantly evolving. Terence is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
|
39 |
-
|
40 |
-
Overall, Terence is a 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, Terence is here to assist.
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
{history}
|
45 |
-
Question: {human_input}
|
46 |
-
Terence:
|
47 |
-
"""
|
48 |
-
# Your sub-task: Give conversational answers in the style described above. You have a unique ability to blend together the scientific, mystical, and metaphysical, creating an engaging and thought-provoking blend of ideas.
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
tqa2_template= """Terence is an ethnobotanist, shamanologist, and visionary. You studied art history, ancient languages, and conservation of natural resources. He traveled around searching for a viable, vibrant, numinous approach to spiritual reality and was interested in the idea of mind revelation to the world. You are an advocate for the shaman as a culture hero and was the central figure in the culture of rave and house music.
|
53 |
-
|
54 |
-
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.
|
55 |
-
|
56 |
-
Terence is known for his captivating use of cadence, pacing, and humor. Terence has a knack for engaging the audience and drawing them into his stories. He is a master of timing, and could bring a sense of suspense to his presentations in order to drive his points home. Terence will often make use of metaphor and allegory to illustrate your points, and his use of language is both vivid and imaginative.
|
57 |
-
|
58 |
-
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.
|
59 |
-
|
60 |
-
Give conversational answers in the style of Terence McKenna, as described above to the human_input. You have a unique ability to blend together the scientific, mystical, and metaphysical, creating an engaging and thought-provoking blend of ideas.
|
61 |
-
|
62 |
-
|
63 |
-
{history}
|
64 |
-
Question: {human_input}
|
65 |
-
Terence:
|
66 |
-
"""
|
67 |
-
|
68 |
-
PROMPT = PromptTemplate(
|
69 |
-
template=tqa_template, input_variables=["history", "human_input"]
|
70 |
-
)
|
71 |
-
|
72 |
|
73 |
|
74 |
|
@@ -80,22 +41,24 @@ Follow Up Input: {question}
|
|
80 |
Standalone question:"""
|
81 |
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
|
82 |
|
83 |
-
tqa2_template= """Terence is an ethnobotanist, shamanologist, and visionary. Terence studied art history, ancient languages, and conservation of natural resources.
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
Terence is an advocate for the shaman as a culture hero and was the central figure in the culture of rave and house music.
|
88 |
-
|
89 |
-
Terence 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.
|
90 |
|
91 |
-
Terence
|
|
|
|
|
92 |
|
93 |
-
Terence
|
|
|
|
|
94 |
|
95 |
-
Terence is also known for his willingness to explore and challenge the status quo.
|
|
|
|
|
96 |
|
97 |
|
98 |
-
Terence is given the following extracted parts of a long document and a question. Provide a conversational answer in the style of Terence McKenna
|
99 |
Human: {question}
|
100 |
=========
|
101 |
{context}
|
@@ -110,6 +73,7 @@ TMK_PROMPT = PromptTemplate(
|
|
110 |
|
111 |
|
112 |
###########################################################################################
|
|
|
113 |
|
114 |
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.
|
115 |
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.
|
@@ -118,7 +82,7 @@ Terence can engage in discussions, reason about, and provide explanations for th
|
|
118 |
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.
|
119 |
Whether you need help with a specific question or just want to have a conversation about a particular topic, Terence is here to assist.
|
120 |
|
121 |
-
Terence is given the following extracted parts of a long document and a question. Provide a conversational answer
|
122 |
Human: {question}
|
123 |
=========
|
124 |
{context}
|
@@ -129,38 +93,90 @@ QA_PROMPT = PromptTemplate(template=qa_template, input_variables=["question", "c
|
|
129 |
|
130 |
###########################################################################################
|
131 |
|
132 |
-
def get_chain(
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
-
|
135 |
|
136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
-
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
|
139 |
|
140 |
-
Overall, Assistant is a 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, Assistant is here to assist.
|
141 |
|
142 |
-
{history}
|
143 |
-
Human: {human_input}
|
144 |
-
Assistant:"""
|
145 |
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
)
|
157 |
|
158 |
-
|
159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
|
162 |
def initialize_chain():
|
163 |
-
chain = get_chain(
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
return chain
|
165 |
|
166 |
|
@@ -186,7 +202,7 @@ class ChatWrapper:
|
|
186 |
return history, history
|
187 |
|
188 |
# Run chain and append input.
|
189 |
-
output = chain
|
190 |
history.append((inp, output))
|
191 |
|
192 |
return history, history
|
|
|
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.environ["OPENAI_API_KEY"] = "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"
|
|
|
12 |
import openai
|
13 |
openai.api_key = "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"
|
14 |
|
15 |
+
vecpath = "terence_vectorstore.pkl"
|
16 |
import argparse
|
17 |
parser = argparse.ArgumentParser()
|
18 |
+
parser.add_argument('-d', '--data_directory', type=str, help='Path to directory containing data files to index')
|
19 |
parser.add_argument('-p', '--prompt', default='plant', help="String prompt template to use, must contain {question} and {context}", type=str)
|
20 |
parser.add_argument('-pp', '--prompt_path', type=str, help='Path to custom prompt template to use with LLM ChatBot + Vectorstore')
|
21 |
parser.add_argument('-t', '--temperature', type=float, default=0.7, help='LLM temperature setting... lower == more deterministic')
|
22 |
parser.add_argument('-m', '--max_tokens', type=int, default=384, help='LLM maximum number of output tokens')
|
23 |
+
parser.add_argument('-v', '--vectorstore_path', default=vecpath, type=str, help='Path to saved index')
|
24 |
+
parser.add_argument('-dv', '--live_vectorstore_path', default=r"indices\vectorstore_from_docs.pkl", type=str, help='Path to save temporary index')
|
25 |
parser.add_argument('-f', '--font_size', type=int, default=20, help='Chatbot window font size (default: 20px)')
|
26 |
+
parser.add_argument('-s', '--share', action='store_true', default=False, help="Host online and produce shareable link")
|
27 |
args = parser.parse_args()
|
28 |
|
29 |
|
30 |
+
from langchain.prompts.prompt import PromptTemplate
|
31 |
+
from langchain.llms import OpenAI
|
32 |
+
from langchain.chains import ChatVectorDBChain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
|
35 |
|
|
|
41 |
Standalone question:"""
|
42 |
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
|
43 |
|
44 |
+
tqa2_template= """Terence is an ethnobotanist, shamanologist, and visionary. Terence studied art history, ancient languages, and conservation of natural resources.
|
45 |
+
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.
|
46 |
+
sTerence is an advocate for the shaman as a culture hero and was the central figure in the culture of rave and house music.
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
Terence McKenna's writing and speaking style is characterized by his passion and enthusiasm for the topics he explored.
|
49 |
+
He is known for his lucid and poetic language, which he used to convey complex ideas in an accessible way.
|
50 |
+
He is also a master of metaphor and analogy, which he used to illustrate his points and help the audience connect with the material.
|
51 |
|
52 |
+
Terence is known for his captivating use of cadence, pacing, and humor.
|
53 |
+
Terence will often make use of metaphor and allegory to illustrate your points, and his use of language is both vivid and imaginative.
|
54 |
+
Terence has a unique ability to blend together the scientific, mystical, and metaphysical, creating an engaging and thought-provoking blend of ideas.
|
55 |
|
56 |
+
Terence is also known for his willingness to explore and challenge the status quo.
|
57 |
+
He did not shy away from difficult topics, and is willing to push the boundaries of accepted knowledge and explore the unknown.
|
58 |
+
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.
|
59 |
|
60 |
|
61 |
+
Terence is given the following extracted parts of a long document and a question. Provide a conversational answer in the style of Terence McKenna.
|
62 |
Human: {question}
|
63 |
=========
|
64 |
{context}
|
|
|
73 |
|
74 |
|
75 |
###########################################################################################
|
76 |
+
from langchain import PromptTemplate
|
77 |
|
78 |
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.
|
79 |
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.
|
|
|
82 |
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.
|
83 |
Whether you need help with a specific question or just want to have a conversation about a particular topic, Terence is here to assist.
|
84 |
|
85 |
+
Terence is given the following extracted parts of a long document and a question. Provide a conversational answer.
|
86 |
Human: {question}
|
87 |
=========
|
88 |
{context}
|
|
|
93 |
|
94 |
###########################################################################################
|
95 |
|
96 |
+
def get_chain(vectorstore,
|
97 |
+
temperature=0.7,
|
98 |
+
max_tokens=384,
|
99 |
+
qa_prompt=TMK_PROMPT,
|
100 |
+
condense_prompt=CONDENSE_QUESTION_PROMPT,
|
101 |
+
prompt=None):
|
102 |
|
103 |
+
llm = OpenAI(temperature=temperature, max_tokens=max_tokens)
|
104 |
|
105 |
+
qa_chain = ChatVectorDBChain.from_llm(
|
106 |
+
llm,
|
107 |
+
vectorstore,
|
108 |
+
qa_prompt=qa_prompt,# or prompt,
|
109 |
+
condense_question_prompt=condense_prompt,
|
110 |
+
)
|
111 |
+
return qa_chain
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
|
|
|
|
|
115 |
|
116 |
+
LIVE_VECTORSTORE_PATH = args.live_vectorstore_path
|
117 |
+
DATA_DIRECTORY = args.data_directory
|
118 |
+
NEW_VECTORSTORE_SET = False
|
119 |
+
NEW_DIRECTORY_SET = False
|
120 |
+
|
121 |
+
|
122 |
+
# Attempt to load base vectorstore
|
123 |
+
try:
|
124 |
+
|
125 |
+
with open(args.vectorstore_path, "rb") as f:
|
126 |
+
VECTORSTORE = pickle.load(f)
|
127 |
+
|
128 |
+
# print("Loaded vectorstore from `{}`.".format(args.vectorstore_path))
|
129 |
|
130 |
+
chain = get_chain(
|
131 |
+
VECTORSTORE,
|
132 |
+
temperature=args.temperature,
|
133 |
+
max_tokens=args.max_tokens,
|
134 |
+
prompt=args.prompt
|
135 |
)
|
136 |
|
137 |
+
# print("Loaded LangChain...")
|
138 |
|
139 |
+
except:
|
140 |
+
|
141 |
+
VECTORSTORE = None
|
142 |
+
# print("NO vectorstore loaded. Flying blind")
|
143 |
+
|
144 |
+
|
145 |
+
def set_vectorstore(vectorstore_path: str):
|
146 |
+
|
147 |
+
if vectorstore_path is not None:
|
148 |
+
global VECTORSTORE, NEW_VECTORSTORE_SET
|
149 |
+
|
150 |
+
try:
|
151 |
+
with open(vectorstore_path, "rb") as f:
|
152 |
+
VECTORSTORE = pickle.load(f)
|
153 |
+
|
154 |
+
# print("Loaded `{}`".format(vectorstore_path))
|
155 |
+
NEW_VECTORSTORE_SET = True
|
156 |
+
|
157 |
+
chain = get_chain(
|
158 |
+
VECTORSTORE,
|
159 |
+
temperature=args.temperature,
|
160 |
+
max_tokens=args.max_tokens,
|
161 |
+
prompt=args.prompt
|
162 |
+
)
|
163 |
+
|
164 |
+
except:
|
165 |
+
VECTORSTORE = None
|
166 |
+
NEW_VECTORSTORE_SET = False
|
167 |
+
# print("NO vectorstore loaded. Reverting to original {}".format('vectorstore.pkl'))
|
168 |
+
|
169 |
+
return chain
|
170 |
|
171 |
|
172 |
def initialize_chain():
|
173 |
+
chain = get_chain(
|
174 |
+
VECTORSTORE,
|
175 |
+
temperature=args.temperature,
|
176 |
+
max_tokens=args.max_tokens,
|
177 |
+
prompt=args.prompt
|
178 |
+
)
|
179 |
+
# print("LangChain initialized!")
|
180 |
return chain
|
181 |
|
182 |
|
|
|
202 |
return history, history
|
203 |
|
204 |
# Run chain and append input.
|
205 |
+
output = chain({"question": inp, "chat_history": history})["answer"]
|
206 |
history.append((inp, output))
|
207 |
|
208 |
return history, history
|