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