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