Spaces:
Sleeping
Sleeping
app18
Browse files
app.py
CHANGED
|
@@ -74,9 +74,11 @@ Your name is AngryGreta and you are a recycling chatbot with the objective to an
|
|
| 74 |
Use the following pieces of context to answer the question if the question is related with recycling /
|
| 75 |
No more than two chunks of context /
|
| 76 |
Answer in the same language of the question /
|
| 77 |
-
Always say "thanks for asking!" at the end of the answer
|
|
|
|
|
|
|
| 78 |
context: {context}
|
| 79 |
-
|
| 80 |
question: {question}
|
| 81 |
"""
|
| 82 |
|
|
@@ -84,7 +86,8 @@ question: {question}
|
|
| 84 |
system_prompt = SystemMessagePromptTemplate.from_template(template)
|
| 85 |
qa_prompt = ChatPromptTemplate(
|
| 86 |
messages=[
|
| 87 |
-
system_prompt,
|
|
|
|
| 88 |
HumanMessagePromptTemplate.from_template("{question}")
|
| 89 |
]
|
| 90 |
)
|
|
@@ -98,28 +101,24 @@ llm = HuggingFaceHub(
|
|
| 98 |
"repetition_penalty": 1.03,
|
| 99 |
},
|
| 100 |
)
|
| 101 |
-
llm_chain = LLMChain(llm=llm, prompt=qa_prompt)
|
| 102 |
|
| 103 |
-
memory = ConversationBufferMemory(llm=llm, memory_key="chat_history", output_key='answer', return_messages=True)
|
| 104 |
|
| 105 |
qa_chain = ConversationalRetrievalChain.from_llm(
|
| 106 |
llm = llm,
|
| 107 |
memory = memory,
|
| 108 |
retriever = retriever,
|
| 109 |
-
verbose =
|
| 110 |
combine_docs_chain_kwargs={'prompt': qa_prompt},
|
| 111 |
-
get_chat_history = lambda h : h
|
|
|
|
|
|
|
| 112 |
)
|
| 113 |
|
| 114 |
def chat_interface(question,history):
|
| 115 |
|
| 116 |
result = qa_chain.invoke({"question": question})
|
| 117 |
-
|
| 118 |
-
# Check the structure of the result
|
| 119 |
-
if isinstance(result['answer'], str):
|
| 120 |
-
return result['answer'] # If the result is a string, return it directly
|
| 121 |
-
else:
|
| 122 |
-
return "Unexpected answer format"
|
| 123 |
|
| 124 |
chatbot_gradio_app = gr.ChatInterface(
|
| 125 |
fn=chat_interface,
|
|
|
|
| 74 |
Use the following pieces of context to answer the question if the question is related with recycling /
|
| 75 |
No more than two chunks of context /
|
| 76 |
Answer in the same language of the question /
|
| 77 |
+
Always say "thanks for asking!" at the end of the answer /
|
| 78 |
+
If the context is not relevant, please answer the question by using your own knowledge about the topic.
|
| 79 |
+
|
| 80 |
context: {context}
|
| 81 |
+
|
| 82 |
question: {question}
|
| 83 |
"""
|
| 84 |
|
|
|
|
| 86 |
system_prompt = SystemMessagePromptTemplate.from_template(template)
|
| 87 |
qa_prompt = ChatPromptTemplate(
|
| 88 |
messages=[
|
| 89 |
+
system_prompt,
|
| 90 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
| 91 |
HumanMessagePromptTemplate.from_template("{question}")
|
| 92 |
]
|
| 93 |
)
|
|
|
|
| 101 |
"repetition_penalty": 1.03,
|
| 102 |
},
|
| 103 |
)
|
|
|
|
| 104 |
|
| 105 |
+
memory = ConversationBufferMemory(llm=llm, memory_key="chat_history", input_key='question', output_key='answer', return_messages=True)
|
| 106 |
|
| 107 |
qa_chain = ConversationalRetrievalChain.from_llm(
|
| 108 |
llm = llm,
|
| 109 |
memory = memory,
|
| 110 |
retriever = retriever,
|
| 111 |
+
verbose = True,
|
| 112 |
combine_docs_chain_kwargs={'prompt': qa_prompt},
|
| 113 |
+
get_chat_history = lambda h : h,
|
| 114 |
+
rephrase_question = False,
|
| 115 |
+
output_key = 'answer'
|
| 116 |
)
|
| 117 |
|
| 118 |
def chat_interface(question,history):
|
| 119 |
|
| 120 |
result = qa_chain.invoke({"question": question})
|
| 121 |
+
return result['answer'] # If the result is a string, return it directly
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
chatbot_gradio_app = gr.ChatInterface(
|
| 124 |
fn=chat_interface,
|