File size: 1,841 Bytes
2920714 462062c 2920714 462062c 2920714 462062c 2920714 462062c 2920714 462062c 2920714 462062c 2920714 462062c 2920714 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | from langchain.prompts import PromptTemplate, ChatPromptTemplate
from langchain.memory import ConversationBufferWindowMemory
from langchain.chains import ConversationalRetrievalChain
def response_chain(db, llm):
"""
Build a conversational RAG chain over the code vector store.
Uses a sliding window memory (last 6 turns) to keep context fresh.
"""
retriever = db.as_retriever(
search_type="similarity",
search_kwargs={"k": 4},
)
memory = ConversationBufferWindowMemory(
k=6,
memory_key="chat_history",
return_messages=True,
output_key="answer",
)
system_prompt = (
"You are an expert software engineer assistant. "
"You have been given relevant code snippets from a GitHub repository as context. "
"Answer the user's question accurately and concisely, referencing the code where useful. "
"If the answer is not in the provided context, say so clearly. "
"Always format code with proper markdown code blocks.\n\n"
"Context:\n{context}"
)
qa_prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
("human", "{question}"),
]
)
condense_prompt = PromptTemplate.from_template(
"Given the chat history and a follow-up question, "
"rewrite the follow-up as a standalone question.\n\n"
"Chat History:\n{chat_history}\n\n"
"Follow-up: {question}\n\n"
"Standalone question:"
)
qa_chain = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=retriever,
memory=memory,
return_source_documents=False,
combine_docs_chain_kwargs={"prompt": qa_prompt},
condense_question_prompt=condense_prompt,
verbose=False,
)
return qa_chain |