graphragzen.query.query.PromptBuilder
- class graphragzen.query.query.PromptBuilder[source]
A class used to build a prompt based on graph data, vector searches, and additional documents or cluster reports.
- embedding_model
An embedding model used to generate embeddings for text and queries.
- Type:
- vector_db
A client for interacting with the vector database.
- Type:
- graph
A graph containing nodes and edges that represent entities and their relationships.
- Type:
nx.Graph
- source_documents
A DataFrame containing source documents for additional context.
- Type:
Optional[pd.DataFrame]
- cluster_report
A DataFrame containing cluster reports for additional context.
- Type:
Optional[pd.DataFrame]
Methods
build_prompt(query[, prompt, ...])Builds a prompt based on the query, graph data, vector searches, and additional documents or cluster reports.
- __init__(embedding_model, vector_db, graph, source_documents=None, cluster_report=None)[source]
Initializes the PromptBuilder instance with the necessary components.
- Parameters:
embedding_model (BaseEmbedder) – An embedding model to generate embeddings for text and queries.
vector_db (VectorDatabase)
graph (nx.Graph) – A graph containing nodes and edges that represent entities and their relationships.
source_documents (pd.DataFrame, optional) – A DataFrame containing source documents for additional context. Defaults to None.
cluster_report (pd.DataFrame, optional) – A DataFrame containing cluster reports for additional context. Should contain the columns ‘cluster’ (string identifier of each cluster), ‘node_name’ (lists of node names that belong to a cluster) and ‘description’ (a string describing the cluster). Defaults to None.
- Return type:
None
- build_prompt(query, prompt="\n---Role---\n\nYou are a helpful assistant responding to questions about data in the tables provided.\n\n\n---Goal---\n\nGenerate a response of the target length and format that responds to the user's question, summarizing all information in the input data tables appropriate for the response length and format, and incorporating any relevant general knowledge.\n\nIf you don't know the answer, just say so. Do not make anything up.\n\nDo not include information where the supporting evidence for it is not provided.\n\n\n---Data tables---\n{context_data}\n\n\n---Query---\n\n{query}\n", score_threshold=0.0, top_k_similar_entities=10, top_k_inside_edges=3, top_k_outside_edges=3, top_k_cluster_descriptions=3, top_k_source_documents=3, source_documents_id_key='chunk_id', source_documents_source_key='chunk', source_documents_feature_delimiter='\n')[source]
Builds a prompt based on the query, graph data, vector searches, and additional documents or cluster reports.
- Parameters:
query (str) – The query for which a prompt is being constructed.
prompt (str, optional) – The base prompt template to be filled with the context data and query. Defaults to LOCAL_SEARCH_PROMPT.
score_threshold (float, optional) – The minimum score threshold for including vector search results. Entities with scores below this threshold are excluded. Defaults to 0.0.
top_k_similar_entities (int, optional) – The number of top similar entities from the graph (retreived through vector search) to include in the prompt. Defaults to 10.
top_k_inside_edges (int, optional) – The number of inside edges by weight related to the similar entities to include in the prompt. An iside edge is defined as an edge whom’s both nodes are already in the entities retrieved through vector search. Defaults to 3.
top_k_outside_edges (int, optional) – The top number of outside edges by weight related to the similar entities to include in the prompt. An outside edge is defined as an edge of whom’s nodes extactly 1 is already in the entities retrieved through vector search. Defaults to 3.
top_k_cluster_descriptions (int, optional) – The number of top cluster descriptions by occurence, followed by rank, to include in the prompt. Defaults to 3.
top_k_source_documents (int, optional) – The number of top source documents by occurence to include in the prompt. Defaults to 3.
source_documents_id_key (str) – The column name in source_documents that represents unique document IDs. Defaults to “chunk_id”.
source_documents_source_key (str) – The column name in source_documents containing the source text. Defaults to “chunk”.
source_documents_feature_delimiter (str) – The delimiter used to split the “source_id” values in the Graph metadata. Default is newline.
- Returns:
The final constructed prompt with context data and the original query formatted within the prompt template.
- Return type:
str