|
|
|
import os |
|
from langchain.memory import ConversationBufferMemory |
|
from langchain.chains import LLMChain |
|
from langchain.prompts.chat import ( |
|
ChatPromptTemplate, |
|
SystemMessagePromptTemplate, |
|
MessagesPlaceholder, |
|
HumanMessagePromptTemplate, |
|
) |
|
|
|
from langchain_groq import ChatGroq |
|
|
|
|
|
memory = ConversationBufferMemory(return_messages=True) |
|
|
|
|
|
restatement_system_prompt = ( |
|
"Given the entire chat history below and the latest user question, |
|
your ONLY job is to rewrite or restate the latest question so it |
|
makes sense on its own. |
|
Do NOT repeat or quote large sections of the history. |
|
Do NOT provide the answer or any additional explanation. |
|
Respond ONLY with a short, standalone question." |
|
|
|
) |
|
|
|
|
|
restatement_prompt = ChatPromptTemplate.from_messages([ |
|
SystemMessagePromptTemplate.from_template(restatement_system_prompt), |
|
MessagesPlaceholder(variable_name="chat_history"), |
|
HumanMessagePromptTemplate.from_template("{input}") |
|
]) |
|
|
|
|
|
|
|
restatement_llm = ChatGroq( |
|
model="llama3-70b-8192", |
|
|
|
groq_api_key=os.environ["GROQ_API_KEY"] |
|
) |
|
|
|
|
|
restatement_chain = LLMChain( |
|
llm=restatement_llm, |
|
prompt=restatement_prompt |
|
) |
|
|