Bindings
SupermatRetriever
Bases: BaseRetriever
Supermat Langchain Custom Retriever. This uses any Langchain VectorStore and overrides the documents retrieval methods to make it work for Supermat. NOTE: Currently this only works on Text chunks.
from supermat.langchain.bindings import SupermatRetriever
from langchain_chroma import Chroma
from langchain_huggingface import HuggingFaceEmbeddings
retriever = SupermatRetriever(
parsed_docs=FileProcessor.process_file(pdf_file_path),
document_name=pdf_file_path.stem,
vector_store=Chroma(
embedding_function=HuggingFaceEmbeddings(
model_name="thenlper/gte-base",
)
),
)
Args:
parsed_docs (ParsedDocumentType): The supermat parsed documents.
vector_store (VectorStore): The vector store used to store the document chunks.
vector_store_retriver_kwargs (dict[str, Any], optional): VectorStore
kwargs used during initialization.
Defaults to {}
.
max_chunk_length (int, optional): Max character length. NOTE: This needs to be based on tokens instead.
Defaults to 8000.
store_sentences (bool, optional): Store sentence level chunks in vector store
which will then be converted to paragraphs before sending to LLM. Defaults to False.
Source code in supermat/langchain/bindings.py
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 |
|
get_default_chain(retriever, llm_model, substitute_references=False, return_context=False)
Default chain that implements citation where LLM returns the referenced id as well instead of directly returning the values verbatim. This saves output tokens being generated and the actual content is returned during post processing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
retriever |
SupermatRetriever
|
SupermatRetriever that retrieves the relevant document chunks for LLM context. |
required |
llm_model |
BaseChatModel | BaseLLM
|
The LLM model used for inference |
required |
substitute_references |
bool
|
Whether to replace the citations direction, or as a separate section. Defaults to False. |
False
|
return_context |
bool
|
Return retrived documents for debugging. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
RunnableSerializable |
RunnableSerializable
|
Langchain chain to run prompt query. |
Source code in supermat/langchain/bindings.py
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 |
|
parse_cite_blocks(text)
Parses the <cite ref='citation_id', start=0, end=None />
cite block in a text.
NOTE: Could not get the LLM to return start and end via prompt templating. This is a demo to show that citations are possible, and thus reduces output tokens from llm.
With citations, we can avoid llm's returning tokens which are already available in context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
Text containing the cite block |
required |
Returns:
Type | Description |
---|---|
tuple[ParsedCite]
|
tuple[ParsedCite]: Parsed citations found in text. |
Source code in supermat/langchain/bindings.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
|
post_process(chain_output, substitute=False)
LLM chain link to replace references with content using regex matching.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
chain_output |
ChainOutput
|
Output of previous Lanchain link. |
required |
substitute |
bool
|
Substitute the reference matched directly. Defaults to False. If, substitute is False, in a new paragraph, the referenced chunk is dumped directly. |
False
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Returns model output with reference ids parsed to actual content. |
Source code in supermat/langchain/bindings.py
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 |
|