Knowledge Graph Indexο
Building the Knowledge Graph Index
KG-based data structures.
- class llama_index.indices.knowledge_graph.GPTKnowledgeGraphIndex(nodes: Optional[Sequence[Node]] = None, index_struct: Optional[KG] = None, kg_triple_extract_template: Optional[KnowledgeGraphPrompt] = None, max_triplets_per_chunk: int = 10, include_embeddings: bool = False, **kwargs: Any)ο
GPT 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.
- 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.
- 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.
- 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.
This is equivalent to deleting the document and then inserting it again.
- Parameters
document (Union[BaseDocument, BaseGPTIndex]) β 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.
- class llama_index.indices.knowledge_graph.KGTableRetriever(index: GPTKnowledgeGraphIndex, query_keyword_extract_template: Optional[QueryKeywordExtractPrompt] = 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)ο
Base GPT KG Table Index Query.
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.