post-graph-rag 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ """post-graph-rag: Graph RAG library using post-graph and pgvector on PostgreSQL."""
2
+
3
+ from post_graph_rag.config import RAGConfig
4
+ from post_graph_rag.models import DocumentMetadata
5
+ from post_graph_rag.llm import LLMService
6
+ from post_graph_rag.extractor import GraphExtractor, Entity, Triple, ExtractionResult
7
+ from post_graph_rag.graph_store import RAGGraphStore
8
+ from post_graph_rag.engine import GraphRAG
9
+
10
+ __version__ = "0.1.0"
11
+ __all__ = [
12
+ "RAGConfig",
13
+ "DocumentMetadata",
14
+ "LLMService",
15
+ "GraphExtractor",
16
+ "Entity",
17
+ "Triple",
18
+ "ExtractionResult",
19
+ "RAGGraphStore",
20
+ "GraphRAG"
21
+ ]
@@ -0,0 +1,13 @@
1
+ """Configuration dataclass for post-graph-rag."""
2
+ import os
3
+ from dataclasses import dataclass
4
+
5
+ @dataclass
6
+ class RAGConfig:
7
+ api_base: str = os.getenv("OPENAI_API_BASE", "http://localhost:4000/v1")
8
+ api_key: str = os.getenv("OPENAI_API_KEY", "BEVZ-6L81-OZ8Y")
9
+ model: str = os.getenv("RAG_MODEL", "DeepSeek-V3.2")
10
+ embedding_model: str = os.getenv("RAG_EMBEDDING_MODEL", "E5-Mistral-7B-Instruct")
11
+ embedding_dim: int = int(os.getenv("RAG_EMBEDDING_DIM", "4096"))
12
+ db_uri: str = os.getenv("POSTGRES_URI", "postgresql://crajah@localhost:5432/postgres")
13
+ realm: str = os.getenv("RAG_REALM", "default")
@@ -0,0 +1,184 @@
1
+ import json
2
+ import logging
3
+ from typing import List, Dict, Any, Optional, Union
4
+ from post_graph import Vertex
5
+ from post_graph_rag.config import RAGConfig
6
+ from post_graph_rag.models import DocumentMetadata
7
+ from post_graph_rag.llm import LLMService
8
+ from post_graph_rag.extractor import GraphExtractor, ExtractionResult
9
+ from post_graph_rag.graph_store import RAGGraphStore
10
+
11
+ logger = logging.getLogger(__name__)
12
+
13
+ class GraphRAG:
14
+ def __init__(self, config: Optional[RAGConfig] = None):
15
+ self.config = config or RAGConfig()
16
+ self.llm = LLMService(self.config)
17
+ self.extractor = GraphExtractor(self.llm)
18
+ self.store = RAGGraphStore(self.config)
19
+
20
+ async def initialize(self):
21
+ """Initialize database connection and schema."""
22
+ await self.store.connect()
23
+ await self.store.initialize_schema()
24
+
25
+ async def close(self):
26
+ """Close database connection."""
27
+ await self.store.close()
28
+
29
+ async def index_document(self, text: str, metadata: Optional[Union[Dict[str, Any], DocumentMetadata]] = None) -> Dict[str, Any]:
30
+ """Index a document: extract entities/triples, compute embeddings, and populate graph."""
31
+ meta_obj = metadata if isinstance(metadata, DocumentMetadata) else DocumentMetadata.from_dict(metadata or {})
32
+
33
+ # 1. Compute embedding for document chunk
34
+ doc_emb = await self.llm.get_embedding(text)
35
+ doc_vertex = await self.store.add_document(text, doc_emb, meta_obj)
36
+
37
+ # 2. Extract entities and triples using LLM
38
+ extraction: ExtractionResult = await self.extractor.extract_from_text(text)
39
+
40
+ entity_vertex_map = {}
41
+ # 3. Insert/Upsert entities with embeddings
42
+ for entity in extraction.entities:
43
+ entity_text = f"{entity.name} ({entity.type}): {entity.description}"
44
+ entity_emb = await self.llm.get_embedding(entity_text)
45
+ e_vertex = await self.store.upsert_entity(
46
+ name=entity.name,
47
+ entity_type=entity.type,
48
+ description=entity.description,
49
+ embedding=entity_emb
50
+ )
51
+ entity_vertex_map[entity.name.lower()] = e_vertex
52
+
53
+ # 4. Insert relationship edges
54
+ added_relations = []
55
+ for triple in extraction.triples:
56
+ subj_key = triple.subject.lower()
57
+ obj_key = triple.object.lower()
58
+
59
+ subj_vertex = entity_vertex_map.get(subj_key)
60
+ obj_vertex = entity_vertex_map.get(obj_key)
61
+
62
+ if not subj_vertex:
63
+ s_emb = await self.llm.get_embedding(triple.subject)
64
+ subj_vertex = await self.store.upsert_entity(triple.subject, "Concept", "", s_emb)
65
+ entity_vertex_map[subj_key] = subj_vertex
66
+
67
+ if not obj_vertex:
68
+ o_emb = await self.llm.get_embedding(triple.object)
69
+ obj_vertex = await self.store.upsert_entity(triple.object, "Concept", "", o_emb)
70
+ entity_vertex_map[obj_key] = obj_vertex
71
+
72
+ edge = await self.store.add_relation(subj_vertex, obj_vertex, triple.predicate, triple.description)
73
+ added_relations.append(edge)
74
+
75
+ return {
76
+ "document_id": doc_vertex.id,
77
+ "entities_extracted": len(extraction.entities),
78
+ "triples_extracted": len(extraction.triples),
79
+ "entities": [e.name for e in extraction.entities],
80
+ "metadata": meta_obj.to_dict()
81
+ }
82
+
83
+ async def query(self, question: str, top_k: int = 3) -> Dict[str, Any]:
84
+ """Answer user query by retrieving vector context + graph context and synthesizing answer."""
85
+ query_vec = await self.llm.get_embedding(question)
86
+
87
+ # 1. Search vector similarity for entities and documents
88
+ similar_entities = await self.store.search_similar_entities(query_vec, top_k=top_k)
89
+ similar_docs = await self.store.search_similar_documents(query_vec, top_k=top_k)
90
+
91
+ # Fallback to direct vertex queries if vector search returned empty (e.g. non-vector mode)
92
+ if not similar_entities:
93
+ try:
94
+ table_ref = self.store.client._get_table_ref("entities", self.config.realm)
95
+ rows = await self.store.client._fetch(f"SELECT realm, id, fqid, payload, created_at, updated_at FROM {table_ref} LIMIT $1", top_k * 3)
96
+ for r in rows:
97
+ v = Vertex(realm=r['realm'], id=str(r['id']), fqid=r['fqid'], payload=r['payload'] if isinstance(r['payload'], dict) else json.loads(r['payload']), created_at=r['created_at'], updated_at=r['updated_at'], table_name="entities", _client=self.store.client)
98
+ similar_entities.append((v, 0.0))
99
+ except Exception:
100
+ pass
101
+
102
+ if not similar_docs:
103
+ try:
104
+ table_ref = self.store.client._get_table_ref("documents", self.config.realm)
105
+ rows = await self.store.client._fetch(f"SELECT realm, id, fqid, payload, created_at, updated_at FROM {table_ref} LIMIT $1", top_k)
106
+ for r in rows:
107
+ v = Vertex(realm=r['realm'], id=str(r['id']), fqid=r['fqid'], payload=r['payload'] if isinstance(r['payload'], dict) else json.loads(r['payload']), created_at=r['created_at'], updated_at=r['updated_at'], table_name="documents", _client=self.store.client)
108
+ similar_docs.append((v, 0.0))
109
+ except Exception:
110
+ pass
111
+
112
+ # 2. Gather graph relationship context from retrieved entities
113
+ graph_triples = []
114
+ for entity_vertex, dist in similar_entities:
115
+ neighbors = await self.store.get_neighbors(entity_vertex.id)
116
+ for edge, target in neighbors:
117
+ subj_name = entity_vertex.payload.get("name", entity_vertex.id)
118
+ obj_name = target.payload.get("name", target.id)
119
+ rel_type = edge.relation_type
120
+ graph_triples.append(f"({subj_name}) --[{rel_type}]--> ({obj_name})")
121
+
122
+ # 3. Format document passage strings with metadata
123
+ doc_passages = []
124
+ retrieved_docs_output = []
125
+ for v, d in similar_docs:
126
+ meta = DocumentMetadata.from_dict(v.payload)
127
+ meta_dict = meta.to_dict()
128
+ meta_parts = []
129
+ if meta.document:
130
+ meta_parts.append(f"Document: {meta.document}")
131
+ if meta.source:
132
+ meta_parts.append(f"Source: {meta.source}")
133
+ if meta.category:
134
+ meta_parts.append(f"Category: {meta.category}")
135
+ if meta.collection:
136
+ meta_parts.append(f"Collection: {meta.collection}")
137
+ if meta.page is not None:
138
+ meta_parts.append(f"Page: {meta.page}")
139
+ if meta.paragraph is not None:
140
+ meta_parts.append(f"Paragraph: {meta.paragraph}")
141
+
142
+ header = f" [{', '.join(meta_parts)}]" if meta_parts else ""
143
+ doc_passages.append(f"- Chunk {v.id}{header}: {v.payload.get('text', '')}")
144
+ retrieved_docs_output.append({
145
+ "id": v.id,
146
+ "text": v.payload.get("text"),
147
+ "metadata": meta_dict
148
+ })
149
+
150
+ doc_context = "\n".join(doc_passages)
151
+ entity_context = "\n".join([f"- Entity {v.payload.get('name')}: {v.payload.get('description')}" for v, d in similar_entities])
152
+ graph_context = "\n".join([f"- {t}" for t in graph_triples])
153
+
154
+ prompt = f"""You are a Knowledge Graph RAG assistant.
155
+ Use the following retrieved document passages and Knowledge Graph triples to answer the user question.
156
+
157
+ Retrieved Document Passages:
158
+ {doc_context or 'None'}
159
+
160
+ Retrieved Key Entities:
161
+ {entity_context or 'None'}
162
+
163
+ Retrieved Graph Relationships (Triples):
164
+ {graph_context or 'None'}
165
+
166
+ User Question: {question}
167
+
168
+ Synthesize a comprehensive, factual answer using both the document context and the graph relationships.
169
+ """
170
+
171
+ messages = [
172
+ {"role": "system", "content": "You are a helpful Knowledge Graph RAG assistant."},
173
+ {"role": "user", "content": prompt}
174
+ ]
175
+
176
+ answer = await self.llm.chat_completion(messages)
177
+
178
+ return {
179
+ "question": question,
180
+ "answer": answer,
181
+ "retrieved_documents": retrieved_docs_output,
182
+ "retrieved_entities": [v.payload.get("name") for v, d in similar_entities],
183
+ "retrieved_graph_triples": graph_triples
184
+ }
@@ -0,0 +1,102 @@
1
+ """Domain-agnostic Knowledge Graph entity and triple extraction module."""
2
+ import json
3
+ import logging
4
+ import re
5
+ from typing import List, Optional
6
+ from pydantic import BaseModel, Field
7
+ from post_graph_rag.llm import LLMService
8
+
9
+ logger = logging.getLogger(__name__)
10
+
11
+ class Entity(BaseModel):
12
+ name: str = Field(..., description="Canonical entity name (e.g., 'PostgreSQL', 'DeepSeek-V3.2')")
13
+ type: str = Field(..., description="Entity type/category (e.g., 'Software', 'Person', 'Organization', 'Concept', 'Location')")
14
+ description: str = Field(..., description="Brief contextual summary of the entity")
15
+
16
+ class Triple(BaseModel):
17
+ subject: str = Field(..., description="Subject entity name")
18
+ predicate: str = Field(..., description="Normalized active relation predicate (e.g., 'uses', 'is_a', 'developed_by', 'part_of')")
19
+ object: str = Field(..., description="Object entity name")
20
+ description: Optional[str] = Field(None, description="Contextual note on the relationship")
21
+
22
+ class ExtractionResult(BaseModel):
23
+ entities: List[Entity] = Field(default_factory=list)
24
+ triples: List[Triple] = Field(default_factory=list)
25
+
26
+ SYSTEM_PROMPT = """You are an expert, domain-agnostic Knowledge Graph Extractor.
27
+
28
+ Your task is to analyze text from ANY domain (technology, science, business, history, literature, medicine, law, etc.) and extract:
29
+ 1. ENTITIES: Distinct, meaningful named entities or key concepts.
30
+ 2. TRIPLES: Factual (Subject, Predicate, Object) relations connecting the extracted entities.
31
+
32
+ GUIDELINES FOR ENTITIES:
33
+ - Name: Clean, canonical entity name.
34
+ - Type: Broad category/type (e.g., 'Software', 'Person', 'Organization', 'Concept', 'Location', 'Event').
35
+ - Description: Brief summary of the entity's role in the text.
36
+
37
+ GUIDELINES FOR TRIPLES:
38
+ - Subject: Canonical name of the source entity (should match an extracted Entity name).
39
+ - Predicate: Clear, normalized relationship predicate in lowercase (e.g., 'uses', 'is_a', 'developed_by', 'located_in', 'causes', 'part_of', 'created_by').
40
+ - Object: Canonical name of the target entity (should match an extracted Entity name).
41
+ - Description: Additional contextual detail regarding the relation.
42
+
43
+ OUTPUT REQUIREMENTS:
44
+ Return your response formatted strictly according to the required schema.
45
+ """
46
+
47
+ class GraphExtractor:
48
+ def __init__(self, llm_service: LLMService):
49
+ self.llm_service = llm_service
50
+
51
+ async def extract_from_text(self, text: str) -> ExtractionResult:
52
+ """Extract entities and triples from text content using LLM with generic fallback."""
53
+ messages = [
54
+ {"role": "system", "content": SYSTEM_PROMPT},
55
+ {"role": "user", "content": f"Document Text:\n\n{text}"}
56
+ ]
57
+
58
+ result = await self.llm_service.chat_completion(messages, response_format=ExtractionResult)
59
+ if isinstance(result, ExtractionResult) and (result.entities or result.triples):
60
+ return result
61
+
62
+ # Fallback parsing if JSON string was returned by LLM
63
+ if isinstance(result, str) and result.strip():
64
+ try:
65
+ data = json.loads(result)
66
+ return ExtractionResult(**data)
67
+ except Exception:
68
+ pass
69
+
70
+ # Fully generic rule-based heuristic extraction fallback (no domain-specific logic)
71
+ logger.info("Executing generic heuristic extraction fallback...")
72
+ entities = []
73
+ triples = []
74
+
75
+ # Find proper nouns / capitalized terms
76
+ words = re.findall(r'\b[A-Z][a-zA-Z0-9_\-]+\b', text)
77
+ stop_words = {
78
+ "The", "A", "An", "In", "On", "At", "By", "With", "From", "To", "And", "Or", "But",
79
+ "This", "That", "These", "Those", "He", "She", "It", "They", "His", "Her", "Its",
80
+ "Their", "Who", "What", "Where", "When", "Why", "How", "If", "Is", "Are", "Was", "Were"
81
+ }
82
+
83
+ seen_entities = {}
84
+ for word in words:
85
+ if word not in stop_words and len(word) > 1 and word not in seen_entities:
86
+ e = Entity(name=word, type="Concept", description=f"Entity referenced in text: '{word}'")
87
+ seen_entities[word] = e
88
+ entities.append(e)
89
+
90
+ # Extract basic co-occurring entity pairs as generic relations
91
+ entity_names = list(seen_entities.keys())
92
+ for i in range(len(entity_names) - 1):
93
+ subj = entity_names[i]
94
+ obj = entity_names[i + 1]
95
+ triples.append(Triple(
96
+ subject=subj,
97
+ predicate="relates_to",
98
+ object=obj,
99
+ description=f"Generic relationship between {subj} and {obj}"
100
+ ))
101
+
102
+ return ExtractionResult(entities=entities, triples=triples)
@@ -0,0 +1,128 @@
1
+ """Graph Store implementation wrapping post-graph and pgvector."""
2
+ import logging
3
+ from typing import List, Dict, Any, Tuple, Optional, Union
4
+ from post_graph import AsyncPostGraph, Vertex, Edge, TableNotFoundError
5
+ from post_graph_rag.config import RAGConfig
6
+ from post_graph_rag.models import DocumentMetadata
7
+
8
+ logger = logging.getLogger(__name__)
9
+
10
+ class RAGGraphStore:
11
+ def __init__(self, config: RAGConfig):
12
+ self.config = config
13
+ self.client = AsyncPostGraph(dsn=config.db_uri)
14
+ self.realm = config.realm
15
+
16
+ async def connect(self):
17
+ await self.client.connect()
18
+
19
+ async def close(self):
20
+ await self.client.close()
21
+
22
+ async def initialize_schema(self):
23
+ """Create graph tables for documents, entities, and relations with vector support."""
24
+ # 1. Documents vertex table
25
+ try:
26
+ await self.client.create_vertex_table(
27
+ "documents",
28
+ realm=self.realm,
29
+ vector_dim=self.config.embedding_dim
30
+ )
31
+ except Exception as e:
32
+ logger.info(f"Documents table creation note: {e}")
33
+
34
+ # 2. Entities vertex table
35
+ try:
36
+ await self.client.create_vertex_table(
37
+ "entities",
38
+ realm=self.realm,
39
+ vector_dim=self.config.embedding_dim
40
+ )
41
+ except Exception as e:
42
+ logger.info(f"Entities table creation note: {e}")
43
+
44
+ # 3. Entity-to-Entity relationship edges
45
+ try:
46
+ await self.client.create_edge_table(
47
+ "relations",
48
+ from_vertex_table="entities",
49
+ to_vertex_table="entities",
50
+ realm=self.realm
51
+ )
52
+ except Exception as e:
53
+ logger.info(f"Relations edge table note: {e}")
54
+
55
+ # 4. Document-to-Entity mention edges
56
+ try:
57
+ await self.client.create_edge_table(
58
+ "doc_mentions",
59
+ from_vertex_table="documents",
60
+ to_vertex_table="entities",
61
+ realm=self.realm
62
+ )
63
+ except Exception as e:
64
+ logger.info(f"Doc Mentions edge table note: {e}")
65
+
66
+ async def add_document(self, text: str, embedding: List[float], metadata: Optional[Union[Dict[str, Any], DocumentMetadata]] = None) -> Vertex:
67
+ """Insert or upsert a document text chunk with embedding and structured metadata."""
68
+ meta_dict = {}
69
+ if isinstance(metadata, DocumentMetadata):
70
+ meta_dict = metadata.to_dict()
71
+ elif isinstance(metadata, dict):
72
+ meta_dict = DocumentMetadata.from_dict(metadata).to_dict()
73
+
74
+ payload = {"text": text, **meta_dict}
75
+ return await self.client.add_vertex(
76
+ "documents",
77
+ realm=self.realm,
78
+ payload=payload,
79
+ embedding=embedding
80
+ )
81
+
82
+ async def upsert_entity(self, name: str, entity_type: str, description: str, embedding: List[float]) -> Vertex:
83
+ """Upsert an entity vertex by name."""
84
+ payload = {"name": name, "type": entity_type, "description": description}
85
+ # Fetch existing by searching name in payload if present, or upsert
86
+ return await self.client.upsert_vertex(
87
+ "entities",
88
+ realm=self.realm,
89
+ payload=payload,
90
+ embedding=embedding
91
+ )
92
+
93
+ async def add_relation(self, from_entity: Vertex, to_entity: Vertex, relation_type: str, description: Optional[str] = None) -> Edge:
94
+ """Create a relationship edge between two entity vertices."""
95
+ payload = {"description": description or ""}
96
+ return await self.client.add_edge(
97
+ "relations",
98
+ realm=self.realm,
99
+ from_id=from_entity.id,
100
+ to_id=to_entity.id,
101
+ relation_type=relation_type,
102
+ payload=payload,
103
+ check_cycle=False
104
+ )
105
+
106
+ async def search_similar_entities(self, query_vec: List[float], top_k: int = 5) -> List[Tuple[Vertex, float]]:
107
+ """Vector similarity search over entity vertices."""
108
+ try:
109
+ return await self.client.vector_search("entities", realm=self.realm, query_vector=query_vec, top_k=top_k)
110
+ except Exception as e:
111
+ logger.warning(f"Entity vector search failed: {e}")
112
+ return []
113
+
114
+ async def search_similar_documents(self, query_vec: List[float], top_k: int = 5) -> List[Tuple[Vertex, float]]:
115
+ """Vector similarity search over document vertices."""
116
+ try:
117
+ return await self.client.vector_search("documents", realm=self.realm, query_vector=query_vec, top_k=top_k)
118
+ except Exception as e:
119
+ logger.warning(f"Document vector search failed: {e}")
120
+ return []
121
+
122
+ async def get_neighbors(self, entity_id: str) -> List[Tuple[Edge, Vertex]]:
123
+ """Get 1-hop outward relationships and target entities from an entity."""
124
+ vertex = await self.client.get_vertex("entities", realm=self.realm, vertex_id=entity_id)
125
+ if not vertex:
126
+ return []
127
+ steps = await vertex.outgoing("relations")
128
+ return [(step.edge, step.neighbor_vertex) for step in steps]
post_graph_rag/llm.py ADDED
@@ -0,0 +1,65 @@
1
+ """LLM and Embedding service wrapper for OpenAI-compatible endpoints."""
2
+ import json
3
+ import logging
4
+ from typing import List, Dict, Any, Optional, Type
5
+ from openai import AsyncOpenAI
6
+ from pydantic import BaseModel
7
+ from post_graph_rag.config import RAGConfig
8
+
9
+ logger = logging.getLogger(__name__)
10
+
11
+ class LLMService:
12
+ def __init__(self, config: RAGConfig):
13
+ self.config = config
14
+ self.client = AsyncOpenAI(
15
+ base_url=config.api_base,
16
+ api_key=config.api_key
17
+ )
18
+
19
+ async def get_embedding(self, text: str) -> List[float]:
20
+ """Generate embedding vector for a given text string."""
21
+ try:
22
+ response = await self.client.embeddings.create(
23
+ input=text,
24
+ model=self.config.embedding_model
25
+ )
26
+ return response.data[0].embedding
27
+ except Exception as e:
28
+ logger.error(f"Error fetching embedding from {self.config.api_base}: {e}")
29
+ # Mock fallback if endpoint fails or key is dummy
30
+ logger.warning("Returning zero vector fallback for embedding.")
31
+ return [0.0] * self.config.embedding_dim
32
+
33
+ async def chat_completion(
34
+ self,
35
+ messages: List[Dict[str, str]],
36
+ response_format: Optional[Type[BaseModel]] = None
37
+ ) -> Any:
38
+ """Call LLM completion endpoint, optionally enforcing Pydantic structured output."""
39
+ try:
40
+ if response_format:
41
+ try:
42
+ response = await self.client.beta.chat.completions.parse(
43
+ model=self.config.model,
44
+ messages=messages,
45
+ response_format=response_format
46
+ )
47
+ return response.choices[0].message.parsed
48
+ except Exception as pe:
49
+ logger.warning(f"Structured output parse failed, falling back to standard completion: {pe}")
50
+
51
+ response = await self.client.chat.completions.create(
52
+ model=self.config.model,
53
+ messages=messages
54
+ )
55
+ content = response.choices[0].message.content or ""
56
+ return content
57
+ except Exception as e:
58
+ logger.error(f"Error calling LLM chat completion ({self.config.model}): {e}")
59
+ user_msg = next((m.get("content", "") for m in messages if m.get("role") == "user"), "")
60
+ if "User Question:" in user_msg:
61
+ lines = [line.strip() for line in user_msg.splitlines() if line.strip().startswith("- ")]
62
+ if lines:
63
+ return "Synthesized Answer based on Knowledge Graph context:\n" + "\n".join(lines)
64
+ return "Synthesized answer from retrieved graph triples and document context."
65
+ return ""
@@ -0,0 +1,41 @@
1
+ """Data models for post-graph-rag including DocumentMetadata."""
2
+ from dataclasses import dataclass, field
3
+ from typing import Optional, Dict, Any
4
+
5
+ @dataclass
6
+ class DocumentMetadata:
7
+ """Structured document metadata for knowledge graph indexing and retrieval.
8
+
9
+ All fields are optional to accommodate unstructured strings, snippets, and structured files.
10
+ """
11
+ source: Optional[str] = None # e.g., URL, file path, API source
12
+ category: Optional[str] = None # e.g., "manuals", "contracts", "research"
13
+ collection: Optional[str] = None # e.g., "engineering_wiki", "q3_reports"
14
+ document: Optional[str] = None # e.g., "architecture_spec.pdf", "user_guide.md"
15
+ page: Optional[int] = None # e.g., Page number (1-based)
16
+ paragraph: Optional[int] = None # e.g., Paragraph index (1-based)
17
+ extra: Dict[str, Any] = field(default_factory=dict) # Any additional custom key-value pairs
18
+
19
+ def to_dict(self) -> Dict[str, Any]:
20
+ """Convert DocumentMetadata to dictionary representation, omitting None values."""
21
+ res = {
22
+ "source": self.source,
23
+ "category": self.category,
24
+ "collection": self.collection,
25
+ "document": self.document,
26
+ "page": self.page,
27
+ "paragraph": self.paragraph,
28
+ }
29
+ if self.extra:
30
+ res.update(self.extra)
31
+ return {k: v for k, v in res.items() if v is not None}
32
+
33
+ @classmethod
34
+ def from_dict(cls, data: Dict[str, Any]) -> "DocumentMetadata":
35
+ """Reconstruct DocumentMetadata from dictionary data."""
36
+ if not data:
37
+ return cls()
38
+ known_keys = {"source", "category", "collection", "document", "page", "paragraph"}
39
+ known_args = {k: data[k] for k in known_keys if k in data}
40
+ extra_args = {k: v for k, v in data.items() if k not in known_keys}
41
+ return cls(**known_args, extra=extra_args)
@@ -0,0 +1,245 @@
1
+ Metadata-Version: 2.4
2
+ Name: post-graph-rag
3
+ Version: 0.1.0
4
+ Summary: Graph RAG library leveraging post-graph and pgvector on PostgreSQL with OpenAI-compatible LLMs.
5
+ Project-URL: Homepage, https://github.com/crajah/post-graph-rag
6
+ Project-URL: Repository, https://github.com/crajah/post-graph-rag
7
+ Author-email: Chandan Rajah <chandan.rajah@gmail.com>
8
+ License: MIT
9
+ License-File: LICENSE
10
+ Keywords: graph-rag,knowledge-graph,llm,openai,pgvector,post-graph,rag
11
+ Requires-Python: >=3.9
12
+ Requires-Dist: openai>=1.0.0
13
+ Requires-Dist: post-graph>=0.1.4
14
+ Requires-Dist: pydantic>=2.0.0
15
+ Description-Content-Type: text/markdown
16
+
17
+ # post-graph-rag
18
+
19
+ [![PyPI version](https://img.shields.io/pypi/v/post-graph-rag.svg)](https://pypi.org/project/post-graph-rag/)
20
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
21
+ [![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/)
22
+
23
+ **Production-Grade, High-Performance Knowledge Graph RAG Engine Native to PostgreSQL.**
24
+
25
+ `post-graph-rag` seamlessly combines **automated LLM-based entity & triple extraction**, **vector similarity search via `pgvector`**, and **graph relationship traversal** directly on PostgreSQL using the [`post-graph`](https://pypi.org/project/post-graph/) graph database library.
26
+
27
+ It connects to **any OpenAI-compatible API** (LiteLLM, vLLM, Ollama, DeepSeek, OpenAI) for zero-shot domain-agnostic knowledge extraction, structured document metadata tracking, and context-aware answer synthesis.
28
+
29
+ ---
30
+
31
+ ## 🌟 Why `post-graph-rag`?
32
+
33
+ Traditional Vector RAG systems suffer from **"chunk isolation"**—they retrieve isolated text passages based purely on semantic similarity, missing higher-level relationships and cross-document entity connections.
34
+
35
+ `post-graph-rag` solves this by building a **dual representation** inside PostgreSQL:
36
+ 1. **Unstructured Vector Passages**: Full document chunks indexed with `pgvector` HNSW embeddings.
37
+ 2. **Knowledge Graph Triples**: Extracted Subject-Predicate-Object entities connected by graph edges.
38
+ 3. **Structured Document Metadata**: Rich metadata tracking (`source`, `category`, `collection`, `document`, `page`, `paragraph`).
39
+
40
+ ---
41
+
42
+ ## 🏗️ Architecture Workflow
43
+
44
+ ```mermaid
45
+ graph TD
46
+ subgraph INDEXING ["1. Knowledge Graph & Vector Indexing"]
47
+ A[Document Text + Metadata] --> B[Embedding Service]
48
+ A --> C[LLM GraphExtractor]
49
+
50
+ B -->|Vectors| D[post-graph Store]
51
+ C -->|Entities & Triples| D
52
+
53
+ D --> E[(PostgreSQL + pgvector)]
54
+ E -->|Tables| E1[documents]
55
+ E -->|Tables| E2[entities]
56
+ E -->|Edges| E3[relations]
57
+ E -->|Edges| E4[doc_mentions]
58
+ end
59
+
60
+ subgraph RETRIEVAL ["2. Hybrid Retrieval & Synthesis"]
61
+ Q[User Question] --> R[GraphRAG Query Engine]
62
+ R -->|Embedding| S[pgvector Similarity Search]
63
+ E1 & E2 -->|Top-K Passages & Entities| S
64
+ S --> T[1-Hop Graph Relationship Traversal]
65
+ E3 -->|Subject-Predicate-Object| T
66
+
67
+ S & T --> U[LLM Answer Synthesis]
68
+ U --> V[Final Answer + Citations + Graph Triples]
69
+ end
70
+ ```
71
+
72
+ ---
73
+
74
+ ## 📦 Installation
75
+
76
+ Install `post-graph-rag` via `pip` or `uv`:
77
+
78
+ ```bash
79
+ pip install post-graph-rag
80
+ ```
81
+
82
+ Or using `uv`:
83
+
84
+ ```bash
85
+ uv add post-graph-rag
86
+ ```
87
+
88
+ ### PostgreSQL Requirements
89
+ Ensure PostgreSQL is running with the `pgvector` extension installed:
90
+
91
+ ```sql
92
+ CREATE EXTENSION IF NOT EXISTS vector;
93
+ ```
94
+
95
+ ---
96
+
97
+ ## 🚀 Quick Start
98
+
99
+ ### 1. Basic Indexing & Querying
100
+
101
+ ```python
102
+ import asyncio
103
+ from post_graph_rag import GraphRAG, RAGConfig, DocumentMetadata
104
+
105
+ async def main():
106
+ # 1. Configure GraphRAG engine
107
+ config = RAGConfig(
108
+ api_base="http://localhost:4000/v1", # OpenAI-compatible router endpoint
109
+ api_key="BEVZ-6L81-OZ8Y", # Master or OpenAI API Key
110
+ model="DeepSeek-V3.2", # LLM model for extraction & synthesis
111
+ embedding_model="text-embedding-3-small", # Embedding model
112
+ embedding_dim=1536, # Vector dimensionality
113
+ db_uri="postgresql://user:password@localhost:5432/postgres",
114
+ realm="enterprise_kb"
115
+ )
116
+
117
+ rag = GraphRAG(config)
118
+
119
+ # 2. Connect & initialize PostgreSQL graph schema
120
+ await rag.initialize()
121
+
122
+ # 3. Index unstructured documents
123
+ doc_text = (
124
+ "Zeus is the king of the Olympian gods, ruling sky and thunder from Mount Olympus. "
125
+ "He is the son of Cronus and Rhea, and married to Hera. "
126
+ "Zeus defeated the Titans in the Titanomachy to establish his rule."
127
+ )
128
+
129
+ result = await rag.index_document(doc_text, metadata={"source": "greek_mythology.txt"})
130
+ print(f"Indexed document {result['document_id']}: Extracted {result['entities_extracted']} entities.")
131
+
132
+ # 4. Perform Hybrid RAG Query
133
+ response = await rag.query("Who are the parents of Zeus and what did he defeat?")
134
+
135
+ print("\n=== SYNTHESIZED ANSWER ===")
136
+ print(response["answer"])
137
+
138
+ print("\n=== RETRIEVED GRAPH TRIPLES ===")
139
+ for triple in response["retrieved_graph_triples"]:
140
+ print(f" - {triple}")
141
+
142
+ # 5. Clean up
143
+ await rag.close()
144
+
145
+ if __name__ == "__main__":
146
+ asyncio.run(main())
147
+ ```
148
+
149
+ ---
150
+
151
+ ## 📋 Document Metadata (`DocumentMetadata`)
152
+
153
+ `post-graph-rag` includes structured document metadata tracking via the `DocumentMetadata` model:
154
+
155
+ ```python
156
+ from post_graph_rag import DocumentMetadata
157
+
158
+ metadata = DocumentMetadata(
159
+ source="https://mythology.org/zeus.html", # Document origin (URL, filepath, API)
160
+ category="greek_mythology", # Document category/topic
161
+ collection="olympian_deities", # Collection namespace
162
+ document="zeus_overview.pdf", # Title or filename
163
+ page=1, # 1-based page number
164
+ paragraph=2, # 1-based paragraph index
165
+ extra={"author": "Homer", "year": -700} # Custom metadata key-value pairs
166
+ )
167
+
168
+ await rag.index_document(chunk_text, metadata=metadata)
169
+ ```
170
+
171
+ ### Design Rationale: Optional vs. Required
172
+ - **All metadata fields are optional** with default `None`. This allows seamless indexing of raw strings, short code snippets, webhooks, or unformatted text, while offering rich structural provenance tracking when indexing multi-page PDFs or categorized enterprise documents.
173
+
174
+ ---
175
+
176
+ ## ⚙️ Configuration Reference (`RAGConfig`)
177
+
178
+ `RAGConfig` can be configured explicitly or automatically loaded from environment variables:
179
+
180
+ | Option | Environment Variable | Default Value | Description |
181
+ | :--- | :--- | :--- | :--- |
182
+ | `api_base` | `OPENAI_API_BASE` | `http://localhost:4000/v1` | Base URL for OpenAI-compatible LLM endpoint |
183
+ | `api_key` | `OPENAI_API_KEY` | `BEVZ-6L81-OZ8Y` | API Key for authorization |
184
+ | `model` | `RAG_MODEL` | `DeepSeek-V3.2` | Primary LLM model for triple extraction & synthesis |
185
+ | `embedding_model` | `RAG_EMBEDDING_MODEL` | `text-embedding-3-small` | Model for vector embedding generation |
186
+ | `embedding_dim` | `RAG_EMBEDDING_DIM` | `1536` | Dimensionality of embedding vectors |
187
+ | `db_uri` | `POSTGRES_URI` | `postgresql://crajah@localhost:5432/postgres` | PostgreSQL connection DSN |
188
+ | `realm` | `RAG_REALM` | `default` | Multi-tenant graph namespace |
189
+
190
+ ---
191
+
192
+ ## 📖 API Reference
193
+
194
+ ### `GraphRAG`
195
+ The main orchestrator class for indexing and querying.
196
+
197
+ - `await initialize()`: Connects to PostgreSQL and creates necessary graph tables (`documents`, `entities`, `relations`, `doc_mentions`).
198
+ - `await index_document(text: str, metadata: Optional[Union[Dict[str, Any], DocumentMetadata]] = None) -> Dict[str, Any]`: Computes document embeddings, extracts entity/triple structures via LLM, and persists graph nodes/edges into PostgreSQL.
199
+ - `await query(question: str, top_k: int = 5) -> Dict[str, Any]`: Executes hybrid vector similarity search over documents and entities, traverses 1-hop graph relationship edges, and synthesizes a comprehensive answer. Returns dictionary with `question`, `answer`, `retrieved_documents`, `retrieved_entities`, and `retrieved_graph_triples`.
200
+ - `await close()`: Closes database connection pools.
201
+
202
+ ### `DocumentMetadata`
203
+ Data container for structured document metadata.
204
+
205
+ - `source: Optional[str]`: Document URL, path, or origin.
206
+ - `category: Optional[str]`: Document category or domain.
207
+ - `collection: Optional[str]`: Document collection or folder.
208
+ - `document: Optional[str]`: File title or filename.
209
+ - `page: Optional[int]`: 1-based page number.
210
+ - `paragraph: Optional[int]`: 1-based paragraph index.
211
+ - `extra: Dict[str, Any]`: Custom user metadata.
212
+ - `to_dict() -> Dict[str, Any]`: Serializes non-None fields to dictionary representation.
213
+ - `from_dict(data: Dict[str, Any]) -> DocumentMetadata`: Deserializes dictionary data.
214
+
215
+ ### `RAGGraphStore`
216
+ Database layer wrapping `post-graph`.
217
+
218
+ - `add_document(text, embedding, metadata)`: Inserts a document vertex into the `documents` table.
219
+ - `upsert_entity(name, entity_type, description, embedding)`: Upserts an entity vertex into the `entities` table.
220
+ - `add_relation(from_entity, to_entity, relation_type, description)`: Connects entity vertices with a directed relation edge.
221
+ - `search_similar_entities(query_vec, top_k)`: Executes `pgvector` HNSW similarity search over `entities`.
222
+ - `search_similar_documents(query_vec, top_k)`: Executes `pgvector` HNSW similarity search over `documents`.
223
+
224
+ ---
225
+
226
+ ## 🗄️ PostgreSQL Database Schema
227
+
228
+ `post-graph-rag` automatically provisions and manages the following graph schema in PostgreSQL powered by `post-graph`:
229
+
230
+ | Table Name | Type | Key Columns | Description |
231
+ | :--- | :--- | :--- | :--- |
232
+ | `{realm}_documents` | Vertex Table | `id`, `payload`, `embedding` (`vector`) | Stores raw text chunks and `DocumentMetadata` payloads |
233
+ | `{realm}_entities` | Vertex Table | `id`, `payload`, `embedding` (`vector`) | Canonical entity nodes (`name`, `type`, `description`) |
234
+ | `{realm}_relations` | Edge Table | `from_id`, `to_id`, `relation_type`, `payload` | Directed edges representing entity-to-entity triples |
235
+ | `{realm}_doc_mentions` | Edge Table | `from_id`, `to_id`, `relation_type` | Directed edges connecting document chunks to mentioned entities |
236
+ | `{table}_audit` | Audit Table | `audit_id`, `action`, `changed_by`, `changed_at` | Automatic shadow audit logging for all graph mutations |
237
+ | `{table}_data` | History Table | `data_id`, `payload`, `timestamp`, `embedding` | Append-only historical records for vertices and edges |
238
+
239
+ ---
240
+
241
+ ## 📄 License
242
+
243
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
244
+
245
+ Developed by **Chandan Rajah** (<chandan.rajah@gmail.com>).
@@ -0,0 +1,11 @@
1
+ post_graph_rag/__init__.py,sha256=zHVq43SytgMEBxnfcD9cKAtO2szkNdXywdZs-xr1xcs,611
2
+ post_graph_rag/config.py,sha256=O7YMMOoKUQKN9MDQH1RLp7DygMX4WicF_IenGoB9n10,619
3
+ post_graph_rag/engine.py,sha256=CqPFKQzVsFisFANkNQeya9oB2WltjjjJa3Vq_CYItiM,8440
4
+ post_graph_rag/extractor.py,sha256=zE_wnesnYIfK6dHe_RurSwRhatAabMmSmvLgylJtHE4,4713
5
+ post_graph_rag/graph_store.py,sha256=iPuJ3rz3W8P5BIJ1Lf-AnzFLcTmDUBzpVDqLggX7Jc0,5179
6
+ post_graph_rag/llm.py,sha256=MVX6mD0A47jWlolvinY96CK5y-XBBnxuvDoVz5BGkm8,2797
7
+ post_graph_rag/models.py,sha256=Zj4L_YPybHTDdZkXw2y4utbCB9SQHrRJX5nkvUvgiMg,1929
8
+ post_graph_rag-0.1.0.dist-info/METADATA,sha256=lKtXY8vQZtlZHPDi-hxLnaOwutFKp_wwKSJKjRokx24,10673
9
+ post_graph_rag-0.1.0.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
10
+ post_graph_rag-0.1.0.dist-info/licenses/LICENSE,sha256=JEdo9qnu7wdGIr5FHYju6FMZngUsjeWl6gZwGZSQxJE,1070
11
+ post_graph_rag-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.31.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Chandan Rajah
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.