Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,10 +26,18 @@ from langchain.chat_models import ChatOpenAI
|
|
| 26 |
from langchain.document_loaders import PyPDFLoader
|
| 27 |
from langchain.chains.question_answering import load_qa_chain
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# turned off due to people using it unethical ways
|
| 30 |
openai.api_key = os.environ['openai_key']
|
| 31 |
os.environ["OPENAI_API_KEY"] = os.environ['openai_key']
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
prompt_templates = {"All Needs Experts": "Respond as if you are combination of all needs assessment experts."}
|
| 35 |
actor_description = {"All Needs Experts": "<div style='float: left;margin: 0px 5px 0px 5px;'><img src='https://na.weshareresearch.com/wp-content/uploads/2023/04/experts2.jpg' alt='needs expert image' style='width:70px;align:top;'></div>A combiation of all needs assessment experts."}
|
|
@@ -86,6 +94,7 @@ def on_prompt_template_change_description(prompt_template):
|
|
| 86 |
|
| 87 |
|
| 88 |
|
|
|
|
| 89 |
def submit_message(prompt, prompt_template, temperature, max_tokens, context_length, state):
|
| 90 |
|
| 91 |
|
|
@@ -102,6 +111,79 @@ def submit_message(prompt, prompt_template, temperature, max_tokens, context_len
|
|
| 102 |
{"prompt": str(prompt), "time": str(datetime.now())}
|
| 103 |
)
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
# with open(prompts_archive_file, "a") as csvfile:
|
| 106 |
# writer = csv.DictWriter(csvfile, fieldnames=["prompt", "time"])
|
| 107 |
# writer.writerow(
|
|
|
|
| 26 |
from langchain.document_loaders import PyPDFLoader
|
| 27 |
from langchain.chains.question_answering import load_qa_chain
|
| 28 |
|
| 29 |
+
import generativeai as gen
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
# turned off due to people using it unethical ways
|
| 35 |
openai.api_key = os.environ['openai_key']
|
| 36 |
os.environ["OPENAI_API_KEY"] = os.environ['openai_key']
|
| 37 |
|
| 38 |
+
gemini.api_key = os.environ['gemini_key']
|
| 39 |
+
os.environ["GEMINI_API_KEY"] = os.environ['gemini_key']
|
| 40 |
+
|
| 41 |
|
| 42 |
prompt_templates = {"All Needs Experts": "Respond as if you are combination of all needs assessment experts."}
|
| 43 |
actor_description = {"All Needs Experts": "<div style='float: left;margin: 0px 5px 0px 5px;'><img src='https://na.weshareresearch.com/wp-content/uploads/2023/04/experts2.jpg' alt='needs expert image' style='width:70px;align:top;'></div>A combiation of all needs assessment experts."}
|
|
|
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
+
|
| 98 |
def submit_message(prompt, prompt_template, temperature, max_tokens, context_length, state):
|
| 99 |
|
| 100 |
|
|
|
|
| 111 |
{"prompt": str(prompt), "time": str(datetime.now())}
|
| 112 |
)
|
| 113 |
|
| 114 |
+
|
| 115 |
+
system_prompt = []
|
| 116 |
+
if prompt_template:
|
| 117 |
+
system_prompt = [{ "role": "system", "content": prompt_template }]
|
| 118 |
+
|
| 119 |
+
prompt_msg = { "role": "user", "content": prompt }
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
#try:
|
| 123 |
+
|
| 124 |
+
with open("embeddings.pkl", 'rb') as f:
|
| 125 |
+
new_docsearch = pickle.load(f)
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
query = str(system_prompt + history + [prompt_msg])
|
| 129 |
+
|
| 130 |
+
docs = new_docsearch.similarity_search(query)
|
| 131 |
+
|
| 132 |
+
gen_ai = GenerativeAI.get_client(model="gemini-1.0-pro")
|
| 133 |
+
response = gen_ai.start_chat(
|
| 134 |
+
messages=query, # Pass both history and current prompt
|
| 135 |
+
max_tokens=max_tokens,
|
| 136 |
+
temperature=temperature # Adjust temperature as needed
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
completion = response.messages[-1] # Extract the completion message
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
get_empty_state()
|
| 143 |
+
state['content'] = completion
|
| 144 |
+
#state.append(completion.copy())
|
| 145 |
+
|
| 146 |
+
completion = { "content": completion }
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
#state['total_tokens'] += completion['usage']['total_tokens']
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
#except Exception as e:
|
| 154 |
+
# history.append(prompt_msg.copy())
|
| 155 |
+
# error = {
|
| 156 |
+
# "role": "system",
|
| 157 |
+
# "content": f"Error: {e}"
|
| 158 |
+
# }
|
| 159 |
+
# history.append(error.copy())
|
| 160 |
+
|
| 161 |
+
#total_tokens_used_msg = f"Total tokens used: {state['total_tokens']}"
|
| 162 |
+
|
| 163 |
+
chat_messages = [(prompt_msg['content'], completion['content'])]
|
| 164 |
+
return '', chat_messages, state # total_tokens_used_msg,
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def submit_message_OLD(prompt, prompt_template, temperature, max_tokens, context_length, state):
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
history = state['messages']
|
| 175 |
+
|
| 176 |
+
if not prompt:
|
| 177 |
+
return gr.update(value=''), [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)], state
|
| 178 |
+
|
| 179 |
+
prompt_template = prompt_templates[prompt_template]
|
| 180 |
+
|
| 181 |
+
with open("prompts_archive.csv", "a") as csvfile:
|
| 182 |
+
writer = csv.DictWriter(csvfile, fieldnames=["prompt", "time"])
|
| 183 |
+
writer.writerow(
|
| 184 |
+
{"prompt": str(prompt), "time": str(datetime.now())}
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
# with open(prompts_archive_file, "a") as csvfile:
|
| 188 |
# writer = csv.DictWriter(csvfile, fieldnames=["prompt", "time"])
|
| 189 |
# writer.writerow(
|