Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -780,180 +780,82 @@ Standalone question:""")
|
|
780 |
return chain,memory
|
781 |
|
782 |
|
783 |
-
"""
|
784 |
-
# 1. load memory using RunnableLambda. Retrieves the chat_history attribute using itemgetter.
|
785 |
-
# `RunnablePassthrough.assign` adds the chat_history to the assign function
|
786 |
-
|
787 |
-
loaded_memory = RunnablePassthrough.assign(
|
788 |
-
chat_history=RunnableLambda(memory.load_memory_variables) | itemgetter("chat_history"),
|
789 |
-
)
|
790 |
-
|
791 |
-
# 2. Pass the follow-up question along with the chat history to the LLM, and parse the answer (standalone_question).
|
792 |
-
|
793 |
-
condense_question_prompt = PromptTemplate(
|
794 |
-
input_variables=['chat_history', 'question'],
|
795 |
-
template=standalone_question_template
|
796 |
-
)
|
797 |
-
|
798 |
-
condense_question_llm = instantiate_LLM(
|
799 |
-
LLM_provider="Google",api_key=google_api_key,temperature=0.1,
|
800 |
-
model_name="gemini-pro"
|
801 |
-
)
|
802 |
-
|
803 |
-
standalone_question_chain = {
|
804 |
-
"standalone_question": {
|
805 |
-
"question": lambda x: x["question"],
|
806 |
-
"chat_history": lambda x: get_buffer_string(x["chat_history"]),
|
807 |
-
}
|
808 |
-
| condense_question_prompt
|
809 |
-
| condense_question_llm
|
810 |
-
| StrOutputParser(),
|
811 |
-
}
|
812 |
-
|
813 |
-
# 3. Combine load_memory and standalone_question_chain
|
814 |
-
|
815 |
-
chain_question = loaded_memory | standalone_question_chain
|
816 |
-
|
817 |
-
|
818 |
-
memory.clear()
|
819 |
-
memory.save_context(
|
820 |
-
{"question": "What does Google stand for?"},
|
821 |
-
{"answer": "Diffuse to Choose."}
|
822 |
-
)
|
823 |
-
|
824 |
-
|
825 |
-
|
826 |
-
print("Chat history:\n",memory.load_memory_variables({}))
|
827 |
-
|
828 |
-
follow_up_question = "plaese give more details about it, including its use cases and implementation."
|
829 |
-
print("\nFollow-up question:\n",follow_up_question)
|
830 |
-
|
831 |
-
# invoke chain_question
|
832 |
-
response = chain_question.invoke({"question":follow_up_question})["standalone_question"]
|
833 |
-
print("\nStandalone_question:\n",response)
|
834 |
-
|
835 |
-
|
836 |
-
|
837 |
-
def _combine_documents(docs, document_prompt, document_separator="\n\n"):
|
838 |
-
doc_strings = [format_document(doc, document_prompt) for doc in docs]
|
839 |
-
return document_separator.join(doc_strings)
|
840 |
-
|
841 |
-
# 1. Retrieve relevant documents
|
842 |
-
|
843 |
-
retrieved_documents = {
|
844 |
-
"docs": itemgetter("standalone_question") | retriever,
|
845 |
-
"question": lambda x: x["standalone_question"],
|
846 |
-
}
|
847 |
-
|
848 |
-
# 2. Get variables ['chat_history', 'context', 'question'] that will be passed to `answer_prompt`
|
849 |
-
|
850 |
-
DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template(template="{page_content}")
|
851 |
-
answer_prompt = ChatPromptTemplate.from_template(answer_template()) # 3 variables are expected ['chat_history', 'context', 'question']
|
852 |
-
|
853 |
-
answer_prompt_variables = {
|
854 |
-
"context": lambda x: _combine_documents(docs=x["docs"],document_prompt=DEFAULT_DOCUMENT_PROMPT),
|
855 |
-
"question": itemgetter("question"),
|
856 |
-
"chat_history": itemgetter("chat_history") # get chat_history from `loaded_memory` variable
|
857 |
-
}
|
858 |
-
|
859 |
-
llm = instantiate_LLM(
|
860 |
-
LLM_provider="Google",api_key=google_api_key,temperature=0.5,
|
861 |
-
model_name="gemini-pro"
|
862 |
-
)
|
863 |
-
|
864 |
-
# 3. Load memory, format `answer_prompt` with variables (context, question and chat_history) and pass the `answer_prompt to LLM.
|
865 |
-
# return answer, docs and standalone_question
|
866 |
-
|
867 |
-
chain_answer = {
|
868 |
-
"answer": loaded_memory | answer_prompt_variables | answer_prompt | llm,
|
869 |
-
"docs": lambda x: [
|
870 |
-
Document(page_content=doc.page_content,metadata=doc.metadata) # return only page_content and metadata
|
871 |
-
for doc in x["docs"]
|
872 |
-
],
|
873 |
-
"standalone_question": lambda x:x["question"] # return standalone_question
|
874 |
-
}
|
875 |
|
876 |
|
877 |
-
conversational_retriever_chain = chain_question | retrieved_documents | chain_answer
|
878 |
-
follow_up_question = "plaese give more details about it, including its use cases and implementation."
|
879 |
-
|
880 |
-
response = conversational_retriever_chain.invoke({"question":follow_up_question})
|
881 |
-
gr.Markdown(response['answer'].content)
|
882 |
-
|
883 |
|
884 |
-
|
885 |
-
|
886 |
-
|
887 |
-
|
888 |
|
|
|
|
|
|
|
|
|
889 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
890 |
|
|
|
|
|
|
|
891 |
|
892 |
-
|
893 |
-
"plaese give more details about it, including its use cases and implementation.",
|
894 |
-
"does it outperform other diffusion-based models? explain in details.",
|
895 |
-
"what is Langchain?"]
|
896 |
|
897 |
-
# Instantiate the retriever and the ConversationalRetrievalChain :
|
898 |
|
|
|
899 |
|
900 |
-
|
901 |
-
|
902 |
-
|
903 |
-
vectorstore_name="Vit_All_HF_Embeddings",
|
904 |
-
retriever_type="Cohere_reranker",
|
905 |
-
base_retriever_search_type="similarity", base_retriever_k=16,
|
906 |
-
compression_retriever_k=20,
|
907 |
-
cohere_api_key=cohere_api_key,cohere_top_n=10,
|
908 |
-
)
|
909 |
-
|
910 |
-
chain_HF,memory_HF = custom_ConversationalRetrievalChain(
|
911 |
-
llm = instantiate_LLM(
|
912 |
-
LLM_provider="HuggingFace",api_key=HF_key,temperature=0.5,
|
913 |
-
model_name="mistralai/Mistral-7B-Instruct-v0.2"
|
914 |
-
),
|
915 |
-
condense_question_llm = instantiate_LLM(
|
916 |
-
LLM_provider="HuggingFace",api_key=HF_key,temperature=0.5,
|
917 |
-
model_name="mistralai/Mistral-7B-Instruct-v0.2"
|
918 |
-
),
|
919 |
-
retriever=retriever_HF,
|
920 |
-
language="english",
|
921 |
-
llm_provider="HuggingFace",
|
922 |
-
model_name="Mistral-7B-Instruct-v0.2"
|
923 |
-
)
|
924 |
-
|
925 |
-
|
926 |
-
memory_HF.clear()
|
927 |
|
928 |
-
|
929 |
|
930 |
-
|
931 |
-
answer = answer[answer.find("\nAnswer: ")+len("\nAnswer: "):]
|
932 |
-
|
933 |
-
gr.markdown("**Question:** "+questions[0]+"\n\n"\
|
934 |
-
+"**Standalone_question:**"+response['standalone_question']+"\n\n"\
|
935 |
-
+"**Answer:** "+answer)
|
936 |
-
|
937 |
-
|
938 |
-
|
939 |
-
|
940 |
|
|
|
|
|
|
|
941 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
942 |
|
943 |
|
944 |
-
for i,question in enumerate(questions):
|
945 |
-
response = chain_gemini.invoke({"question":question})
|
946 |
-
answer = response['answer'].content
|
947 |
-
print(f"Question[{i}]:",question)
|
948 |
-
print("Standalone_question:",response['standalone_question'])
|
949 |
-
print("Answer:\n",answer,f"\n\n{'-' * 100}\n")
|
950 |
|
951 |
-
|
952 |
-
|
|
|
|
|
|
|
|
|
|
|
953 |
|
|
|
954 |
|
|
|
|
|
955 |
|
956 |
|
|
|
|
|
957 |
|
958 |
|
959 |
|
|
|
780 |
return chain,memory
|
781 |
|
782 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
783 |
|
784 |
|
|
|
|
|
|
|
|
|
|
|
|
|
785 |
|
786 |
+
def submit_message_OLD(prompt, prompt_template, temperature, max_tokens, context_length, state):
|
787 |
+
|
788 |
+
|
789 |
+
history = state['messages']
|
790 |
|
791 |
+
if not prompt:
|
792 |
+
return gr.update(value=''), [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)], state
|
793 |
+
|
794 |
+
prompt_template = prompt_templates[prompt_template]
|
795 |
|
796 |
+
with open("prompts_archive.csv", "a") as csvfile:
|
797 |
+
writer = csv.DictWriter(csvfile, fieldnames=["prompt", "time"])
|
798 |
+
writer.writerow(
|
799 |
+
{"prompt": str(prompt), "time": str(datetime.now())}
|
800 |
+
)
|
801 |
+
|
802 |
+
# with open(prompts_archive_file, "a") as csvfile:
|
803 |
+
# writer = csv.DictWriter(csvfile, fieldnames=["prompt", "time"])
|
804 |
+
# writer.writerow(
|
805 |
+
# {"prompt": str(prompt), "time": str(datetime.now())}
|
806 |
+
# )
|
807 |
+
# commit_url = repo.push_to_hub()
|
808 |
+
# print(commit_url)
|
809 |
|
810 |
+
system_prompt = []
|
811 |
+
if prompt_template:
|
812 |
+
system_prompt = [{ "role": "system", "content": prompt_template }]
|
813 |
|
814 |
+
prompt_msg = { "role": "user", "content": prompt }
|
|
|
|
|
|
|
815 |
|
|
|
816 |
|
817 |
+
#try:
|
818 |
|
819 |
+
with open("embeddings.pkl", 'rb') as f:
|
820 |
+
new_docsearch = pickle.load(f)
|
821 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
822 |
|
823 |
+
query = str(system_prompt + history + [prompt_msg])
|
824 |
|
825 |
+
docs = new_docsearch.similarity_search(query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
826 |
|
827 |
+
chain = load_qa_chain(ChatOpenAI(temperature=temperature, max_tokens=max_tokens, model_name="gpt-3.5-turbo"), chain_type="stuff")
|
828 |
+
#completion = chain.run(input_documents=docs, question=query)
|
829 |
+
|
830 |
|
831 |
+
|
832 |
+
get_empty_state()
|
833 |
+
state['content'] = completion
|
834 |
+
#state.append(completion.copy())
|
835 |
+
|
836 |
+
completion = { "content": completion }
|
837 |
+
|
838 |
+
|
839 |
+
#state['total_tokens'] += completion['usage']['total_tokens']
|
840 |
|
841 |
|
|
|
|
|
|
|
|
|
|
|
|
|
842 |
|
843 |
+
#except Exception as e:
|
844 |
+
# history.append(prompt_msg.copy())
|
845 |
+
# error = {
|
846 |
+
# "role": "system",
|
847 |
+
# "content": f"Error: {e}"
|
848 |
+
# }
|
849 |
+
# history.append(error.copy())
|
850 |
|
851 |
+
#total_tokens_used_msg = f"Total tokens used: {state['total_tokens']}"
|
852 |
|
853 |
+
chat_messages = [(prompt_msg['content'], completion['content'])]
|
854 |
+
return '', chat_messages, state # total_tokens_used_msg,
|
855 |
|
856 |
|
857 |
+
def clear_conversation():
|
858 |
+
return gr.update(value=None, visible=True), None, "", get_empty_state()
|
859 |
|
860 |
|
861 |
|