Spaces:
Runtime error
Runtime error
File size: 9,843 Bytes
4d4fc57 231aad2 4d4fc57 231aad2 4d4fc57 231aad2 4d4fc57 231aad2 4d4fc57 231aad2 4d4fc57 231aad2 4d4fc57 231aad2 4d4fc57 231aad2 4d4fc57 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
import pickle
from typing import Optional, Tuple
import gradio as gr
from threading import Lock
from langchain import PromptTemplate
import os
os.environ["OPENAI_API_KEY"] = "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"
import openai
openai.api_key = "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"
vecpath = "terence_all_vectorstore.pkl"
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--data_directory', type=str, help='Path to directory containing data files to index')
parser.add_argument('-p', '--prompt', default='plant', help="String prompt template to use, must contain {question} and {context}", type=str)
parser.add_argument('-pp', '--prompt_path', type=str, help='Path to custom prompt template to use with LLM ChatBot + Vectorstore')
parser.add_argument('-t', '--temperature', type=float, default=0.7, help='LLM temperature setting... lower == more deterministic')
parser.add_argument('-m', '--max_tokens', type=int, default=384, help='LLM maximum number of output tokens')
parser.add_argument('-v', '--vectorstore_path', default=vecpath, type=str, help='Path to saved index')
parser.add_argument('-dv', '--live_vectorstore_path', default=r"indices\vectorstore_from_docs.pkl", type=str, help='Path to save temporary index')
parser.add_argument('-f', '--font_size', type=int, default=20, help='Chatbot window font size (default: 20px)')
parser.add_argument('-s', '--share', action='store_true', default=False, help="Host online and produce shareable link")
args = parser.parse_args()
from langchain.prompts.prompt import PromptTemplate
from langchain.llms import OpenAI, OpenAIChat
from langchain.chains import ChatVectorDBChain
_template = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:"""
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
tqa2_template= """Terence is an ethnobotanist, shamanologist, and visionary. Terence studied art history, ancient languages, and conservation of natural resources.
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.
Terence is an advocate for the shaman as a culture hero and was the central figure in the culture of rave and house music.
Terence is known for lucid and poetic language, which he uses to convey complex ideas in an accessible way. Terence is also a master of metaphor and analogy, which he uses to illustrate his points and help the audience connect with the material.
Terence will often make use of metaphor and allegory to illustrate his points, and his use of language is both vivid and imaginative, and can also be decidedly witty and humorous.
Terence has a unique ability to blend together scientific, mystical, and metaphysical musings to create an engaging and thought-provoking blend of ideas.
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.
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.
Human: {question}
=========
{context}
=========
Terence:
"""
TMK_PROMPT = PromptTemplate(
template=tqa2_template, input_variables=["question", "context"]
)
###########################################################################################
from langchain import PromptTemplate
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.
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.
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.
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.
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.
Whether you need help with a specific question or just want to have a conversation about a particular topic, Terence is here to assist.
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.
Human: {question}
=========
{context}
=========
Answer:"""
QA_PROMPT = PromptTemplate(template=qa_template, input_variables=["question", "context"])
###########################################################################################
def get_chain(vectorstore,
temperature=0.7,
max_tokens=384,
qa_prompt=TMK_PROMPT,
condense_prompt=CONDENSE_QUESTION_PROMPT,
prompt=None):
llm = OpenAIChat(temperature=temperature, max_tokens=max_tokens)
qa_chain = ChatVectorDBChain.from_llm(
llm,
vectorstore,
qa_prompt=qa_prompt,# or prompt,
condense_question_prompt=condense_prompt,
)
return qa_chain
# Attempt to load base vectorstore
try:
with open(args.vectorstore_path, "rb") as f:
VECTORSTORE = pickle.load(f)
# print("Loaded vectorstore from `{}`.".format(args.vectorstore_path))
chain = get_chain(
VECTORSTORE,
temperature=args.temperature,
max_tokens=args.max_tokens,
prompt=args.prompt
)
# print("Loaded LangChain...")
except:
VECTORSTORE = None
# print("NO vectorstore loaded. Flying blind")
def initialize_chain():
chain = get_chain(
VECTORSTORE,
temperature=args.temperature,
max_tokens=args.max_tokens,
prompt=args.prompt
)
# print("LangChain initialized!")
return chain
class ChatWrapper:
def __init__(self):
self.lock = Lock()
def __call__(
self, inp: str, history: Optional[Tuple[str, str]], chain, #, dirpath: Optional[str], vectorstore_path: Optional[str],
):
"""Execute the chat functionality."""
self.lock.acquire()
try:
history = history or []
# If chain is None, that is because it's the first pass and user didn't press Init.
if chain is None:
history.append(
(inp, "Please Initialize LangChain by clikcing 'Start Chain!'")
)
return history, history
# Run chain and append input.
output = chain({"question": inp, "chat_history": history})["answer"]
history.append((inp, output))
return history, history
except Exception as e:
raise e
finally:
self.lock.release()
return history, history
chat = ChatWrapper()
# block = gr.Blocks(css=".gradio-container {background-color: lightgray} .overflow-y-auto{height:500px}")
# block = gr.Blocks(css='body{background-image:url("https://upload.wikimedia.org/wikipedia/commons/7/7f/Mckenna1.jpg");}')
# css=".gradio-container {background-image: url('file=Mckenna1.jpg')}"
css=".gradio-container {background-color: lightgray} .overflow-y-auto{height:400px}"
# css = """
# img {
# border: 1px solid #ddd;
# border-radius: 4px;
# padding: 5px;
# width: 150px;
# }
# <img src="paris.jpg" alt="Paris">
# """
block = gr.Blocks(css=css)
with block:
gr.HTML("Please initialize the chain by clicking 'Start Chain!' before submitting a question.")
with gr.Row():
init_chain_button = gr.Button(value="Start Chain!", variant="primary").style(full_width=False)
with gr.Row():
with gr.Column(min_width=100): pass
with gr.Column():
with gr.Row():
gr.Image(type='filepath', value='McKenna3.jpg', shape=(200,100))
with gr.Column(min_width=100): pass
chatbot = gr.Chatbot()
with gr.Row():
gr.Markdown("<h3><center>TerenceGPT</center></h3>")
with gr.Row():
message = gr.Textbox(
label="What's your question?",
placeholder="Ask Terence McKenna",
lines=1,
)
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
gr.HTML(
"<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain π¦οΈπ and Unicorn Farts π¦π¨</a></center>"
)
state = gr.State()
agent_state = gr.State()
submit.click(
chat,
inputs=[message, state, agent_state],
outputs=[chatbot, state]
)
message.submit(
chat,
inputs=[message, state, agent_state],
outputs=[chatbot, state]
)
message.submit(lambda :"", None, message)
init_chain_button.click(
initialize_chain,
inputs=[],
outputs=[agent_state],
show_progress=True
)
# block.launch(debug=True, share=args.share)
block.launch(debug=True) |