Api overview
# Copyright (c) 2024 Microsoft Corporation.
# Licensed under the MIT License.
API Overview¶
This notebook provides a demonstration of how to interact with graphrag as a library using the API as opposed to the CLI. Note that graphrag's CLI actually connects to the library through this API for all operations.
from pathlib import Path
from pprint import pprint
import pandas as pd
import graphrag.api as api
from graphrag.config.load_config import load_config
from graphrag.index.typing.pipeline_run_result import PipelineRunResult
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) Cell In[2], line 4 1 from pathlib import Path 2 from pprint import pprint ----> 4 import pandas as pd 6 import graphrag.api as api 7 from graphrag.config.load_config import load_config ModuleNotFoundError: No module named 'pandas'
PROJECT_DIRECTORY = "<your project directory>"
Prerequisite¶
As a prerequisite to all API operations, a GraphRagConfig
object is required. It is the primary means to control the behavior of graphrag and can be instantiated from a settings.yaml
configuration file.
Please refer to the CLI docs for more detailed information on how to generate the settings.yaml
file.
Generate a GraphRagConfig
object¶
graphrag_config = load_config(Path(PROJECT_DIRECTORY))
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[4], line 1 ----> 1 graphrag_config = load_config(Path(PROJECT_DIRECTORY)) NameError: name 'load_config' is not defined
Indexing API¶
Indexing is the process of ingesting raw text data and constructing a knowledge graph. GraphRAG currently supports plaintext (.txt
) and .csv
file formats.
Build an index¶
index_result: list[PipelineRunResult] = await api.build_index(config=graphrag_config)
# index_result is a list of workflows that make up the indexing pipeline that was run
for workflow_result in index_result:
status = f"error\n{workflow_result.errors}" if workflow_result.errors else "success"
print(f"Workflow Name: {workflow_result.workflow}\tStatus: {status}")
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[5], line 1 ----> 1 index_result: list[PipelineRunResult] = await api.build_index(config=graphrag_config) 3 # index_result is a list of workflows that make up the indexing pipeline that was run 4 for workflow_result in index_result: NameError: name 'api' is not defined
Query an index¶
To query an index, several index files must first be read into memory and passed to the query API.
entities = pd.read_parquet(f"{PROJECT_DIRECTORY}/output/entities.parquet")
communities = pd.read_parquet(f"{PROJECT_DIRECTORY}/output/communities.parquet")
community_reports = pd.read_parquet(
f"{PROJECT_DIRECTORY}/output/community_reports.parquet"
)
response, context = await api.global_search(
config=graphrag_config,
entities=entities,
communities=communities,
community_reports=community_reports,
community_level=2,
dynamic_community_selection=False,
response_type="Multiple Paragraphs",
query="Who is Scrooge and what are his main relationships?",
)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[6], line 1 ----> 1 entities = pd.read_parquet(f"{PROJECT_DIRECTORY}/output/entities.parquet") 2 communities = pd.read_parquet(f"{PROJECT_DIRECTORY}/output/communities.parquet") 3 community_reports = pd.read_parquet( 4 f"{PROJECT_DIRECTORY}/output/community_reports.parquet" 5 ) NameError: name 'pd' is not defined
The response object is the official reponse from graphrag while the context object holds various metadata regarding the querying process used to obtain the final response.
print(response)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[7], line 1 ----> 1 print(response) NameError: name 'response' is not defined
Digging into the context a bit more provides users with extremely granular information such as what sources of data (down to the level of text chunks) were ultimately retrieved and used as part of the context sent to the LLM model).
pprint(context) # noqa: T203
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[8], line 1 ----> 1 pprint(context) # noqa: T203 NameError: name 'context' is not defined