Create contextualize_chain.py
Browse files- contextualize_chain.py +16 -0
contextualize_chain.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.chains import LLMChain
|
2 |
+
from langchain_groq import ChatGroq
|
3 |
+
from prompts import contextualize_prompt # A new prompt for contextualizing the query
|
4 |
+
|
5 |
+
class ContextualizeChain(LLMChain):
|
6 |
+
def format_query(self, user_query: str, chat_history: list) -> str:
|
7 |
+
# Use invoke() to format the query based on chat history
|
8 |
+
return self.invoke({"input": user_query, "chat_history": chat_history})
|
9 |
+
|
10 |
+
def get_contextualize_chain():
|
11 |
+
chat_groq_model = ChatGroq(model="Gemma2-9b-It", groq_api_key=os.environ["GROQ_API_KEY"])
|
12 |
+
chain = ContextualizeChain(
|
13 |
+
llm=chat_groq_model,
|
14 |
+
prompt=contextualize_prompt # A prompt for contextualizing the question
|
15 |
+
)
|
16 |
+
return chain
|