Knowledge Graph Indexο
Building the Knowledge Graph Index
KG-based data structures.
- llama_index.indices.knowledge_graph.GPTKnowledgeGraphIndexο
alias of
KnowledgeGraphIndex
- class llama_index.indices.knowledge_graph.KGTableRetriever(index: KnowledgeGraphIndex, query_keyword_extract_template: Optional[Prompt] = None, max_keywords_per_query: int = 10, num_chunks_per_query: int = 10, include_text: bool = True, retriever_mode: Optional[KGRetrieverMode] = KGRetrieverMode.KEYWORD, similarity_top_k: int = 2, **kwargs: Any)ο
KG Table Retriever.
Arguments are shared among subclasses.
- Parameters
query_keyword_extract_template (Optional[QueryKGExtractPrompt]) β A Query KG Extraction Prompt (see Prompt Templates).
refine_template (Optional[RefinePrompt]) β A Refinement Prompt (see Prompt Templates).
text_qa_template (Optional[QuestionAnswerPrompt]) β A Question Answering Prompt (see Prompt Templates).
max_keywords_per_query (int) β Maximum number of keywords to extract from query.
num_chunks_per_query (int) β Maximum number of text chunks to query.
include_text (bool) β Use the document text source from each relevant triplet during queries.
retriever_mode (KGRetrieverMode) β Specifies whether to use keyowrds, embeddings, or both to find relevant triplets. Should be one of βkeywordβ, βembeddingβ, or βhybridβ.
similarity_top_k (int) β The number of top embeddings to use (if embeddings are used).
- retrieve(str_or_query_bundle: Union[str, QueryBundle]) List[NodeWithScore] ο
Retrieve nodes given query.
- Parameters
str_or_query_bundle (QueryType) β Either a query string or a QueryBundle object.
- class llama_index.indices.knowledge_graph.KnowledgeGraphIndex(nodes: Optional[Sequence[Node]] = None, index_struct: Optional[KG] = None, kg_triple_extract_template: Optional[Prompt] = None, max_triplets_per_chunk: int = 10, include_embeddings: bool = False, **kwargs: Any)ο
Knowledge Graph Index.
Build a KG by extracting triplets, and leveraging the KG during query-time.
- Parameters
kg_triple_extract_template (KnowledgeGraphPrompt) β The prompt to use for extracting triplets.
max_triplets_per_chunk (int) β The maximum number of triplets to extract.
- add_node(keywords: List[str], node: Node) None ο
Add node.
Used for manual insertion of nodes (keyed by keywords).
- Parameters
keywords (List[str]) β Keywords to index the node.
node (Node) β Node to be indexed.
- delete_nodes(doc_ids: List[str], delete_from_docstore: bool = False, **delete_kwargs: Any) None ο
Delete a list of nodes from the index.
- Parameters
doc_ids (List[str]) β A list of doc_ids from the nodes to delete
- delete_ref_doc(ref_doc_id: str, delete_from_docstore: bool = False, **delete_kwargs: Any) None ο
Delete a document and itβs nodes by using ref_doc_id.
- classmethod from_documents(documents: Sequence[Document], storage_context: Optional[StorageContext] = None, service_context: Optional[ServiceContext] = None, **kwargs: Any) IndexType ο
Create index from documents.
- Parameters
documents (Optional[Sequence[BaseDocument]]) β List of documents to build the index from.
- get_networkx_graph() Any ο
Get networkx representation of the graph structure.
NOTE: This function requires networkx to be installed. NOTE: This is a beta feature.
- property index_id: strο
Get the index struct.
- property ref_doc_info: Dict[str, RefDocInfo]ο
Retrieve a dict mapping of ingested documents and their nodes+metadata.
- refresh(documents: Sequence[Document], **update_kwargs: Any) List[bool] ο
Refresh an index with documents that have changed.
This allows users to save LLM and Embedding model calls, while only updating documents that have any changes in text or extra_info. It will also insert any documents that previously were not stored.
- refresh_ref_docs(documents: Sequence[Document], **update_kwargs: Any) List[bool] ο
Refresh an index with documents that have changed.
This allows users to save LLM and Embedding model calls, while only updating documents that have any changes in text or extra_info. It will also insert any documents that previously were not stored.
- set_index_id(index_id: str) None ο
Set the index id.
NOTE: if you decide to set the index_id on the index_struct manually, you will need to explicitly call add_index_struct on the index_store to update the index store.
- Parameters
index_id (str) β Index id to set.
- update(document: Document, **update_kwargs: Any) None ο
Update a document and itβs corresponding nodes.
This is equivalent to deleting the document and then inserting it again.
- Parameters
document (Union[BaseDocument, BaseIndex]) β document to update
insert_kwargs (Dict) β kwargs to pass to insert
delete_kwargs (Dict) β kwargs to pass to delete
- update_ref_doc(document: Document, **update_kwargs: Any) None ο
Update a document and itβs corresponding nodes.
This is equivalent to deleting the document and then inserting it again.
- Parameters
document (Union[BaseDocument, BaseIndex]) β document to update
insert_kwargs (Dict) β kwargs to pass to insert
delete_kwargs (Dict) β kwargs to pass to delete
- upsert_triplet(triplet: Tuple[str, str, str]) None ο
Insert triplets.
Used for manual insertion of KG triplets (in the form of (subject, relationship, object)).
- Args
triplet (str): Knowledge triplet
- upsert_triplet_and_node(triplet: Tuple[str, str, str], node: Node) None ο
Upsert KG triplet and node.
Calls both upsert_triplet and add_node. Behavior is idempotent; if Node already exists, only triplet will be added.
- Parameters
keywords (List[str]) β Keywords to index the node.
node (Node) β Node to be indexed.