Spaces:
Runtime error
Runtime error
Jason St George
commited on
Commit
·
4d4fc57
1
Parent(s):
aa6fd31
update to chatGPT api
Browse files- app.py +34 -36
- appChatGPT.py +272 -0
- appv2.py +286 -0
- terence_vectorstore.pkl +2 -2
app.py
CHANGED
@@ -41,16 +41,24 @@ 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 |
|
46 |
-
Terence McKenna's writing and speaking style is characterized by his passion and enthusiasm for the topics he explored.
|
|
|
|
|
47 |
|
48 |
-
Terence is known for his captivating use of cadence, pacing, and humor.
|
|
|
|
|
49 |
|
50 |
-
Terence is also known for his willingness to explore and challenge the status quo.
|
|
|
|
|
51 |
|
52 |
|
53 |
-
Terence is given the following extracted parts of a long document and a question. Provide a conversational answer in the style of Terence McKenna.
|
54 |
Human: {question}
|
55 |
=========
|
56 |
{context}
|
@@ -134,28 +142,6 @@ except:
|
|
134 |
# print("NO vectorstore loaded. Flying blind")
|
135 |
|
136 |
|
137 |
-
def set_prompt(prompt: str):
|
138 |
-
|
139 |
-
if prompt is not None:
|
140 |
-
assert "question" in prompt
|
141 |
-
assert "context" in prompt
|
142 |
-
|
143 |
-
PROMPT = PromptTemplate(
|
144 |
-
template=prompt, input_variables=["question", "context"]
|
145 |
-
)
|
146 |
-
|
147 |
-
global VECTORSTORE
|
148 |
-
chain = get_chain(
|
149 |
-
VECTORSTORE,
|
150 |
-
temperature=args.temperature,
|
151 |
-
max_tokens=args.max_tokens,
|
152 |
-
qa_prompt=PROMPT
|
153 |
-
)
|
154 |
-
|
155 |
-
# print("PROMPT set to {}".format(prompt))
|
156 |
-
return chain
|
157 |
-
|
158 |
-
|
159 |
def set_vectorstore(vectorstore_path: str):
|
160 |
|
161 |
if vectorstore_path is not None:
|
@@ -235,16 +221,29 @@ chat = ChatWrapper()
|
|
235 |
# block = gr.Blocks(css='body{background-image:url("https://upload.wikimedia.org/wikipedia/commons/7/7f/Mckenna1.jpg");}')
|
236 |
# css=".gradio-container {background-image: url('file=Mckenna1.jpg')}"
|
237 |
css=".gradio-container {background-color: lightgray} .overflow-y-auto{height:500px}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
block = gr.Blocks(css=css)
|
239 |
|
240 |
with block:
|
|
|
|
|
|
|
|
|
|
|
241 |
chatbot = gr.Chatbot()
|
242 |
-
|
243 |
-
# with gr.Row():
|
244 |
-
# gr.Markdown("<h3><center>TerenceGPT</center></h3>")
|
245 |
|
246 |
-
|
247 |
-
|
248 |
|
249 |
with gr.Row():
|
250 |
|
@@ -255,10 +254,9 @@ with block:
|
|
255 |
)
|
256 |
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
|
257 |
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
gr.HTML(
|
263 |
"<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗 and Unicorn Farts 🦄💨</a></center>"
|
264 |
)
|
|
|
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}
|
|
|
142 |
# print("NO vectorstore loaded. Flying blind")
|
143 |
|
144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
def set_vectorstore(vectorstore_path: str):
|
146 |
|
147 |
if vectorstore_path is not None:
|
|
|
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:500px}"
|
224 |
+
|
225 |
+
css = """
|
226 |
+
img {
|
227 |
+
border: 1px solid #ddd;
|
228 |
+
border-radius: 4px;
|
229 |
+
padding: 5px;
|
230 |
+
width: 150px;
|
231 |
+
}
|
232 |
+
|
233 |
+
<img src="paris.jpg" alt="Paris">
|
234 |
+
"""
|
235 |
block = gr.Blocks(css=css)
|
236 |
|
237 |
with block:
|
238 |
+
|
239 |
+
gr.HTML("Please initialize the chain by clicking 'Start Chain!' before submitting a question.")
|
240 |
+
with gr.Row():
|
241 |
+
init_chain_button = gr.Button(value="Start Chain!", variant="primary").style(full_width=False)
|
242 |
+
|
243 |
chatbot = gr.Chatbot()
|
|
|
|
|
|
|
244 |
|
245 |
+
with gr.Row():
|
246 |
+
gr.Markdown("<h3><center>TerenceGPT</center></h3>")
|
247 |
|
248 |
with gr.Row():
|
249 |
|
|
|
254 |
)
|
255 |
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
|
256 |
|
257 |
+
with gr.Column():
|
258 |
+
with gr.Row():
|
259 |
+
gr.Image(type='filepath', value='McKenna3.jpg', shape=(200,100))
|
|
|
260 |
gr.HTML(
|
261 |
"<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗 and Unicorn Farts 🦄💨</a></center>"
|
262 |
)
|
appChatGPT.py
ADDED
@@ -0,0 +1,272 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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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"
|
12 |
+
|
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=False, 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 |
+
|
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 |
+
|
75 |
+
_template = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
|
76 |
+
|
77 |
+
Chat History:
|
78 |
+
{chat_history}
|
79 |
+
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 |
+
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}
|
102 |
+
=========
|
103 |
+
Terence:
|
104 |
+
"""
|
105 |
+
|
106 |
+
TMK_PROMPT = PromptTemplate(
|
107 |
+
template=tqa2_template, input_variables=["question", "context"]
|
108 |
+
)
|
109 |
+
|
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.
|
116 |
+
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.
|
117 |
+
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.
|
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}
|
125 |
+
=========
|
126 |
+
Answer:"""
|
127 |
+
|
128 |
+
QA_PROMPT = PromptTemplate(template=qa_template, input_variables=["question", "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 |
+
|
167 |
+
|
168 |
+
class ChatWrapper:
|
169 |
+
|
170 |
+
def __init__(self):
|
171 |
+
self.lock = Lock()
|
172 |
+
|
173 |
+
def __call__(
|
174 |
+
self, inp: str, history: Optional[Tuple[str, str]], chain, #, dirpath: Optional[str], vectorstore_path: Optional[str],
|
175 |
+
):
|
176 |
+
"""Execute the chat functionality."""
|
177 |
+
self.lock.acquire()
|
178 |
+
try:
|
179 |
+
history = history or []
|
180 |
+
|
181 |
+
# If chain is None, that is because it's the first pass and user didn't press Init.
|
182 |
+
if chain is None:
|
183 |
+
history.append(
|
184 |
+
(inp, "Please Initialize LangChain by clikcing 'Start Chain!'")
|
185 |
+
)
|
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
|
193 |
+
|
194 |
+
except Exception as e:
|
195 |
+
raise e
|
196 |
+
|
197 |
+
finally:
|
198 |
+
self.lock.release()
|
199 |
+
|
200 |
+
return history, history
|
201 |
+
|
202 |
+
|
203 |
+
chat = ChatWrapper()
|
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:500px}"
|
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:
|
222 |
+
|
223 |
+
gr.HTML("Please initialize the chain by clicking 'Start Chain!' before submitting a question.")
|
224 |
+
with gr.Row():
|
225 |
+
init_chain_button = gr.Button(value="Start Chain!", variant="primary").style(full_width=False)
|
226 |
+
|
227 |
+
chatbot = gr.Chatbot()
|
228 |
+
|
229 |
+
with gr.Row():
|
230 |
+
gr.Markdown("<h3><center>TerenceGPT</center></h3>")
|
231 |
+
|
232 |
+
with gr.Row():
|
233 |
+
|
234 |
+
message = gr.Textbox(
|
235 |
+
label="What's your question?",
|
236 |
+
placeholder="Ask Terence McKenna",
|
237 |
+
lines=1,
|
238 |
+
)
|
239 |
+
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
|
240 |
+
|
241 |
+
with gr.Column():
|
242 |
+
with gr.Row():
|
243 |
+
gr.Image(type='filepath', value='McKenna3.jpg', shape=(200,100))
|
244 |
+
gr.HTML(
|
245 |
+
"<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗 and Unicorn Farts 🦄💨</a></center>"
|
246 |
+
)
|
247 |
+
|
248 |
+
state = gr.State()
|
249 |
+
agent_state = gr.State()
|
250 |
+
|
251 |
+
submit.click(
|
252 |
+
chat,
|
253 |
+
inputs=[message, state, agent_state],
|
254 |
+
outputs=[chatbot, state]
|
255 |
+
)
|
256 |
+
|
257 |
+
message.submit(
|
258 |
+
chat,
|
259 |
+
inputs=[message, state, agent_state],
|
260 |
+
outputs=[chatbot, state]
|
261 |
+
)
|
262 |
+
message.submit(lambda :"", None, message)
|
263 |
+
|
264 |
+
init_chain_button.click(
|
265 |
+
initialize_chain,
|
266 |
+
inputs=[],
|
267 |
+
outputs=[agent_state],
|
268 |
+
show_progress=True
|
269 |
+
)
|
270 |
+
|
271 |
+
# block.launch(debug=True, share=args.share)
|
272 |
+
block.launch(debug=True)
|
appv2.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 |
+
from langchain import PromptTemplate
|
8 |
+
|
9 |
+
import os
|
10 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
11 |
+
|
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, OpenAIChat
|
32 |
+
from langchain.chains import ChatVectorDBChain
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
_template = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
|
37 |
+
|
38 |
+
Chat History:
|
39 |
+
{chat_history}
|
40 |
+
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 |
+
|
46 |
+
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.
|
47 |
+
|
48 |
+
Terence is an advocate for the shaman as a culture hero and was the central figure in the culture of rave and house music.
|
49 |
+
|
50 |
+
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.
|
51 |
+
|
52 |
+
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.
|
53 |
+
|
54 |
+
Terence has a unique ability to blend together scientific, mystical, and metaphysical musings to create an engaging and thought-provoking blend of ideas.
|
55 |
+
|
56 |
+
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.
|
57 |
+
|
58 |
+
|
59 |
+
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.
|
60 |
+
Human: {question}
|
61 |
+
=========
|
62 |
+
{context}
|
63 |
+
=========
|
64 |
+
Terence:
|
65 |
+
"""
|
66 |
+
|
67 |
+
TMK_PROMPT = PromptTemplate(
|
68 |
+
template=tqa2_template, input_variables=["question", "context"]
|
69 |
+
)
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
###########################################################################################
|
74 |
+
from langchain import PromptTemplate
|
75 |
+
|
76 |
+
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.
|
77 |
+
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.
|
78 |
+
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.
|
79 |
+
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.
|
80 |
+
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.
|
81 |
+
Whether you need help with a specific question or just want to have a conversation about a particular topic, Terence is here to assist.
|
82 |
+
|
83 |
+
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.
|
84 |
+
Human: {question}
|
85 |
+
=========
|
86 |
+
{context}
|
87 |
+
=========
|
88 |
+
Answer:"""
|
89 |
+
|
90 |
+
QA_PROMPT = PromptTemplate(template=qa_template, input_variables=["question", "context"])
|
91 |
+
|
92 |
+
###########################################################################################
|
93 |
+
|
94 |
+
def get_chain(vectorstore,
|
95 |
+
temperature=0.7,
|
96 |
+
max_tokens=384,
|
97 |
+
qa_prompt=TMK_PROMPT,
|
98 |
+
condense_prompt=CONDENSE_QUESTION_PROMPT,
|
99 |
+
prompt=None):
|
100 |
+
|
101 |
+
llm = OpenAIChat(temperature=temperature, max_tokens=max_tokens)
|
102 |
+
|
103 |
+
qa_chain = ChatVectorDBChain.from_llm(
|
104 |
+
llm,
|
105 |
+
vectorstore,
|
106 |
+
qa_prompt=qa_prompt,# or prompt,
|
107 |
+
condense_question_prompt=condense_prompt,
|
108 |
+
)
|
109 |
+
return qa_chain
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
LIVE_VECTORSTORE_PATH = args.live_vectorstore_path
|
115 |
+
DATA_DIRECTORY = args.data_directory
|
116 |
+
NEW_VECTORSTORE_SET = False
|
117 |
+
NEW_DIRECTORY_SET = False
|
118 |
+
|
119 |
+
|
120 |
+
# Attempt to load base vectorstore
|
121 |
+
try:
|
122 |
+
|
123 |
+
with open(args.vectorstore_path, "rb") as f:
|
124 |
+
VECTORSTORE = pickle.load(f)
|
125 |
+
|
126 |
+
# print("Loaded vectorstore from `{}`.".format(args.vectorstore_path))
|
127 |
+
|
128 |
+
chain = get_chain(
|
129 |
+
VECTORSTORE,
|
130 |
+
temperature=args.temperature,
|
131 |
+
max_tokens=args.max_tokens,
|
132 |
+
prompt=args.prompt
|
133 |
+
)
|
134 |
+
|
135 |
+
# print("Loaded LangChain...")
|
136 |
+
|
137 |
+
except:
|
138 |
+
|
139 |
+
VECTORSTORE = None
|
140 |
+
# print("NO vectorstore loaded. Flying blind")
|
141 |
+
|
142 |
+
|
143 |
+
def set_vectorstore(vectorstore_path: str):
|
144 |
+
|
145 |
+
if vectorstore_path is not None:
|
146 |
+
global VECTORSTORE, NEW_VECTORSTORE_SET
|
147 |
+
|
148 |
+
try:
|
149 |
+
with open(vectorstore_path, "rb") as f:
|
150 |
+
VECTORSTORE = pickle.load(f)
|
151 |
+
|
152 |
+
# print("Loaded `{}`".format(vectorstore_path))
|
153 |
+
NEW_VECTORSTORE_SET = True
|
154 |
+
|
155 |
+
chain = get_chain(
|
156 |
+
VECTORSTORE,
|
157 |
+
temperature=args.temperature,
|
158 |
+
max_tokens=args.max_tokens,
|
159 |
+
prompt=args.prompt
|
160 |
+
)
|
161 |
+
|
162 |
+
except:
|
163 |
+
VECTORSTORE = None
|
164 |
+
NEW_VECTORSTORE_SET = False
|
165 |
+
# print("NO vectorstore loaded. Reverting to original {}".format('vectorstore.pkl'))
|
166 |
+
|
167 |
+
return chain
|
168 |
+
|
169 |
+
|
170 |
+
def initialize_chain():
|
171 |
+
chain = get_chain(
|
172 |
+
VECTORSTORE,
|
173 |
+
temperature=args.temperature,
|
174 |
+
max_tokens=args.max_tokens,
|
175 |
+
prompt=args.prompt
|
176 |
+
)
|
177 |
+
# print("LangChain initialized!")
|
178 |
+
return chain
|
179 |
+
|
180 |
+
|
181 |
+
|
182 |
+
class ChatWrapper:
|
183 |
+
|
184 |
+
def __init__(self):
|
185 |
+
self.lock = Lock()
|
186 |
+
|
187 |
+
def __call__(
|
188 |
+
self, inp: str, history: Optional[Tuple[str, str]], chain, #, dirpath: Optional[str], vectorstore_path: Optional[str],
|
189 |
+
):
|
190 |
+
"""Execute the chat functionality."""
|
191 |
+
self.lock.acquire()
|
192 |
+
try:
|
193 |
+
history = history or []
|
194 |
+
|
195 |
+
# If chain is None, that is because it's the first pass and user didn't press Init.
|
196 |
+
if chain is None:
|
197 |
+
history.append(
|
198 |
+
(inp, "Please Initialize LangChain by clikcing 'Start Chain!'")
|
199 |
+
)
|
200 |
+
return history, history
|
201 |
+
|
202 |
+
# Run chain and append input.
|
203 |
+
output = chain({"question": inp, "chat_history": history})["answer"]
|
204 |
+
history.append((inp, output))
|
205 |
+
|
206 |
+
return history, history
|
207 |
+
|
208 |
+
except Exception as e:
|
209 |
+
raise e
|
210 |
+
|
211 |
+
finally:
|
212 |
+
self.lock.release()
|
213 |
+
|
214 |
+
return history, history
|
215 |
+
|
216 |
+
|
217 |
+
chat = ChatWrapper()
|
218 |
+
# block = gr.Blocks(css=".gradio-container {background-color: lightgray} .overflow-y-auto{height:500px}")
|
219 |
+
# block = gr.Blocks(css='body{background-image:url("https://upload.wikimedia.org/wikipedia/commons/7/7f/Mckenna1.jpg");}')
|
220 |
+
# css=".gradio-container {background-image: url('file=Mckenna1.jpg')}"
|
221 |
+
css=".gradio-container {background-color: lightgray} .overflow-y-auto{height:500px}"
|
222 |
+
|
223 |
+
css = """
|
224 |
+
img {
|
225 |
+
border: 1px solid #ddd;
|
226 |
+
border-radius: 4px;
|
227 |
+
padding: 5px;
|
228 |
+
width: 150px;
|
229 |
+
}
|
230 |
+
|
231 |
+
<img src="paris.jpg" alt="Paris">
|
232 |
+
"""
|
233 |
+
block = gr.Blocks(css=css)
|
234 |
+
|
235 |
+
with block:
|
236 |
+
|
237 |
+
gr.HTML("Please initialize the chain by clicking 'Start Chain!' before submitting a question.")
|
238 |
+
with gr.Row():
|
239 |
+
init_chain_button = gr.Button(value="Start Chain!", variant="primary").style(full_width=False)
|
240 |
+
|
241 |
+
chatbot = gr.Chatbot()
|
242 |
+
|
243 |
+
with gr.Row():
|
244 |
+
gr.Markdown("<h3><center>TerenceGPT</center></h3>")
|
245 |
+
|
246 |
+
with gr.Row():
|
247 |
+
|
248 |
+
message = gr.Textbox(
|
249 |
+
label="What's your question?",
|
250 |
+
placeholder="Ask Terence McKenna",
|
251 |
+
lines=1,
|
252 |
+
)
|
253 |
+
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
|
254 |
+
|
255 |
+
with gr.Column():
|
256 |
+
with gr.Row():
|
257 |
+
gr.Image(type='filepath', value='McKenna3.jpg', shape=(200,100))
|
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_chain,
|
280 |
+
inputs=[],
|
281 |
+
outputs=[agent_state],
|
282 |
+
show_progress=True
|
283 |
+
)
|
284 |
+
|
285 |
+
# block.launch(debug=True, share=args.share)
|
286 |
+
block.launch(debug=True)
|
terence_vectorstore.pkl
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4a0e45869c54e86a06866eacf894adc7d2aa6dae896624c89a90b8b06ad7ca63
|
3 |
+
size 40263255
|