-
Notifications
You must be signed in to change notification settings - Fork 5k
/
base.py
89 lines (67 loc) · 2.89 KB
/
base.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
"""Empty index.
An index that doesn't contain any documents. Can only be used for
pure LLM calls.
"""
from typing import Any, Dict, Optional, Sequence
from llama_index.legacy.core.base_query_engine import BaseQueryEngine
from llama_index.legacy.core.base_retriever import BaseRetriever
from llama_index.legacy.data_structs.data_structs import EmptyIndexStruct
from llama_index.legacy.indices.base import BaseIndex
from llama_index.legacy.schema import BaseNode
from llama_index.legacy.service_context import ServiceContext
from llama_index.legacy.storage.docstore.types import RefDocInfo
class EmptyIndex(BaseIndex[EmptyIndexStruct]):
"""Empty Index.
An index that doesn't contain any documents. Used for
pure LLM calls.
NOTE: this exists because an empty index it allows certain properties,
such as the ability to be composed with other indices + token
counting + others.
"""
index_struct_cls = EmptyIndexStruct
def __init__(
self,
index_struct: Optional[EmptyIndexStruct] = None,
service_context: Optional[ServiceContext] = None,
**kwargs: Any,
) -> None:
"""Initialize params."""
super().__init__(
nodes=None,
index_struct=index_struct or EmptyIndexStruct(),
service_context=service_context,
**kwargs,
)
def as_retriever(self, **kwargs: Any) -> BaseRetriever:
# NOTE: lazy import
from llama_index.legacy.indices.empty.retrievers import EmptyIndexRetriever
return EmptyIndexRetriever(self)
def as_query_engine(self, **kwargs: Any) -> BaseQueryEngine:
if "response_mode" not in kwargs:
kwargs["response_mode"] = "generation"
else:
if kwargs["response_mode"] != "generation":
raise ValueError("EmptyIndex only supports response_mode=generation.")
return super().as_query_engine(**kwargs)
def _build_index_from_nodes(self, nodes: Sequence[BaseNode]) -> EmptyIndexStruct:
"""Build the index from documents.
Args:
documents (List[BaseDocument]): A list of documents.
Returns:
IndexList: The created summary index.
"""
del nodes # Unused
return EmptyIndexStruct()
def _insert(self, nodes: Sequence[BaseNode], **insert_kwargs: Any) -> None:
"""Insert a document."""
del nodes # Unused
raise NotImplementedError("Cannot insert into an empty index.")
def _delete_node(self, node_id: str, **delete_kwargs: Any) -> None:
"""Delete a node."""
raise NotImplementedError("Cannot delete from an empty index.")
@property
def ref_doc_info(self) -> Dict[str, RefDocInfo]:
"""Retrieve a dict mapping of ingested documents and their nodes+metadata."""
raise NotImplementedError("ref_doc_info not supported for an empty index.")
# legacy
GPTEmptyIndex = EmptyIndex