@anvia/graph 1.1.4 → 1.1.6
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.
- package/README.md +39 -124
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -1,9 +1,23 @@
|
|
|
1
1
|
# @anvia/graph
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Turn text into typed entities and relationships for GraphRAG. Define a graph schema once,
|
|
4
|
+
then use it for extraction, retrieval tools, and ingestion with Neo4j or Memgraph.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
pnpm add @anvia/graph @anvia/core @anvia/openai zod
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
`@anvia/openai` supplies the extraction model below; other completion adapters work too.
|
|
13
|
+
|
|
14
|
+
## Quickstart
|
|
15
|
+
|
|
16
|
+
Set `OPENAI_API_KEY`. This example extracts graph facts without requiring a graph database.
|
|
4
17
|
|
|
5
18
|
```ts
|
|
6
19
|
import { defineGraphSchema, extractGraphFacts } from "@anvia/graph";
|
|
20
|
+
import { OpenAIClient } from "@anvia/openai";
|
|
7
21
|
import { z } from "zod";
|
|
8
22
|
|
|
9
23
|
const schema = defineGraphSchema({
|
|
@@ -16,134 +30,35 @@ const schema = defineGraphSchema({
|
|
|
16
30
|
},
|
|
17
31
|
relationships: {},
|
|
18
32
|
});
|
|
19
|
-
|
|
20
|
-
const facts = await extractGraphFacts({
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
```ts
|
|
31
|
-
import { ingestGraphText } from "@anvia/graph";
|
|
32
|
-
|
|
33
|
-
const ingestion = await ingestGraphText({
|
|
34
|
-
graph,
|
|
35
|
-
document: {
|
|
36
|
-
id: "incident-42",
|
|
37
|
-
text,
|
|
38
|
-
metadata: { tenant: "acme" },
|
|
39
|
-
},
|
|
40
|
-
extractionModel,
|
|
41
|
-
embeddingModel,
|
|
42
|
-
chunking: {
|
|
43
|
-
strategy: "recursive",
|
|
44
|
-
maxSize: 1_000,
|
|
45
|
-
overlap: 100,
|
|
46
|
-
separators: ["\n\n", "\n", " "],
|
|
47
|
-
},
|
|
48
|
-
conflict: "error",
|
|
49
|
-
orphanEntities: "delete",
|
|
50
|
-
factConflicts: {
|
|
51
|
-
entity: {
|
|
52
|
-
properties: {
|
|
53
|
-
summary: "prefer-longest",
|
|
54
|
-
aliases: "union",
|
|
55
|
-
confidence: "max",
|
|
56
|
-
},
|
|
33
|
+
const client = new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY! });
|
|
34
|
+
const facts = await extractGraphFacts({
|
|
35
|
+
model: client.completionModel({ modelId: "gpt-5.6", api: "responses" }),
|
|
36
|
+
schema,
|
|
37
|
+
chunks: [
|
|
38
|
+
{
|
|
39
|
+
id: "chunk-1",
|
|
40
|
+
documentId: "products",
|
|
41
|
+
index: 0,
|
|
42
|
+
text: "The product with ID anvia is named Anvia.",
|
|
57
43
|
},
|
|
58
|
-
|
|
59
|
-
});
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
Extraction conflicts and stored-graph write conflicts are separate policies. `factConflicts`
|
|
63
|
-
resolves disagreements between chunks before persistence; `conflict` controls collisions with facts
|
|
64
|
-
already stored by the graph adapter. Extraction rejects disagreements by default. Built-in
|
|
65
|
-
property strategies include `prefer-first`, `prefer-last`, `prefer-defined`, `prefer-longest`,
|
|
66
|
-
`union`, `max`, and `min`; a custom resolver receives structured candidate and source chunk data.
|
|
67
|
-
Conflict errors and resolved warnings include the fact type, stable key, parsed identity, property,
|
|
68
|
-
candidate values, and source chunk IDs. Resolved disagreements are returned in `warnings`.
|
|
69
|
-
|
|
70
|
-
Use `ingestGraphDocuments()` for a batch. Both functions extract facts, embed chunks and entities,
|
|
71
|
-
and atomically replace each source document in the managed graph. Existing graph registrations are
|
|
72
|
-
read-only and are rejected as ingestion targets by TypeScript.
|
|
73
|
-
|
|
74
|
-
The result also exposes `vectorDocuments`, grouped by source document ID, so a vector store can
|
|
75
|
-
reuse the chunk embeddings without another model call:
|
|
76
|
-
|
|
77
|
-
```ts
|
|
78
|
-
await vectorStore.upsert({ documents: ingestion.vectorDocuments });
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
Every ingestion result includes a `receipt` with document, entity, relationship, and vector IDs,
|
|
82
|
-
the graph write counts, warnings, an optional caller-supplied `revision`, and vector-stage status.
|
|
83
|
-
Use `ingestGraphTextToStores()` or `ingestGraphDocumentsToStores()` to write the graph followed by
|
|
84
|
-
the vector store. If the second stage fails, `GraphIngestionStageError` exposes a receipt with a
|
|
85
|
-
completed graph stage and failed vector stage for queue reconciliation.
|
|
86
|
-
|
|
87
|
-
The graph and vector writes are separate transactions. Applications that require cross-store
|
|
88
|
-
reconciliation should persist their own ingestion status and retry the incomplete write. Advanced
|
|
89
|
-
callers can use `prepareGraphDocuments()` to run chunking, extraction, and embedding without writing,
|
|
90
|
-
then pass its graph fields to `graph.replaceDocuments()` and its `vectorDocuments` to a vector store.
|
|
91
|
-
|
|
92
|
-
## Search tool
|
|
93
|
-
|
|
94
|
-
Create the same Agent tool for any graph adapter implementing `GraphContextRetriever`:
|
|
95
|
-
|
|
96
|
-
```ts
|
|
97
|
-
import { createGraphSearchTool } from "@anvia/graph";
|
|
98
|
-
|
|
99
|
-
const searchGraph = createGraphSearchTool({
|
|
100
|
-
name: "search_graph",
|
|
101
|
-
description: "Search connected entities and supporting evidence.",
|
|
102
|
-
graph,
|
|
103
|
-
model: embeddingModel,
|
|
104
|
-
search: {
|
|
105
|
-
type: "vector",
|
|
106
|
-
seeds: ["entities"],
|
|
107
|
-
topK: 8,
|
|
108
|
-
},
|
|
109
|
-
traversal: {
|
|
110
|
-
relationships: ["AFFECTS"],
|
|
111
|
-
direction: "both",
|
|
112
|
-
maxDepth: 2,
|
|
113
|
-
maxNodes: 40,
|
|
114
|
-
maxRelationships: 80,
|
|
115
|
-
},
|
|
116
|
-
evidence: { type: "chunks", maxChunks: 12 },
|
|
44
|
+
],
|
|
117
45
|
});
|
|
46
|
+
console.log(facts.output.entities);
|
|
118
47
|
```
|
|
119
48
|
|
|
120
|
-
|
|
121
|
-
chunks, while existing graph registrations use `{ type: "none" }`.
|
|
122
|
-
|
|
123
|
-
## Exploration
|
|
49
|
+
## Build on the same schema
|
|
124
50
|
|
|
125
|
-
|
|
51
|
+
- Ingest documents with chunking, extraction, and embeddings into managed graphs.
|
|
52
|
+
- Reuse chunk embeddings in vector stores and track partial writes with ingestion receipts.
|
|
53
|
+
- Resolve extraction conflicts with explicit property policies.
|
|
54
|
+
- Create bounded graph search tools and explore graph neighborhoods.
|
|
126
55
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
nodeTypes: ["Product"],
|
|
131
|
-
includeProvenance: true,
|
|
132
|
-
maxNodes: 100,
|
|
133
|
-
maxRelationships: 200,
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
const neighborhood = await graph.explore({
|
|
137
|
-
mode: "expand",
|
|
138
|
-
nodeIds: [overview.nodes[0]!.id],
|
|
139
|
-
direction: "both",
|
|
140
|
-
maxDepth: 1,
|
|
141
|
-
});
|
|
142
|
-
```
|
|
56
|
+
Persistence and provisioning belong to [Neo4j](https://github.com/anvia-hq/anvia/blob/main/packages/graph-neo4j/README.md)
|
|
57
|
+
and [Memgraph](https://github.com/anvia-hq/anvia/blob/main/packages/graph-memgraph/README.md).
|
|
58
|
+
Property schemas must use `z.strictObject()` and preserve input values without transforms or defaults.
|
|
143
59
|
|
|
144
|
-
|
|
145
|
-
same graph, not persistence or cross-provider migration. Adapters cap overview and expansion requests,
|
|
146
|
-
return truncation metadata, and omit reserved Anvia properties such as stored embeddings.
|
|
60
|
+
## Learn more
|
|
147
61
|
|
|
148
|
-
|
|
149
|
-
|
|
62
|
+
- [Usage guide](https://github.com/anvia-hq/anvia/blob/main/docs/packages/graph.md)
|
|
63
|
+
- [Anvia overview](https://github.com/anvia-hq/anvia/blob/main/README.md)
|
|
64
|
+
- [Contributing](https://github.com/anvia-hq/anvia/blob/main/CONTRIBUTING.md)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anvia/graph",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.6",
|
|
4
4
|
"description": "Provider-neutral knowledge graph primitives for Anvia.",
|
|
5
5
|
"author": "anvia",
|
|
6
6
|
"license": "MIT",
|
|
@@ -32,13 +32,13 @@
|
|
|
32
32
|
"@types/node": "^24.9.1",
|
|
33
33
|
"tsup": "^8.5.0",
|
|
34
34
|
"typescript": "^5.9.3",
|
|
35
|
-
"vitest": "^4.
|
|
36
|
-
"zod": "^4.6.
|
|
37
|
-
"@anvia/core": "1.
|
|
35
|
+
"vitest": "^4.1.11",
|
|
36
|
+
"zod": "^4.6.7",
|
|
37
|
+
"@anvia/core": "1.17.2"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"zod": "^4.6.
|
|
41
|
-
"@anvia/core": "^1.
|
|
40
|
+
"zod": "^4.6.7",
|
|
41
|
+
"@anvia/core": "^1.17.2"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "tsup src/index.ts src/explore.ts --format esm --dts --sourcemap --clean",
|