-
Notifications
You must be signed in to change notification settings - Fork 161
/
doc_chat_agent.py
1394 lines (1254 loc) · 54 KB
/
doc_chat_agent.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
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]"
"""
import logging
from functools import cache
from typing import Any, Dict, List, Optional, Set, Tuple, no_type_check
import nest_asyncio
import numpy as np
import pandas as pd
from rich.prompt import Prompt
from langroid.agent.batch import run_batch_tasks
from langroid.agent.chat_agent import ChatAgent, ChatAgentConfig
from langroid.agent.chat_document import ChatDocMetaData, ChatDocument
from langroid.agent.special.relevance_extractor_agent import (
RelevanceExtractorAgent,
RelevanceExtractorAgentConfig,
)
from langroid.agent.task import Task
from langroid.agent.tools.retrieval_tool import RetrievalTool
from langroid.embedding_models.models import OpenAIEmbeddingsConfig
from langroid.language_models.base import StreamingIfAllowed
from langroid.language_models.openai_gpt import OpenAIChatModel, OpenAIGPTConfig
from langroid.mytypes import DocMetaData, Document, Entity
from langroid.parsing.document_parser import DocumentType
from langroid.parsing.parser import Parser, ParsingConfig, PdfParsingConfig, Splitter
from langroid.parsing.repo_loader import RepoLoader
from langroid.parsing.search import (
find_closest_matches_with_bm25,
find_fuzzy_matches_in_docs,
preprocess_text,
)
from langroid.parsing.table_loader import describe_dataframe
from langroid.parsing.url_loader import URLLoader
from langroid.parsing.urls import get_list_from_user, get_urls_paths_bytes_indices
from langroid.parsing.utils import batched
from langroid.prompts.prompts_config import PromptsConfig
from langroid.prompts.templates import SUMMARY_ANSWER_PROMPT_GPT4
from langroid.utils.constants import NO_ANSWER
from langroid.utils.output import show_if_debug, status
from langroid.utils.pydantic_utils import dataframe_to_documents, extract_fields
from langroid.vector_store.base import VectorStore, VectorStoreConfig
from langroid.vector_store.lancedb import LanceDBConfig
@cache
def apply_nest_asyncio() -> None:
nest_asyncio.apply()
logger = logging.getLogger(__name__)
DEFAULT_DOC_CHAT_INSTRUCTIONS = """
Your task is to answer questions about various documents.
You will be given various passages from these documents, and asked to answer questions
about them, or summarize them into coherent answers.
"""
DEFAULT_DOC_CHAT_SYSTEM_MESSAGE = """
You are a helpful assistant, helping me understand a collection of documents.
"""
has_sentence_transformers = False
try:
from sentence_transformers import SentenceTransformer # noqa: F401
has_sentence_transformers = True
except ImportError:
pass
def extract_markdown_references(md_string: str) -> list[int]:
"""
Extracts markdown references (e.g., [^1], [^2]) from a string and returns
them as a sorted list of integers.
Args:
md_string (str): The markdown string containing references.
Returns:
list[int]: A sorted list of unique integers from the markdown references.
"""
import re
# Regex to find all occurrences of [^<number>]
matches = re.findall(r"\[\^(\d+)\]", md_string)
# Convert matches to integers, remove duplicates with set, and sort
return sorted(set(int(match) for match in matches))
def format_footnote_text(content: str, width: int = 80) -> str:
"""
Formats the content part of a footnote (i.e. not the first line that
appears right after the reference [^4])
It wraps the text so that no line is longer than the specified width and indents
lines as necessary for markdown footnotes.
Args:
content (str): The text of the footnote to be formatted.
width (int): Maximum width of the text lines.
Returns:
str: Properly formatted markdown footnote text.
"""
import textwrap
# Wrap the text to the specified width
wrapped_lines = textwrap.wrap(content, width)
if len(wrapped_lines) == 0:
return ""
indent = " " # Indentation for markdown footnotes
return indent + ("\n" + indent).join(wrapped_lines)
class DocChatAgentConfig(ChatAgentConfig):
system_message: str = DEFAULT_DOC_CHAT_SYSTEM_MESSAGE
user_message: str = DEFAULT_DOC_CHAT_INSTRUCTIONS
summarize_prompt: str = SUMMARY_ANSWER_PROMPT_GPT4
# extra fields to include in content as key=value pairs
# (helps retrieval for table-like data)
add_fields_to_content: List[str] = []
filter_fields: List[str] = [] # fields usable in filter
retrieve_only: bool = False # only retr relevant extracts, don't gen summary answer
extraction_granularity: int = 1 # granularity (in sentences) for relev extraction
filter: str | None = (
None # filter condition for various lexical/semantic search fns
)
conversation_mode: bool = True # accumulate message history?
# In assistant mode, DocChatAgent receives questions from another Agent,
# and those will already be in stand-alone form, so in this mode
# there is no need to convert them to stand-alone form.
assistant_mode: bool = False
# Use LLM to generate hypothetical answer A to the query Q,
# and use the embed(A) to find similar chunks in vecdb.
# Referred to as HyDE in the paper:
# https://arxiv.org/pdf/2212.10496.pdf
# It is False by default; its benefits depends on the context.
hypothetical_answer: bool = False
n_query_rephrases: int = 0
n_neighbor_chunks: int = 0 # how many neighbors on either side of match to retrieve
n_fuzzy_neighbor_words: int = 100 # num neighbor words to retrieve for fuzzy match
use_fuzzy_match: bool = True
use_bm25_search: bool = True
cross_encoder_reranking_model: str = (
"cross-encoder/ms-marco-MiniLM-L-6-v2" if has_sentence_transformers else ""
)
rerank_diversity: bool = True # rerank to maximize diversity?
rerank_periphery: bool = True # rerank to avoid Lost In the Middle effect?
rerank_after_adding_context: bool = True # rerank after adding context window?
embed_batch_size: int = 500 # get embedding of at most this many at a time
cache: bool = True # cache results
debug: bool = False
stream: bool = True # allow streaming where needed
split: bool = True # use chunking
relevance_extractor_config: None | RelevanceExtractorAgentConfig = (
RelevanceExtractorAgentConfig(
llm=None # use the parent's llm unless explicitly set here
)
)
doc_paths: List[str | bytes] = []
default_paths: List[str] = [
"https://news.ycombinator.com/item?id=35629033",
"https://www.newyorker.com/tech/annals-of-technology/chatgpt-is-a-blurry-jpeg-of-the-web",
"https://www.wired.com/1995/04/maes/",
"https://cthiriet.com/articles/scaling-laws",
"https://www.jasonwei.net/blog/emergence",
"https://www.quantamagazine.org/the-unpredictable-abilities-emerging-from-large-ai-models-20230316/",
"https://ai.googleblog.com/2022/11/characterizing-emergent-phenomena-in.html",
]
parsing: ParsingConfig = ParsingConfig( # modify as needed
splitter=Splitter.TOKENS,
chunk_size=1000, # aim for this many tokens per chunk
overlap=100, # overlap between chunks
max_chunks=10_000,
# aim to have at least this many chars per chunk when
# truncating due to punctuation
min_chunk_chars=200,
discard_chunk_chars=5, # discard chunks with fewer than this many chars
n_similar_docs=3,
n_neighbor_ids=0, # num chunk IDs to store on either side of each chunk
pdf=PdfParsingConfig(
# NOTE: PDF parsing is extremely challenging, and each library
# has its own strengths and weaknesses.
# Try one that works for your use case.
# or "unstructured", "pdfplumber", "fitz", "pypdf"
library="pdfplumber",
),
)
from langroid.embedding_models.models import SentenceTransformerEmbeddingsConfig
hf_embed_config = SentenceTransformerEmbeddingsConfig(
model_type="sentence-transformer",
model_name="BAAI/bge-large-en-v1.5",
)
oai_embed_config = OpenAIEmbeddingsConfig(
model_type="openai",
model_name="text-embedding-ada-002",
dims=1536,
)
# Allow vecdb to be None in case we want to explicitly set it later
vecdb: Optional[VectorStoreConfig] = LanceDBConfig(
collection_name="doc-chat-lancedb",
replace_collection=True,
storage_path=".lancedb/data/",
embedding=hf_embed_config if has_sentence_transformers else oai_embed_config,
)
llm: OpenAIGPTConfig = OpenAIGPTConfig(
type="openai",
chat_model=OpenAIChatModel.GPT4,
completion_model=OpenAIChatModel.GPT4,
timeout=40,
)
prompts: PromptsConfig = PromptsConfig(
max_tokens=1000,
)
class DocChatAgent(ChatAgent):
"""
Agent for chatting with a collection of documents.
"""
def __init__(
self,
config: DocChatAgentConfig,
):
super().__init__(config)
self.config: DocChatAgentConfig = config
self.original_docs: List[Document] = []
self.original_docs_length = 0
self.from_dataframe = False
self.df_description = ""
self.chunked_docs: List[Document] = []
self.chunked_docs_clean: List[Document] = []
self.response: None | Document = None
if len(config.doc_paths) > 0:
self.ingest()
def clear(self) -> None:
"""Clear the document collection and the specific collection in vecdb"""
self.original_docs = []
self.original_docs_length = 0
self.chunked_docs = []
self.chunked_docs_clean = []
if self.vecdb is None:
logger.warning("Attempting to clear VecDB, but VecDB not set.")
return
collection_name = self.vecdb.config.collection_name
if collection_name is None:
return
try:
# Note we may have used a vecdb with a config.collection_name
# different from the agent's config.vecdb.collection_name!!
self.vecdb.delete_collection(collection_name)
self.vecdb = VectorStore.create(self.vecdb.config)
except Exception as e:
logger.warning(
f"""
Error while deleting collection {collection_name}:
{e}
"""
)
def ingest(self) -> None:
"""
Chunk + embed + store docs specified by self.config.doc_paths
Returns:
dict with keys:
n_splits: number of splits
urls: list of urls
paths: list of file paths
"""
if len(self.config.doc_paths) == 0:
# we must be using a previously defined collection
# But let's get all the chunked docs so we can
# do keyword and other non-vector searches
if self.vecdb is None:
raise ValueError("VecDB not set")
self.setup_documents(filter=self.config.filter)
return
self.ingest_doc_paths(self.config.doc_paths) # type: ignore
def ingest_doc_paths(
self,
paths: str | bytes | List[str | bytes],
metadata: (
List[Dict[str, Any]] | Dict[str, Any] | DocMetaData | List[DocMetaData]
) = [],
doc_type: str | DocumentType | None = None,
) -> List[Document]:
"""Split, ingest docs from specified paths,
do not add these to config.doc_paths.
Args:
paths: 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.
metadata: List of metadata dicts, one for each path.
If a single dict is passed in, it is used for all paths.
doc_type: DocumentType to use for parsing, if known.
MUST apply to all docs if specified.
This is especially useful when the `paths` are of bytes type,
to help with document type detection.
Returns:
List of Document objects
"""
if isinstance(paths, str) or isinstance(paths, bytes):
paths = [paths]
all_paths = paths
paths_meta: Dict[int, Any] = {}
urls_meta: Dict[int, Any] = {}
idxs = range(len(all_paths))
url_idxs, path_idxs, bytes_idxs = get_urls_paths_bytes_indices(all_paths)
urls = [all_paths[i] for i in url_idxs]
paths = [all_paths[i] for i in path_idxs]
bytes_list = [all_paths[i] for i in bytes_idxs]
path_idxs.extend(bytes_idxs)
paths.extend(bytes_list)
if (isinstance(metadata, list) and len(metadata) > 0) or not isinstance(
metadata, list
):
if isinstance(metadata, list):
idx2meta = {
p: (
m
if isinstance(m, dict)
else (isinstance(m, DocMetaData) and m.dict())
) # appease mypy
for p, m in zip(idxs, metadata)
}
elif isinstance(metadata, dict):
idx2meta = {p: metadata for p in idxs}
else:
idx2meta = {p: metadata.dict() for p in idxs}
urls_meta = {u: idx2meta[u] for u in url_idxs}
paths_meta = {p: idx2meta[p] for p in path_idxs}
docs: List[Document] = []
parser = Parser(self.config.parsing)
if len(urls) > 0:
for ui in url_idxs:
meta = urls_meta.get(ui, {})
loader = URLLoader(urls=[all_paths[ui]], parser=parser) # type: ignore
url_docs = loader.load()
# update metadata of each doc with meta
for d in url_docs:
d.metadata = d.metadata.copy(update=meta)
docs.extend(url_docs)
if len(paths) > 0: # paths OR bytes are handled similarly
for pi in path_idxs:
meta = paths_meta.get(pi, {})
p = all_paths[pi]
path_docs = RepoLoader.get_documents(
p,
parser=parser,
doc_type=doc_type,
)
# update metadata of each doc with meta
for d in path_docs:
d.metadata = d.metadata.copy(update=meta)
docs.extend(path_docs)
n_docs = len(docs)
n_splits = self.ingest_docs(docs, split=self.config.split)
if n_docs == 0:
return []
n_urls = len(urls)
n_paths = len(paths)
print(
f"""
[green]I have processed the following {n_urls} URLs
and {n_paths} docs into {n_splits} parts:
""".strip()
)
path_reps = [p if isinstance(p, str) else "bytes" for p in paths]
print("\n".join([u for u in urls if isinstance(u, str)])) # appease mypy
print("\n".join(path_reps))
return docs
def ingest_docs(
self,
docs: List[Document],
split: bool = True,
metadata: (
List[Dict[str, Any]] | Dict[str, Any] | DocMetaData | List[DocMetaData]
) = [],
) -> int:
"""
Chunk docs into pieces, map each chunk to vec-embedding, store in vec-db
Args:
docs: List of Document objects
split: Whether to split docs into chunks. Default is True.
If False, docs are treated as "chunks" and are not split.
metadata: 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.
"""
if isinstance(metadata, list) and len(metadata) > 0:
for d, m in zip(docs, metadata):
d.metadata = d.metadata.copy(
update=m if isinstance(m, dict) else m.dict() # type: ignore
)
elif isinstance(metadata, dict):
for d in docs:
d.metadata = d.metadata.copy(update=metadata)
elif isinstance(metadata, DocMetaData):
for d in docs:
d.metadata = d.metadata.copy(update=metadata.dict())
self.original_docs.extend(docs)
if self.parser is None:
raise ValueError("Parser not set")
for d in docs:
if d.metadata.id in [None, ""]:
d.metadata.id = d._unique_hash_id()
if split:
docs = self.parser.split(docs)
else:
self.parser.add_window_ids(docs)
if self.vecdb is None:
raise ValueError("VecDB not set")
# If any additional fields need to be added to content,
# add them as key=value pairs for all docs, before batching.
# This helps retrieval for table-like data.
# Note we need to do this at stage so that the embeddings
# are computed on the full content with these additional fields.
if len(self.config.add_fields_to_content) > 0:
fields = [
f for f in extract_fields(docs[0], self.config.add_fields_to_content)
]
if len(fields) > 0:
for d in docs:
key_vals = extract_fields(d, fields)
d.content = (
",".join(f"{k}={v}" for k, v in key_vals.items())
+ ",content="
+ d.content
)
docs = docs[: self.config.parsing.max_chunks]
# add embeddings in batches, to stay under limit of embeddings API
batches = list(batched(docs, self.config.embed_batch_size))
for batch in batches:
self.vecdb.add_documents(batch)
self.original_docs_length = self.doc_length(docs)
self.setup_documents(docs, filter=self.config.filter)
return len(docs)
def retrieval_tool(self, msg: RetrievalTool) -> str:
"""Handle the RetrievalTool message"""
self.config.retrieve_only = True
self.config.parsing.n_similar_docs = msg.num_results
content_doc = self.answer_from_docs(msg.query)
return content_doc.content
@staticmethod
def document_compatible_dataframe(
df: pd.DataFrame,
content: str = "content",
metadata: List[str] = [],
) -> Tuple[pd.DataFrame, List[str]]:
"""
Convert dataframe so it is compatible with Document class:
- has "content" column
- has an "id" column to be used as Document.metadata.id
Args:
df: dataframe to convert
content: name of content column
metadata: list of metadata column names
Returns:
Tuple[pd.DataFrame, List[str]]: dataframe, metadata
- dataframe: dataframe with "content" column and "id" column
- metadata: list of metadata column names, including "id"
"""
if content not in df.columns:
raise ValueError(
f"""
Content column {content} not in dataframe,
so we cannot ingest into the DocChatAgent.
Please specify the `content` parameter as a suitable
text-based column in the dataframe.
"""
)
if content != "content":
# rename content column to "content", leave existing column intact
df = df.rename(columns={content: "content"}, inplace=False)
actual_metadata = metadata.copy()
if "id" not in df.columns:
docs = dataframe_to_documents(df, content="content", metadata=metadata)
ids = [str(d.id()) for d in docs]
df["id"] = ids
if "id" not in actual_metadata:
actual_metadata += ["id"]
return df, actual_metadata
def ingest_dataframe(
self,
df: pd.DataFrame,
content: str = "content",
metadata: List[str] = [],
) -> int:
"""
Ingest a dataframe into vecdb.
"""
self.from_dataframe = True
self.df_description = describe_dataframe(
df, filter_fields=self.config.filter_fields, n_vals=5
)
df, metadata = DocChatAgent.document_compatible_dataframe(df, content, metadata)
docs = dataframe_to_documents(df, content="content", metadata=metadata)
# When ingesting a dataframe we will no longer do any chunking,
# so we mark each doc as a chunk.
# TODO - revisit this since we may still want to chunk large text columns
for d in docs:
d.metadata.is_chunk = True
return self.ingest_docs(docs)
def set_filter(self, filter: str) -> None:
self.config.filter = filter
self.setup_documents(filter=filter)
def setup_documents(
self,
docs: List[Document] = [],
filter: str | None = None,
) -> 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.
Args:
docs: List of Document objects. This is empty when we are calling this
method after initial doc ingestion.
filter: Filter condition for various lexical/semantic search fns.
"""
if filter is None and len(docs) > 0:
# no filter, so just use the docs passed in
self.chunked_docs.extend(docs)
else:
if self.vecdb is None:
raise ValueError("VecDB not set")
self.chunked_docs = self.vecdb.get_all_documents(where=filter or "")
self.chunked_docs_clean = [
Document(content=preprocess_text(d.content), metadata=d.metadata)
for d in self.chunked_docs
]
def get_field_values(self, fields: list[str]) -> Dict[str, str]:
"""Get string-listing of possible values of each filterable field,
e.g.
{
"genre": "crime, drama, mystery, ... (10 more)",
"certificate": "R, PG-13, PG, R",
}
"""
field_values: Dict[str, Set[str]] = {}
# make empty set for each field
for f in fields:
field_values[f] = set()
if self.vecdb is None:
raise ValueError("VecDB not set")
# get all documents and accumulate possible values of each field until 10
docs = self.vecdb.get_all_documents() # only works for vecdbs that support this
for d in docs:
# extract fields from d
doc_field_vals = extract_fields(d, fields)
for field, val in doc_field_vals.items():
field_values[field].add(val)
# For each field make a string showing list of possible values,
# truncate to 20 values, and if there are more, indicate how many
# more there are, e.g. Genre: crime, drama, mystery, ... (20 more)
field_values_list = {}
for f in fields:
vals = list(field_values[f])
n = len(vals)
remaining = n - 20
vals = vals[:20]
if n > 20:
vals.append(f"(...{remaining} more)")
# make a string of the values, ensure they are strings
field_values_list[f] = ", ".join(str(v) for v in vals)
return field_values_list
def doc_length(self, docs: List[Document]) -> int:
"""
Calc token-length of a list of docs
Args:
docs: list of Document objects
Returns:
int: number of tokens
"""
if self.parser is None:
raise ValueError("Parser not set")
return self.parser.num_tokens(self.doc_string(docs))
def user_docs_ingest_dialog(self) -> None:
"""
Ask user to select doc-collection, enter filenames/urls, and ingest into vecdb.
"""
if self.vecdb is None:
raise ValueError("VecDB not set")
n_deletes = self.vecdb.clear_empty_collections()
collections = self.vecdb.list_collections()
collection_name = "NEW"
is_new_collection = False
replace_collection = False
if len(collections) > 0:
n = len(collections)
delete_str = (
f"(deleted {n_deletes} empty collections)" if n_deletes > 0 else ""
)
print(f"Found {n} collections: {delete_str}")
for i, option in enumerate(collections, start=1):
print(f"{i}. {option}")
while True:
choice = Prompt.ask(
f"Enter 1-{n} to select a collection, "
"or hit ENTER to create a NEW collection, "
"or -1 to DELETE ALL COLLECTIONS",
default="0",
)
try:
if -1 <= int(choice) <= n:
break
except Exception:
pass
if choice == "-1":
confirm = Prompt.ask(
"Are you sure you want to delete all collections?",
choices=["y", "n"],
default="n",
)
if confirm == "y":
self.vecdb.clear_all_collections(really=True)
collection_name = "NEW"
if int(choice) > 0:
collection_name = collections[int(choice) - 1]
print(f"Using collection {collection_name}")
choice = Prompt.ask(
"Would you like to replace this collection?",
choices=["y", "n"],
default="n",
)
replace_collection = choice == "y"
if collection_name == "NEW":
is_new_collection = True
collection_name = Prompt.ask(
"What would you like to name the NEW collection?",
default="doc-chat",
)
self.vecdb.set_collection(collection_name, replace=replace_collection)
default_urls_str = (
" (or leave empty for default URLs)" if is_new_collection else ""
)
print(f"[blue]Enter some URLs or file/dir paths below {default_urls_str}")
inputs = get_list_from_user()
if len(inputs) == 0:
if is_new_collection:
inputs = self.config.default_paths
self.config.doc_paths = inputs # type: ignore
self.ingest()
def llm_response(
self,
query: None | str | ChatDocument = None,
) -> Optional[ChatDocument]:
if not self.llm_can_respond(query):
return None
query_str: str | None
if isinstance(query, ChatDocument):
query_str = query.content
else:
query_str = query
if query_str is None or query_str.startswith("!"):
# direct query to LLM
query_str = query_str[1:] if query_str is not None else None
if self.llm is None:
raise ValueError("LLM not set")
response = super().llm_response(query_str)
if query_str is not None:
self.update_dialog(
query_str, "" if response is None else response.content
)
return response
if query_str == "":
return None
elif query_str == "?" and self.response is not None:
return self.justify_response()
elif (query_str.startswith(("summar", "?")) and self.response is None) or (
query_str == "??"
):
return self.summarize_docs()
else:
self.callbacks.show_start_response(entity="llm")
response = self.answer_from_docs(query_str)
# Citation details (if any) are NOT generated by LLM
# (We extract these from LLM's numerical citations),
# so render them here
self._render_llm_response(response, citation_only=True)
return ChatDocument(
content=response.content,
metadata=ChatDocMetaData(
source=response.metadata.source,
sender=Entity.LLM,
),
)
async def llm_response_async(
self,
query: None | str | ChatDocument = None,
) -> Optional[ChatDocument]:
apply_nest_asyncio()
if not self.llm_can_respond(query):
return None
query_str: str | None
if isinstance(query, ChatDocument):
query_str = query.content
else:
query_str = query
if query_str is None or query_str.startswith("!"):
# direct query to LLM
query_str = query_str[1:] if query_str is not None else None
if self.llm is None:
raise ValueError("LLM not set")
response = await super().llm_response_async(query_str)
if query_str is not None:
self.update_dialog(
query_str, "" if response is None else response.content
)
return response
if query_str == "":
return None
elif query_str == "?" and self.response is not None:
return self.justify_response()
elif (query_str.startswith(("summar", "?")) and self.response is None) or (
query_str == "??"
):
return self.summarize_docs()
else:
self.callbacks.show_start_response(entity="llm")
response = self.answer_from_docs(query_str)
self._render_llm_response(response, citation_only=True)
return ChatDocument(
content=response.content,
metadata=ChatDocMetaData(
source=response.metadata.source,
sender=Entity.LLM,
),
)
@staticmethod
def doc_string(docs: List[Document]) -> str:
"""
Generate a string representation of a list of docs.
Args:
docs: list of Document objects
Returns:
str: string representation
"""
contents = [f"Extract: {d.content}" for d in docs]
sources = [d.metadata.source for d in docs]
sources = [f"Source: {s}" if s is not None else "" for s in sources]
return "\n".join(
[
f"""
[{i+1}]
{content}
{source}
"""
for i, (content, source) in enumerate(zip(contents, sources))
]
)
def get_summary_answer(
self, question: str, passages: List[Document]
) -> ChatDocument:
"""
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
"""
passages_str = self.doc_string(passages)
# Substitute Q and P into the templatized prompt
final_prompt = self.config.summarize_prompt.format(
question=question, extracts=passages_str
)
show_if_debug(final_prompt, "SUMMARIZE_PROMPT= ")
# Generate the final verbatim extract based on the final prompt.
# Note this will send entire message history, plus this final_prompt
# to the LLM, and self.message_history will be updated to include
# 2 new LLMMessage objects:
# one for `final_prompt`, and one for the LLM response
if self.config.conversation_mode:
# respond with temporary context
answer_doc = super()._llm_response_temp_context(question, final_prompt)
else:
answer_doc = super().llm_response_forget(final_prompt)
final_answer = answer_doc.content.strip()
show_if_debug(final_answer, "SUMMARIZE_RESPONSE= ")
citations = extract_markdown_references(final_answer)
citations_str = ""
if len(citations) > 0:
# append [i] source, content for each citation
citations_str = "\n".join(
[
f"[^{c}] {passages[c-1].metadata.source}"
f"\n{format_footnote_text(passages[c-1].content)}"
for c in citations
]
)
return ChatDocument(
content=final_answer, # does not contain citations
metadata=ChatDocMetaData(
source=citations_str, # only the citations
sender=Entity.LLM,
has_citation=len(citations) > 0,
cached=getattr(answer_doc.metadata, "cached", False),
),
)
def llm_hypothetical_answer(self, query: str) -> str:
if self.llm is None:
raise ValueError("LLM not set")
with status("[cyan]LLM generating hypothetical answer..."):
with StreamingIfAllowed(self.llm, False):
# TODO: provide an easy way to
# Adjust this prompt depending on context.
answer = self.llm_response_forget(
f"""
Give an ideal answer to the following query,
in up to 3 sentences. Do not explain yourself,
and do not apologize, just show
a good possible answer, even if you do not have any information.
Preface your answer with "HYPOTHETICAL ANSWER: "
QUERY: {query}
"""
).content
return answer
def llm_rephrase_query(self, query: str) -> List[str]:
if self.llm is None:
raise ValueError("LLM not set")
with status("[cyan]LLM generating rephrases of query..."):
with StreamingIfAllowed(self.llm, False):
rephrases = self.llm_response_forget(
f"""
Rephrase the following query in {self.config.n_query_rephrases}
different equivalent ways, separate them with 2 newlines.
QUERY: {query}
"""
).content.split("\n\n")
return rephrases
def get_similar_chunks_bm25(
self, query: str, multiple: int
) -> List[Tuple[Document, float]]:
# find similar docs using bm25 similarity:
# these may sometimes be more likely to contain a relevant verbatim extract
with status("[cyan]Searching for similar chunks using bm25..."):
if self.chunked_docs is None or len(self.chunked_docs) == 0:
logger.warning("No chunked docs; cannot use bm25-similarity")
return []
if self.chunked_docs_clean is None or len(self.chunked_docs_clean) == 0:
logger.warning("No cleaned chunked docs; cannot use bm25-similarity")
return []
docs_scores = find_closest_matches_with_bm25(
self.chunked_docs,
self.chunked_docs_clean, # already pre-processed!
query,
k=self.config.parsing.n_similar_docs * multiple,
)
return docs_scores
def get_fuzzy_matches(self, query: str, multiple: int) -> List[Document]:
# find similar docs using fuzzy matching:
# these may sometimes be more likely to contain a relevant verbatim extract
with status("[cyan]Finding fuzzy matches in chunks..."):
if self.chunked_docs is None:
logger.warning("No chunked docs; cannot use fuzzy matching")
return []
if self.chunked_docs_clean is None:
logger.warning("No cleaned chunked docs; cannot use fuzzy-search")
return []
fuzzy_match_docs = find_fuzzy_matches_in_docs(
query,
self.chunked_docs,
self.chunked_docs_clean,
k=self.config.parsing.n_similar_docs * multiple,
words_before=self.config.n_fuzzy_neighbor_words,
words_after=self.config.n_fuzzy_neighbor_words,
)
return fuzzy_match_docs
def rerank_with_cross_encoder(
self, query: str, passages: List[Document]
) -> List[Document]:
with status("[cyan]Re-ranking retrieved chunks using cross-encoder..."):
try:
from sentence_transformers import CrossEncoder
except ImportError:
raise ImportError(
"""
To use cross-encoder re-ranking, you must install
langroid with the [hf-embeddings] extra, e.g.:
pip install "langroid[hf-embeddings]"
"""
)
model = CrossEncoder(self.config.cross_encoder_reranking_model)
scores = model.predict([(query, p.content) for p in passages])
# Convert to [0,1] so we might could use a cutoff later.
scores = 1.0 / (1 + np.exp(-np.array(scores)))
# get top k scoring passages
sorted_pairs = sorted(
zip(scores, passages),
key=lambda x: x[0],
reverse=True,
)
passages = [
d for _, d in sorted_pairs[: self.config.parsing.n_similar_docs]
]
return passages
def rerank_with_diversity(self, passages: List[Document]) -> List[Document]:
"""
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.
"""
if self.vecdb is None:
logger.warning("No vecdb; cannot use rerank_with_diversity")
return passages
emb_model = self.vecdb.embedding_model
emb_fn = emb_model.embedding_fn()
embs = emb_fn([p.content for p in passages])
embs_arr = [np.array(e) for e in embs]
indices = list(range(len(passages)))
# Helper function to compute average similarity to
# items in the current result list.
def avg_similarity_to_result(i: int, result: List[int]) -> float:
return sum( # type: ignore
(embs_arr[i] @ embs_arr[j])
/ (np.linalg.norm(embs_arr[i]) * np.linalg.norm(embs_arr[j]))
for j in result
) / len(result)