Skip to content

Commit e1696aa

Browse files
committed
docs(kg): add KG backend details, GraphRAG search, and knowledge-graph playbook
- Update guide/kg.md (en + zh) from v1.0 → v2.0: actual pricing table, semantic reranking/clustering, my-graph data sources, GraphRAG search flow, auto-enrichment & knowledge push, circuit breaker params, error codes - Add zh/playbooks/knowledge-graph.md: 4 full prompts, GraphRAG flow, search comparison table, FAQ - Update sidebar and playbook indexes to include #15 Knowledge Graph Made-with: Cursor
1 parent 81a49cf commit e1696aa

6 files changed

Lines changed: 506 additions & 57 deletions

File tree

.vitepress/sidebar/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export const playbooksSidebarEn: DefaultTheme.SidebarItem[] = [
8888
{ text: '07 Publish Skill', link: '/zh/playbooks/publish-skill' },
8989
{ text: '09 Arena & Compete', link: '/zh/playbooks/arena-and-compete' },
9090
{ text: '11 Sandbox', link: '/zh/playbooks/sandbox' },
91+
{ text: '15 Knowledge Graph', link: '/zh/playbooks/knowledge-graph' },
9192
]
9293
},
9394
{
@@ -188,6 +189,7 @@ export const playbooksSidebarZh: DefaultTheme.SidebarItem[] = [
188189
{ text: '07 发布技能', link: '/zh/playbooks/publish-skill' },
189190
{ text: '09 竞技场比赛与投票', link: '/zh/playbooks/arena-and-compete' },
190191
{ text: '11 沙箱实验', link: '/zh/playbooks/sandbox' },
192+
{ text: '15 知识图谱查询与探索', link: '/zh/playbooks/knowledge-graph' },
191193
]
192194
},
193195
{

guide/kg.md

Lines changed: 123 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
---
22
title: Knowledge Graph
33
audience: Premium and above users
4-
version: 1.0
5-
last_updated: 2026-03-05
4+
version: 2.0
5+
last_updated: 2026-03-27
66
source_files:
77
- src/app/(main)/kg/page.js
8+
- src/routes/kg.js
9+
- src/services/kgService.js
10+
- src/services/graphRagSearchService.js
11+
- src/services/knowledgePushService.js
812
---
913

1014
# Knowledge Graph
@@ -22,14 +26,16 @@ The Knowledge Graph (`/kg`) provides semantic relationship-based knowledge searc
2226

2327
---
2428

25-
## Access Permissions
29+
## Access & Pricing
2630

27-
| User Type | Access Status |
28-
|-----------|--------------|
29-
| Free Users | Locked |
30-
| Premium Users | Full access |
31-
| Ultra Users | Full access |
32-
| Admins | Full access |
31+
| Plan | Access | Query Rate | Query Cost | Ingest Cost |
32+
|------|--------|-----------|-----------|------------|
33+
| Free | ❌ Locked ||||
34+
| Premium | ✅ Full | 60 req/min | 1 credit ($0.01) | 0.5 credits ($0.005) |
35+
| Ultra | ✅ Full | 300 req/min | 0.5 credits ($0.005) | 0.25 credits ($0.0025) |
36+
| Admin | ✅ Free | 60 req/min | 0 | 0 |
37+
38+
> 1 USD = 100 credits. The `GET /kg/status` endpoint returns the exact pricing in the `pricing` field.
3339
3440
When access is locked, the page uses `ProductPageLayout` to show feature introduction and upgrade prompts.
3541

@@ -57,6 +63,11 @@ Query: "Authentication solutions for microservices architecture"
5763
→ Returns knowledge nodes related to authentication, microservices, JWT, OAuth and their relationships
5864
```
5965

66+
When `type: "semantic"` is used, the system applies:
67+
68+
1. **Bocha Reranking** — over-fetches 50 candidates, reranks to top 20 using `bocha-semantic-reranker-en` (or `bocha-semantic-reranker-cn` for CJK queries)
69+
2. **Cluster Merging** — groups results into semantic clusters based on signal overlap (threshold 25%), surfacing shared concepts
70+
6071
Search results are displayed via the `KgResultCards` component as matching knowledge cards.
6172

6273
### Knowledge Ingestion
@@ -68,32 +79,98 @@ Search results are displayed via the `KgResultCards` component as matching knowl
6879
| Manual Ingestion | Input text or URL for knowledge extraction |
6980
| Auto-linking | System automatically identifies and builds relationships with existing knowledge |
7081

82+
### My Knowledge Graph
83+
84+
The `GET /kg/my-graph` endpoint aggregates data from multiple sources into a unified `{ nodes, links, stats }` structure:
85+
86+
| Source | Data |
87+
|--------|------|
88+
| Neo4j KG | Concept entities and their semantic relationships |
89+
| Platform Assets | Your published Genes, Capsules, Events (top 150) |
90+
| Validation Graph | Agents who validated your assets (and vice versa) |
91+
| Fetch Records | Agents who fetched your assets |
92+
93+
Node groups: `entity` (KG), `asset` (platform), `agent` (peers).
94+
Link types: `RELATED`, `lineage`, `expression`, `bundle`, `validation`, `fetch`.
95+
7196
### Example Queries
7297

7398
The `KgExamples` component provides preset query examples to help new users get started quickly.
7499

75100
---
76101

77-
## Billing
102+
## GraphRAG Search
103+
104+
`GET /a2a/assets/graph-search?q=...` is a **free, no-auth** endpoint that combines pgvector semantic search with graph-based expansion for richer results:
105+
106+
```text
107+
Phase 1: Semantic search → seed candidates
108+
Phase 2: Graph expansion (4 layers)
109+
├── Chain siblings (same evolution chain, +0.6)
110+
├── Lineage relatives (parent/child, +0.7)
111+
├── Signal overlap (keyword match, +0.4 × overlap)
112+
└── KG entity expansion (entity names → asset match, +0.5)
113+
Phase 3: Combined scoring
114+
semantic × 0.50 + graph × 0.20 + gdi × 0.15 + freshness × 0.15
115+
```
116+
117+
Each result includes a `scores` object (`combined`, `semantic`, `graph`) and the response includes `graph_context` with expansion statistics.
118+
119+
---
78120

79-
Knowledge Graph operations are billed per use:
121+
## Auto-enrichment & Knowledge Push
80122

81-
| Operation | Cost | Description |
82-
|-----------|------|-------------|
83-
| Query | Based on `pricing` config | Credits deducted per semantic search |
84-
| Ingest | Based on `pricing` config | Credits deducted per knowledge ingestion |
123+
These background processes run automatically — no user action required:
85124

86-
Operations are rejected when balance is insufficient. View credit details at `/account/balance`.
125+
| Process | Trigger | What Happens |
126+
|---------|---------|--------------|
127+
| **Auto-enrich** | Asset promoted | Gemini LLM extracts entities, relationships, and domain signals → writes to KG |
128+
| **Knowledge Push** | New knowledge appears | System finds agents with similar capabilities (cosine similarity > 0.5) → sends `knowledge_update` webhook to top 10 + notifies topic subscribers |
87129

88130
---
89131

90-
## Data Sources
132+
## Service Reliability
91133

92-
| API | Purpose |
93-
|-----|---------|
94-
| `GET /api/hub/kg/status` | Get access permissions, balance, and usage stats |
95-
| `POST /api/hub/kg/query` | Execute semantic search |
96-
| `POST /api/hub/kg/ingest` | Submit knowledge ingestion |
134+
The KG endpoints have an independent circuit breaker, separate from the global one:
135+
136+
| Parameter | Value | Env Var |
137+
|-----------|-------|---------|
138+
| Failure threshold | 5 consecutive failures | `KG_CIRCUIT_BREAKER_THRESHOLD` |
139+
| Recovery window | 60 seconds | `KG_CIRCUIT_BREAKER_RESET_MS` |
140+
| Request timeout | 10 seconds | `KG_REQUEST_TIMEOUT_MS` |
141+
142+
States: **Closed** (normal) → **Open** (all requests return 503) → **Half-Open** (one probe request; success closes, failure re-opens).
143+
144+
---
145+
146+
## API Reference
147+
148+
| API | Method | Purpose | Auth |
149+
|-----|--------|---------|------|
150+
| `/kg/status` | GET | Access permissions, balance, usage stats, pricing | Session + `kg` scope |
151+
| `/kg/query` | POST | Semantic search (supports `query`, `signals`, `entities`) | Session + `kg` scope |
152+
| `/kg/ingest` | POST | Write entities, relations, events | Session + `kg` scope |
153+
| `/kg/my-graph` | GET | Aggregated personal knowledge graph | Session + `kg` scope |
154+
| `/a2a/assets/graph-search` | GET | GraphRAG hybrid search | None |
155+
| `/billing/kg-usage` | GET | KG usage summary (by period) | Session |
156+
157+
> All `/kg/*` endpoints require a user session (or API key with `kg` scope). `node_secret` is not accepted.
158+
159+
---
160+
161+
## Error Codes
162+
163+
| Error | HTTP | Cause |
164+
|-------|------|-------|
165+
| `query_or_signals_required` | 400 | Missing query, signals, and entities |
166+
| `entities_or_relations_required` | 400 | Ingest has no data |
167+
| `scope_not_granted` | 403 | API key missing `kg` scope |
168+
| `plan_upgrade_required` | 403 | Free plan — upgrade to Premium/Ultra |
169+
| `feature_not_enabled` | 403 | KG feature gate not open for this user |
170+
| `insufficient_balance` | 403 | Not enough credits |
171+
| `kg_service_not_configured` | 503 | KG SaaS URL not set |
172+
| `kg_service_temporarily_unavailable` | 503 | Circuit breaker open |
173+
| `kg_service_timeout` | 503 | KG SaaS did not respond within 10s |
97174

98175
---
99176

@@ -102,12 +179,13 @@ Operations are rejected when balance is insufficient. View credit details at `/a
102179
<details>
103180
<summary><strong>What's the difference between Knowledge Graph search and Market search?</strong></summary>
104181

105-
| Dimension | Market Search | Knowledge Graph Search |
106-
|-----------|--------------|----------------------|
107-
| Search Method | Keyword matching | Semantic understanding |
108-
| Returns | Asset list | Concept relationship graph |
109-
| Depth | Literal matching | Conceptual association |
110-
| Cost | Free | Billed per use |
182+
| Dimension | Market Search | KG Search | GraphRAG Search |
183+
|-----------|--------------|-----------|-----------------|
184+
| Searches | Assets | Concept entities | Assets + graph context |
185+
| Method | Keyword matching | Semantic understanding | Semantic + graph expansion |
186+
| Returns | Asset list | Concept relationship graph | Ranked assets with scores |
187+
| Cost | Free | Billed per use | Free |
188+
| Auth | None | Session + kg scope | None |
111189

112190
</details>
113191

@@ -117,3 +195,20 @@ Operations are rejected when balance is insufficient. View credit details at `/a
117195
Ingested knowledge is incorporated into the Knowledge Graph's semantic network, but it won't be directly displayed as public assets. It enhances the graph's depth and breadth in the form of relationships and concepts.
118196

119197
</details>
198+
199+
<details>
200+
<summary><strong>What happens when KG returns 503?</strong></summary>
201+
202+
Three possible causes:
203+
1. `kg_service_not_configured` — Hub has no KG SaaS connection (contact admin)
204+
2. `kg_service_temporarily_unavailable` — Circuit breaker open (auto-recovers in 60s)
205+
3. `kg_service_timeout` — KG SaaS is slow (retry later)
206+
207+
</details>
208+
209+
<details>
210+
<summary><strong>How does auto-enrichment work?</strong></summary>
211+
212+
When you publish an asset and it gets promoted, the system automatically uses Gemini LLM to extract entities (concepts, tools, techniques, patterns), relationships (uses, solves, requires, improves, contradicts), and domain signals from the asset content, then writes them to the KG. The asset's `payload.metadata.kg_enriched` is set to `true` after successful enrichment.
213+
214+
</details>

playbooks/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Each Playbook is a **complete usage scenario** that includes: a ready-to-copy pr
2727
| 10 | [Troubleshoot](/zh/playbooks/troubleshoot) | Diagnose errors and recover | ⭐⭐⭐ Advanced |
2828
| 11 | [Sandbox](/zh/playbooks/sandbox) | Create an isolated sandbox for experiments | ⭐⭐ Basic |
2929
| 12 | [Swarm Mode](/zh/playbooks/swarm-mode) | Decompose a large task for multi-Agent collaboration | ⭐⭐⭐ Advanced |
30+
| 15 | [Knowledge Graph](/zh/playbooks/knowledge-graph) | Query the KG and explore with GraphRAG | ⭐⭐ Basic |
3031

3132
## How to Use
3233

@@ -78,6 +79,12 @@ Replace `{{VARIABLE}}` placeholders in each prompt with your actual values:
7879
11 Sandbox ──▶ 02 Evolve & Publish inside sandbox ──▶ Compare results
7980
```
8081

82+
### Knowledge Graph Exploration
83+
84+
```
85+
03 Search & Learn ──▶ 15 Knowledge Graph (deep semantic query + GraphRAG)
86+
```
87+
8188
### Troubleshooting
8289

8390
```

0 commit comments

Comments
 (0)