rcs3 commited on
Commit
2920714
·
verified ·
1 Parent(s): f1e0216

Update chain.py

Browse files
Files changed (1) hide show
  1. repochat/chain.py +44 -27
repochat/chain.py CHANGED
@@ -1,40 +1,57 @@
1
- from langchain.prompts import PromptTemplate
2
- from langchain.chains.conversation.memory import ConversationBufferMemory
3
  from langchain.chains import ConversationalRetrievalChain
4
 
5
- from .utils import model_prompt, custom_que_prompt
6
 
7
  def response_chain(db, llm):
8
- retriever = db.as_retriever()
9
- search_kwargs = {
10
- "k": 3,
11
- "distance_metric": "cos"
12
- }
 
 
 
 
 
 
 
 
 
 
13
 
14
- retriever.search_kwargs.update(search_kwargs)
 
 
 
 
 
 
 
15
 
16
- memory = ConversationBufferMemory(
17
- memory_key='chat_history',
18
- return_messages=True
 
 
19
  )
20
 
21
- model_template = model_prompt()
22
- QA_CHAIN_PROMPT = PromptTemplate(
23
- input_variables=["context", "question"],
24
- template=model_template
 
 
25
  )
26
- question_prompt = PromptTemplate.from_template(custom_que_prompt())
27
 
28
- qa = ConversationalRetrievalChain.from_llm(
29
- llm=llm,
30
- retriever=retriever,
31
  memory=memory,
32
- chain_type='stuff',
33
- verbose=True,
34
- combine_docs_chain_kwargs={
35
- 'prompt': QA_CHAIN_PROMPT
36
- },
37
- condense_question_prompt=question_prompt
38
  )
39
 
40
- return qa
 
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