doc_chat_agent
langroid/agent/special/doc_chat_agent.py
Agent that supports asking queries about a set of documents, using retrieval-augmented generation (RAG).
Functionality includes:
- summarizing a document, with a custom instruction; see summarize_docs
- asking a question about a document; see answer_from_docs
Note: to use the sentence-transformer embeddings, you must install langroid with the [hf-embeddings] extra, e.g.:
pip install "langroid[hf-embeddings]"
DocChatAgent(config)
¶
Bases: ChatAgent
Agent for chatting with a collection of documents.
Source code in langroid/agent/special/doc_chat_agent.py
clear()
¶
Clear the document collection and the specific collection in vecdb
Source code in langroid/agent/special/doc_chat_agent.py
ingest()
¶
Chunk + embed + store docs specified by self.config.doc_paths
Source code in langroid/agent/special/doc_chat_agent.py
ingest_doc_paths(paths, metadata=[], doc_type=None)
¶
Split, ingest docs from specified paths, do not add these to config.doc_paths.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
paths |
str | bytes | List[str | bytes]
|
document paths, urls or byte-content of docs. The bytes option is intended to support cases where a document has already been read in as bytes (e.g. from an API or a database), and we want to avoid having to write it to a temporary file just to read it back in. |
required |
metadata |
List[Dict[str, Any]] | Dict[str, Any] | DocMetaData | List[DocMetaData]
|
List of metadata dicts, one for each path. If a single dict is passed in, it is used for all paths. |
[]
|
doc_type |
str | DocumentType | None
|
DocumentType to use for parsing, if known.
MUST apply to all docs if specified.
This is especially useful when the |
None
|
Returns: List of Document objects
Source code in langroid/agent/special/doc_chat_agent.py
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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
|
ingest_docs(docs, split=True, metadata=[])
¶
Chunk docs into pieces, map each chunk to vec-embedding, store in vec-db
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs |
List[Document]
|
List of Document objects |
required |
split |
bool
|
Whether to split docs into chunks. Default is True. If False, docs are treated as "chunks" and are not split. |
True
|
metadata |
List[Dict[str, Any]] | Dict[str, Any] | DocMetaData | List[DocMetaData]
|
List of metadata dicts, one for each doc, to augment whatever metadata is already in the doc. [ASSUME no conflicting keys between the two metadata dicts.] If a single dict is passed in, it is used for all docs. |
[]
|
Source code in langroid/agent/special/doc_chat_agent.py
retrieval_tool(msg)
¶
Handle the RetrievalTool message
Source code in langroid/agent/special/doc_chat_agent.py
document_compatible_dataframe(df, content='content', metadata=[])
staticmethod
¶
Convert dataframe so it is compatible with Document class: - has "content" column - has an "id" column to be used as Document.metadata.id
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
dataframe to convert |
required |
content |
str
|
name of content column |
'content'
|
metadata |
List[str]
|
list of metadata column names |
[]
|
Returns:
Type | Description |
---|---|
Tuple[DataFrame, List[str]]
|
Tuple[pd.DataFrame, List[str]]: dataframe, metadata - dataframe: dataframe with "content" column and "id" column - metadata: list of metadata column names, including "id" |
Source code in langroid/agent/special/doc_chat_agent.py
ingest_dataframe(df, content='content', metadata=[])
¶
Ingest a dataframe into vecdb.
Source code in langroid/agent/special/doc_chat_agent.py
setup_documents(docs=[], filter=None)
¶
Setup self.chunked_docs
and self.chunked_docs_clean
based on possible filter.
These will be used in various non-vector-based search functions,
e.g. self.get_similar_chunks_bm25(), self.get_fuzzy_matches(), etc.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs |
List[Document]
|
List of Document objects. This is empty when we are calling this method after initial doc ingestion. |
[]
|
filter |
str | None
|
Filter condition for various lexical/semantic search fns. |
None
|
Source code in langroid/agent/special/doc_chat_agent.py
get_field_values(fields)
¶
Get string-listing of possible values of each field, e.g. { "genre": "crime, drama, mystery, ... (10 more)", "certificate": "R, PG-13, PG, R", } The field names may have "metadata." prefix, e.g. "metadata.genre".
Source code in langroid/agent/special/doc_chat_agent.py
doc_length(docs)
¶
Calc token-length of a list of docs Args: docs: list of Document objects Returns: int: number of tokens
Source code in langroid/agent/special/doc_chat_agent.py
user_docs_ingest_dialog()
¶
Ask user to select doc-collection, enter filenames/urls, and ingest into vecdb.
Source code in langroid/agent/special/doc_chat_agent.py
doc_string(docs)
staticmethod
¶
Generate a string representation of a list of docs. Args: docs: list of Document objects Returns: str: string representation
Source code in langroid/agent/special/doc_chat_agent.py
get_summary_answer(question, passages)
¶
Given a question and a list of (possibly) doc snippets,
generate an answer if possible
Args:
question: question to answer
passages: list of Document
objects each containing a possibly relevant
snippet, and metadata
Returns:
a Document
object containing the answer,
and metadata containing source citations
Source code in langroid/agent/special/doc_chat_agent.py
rerank_with_diversity(passages)
¶
Rerank a list of items in such a way that each successive item is least similar (on average) to the earlier items.
Args: query (str): The query for which the passages are relevant. passages (List[Document]): A list of Documents to be reranked.
Returns: List[Documents]: A reranked list of Documents.
Source code in langroid/agent/special/doc_chat_agent.py
rerank_to_periphery(passages)
¶
Rerank to avoid Lost In the Middle (LIM) problem, where LLMs pay more attention to items at the ends of a list, rather than the middle. So we re-rank to make the best passages appear at the periphery of the list. https://arxiv.org/abs/2307.03172
Example reranking: 1 2 3 4 5 6 7 8 9 ==> 1 3 5 7 9 8 6 4 2
Parameters:
Name | Type | Description | Default |
---|---|---|---|
passages |
List[Document]
|
A list of Documents to be reranked. |
required |
Returns:
Type | Description |
---|---|
List[Document]
|
List[Documents]: A reranked list of Documents. |
Source code in langroid/agent/special/doc_chat_agent.py
add_context_window(docs_scores)
¶
In each doc's metadata, there may be a window_ids field indicating the ids of the chunks around the current chunk. We use these stored window_ids to retrieve the desired number (self.config.n_neighbor_chunks) of neighbors on either side of the current chunk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs_scores |
List[Tuple[Document, float]]
|
List of pairs of documents to add context windows to together with their match scores. |
required |
Returns:
Type | Description |
---|---|
List[Tuple[Document, float]]
|
List[Tuple[Document, float]]: List of (Document, score) tuples. |
Source code in langroid/agent/special/doc_chat_agent.py
get_semantic_search_results(query, k=10)
¶
Get semantic search results from vecdb. Args: query (str): query to search for k (int): number of results to return Returns: List[Tuple[Document, float]]: List of (Document, score) tuples.
Source code in langroid/agent/special/doc_chat_agent.py
get_relevant_chunks(query, query_proxies=[])
¶
The retrieval stage in RAG: get doc-chunks that are most "relevant" to the query (and possibly any proxy queries), from the document-store, which currently is the vector store, but in theory could be any document store, or even web-search. This stage does NOT involve an LLM, and the retrieved chunks could either be pre-chunked text (from the initial pre-processing stage where chunks were stored in the vector store), or they could be dynamically retrieved based on a window around a lexical match.
These are the steps (some optional based on config): - semantic search based on vector-embedding distance, from vecdb - lexical search using bm25-ranking (keyword similarity) - fuzzy matching (keyword similarity) - re-ranking of doc-chunks by relevance to query, using cross-encoder, and pick top k
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
str
|
original query (assumed to be in stand-alone form) |
required |
query_proxies |
List[str]
|
possible rephrases, or hypothetical answer to query (e.g. for HyDE-type retrieval) |
[]
|
Returns:
Source code in langroid/agent/special/doc_chat_agent.py
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 |
|
get_relevant_extracts(query)
¶
Get list of (verbatim) extracts from doc-chunks relevant to answering a query.
These are the stages (some optional based on config): - use LLM to convert query to stand-alone query - optionally use LLM to rephrase query to use below - optionally use LLM to generate hypothetical answer (HyDE) to use below. - get_relevant_chunks(): get doc-chunks relevant to query and proxies - use LLM to get relevant extracts from doc-chunks
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
str
|
query to search for |
required |
Returns:
Name | Type | Description |
---|---|---|
query |
str
|
stand-alone version of input query |
List[Document]
|
List[Document]: list of relevant extracts |
Source code in langroid/agent/special/doc_chat_agent.py
get_verbatim_extracts(query, passages)
¶
Run RelevanceExtractorAgent in async/concurrent mode on passages, to extract portions relevant to answering query, from each passage. Args: query (str): query to answer passages (List[Documents]): list of passages to extract from
Returns:
Type | Description |
---|---|
List[Document]
|
List[Document]: list of Documents containing extracts and metadata. |
Source code in langroid/agent/special/doc_chat_agent.py
answer_from_docs(query)
¶
Answer query based on relevant docs from the VecDB
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
str
|
query to answer |
required |
Returns:
Name | Type | Description |
---|---|---|
Document |
ChatDocument
|
answer |
Source code in langroid/agent/special/doc_chat_agent.py
summarize_docs(instruction='Give a concise summary of the following text:')
¶
Summarize all docs
Source code in langroid/agent/special/doc_chat_agent.py
justify_response()
¶
Show evidence for last response