-
Notifications
You must be signed in to change notification settings - Fork 2
/
llm_helper.py
332 lines (283 loc) · 12.3 KB
/
llm_helper.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
from typing import Optional, List
from langchain.agents import AgentExecutor
from langchain.agents.format_scratchpad.openai_tools import format_to_openai_tool_messages
from langchain.agents.output_parsers.openai_tools import OpenAIToolsAgentOutputParser
from langchain.chat_models import ChatOpenAI
from langchain.schema.runnable import RunnableMap
from langchain.prompts.prompt import PromptTemplate
from langchain.prompts import ChatPromptTemplate
from langchain.schema.runnable import RunnablePassthrough
from langchain.schema.output_parser import StrOutputParser
from operator import itemgetter
from langchain.schema.messages import HumanMessage, SystemMessage, AIMessage
from langchain.callbacks.streamlit.streamlit_callback_handler import StreamlitCallbackHandler
from langchain_core.prompts import MessagesPlaceholder
def format_docs(docs):
res = ""
# res = str(docs)
for doc in docs:
escaped_page_content = doc.page_content.replace("\n", "\\n")
res += "<doc>\n"
res += f" <content>{escaped_page_content}</content>\n"
for m in doc.metadata:
res += f" <{m}>{doc.metadata[m]}</{m}>\n"
res += "</doc>\n"
return res
def convert_message(m):
if m["role"] == "user":
return HumanMessage(content=m["content"])
elif m["role"] == "assistant":
return AIMessage(content=m["content"])
elif m["role"] == "system":
return SystemMessage(content=m["content"])
else:
raise ValueError(f"Unknown role {m['role']}")
_condense_template = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question, in its original language.
Chat History:
{chat_history}
Follow Up Input: {input}
Standalone question:"""
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_condense_template)
_rag_template = """Answer the question based only on the following context, citing the page number(s) of the document(s) you used to answer the question:
{context}
Question: {question}
"""
ANSWER_PROMPT = ChatPromptTemplate.from_template(_rag_template)
def _format_chat_history(chat_history):
def format_single_chat_message(m):
if type(m) is HumanMessage:
return "Human: " + m.content
elif type(m) is AIMessage:
return "Assistant: " + m.content
elif type(m) is SystemMessage:
return "System: " + m.content
else:
raise ValueError(f"Unknown role {m['role']}")
return "\n".join([format_single_chat_message(m) for m in chat_history])
def get_standalone_question_from_chat_history_chain():
_inputs = RunnableMap(
standalone_question=RunnablePassthrough.assign(
chat_history=lambda x: _format_chat_history(x["chat_history"])
)
| CONDENSE_QUESTION_PROMPT
| ChatOpenAI(temperature=0)
| StrOutputParser(),
)
return _inputs
def get_rag_chain(file_name="Mahmoudi_Nima_202202_PhD.pdf", index_folder="index", retrieval_cb=None):
vectorstore = get_search_index(file_name, index_folder)
retriever = vectorstore.as_retriever()
if retrieval_cb is None:
retrieval_cb = lambda x: x
def context_update_fn(q):
retrieval_cb([q])
return q
_inputs = RunnableMap(
standalone_question=RunnablePassthrough.assign(
chat_history=lambda x: _format_chat_history(x["chat_history"])
)
| CONDENSE_QUESTION_PROMPT
| ChatOpenAI(temperature=0)
| StrOutputParser(),
)
_context = {
"context": itemgetter("standalone_question") | RunnablePassthrough(context_update_fn) | retriever | format_docs,
"question": lambda x: x["standalone_question"],
}
conversational_qa_chain = _inputs | _context | ANSWER_PROMPT | ChatOpenAI()
return conversational_qa_chain
def reciprocal_rank_fusion(results: List[List], k=60):
from langchain.load import dumps, loads
fused_scores = {}
for docs in results:
# Assumes the docs are returned in sorted order of relevance
for rank, doc in enumerate(docs):
doc_str = dumps(doc)
if doc_str not in fused_scores:
fused_scores[doc_str] = 0
fused_scores[doc_str] += 1 / (rank + k)
reranked_results = [
(loads(doc), score)
for doc, score in sorted(fused_scores.items(), key=lambda x: x[1], reverse=True)
]
return reranked_results
def get_search_query_generation_chain():
from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate
prompt = ChatPromptTemplate(
input_variables=['original_query'],
messages=[
SystemMessagePromptTemplate(
prompt=PromptTemplate(
input_variables=[],
template='You are a helpful assistant that generates multiple search queries based on a single input query.'
)
),
HumanMessagePromptTemplate(
prompt=PromptTemplate(
input_variables=['original_query'],
template='Generate multiple search queries related to: {original_query} \n OUTPUT (4 queries):'
)
)
]
)
generate_queries = (
prompt |
ChatOpenAI(temperature=0) |
StrOutputParser() |
(lambda x: x.split("\n"))
)
return generate_queries
def get_rag_fusion_chain(file_name="Mahmoudi_Nima_202202_PhD.pdf", index_folder="index", retrieval_cb=None):
vectorstore = get_search_index(file_name, index_folder)
retriever = vectorstore.as_retriever()
query_generation_chain = get_search_query_generation_chain()
_inputs = RunnableMap(
standalone_question=RunnablePassthrough.assign(
chat_history=lambda x: _format_chat_history(x["chat_history"])
)
| CONDENSE_QUESTION_PROMPT
| ChatOpenAI(temperature=0)
| StrOutputParser(),
)
if retrieval_cb is None:
retrieval_cb = lambda x: x
_context = {
"context":
RunnablePassthrough.assign(
original_query=lambda x: x["standalone_question"]
)
| query_generation_chain
| retrieval_cb
| retriever.map()
| reciprocal_rank_fusion
| (lambda x: [item[0] for item in x])
| format_docs,
"question": lambda x: x["standalone_question"],
}
conversational_qa_chain = _inputs | _context | ANSWER_PROMPT | ChatOpenAI()
return conversational_qa_chain
def get_search_tool_from_indexes(search_indexes, st_cb: Optional[StreamlitCallbackHandler] = None):
from langchain.agents import tool
from agent_helper import retry_and_streamlit_callback
@tool
@retry_and_streamlit_callback(st_cb=st_cb, tool_name="Content Search Tool")
def search(query: str) -> str:
"""Search the contents of the source documents for the queries."""
docs = []
for index in search_indexes:
docs += index.similarity_search(query, k=5)
return format_docs(docs)
return search
def get_lc_oai_tools(file_names: List[str], index_folder: str = "index",
st_cb: Optional[StreamlitCallbackHandler] = None):
from langchain.tools.render import format_tool_to_openai_tool
search_indexes = get_search_index(file_names, index_folder)
# Assuming get_search_index now returns a list of indexes for multiple files
lc_tool = get_search_tool_from_indexes(search_indexes, st_cb=st_cb)
oai_tool = format_tool_to_openai_tool(lc_tool)
return [lc_tool], [oai_tool]
def get_agent_chain(file_names: List[str], index_folder="index", callbacks=None,
st_cb: Optional[StreamlitCallbackHandler] = None):
if callbacks is None:
callbacks = []
lc_tools, oai_tools = get_lc_oai_tools(file_names, index_folder, st_cb)
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant, use the search tool to answer the user's question and cite only the page number when you use information coming (like [p1]) from the source document.\nchat history: {chat_history}"),
("user", "{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad"),
])
llm = ChatOpenAI(temperature=0, model="gpt-4-turbo-2024-04-09")
# Ensure oai_tools is a list of correctly formatted tools.
# If oai_tools contains more than one tool, make sure they are formatted correctly as a list.
agent = ({
"input": lambda x: x["input"],
"agent_scratchpad": lambda x: format_to_openai_tool_messages(x["intermediate_steps"]),
"chat_history": lambda x: _format_chat_history(x["chat_history"]),
}
| prompt
| llm.bind(tools=oai_tools) # Pass the entire list of tools if multiple tools are expected
| OpenAIToolsAgentOutputParser())
agent_executor = AgentExecutor(agent=agent, tools=lc_tools, verbose=True, callbacks=callbacks)
return agent_executor
from typing import Optional, List, Tuple
# Add imports for handling multiple documents
from langchain.vectorstores import FAISS
from langchain.embeddings.openai import OpenAIEmbeddings
# Modify the function to accept a list of file names
def get_search_index(file_names: List[str], index_folder: str = "index") -> List[FAISS]:
search_indexes = []
for file_name in file_names:
search_index = FAISS.load_local(
folder_path=index_folder,
index_name=file_name + ".index",
embeddings=OpenAIEmbeddings(),
)
search_indexes.append(search_index)
return search_indexes
# Adjusted to handle multiple indexes correctly
def get_rag_chain_files(file_names: List[str], index_folder: str = "index", retrieval_cb=None):
vectorstores = get_search_index(file_names, index_folder)
# This function now handles multiple retrievers correctly
_inputs = RunnableMap(
standalone_question=RunnablePassthrough.assign(
chat_history=lambda x: _format_chat_history(x["chat_history"])
)
| CONDENSE_QUESTION_PROMPT
| ChatOpenAI(temperature=0)
| StrOutputParser(),
)
if retrieval_cb is None:
retrieval_cb = lambda x: x
def multi_retriever_fusion(query):
docs = []
for vectorstore in vectorstores:
retriever = vectorstore.as_retriever()
retrieved_docs = retriever.get_relevant_documents(query)
docs += retrieved_docs
return format_docs(docs)
_context = {
"context": itemgetter("standalone_question") | RunnablePassthrough(retrieval_cb) | multi_retriever_fusion,
"question": lambda x: x["standalone_question"],
}
conversational_qa_chain = _inputs | _context | ANSWER_PROMPT | ChatOpenAI()
return conversational_qa_chain
def get_rag_fusion_chain_files(file_names: List[str], index_folder: str = "index", retrieval_cb=None):
vectorstores = get_search_index(file_names, index_folder)
query_generation_chain = get_search_query_generation_chain()
_inputs = RunnableMap(
standalone_question=RunnablePassthrough.assign(
chat_history=lambda x: _format_chat_history(x["chat_history"])
)
| CONDENSE_QUESTION_PROMPT
| ChatOpenAI(temperature=0)
| StrOutputParser(),
)
if retrieval_cb is None:
retrieval_cb = lambda x: x
def retrieve_and_fuse_queries(queries):
all_docs = []
for query in queries:
docs_for_query = []
for vectorstore in vectorstores:
retriever = vectorstore.as_retriever()
retrieved_docs = retriever.get_relevant_documents(query)
docs_for_query += [doc for doc in retrieved_docs]
all_docs.append(docs_for_query)
fused_docs = reciprocal_rank_fusion(all_docs)
return [doc for doc, _ in fused_docs]
_context = {
"context":
RunnablePassthrough.assign(
original_query=lambda x: x["standalone_question"]
)
| query_generation_chain
| retrieval_cb
| (lambda queries: retrieve_and_fuse_queries(queries))
| format_docs,
"question": lambda x: x["standalone_question"],
}
conversational_qa_chain = _inputs | _context | ANSWER_PROMPT | ChatOpenAI()
return conversational_qa_chain
if __name__ == "__main__":
print('='*200)
print('RAG Chain')