Update cleaner_chain.py
Browse files- cleaner_chain.py +13 -4
cleaner_chain.py
CHANGED
|
@@ -1,13 +1,22 @@
|
|
| 1 |
-
# cleaner_chain.py
|
| 2 |
import os
|
| 3 |
from langchain.chains import LLMChain
|
| 4 |
from langchain_groq import ChatGroq
|
| 5 |
from prompts import cleaner_prompt
|
| 6 |
|
| 7 |
class CleanerChain(LLMChain):
|
| 8 |
-
def merge(self, kb: str, web: str) -> str:
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def get_cleaner_chain() -> CleanerChain:
|
| 13 |
chat_groq_model = ChatGroq(model="Gemma2-9b-It", groq_api_key=os.environ["GROQ_API_KEY"])
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from langchain.chains import LLMChain
|
| 3 |
from langchain_groq import ChatGroq
|
| 4 |
from prompts import cleaner_prompt
|
| 5 |
|
| 6 |
class CleanerChain(LLMChain):
|
| 7 |
+
def merge(self, kb: str, web: str, chat_history: list) -> str:
|
| 8 |
+
"""
|
| 9 |
+
Merges the KB answer and web answer with chat history context.
|
| 10 |
+
Appends the chat history before invoking the cleaner chain.
|
| 11 |
+
"""
|
| 12 |
+
# Convert chat history into a formatted string
|
| 13 |
+
history_context = "\n".join([f"User: {msg['content']}" for msg in chat_history])
|
| 14 |
+
|
| 15 |
+
# Add the chat history context to the kb and web answers
|
| 16 |
+
combined_input = f"{history_context}\nKB Answer: {kb}\nWeb Answer: {web}"
|
| 17 |
+
|
| 18 |
+
# Use invoke() to merge the context with the knowledge base and web answers
|
| 19 |
+
return self.invoke({"kb_answer": combined_input, "web_answer": web})
|
| 20 |
|
| 21 |
def get_cleaner_chain() -> CleanerChain:
|
| 22 |
chat_groq_model = ChatGroq(model="Gemma2-9b-It", groq_api_key=os.environ["GROQ_API_KEY"])
|