@anvia/neo4j 1.0.0-rc.10
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/LICENSE +21 -0
- package/README.md +212 -0
- package/dist/index.d.ts +357 -0
- package/dist/index.js +2272 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Indra Zulfi
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
# @anvia/neo4j
|
|
2
|
+
|
|
3
|
+
Schema-first local GraphRAG for Neo4j 2026.01 and newer.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @anvia/neo4j @anvia/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Managed knowledge graph
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { Agent } from "@anvia/core/agent";
|
|
15
|
+
import { embedDocuments } from "@anvia/core/embeddings";
|
|
16
|
+
import {
|
|
17
|
+
Neo4jClient,
|
|
18
|
+
createNeo4jGraphSearchTool,
|
|
19
|
+
defineNeo4jGraphSchema,
|
|
20
|
+
extractGraphFacts,
|
|
21
|
+
} from "@anvia/neo4j";
|
|
22
|
+
import { z } from "zod";
|
|
23
|
+
|
|
24
|
+
const schema = defineNeo4jGraphSchema({
|
|
25
|
+
nodes: {
|
|
26
|
+
Product: {
|
|
27
|
+
description: "A product or service.",
|
|
28
|
+
identity: ["id"],
|
|
29
|
+
properties: z.strictObject({ id: z.string(), name: z.string() }),
|
|
30
|
+
},
|
|
31
|
+
Incident: {
|
|
32
|
+
description: "An operational incident.",
|
|
33
|
+
identity: ["id"],
|
|
34
|
+
properties: z.strictObject({ id: z.string(), title: z.string() }),
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
relationships: {
|
|
38
|
+
AFFECTS: {
|
|
39
|
+
description: "An incident affects a product.",
|
|
40
|
+
from: "Incident",
|
|
41
|
+
to: "Product",
|
|
42
|
+
properties: z.strictObject({ severity: z.enum(["low", "high"]) }),
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
await using client = new Neo4jClient({
|
|
48
|
+
uri: process.env.NEO4J_URI!,
|
|
49
|
+
auth: {
|
|
50
|
+
username: process.env.NEO4J_USERNAME!,
|
|
51
|
+
password: process.env.NEO4J_PASSWORD!,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
const graph = client.managedKnowledgeGraph({
|
|
55
|
+
name: "support",
|
|
56
|
+
schema,
|
|
57
|
+
resources: {
|
|
58
|
+
labels: { document: "SupportDocument", chunk: "SupportChunk", entity: "SupportEntity" },
|
|
59
|
+
indexes: {
|
|
60
|
+
chunks: {
|
|
61
|
+
vector: { name: "support_chunks_vector", dimensions: 1536, similarity: "cosine" },
|
|
62
|
+
fulltext: { name: "support_chunks_text" },
|
|
63
|
+
},
|
|
64
|
+
entities: {
|
|
65
|
+
vector: { name: "support_entities_vector", dimensions: 1536, similarity: "cosine" },
|
|
66
|
+
fulltext: { name: "support_entities_text", properties: ["id", "name", "title"] },
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
await graph.ensure({ indexTimeoutMs: 60_000 });
|
|
73
|
+
|
|
74
|
+
const facts = await extractGraphFacts({ model: extractionModel, schema, chunks });
|
|
75
|
+
const [embeddedChunks, embeddedEntities] = await Promise.all([
|
|
76
|
+
embedDocuments({
|
|
77
|
+
model: embeddingModel,
|
|
78
|
+
documents: chunks,
|
|
79
|
+
id: (chunk) => chunk.id,
|
|
80
|
+
content: (chunk) => chunk.text,
|
|
81
|
+
}),
|
|
82
|
+
embedDocuments({
|
|
83
|
+
model: embeddingModel,
|
|
84
|
+
documents: [...facts.output.entities],
|
|
85
|
+
id: (entity) => entity.key,
|
|
86
|
+
content: (entity) => formatEntity(entity),
|
|
87
|
+
}),
|
|
88
|
+
]);
|
|
89
|
+
|
|
90
|
+
const write = await graph.replaceDocuments({
|
|
91
|
+
documents,
|
|
92
|
+
chunks: embeddedChunks.documents,
|
|
93
|
+
entities: embeddedEntities.documents,
|
|
94
|
+
relationships: facts.output.relationships,
|
|
95
|
+
mentions: facts.output.mentions,
|
|
96
|
+
conflict: "error",
|
|
97
|
+
orphanEntities: "delete",
|
|
98
|
+
});
|
|
99
|
+
console.log(write);
|
|
100
|
+
|
|
101
|
+
const searchGraph = createNeo4jGraphSearchTool({
|
|
102
|
+
name: "search_support_graph",
|
|
103
|
+
description: "Search connected support incidents and products.",
|
|
104
|
+
graph,
|
|
105
|
+
model: embeddingModel,
|
|
106
|
+
search: {
|
|
107
|
+
type: "hybrid",
|
|
108
|
+
seeds: ["chunks", "entities"],
|
|
109
|
+
topK: 8,
|
|
110
|
+
candidatesPerSeed: 20,
|
|
111
|
+
rrfK: 60,
|
|
112
|
+
},
|
|
113
|
+
traversal: {
|
|
114
|
+
relationships: ["AFFECTS"],
|
|
115
|
+
direction: "both",
|
|
116
|
+
maxDepth: 2,
|
|
117
|
+
maxNodes: 40,
|
|
118
|
+
maxRelationships: 80,
|
|
119
|
+
},
|
|
120
|
+
evidence: { type: "chunks", maxChunks: 12 },
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const agent = new Agent({ id: "support", model: chatModel, tools: [searchGraph] });
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
The application owns file discovery, reading, chunking, entity embedding text, model loading,
|
|
127
|
+
retry policy, and Agent lifecycle. `extractGraphFacts()` performs model extraction but no writes.
|
|
128
|
+
`replaceDocuments()` performs one deterministic transaction but no model calls.
|
|
129
|
+
|
|
130
|
+
Graph property schemas must use `z.strictObject()` and must not coerce, default, transform,
|
|
131
|
+
preprocess, catch, trim, or otherwise overwrite values. This keeps extraction, comparison, and
|
|
132
|
+
stored Neo4j properties exact.
|
|
133
|
+
|
|
134
|
+
Re-ingestion is document-scoped. `replaceDocuments()` removes the old chunks and their provenance
|
|
135
|
+
before inserting the new representation. Facts supported by other documents remain. Use
|
|
136
|
+
`deleteDocuments({ documentIds, orphanEntities })` for explicit removal.
|
|
137
|
+
|
|
138
|
+
Both write operations return exact logical before-and-after counts for documents, chunks, entities,
|
|
139
|
+
relationships, and mentions. A resource is `created`, `updated`, `deleted`, or `unchanged` by its
|
|
140
|
+
stable public identity; the result is produced inside the same transaction as the mutation.
|
|
141
|
+
|
|
142
|
+
Retrieval always requires an explicit evidence mode. `{ type: "none" }` performs no evidence read.
|
|
143
|
+
Managed graphs can use `{ type: "chunks", maxChunks }` to hydrate exact source chunks in ranked
|
|
144
|
+
provenance order. Seeds, nodes, and relationships refer to those records through `sourceChunkIds`.
|
|
145
|
+
|
|
146
|
+
## Existing graph
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
const graph = client.knowledgeGraph({
|
|
150
|
+
schema,
|
|
151
|
+
seeds: {
|
|
152
|
+
incidents: {
|
|
153
|
+
nodeTypes: ["Incident"],
|
|
154
|
+
vectorIndex: {
|
|
155
|
+
name: "existing_incident_vectors",
|
|
156
|
+
property: "embedding",
|
|
157
|
+
dimensions: 1536,
|
|
158
|
+
similarity: "cosine",
|
|
159
|
+
},
|
|
160
|
+
fulltextIndex: {
|
|
161
|
+
name: "existing_incident_text",
|
|
162
|
+
properties: ["title"],
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
products: {
|
|
166
|
+
nodeTypes: ["Product"],
|
|
167
|
+
vectorIndex: {
|
|
168
|
+
name: "existing_product_vectors",
|
|
169
|
+
property: "embedding",
|
|
170
|
+
dimensions: 1536,
|
|
171
|
+
similarity: "cosine",
|
|
172
|
+
},
|
|
173
|
+
fulltextIndex: {
|
|
174
|
+
name: "existing_product_text",
|
|
175
|
+
properties: ["name"],
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
await graph.validate();
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Existing registrations are read-only and expose no provisioning or write methods. Use
|
|
185
|
+
`client.nativeDriver()` for application-owned Cypher.
|
|
186
|
+
|
|
187
|
+
Existing graph registrations do not own Anvia document and chunk resources, so their retrieval and
|
|
188
|
+
tool registrations accept only `evidence: { type: "none" }`.
|
|
189
|
+
|
|
190
|
+
Each existing seed describes exactly one indexed node type and the complete expected vector and
|
|
191
|
+
full-text index definitions. Register additional node types as separate seeds and fuse them with
|
|
192
|
+
hybrid retrieval.
|
|
193
|
+
|
|
194
|
+
Package operations use explicit transactions and do not invoke Neo4j's managed retry APIs. Abort
|
|
195
|
+
signals roll back the active transaction. Applications decide whether and how to retry an entire
|
|
196
|
+
operation.
|
|
197
|
+
|
|
198
|
+
`Neo4jClient` supports `await using` and closes a driver that it creates from `uri` and `auth`.
|
|
199
|
+
When a caller supplies `driver`, that driver remains caller-owned; closing or disposing the client
|
|
200
|
+
only closes the Anvia client handle.
|
|
201
|
+
|
|
202
|
+
The public property boundary is intentionally JSON-safe. Unsafe Neo4j integers, temporal and
|
|
203
|
+
spatial objects, nodes, relationships, paths, maps, and heterogeneous property arrays are rejected
|
|
204
|
+
instead of being coerced.
|
|
205
|
+
|
|
206
|
+
## Development
|
|
207
|
+
|
|
208
|
+
```sh
|
|
209
|
+
pnpm --filter @anvia/neo4j typecheck
|
|
210
|
+
pnpm --filter @anvia/neo4j test
|
|
211
|
+
pnpm --filter @anvia/neo4j build
|
|
212
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import { Driver, QueryResult, ManagedTransaction } from 'neo4j-driver';
|
|
2
|
+
import * as _anvia_core_embeddings from '@anvia/core/embeddings';
|
|
3
|
+
import { EmbeddedDocument } from '@anvia/core/embeddings';
|
|
4
|
+
import * as _anvia_core from '@anvia/core';
|
|
5
|
+
import { RetrySetting, CompletionModel, Usage } from '@anvia/core';
|
|
6
|
+
import { Tool } from '@anvia/core/tool';
|
|
7
|
+
import { z } from 'zod';
|
|
8
|
+
|
|
9
|
+
type Neo4jPropertyPrimitive = string | number | boolean;
|
|
10
|
+
type Neo4jPropertyValue = Neo4jPropertyPrimitive | readonly string[] | readonly number[] | readonly boolean[];
|
|
11
|
+
type Neo4jProperties = Readonly<Record<string, Neo4jPropertyValue>>;
|
|
12
|
+
type Neo4jNodeIdentity = Readonly<Record<string, Neo4jPropertyPrimitive>>;
|
|
13
|
+
type Neo4jNodeDefinition = Readonly<{
|
|
14
|
+
description: string;
|
|
15
|
+
identity: readonly string[];
|
|
16
|
+
properties: z.ZodObject<z.ZodRawShape>;
|
|
17
|
+
}>;
|
|
18
|
+
type Neo4jRelationshipDefinition = Readonly<{
|
|
19
|
+
description: string;
|
|
20
|
+
from: string;
|
|
21
|
+
to: string;
|
|
22
|
+
identity?: readonly string[] | undefined;
|
|
23
|
+
properties: z.ZodObject<z.ZodRawShape>;
|
|
24
|
+
}>;
|
|
25
|
+
type Neo4jGraphSchemaOptions = Readonly<{
|
|
26
|
+
nodes: Readonly<Record<string, Neo4jNodeDefinition>>;
|
|
27
|
+
relationships: Readonly<Record<string, Neo4jRelationshipDefinition>>;
|
|
28
|
+
}>;
|
|
29
|
+
type Neo4jGraphSchema<Options extends Neo4jGraphSchemaOptions = Neo4jGraphSchemaOptions> = Readonly<Options & {
|
|
30
|
+
kind: "neo4j-graph-schema";
|
|
31
|
+
}>;
|
|
32
|
+
type NodeDefinitions<Schema extends Neo4jGraphSchema> = Schema["nodes"];
|
|
33
|
+
type RelationshipDefinitions<Schema extends Neo4jGraphSchema> = Schema["relationships"];
|
|
34
|
+
type Neo4jGraphEntity<Schema extends Neo4jGraphSchema = Neo4jGraphSchema> = {
|
|
35
|
+
[Type in keyof NodeDefinitions<Schema> & string]: Readonly<{
|
|
36
|
+
key: string;
|
|
37
|
+
type: Type;
|
|
38
|
+
identity: Neo4jNodeIdentity;
|
|
39
|
+
properties: z.output<NodeDefinitions<Schema>[Type]["properties"]> & Neo4jProperties;
|
|
40
|
+
sourceChunkIds: readonly string[];
|
|
41
|
+
}>;
|
|
42
|
+
}[keyof NodeDefinitions<Schema> & string];
|
|
43
|
+
type Neo4jGraphRelationship<Schema extends Neo4jGraphSchema = Neo4jGraphSchema> = {
|
|
44
|
+
[Type in keyof RelationshipDefinitions<Schema> & string]: Readonly<{
|
|
45
|
+
key: string;
|
|
46
|
+
type: Type;
|
|
47
|
+
from: string;
|
|
48
|
+
to: string;
|
|
49
|
+
properties: z.output<RelationshipDefinitions<Schema>[Type]["properties"]> & Neo4jProperties;
|
|
50
|
+
sourceChunkIds: readonly string[];
|
|
51
|
+
}>;
|
|
52
|
+
}[keyof RelationshipDefinitions<Schema> & string];
|
|
53
|
+
type Neo4jGraphMention = Readonly<{
|
|
54
|
+
chunkId: string;
|
|
55
|
+
entityKey: string;
|
|
56
|
+
}>;
|
|
57
|
+
type Neo4jGraphFacts<Schema extends Neo4jGraphSchema = Neo4jGraphSchema> = Readonly<{
|
|
58
|
+
entities: readonly Neo4jGraphEntity<Schema>[];
|
|
59
|
+
relationships: readonly Neo4jGraphRelationship<Schema>[];
|
|
60
|
+
mentions: readonly Neo4jGraphMention[];
|
|
61
|
+
}>;
|
|
62
|
+
type Neo4jGraphChunk<Metadata extends Neo4jProperties = Neo4jProperties> = Readonly<{
|
|
63
|
+
id: string;
|
|
64
|
+
documentId: string;
|
|
65
|
+
index: number;
|
|
66
|
+
text: string;
|
|
67
|
+
metadata?: Metadata | undefined;
|
|
68
|
+
}>;
|
|
69
|
+
type Neo4jGraphDocument<Properties extends Neo4jProperties = Neo4jProperties> = Readonly<{
|
|
70
|
+
id: string;
|
|
71
|
+
properties?: Properties | undefined;
|
|
72
|
+
}>;
|
|
73
|
+
type ExtractGraphFactsOptions<Schema extends Neo4jGraphSchema, Model extends CompletionModel> = Readonly<{
|
|
74
|
+
model: Model;
|
|
75
|
+
schema: Schema;
|
|
76
|
+
chunks: readonly Neo4jGraphChunk[];
|
|
77
|
+
instructions?: string | undefined;
|
|
78
|
+
retries?: RetrySetting | undefined;
|
|
79
|
+
concurrency?: number | undefined;
|
|
80
|
+
abortSignal?: AbortSignal | undefined;
|
|
81
|
+
}>;
|
|
82
|
+
type ExtractGraphFactsResult<Schema extends Neo4jGraphSchema> = Readonly<{
|
|
83
|
+
output: Neo4jGraphFacts<Schema>;
|
|
84
|
+
usage: Usage;
|
|
85
|
+
}>;
|
|
86
|
+
type Neo4jVectorIndex = Readonly<{
|
|
87
|
+
name: string;
|
|
88
|
+
dimensions: number;
|
|
89
|
+
similarity: "cosine" | "euclidean";
|
|
90
|
+
}>;
|
|
91
|
+
type Neo4jFulltextIndex = Readonly<{
|
|
92
|
+
name: string;
|
|
93
|
+
properties?: readonly string[] | undefined;
|
|
94
|
+
}>;
|
|
95
|
+
type ManagedNeo4jKnowledgeGraphOptions<Schema extends Neo4jGraphSchema> = Readonly<{
|
|
96
|
+
name: string;
|
|
97
|
+
schema: Schema;
|
|
98
|
+
resources: {
|
|
99
|
+
labels: {
|
|
100
|
+
document: string;
|
|
101
|
+
chunk: string;
|
|
102
|
+
entity: string;
|
|
103
|
+
};
|
|
104
|
+
indexes: {
|
|
105
|
+
chunks: {
|
|
106
|
+
vector: Neo4jVectorIndex;
|
|
107
|
+
fulltext?: Neo4jFulltextIndex | undefined;
|
|
108
|
+
};
|
|
109
|
+
entities: {
|
|
110
|
+
vector: Neo4jVectorIndex;
|
|
111
|
+
fulltext?: Neo4jFulltextIndex | undefined;
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
}>;
|
|
116
|
+
type ExistingNeo4jSeed = Readonly<{
|
|
117
|
+
nodeTypes: readonly string[];
|
|
118
|
+
vectorIndex: Neo4jVectorIndex & Readonly<{
|
|
119
|
+
property: string;
|
|
120
|
+
}>;
|
|
121
|
+
fulltextIndex?: (Neo4jFulltextIndex & Readonly<{
|
|
122
|
+
properties: readonly string[];
|
|
123
|
+
}>) | undefined;
|
|
124
|
+
}>;
|
|
125
|
+
type Neo4jKnowledgeGraphOptions<Schema extends Neo4jGraphSchema> = Readonly<{
|
|
126
|
+
schema: Schema;
|
|
127
|
+
seeds: Readonly<Record<string, ExistingNeo4jSeed>>;
|
|
128
|
+
}>;
|
|
129
|
+
type Neo4jClientOptions = Readonly<{
|
|
130
|
+
uri: string;
|
|
131
|
+
auth: {
|
|
132
|
+
username: string;
|
|
133
|
+
password: string;
|
|
134
|
+
};
|
|
135
|
+
database?: string | undefined;
|
|
136
|
+
driver?: never;
|
|
137
|
+
}> | Readonly<{
|
|
138
|
+
driver: Driver;
|
|
139
|
+
database?: string | undefined;
|
|
140
|
+
uri?: never;
|
|
141
|
+
auth?: never;
|
|
142
|
+
}>;
|
|
143
|
+
type Neo4jGraphValidateOptions = Readonly<{
|
|
144
|
+
abortSignal?: AbortSignal | undefined;
|
|
145
|
+
}>;
|
|
146
|
+
type Neo4jGraphEnsureOptions = Readonly<{
|
|
147
|
+
indexTimeoutMs: number;
|
|
148
|
+
abortSignal?: AbortSignal | undefined;
|
|
149
|
+
}>;
|
|
150
|
+
type Neo4jGraphConflict = "error" | "overwrite" | "keep-existing";
|
|
151
|
+
type Neo4jOrphanEntityPolicy = "delete" | "keep";
|
|
152
|
+
type ReplaceNeo4jDocumentsOptions<Schema extends Neo4jGraphSchema> = Readonly<{
|
|
153
|
+
documents: readonly Neo4jGraphDocument[];
|
|
154
|
+
chunks: readonly EmbeddedDocument<Neo4jGraphChunk>[];
|
|
155
|
+
entities: readonly EmbeddedDocument<Neo4jGraphEntity<Schema>>[];
|
|
156
|
+
relationships: readonly Neo4jGraphRelationship<Schema>[];
|
|
157
|
+
mentions: readonly Neo4jGraphMention[];
|
|
158
|
+
conflict: Neo4jGraphConflict;
|
|
159
|
+
orphanEntities: Neo4jOrphanEntityPolicy;
|
|
160
|
+
abortSignal?: AbortSignal | undefined;
|
|
161
|
+
}>;
|
|
162
|
+
type DeleteNeo4jDocumentsOptions = Readonly<{
|
|
163
|
+
documentIds: readonly string[];
|
|
164
|
+
orphanEntities: Neo4jOrphanEntityPolicy;
|
|
165
|
+
abortSignal?: AbortSignal | undefined;
|
|
166
|
+
}>;
|
|
167
|
+
type Neo4jVectorSearchOptions = Readonly<{
|
|
168
|
+
type: "vector";
|
|
169
|
+
seeds: readonly string[];
|
|
170
|
+
topK: number;
|
|
171
|
+
minScore?: number | undefined;
|
|
172
|
+
}>;
|
|
173
|
+
type Neo4jHybridSearchOptions = Readonly<{
|
|
174
|
+
type: "hybrid";
|
|
175
|
+
seeds: readonly string[];
|
|
176
|
+
topK: number;
|
|
177
|
+
candidatesPerSeed: number;
|
|
178
|
+
rrfK: number;
|
|
179
|
+
}>;
|
|
180
|
+
type Neo4jGraphSearchOptions = Neo4jVectorSearchOptions | Neo4jHybridSearchOptions;
|
|
181
|
+
type Neo4jGraphEvidenceOptions = Readonly<{
|
|
182
|
+
type: "none";
|
|
183
|
+
}> | Readonly<{
|
|
184
|
+
type: "chunks";
|
|
185
|
+
maxChunks: number;
|
|
186
|
+
}>;
|
|
187
|
+
type Neo4jGraphEvidence = Readonly<{
|
|
188
|
+
chunkId: string;
|
|
189
|
+
documentId: string;
|
|
190
|
+
index: number;
|
|
191
|
+
text: string;
|
|
192
|
+
metadata: Neo4jProperties;
|
|
193
|
+
}>;
|
|
194
|
+
type Neo4jGraphTraversalOptions<Schema extends Neo4jGraphSchema = Neo4jGraphSchema> = Readonly<{
|
|
195
|
+
relationships: readonly (keyof Schema["relationships"] & string)[];
|
|
196
|
+
direction: "outgoing" | "incoming" | "both";
|
|
197
|
+
maxDepth: number;
|
|
198
|
+
maxNodes: number;
|
|
199
|
+
maxRelationships: number;
|
|
200
|
+
}>;
|
|
201
|
+
type Neo4jGraphContextSeed = Readonly<{
|
|
202
|
+
key: string;
|
|
203
|
+
type: string;
|
|
204
|
+
score: number;
|
|
205
|
+
properties: Neo4jProperties;
|
|
206
|
+
sourceChunkIds: readonly string[];
|
|
207
|
+
}>;
|
|
208
|
+
type Neo4jGraphContextNode = Readonly<{
|
|
209
|
+
key: string;
|
|
210
|
+
type: string;
|
|
211
|
+
identity: Neo4jNodeIdentity;
|
|
212
|
+
properties: Neo4jProperties;
|
|
213
|
+
sourceChunkIds: readonly string[];
|
|
214
|
+
}>;
|
|
215
|
+
type Neo4jGraphContextRelationship = Readonly<{
|
|
216
|
+
key: string;
|
|
217
|
+
type: string;
|
|
218
|
+
from: string;
|
|
219
|
+
to: string;
|
|
220
|
+
properties: Neo4jProperties;
|
|
221
|
+
sourceChunkIds: readonly string[];
|
|
222
|
+
}>;
|
|
223
|
+
type Neo4jGraphContext = Readonly<{
|
|
224
|
+
seeds: readonly Neo4jGraphContextSeed[];
|
|
225
|
+
nodes: readonly Neo4jGraphContextNode[];
|
|
226
|
+
relationships: readonly Neo4jGraphContextRelationship[];
|
|
227
|
+
evidence: readonly Neo4jGraphEvidence[];
|
|
228
|
+
}>;
|
|
229
|
+
type Neo4jGraphEvidenceCapability = "none" | "chunks";
|
|
230
|
+
type AnyNeo4jKnowledgeGraph<Schema extends Neo4jGraphSchema> = Neo4jKnowledgeGraph<Schema> | ManagedNeo4jKnowledgeGraph<Schema>;
|
|
231
|
+
type RetrieveGraphContextBase<Schema extends Neo4jGraphSchema> = Readonly<{
|
|
232
|
+
model: _anvia_core_embeddings.EmbeddingModel;
|
|
233
|
+
query: string;
|
|
234
|
+
search: Neo4jGraphSearchOptions;
|
|
235
|
+
traversal: Neo4jGraphTraversalOptions<Schema>;
|
|
236
|
+
retries?: RetrySetting | undefined;
|
|
237
|
+
abortSignal?: AbortSignal | undefined;
|
|
238
|
+
}>;
|
|
239
|
+
type RetrieveGraphContextOptions<Schema extends Neo4jGraphSchema> = (RetrieveGraphContextBase<Schema> & Readonly<{
|
|
240
|
+
graph: AnyNeo4jKnowledgeGraph<Schema>;
|
|
241
|
+
evidence: Readonly<{
|
|
242
|
+
type: "none";
|
|
243
|
+
}>;
|
|
244
|
+
}>) | (RetrieveGraphContextBase<Schema> & Readonly<{
|
|
245
|
+
graph: ManagedNeo4jKnowledgeGraph<Schema>;
|
|
246
|
+
evidence: Readonly<{
|
|
247
|
+
type: "chunks";
|
|
248
|
+
maxChunks: number;
|
|
249
|
+
}>;
|
|
250
|
+
}>);
|
|
251
|
+
type CreateNeo4jGraphSearchToolBase<Schema extends Neo4jGraphSchema> = Readonly<{
|
|
252
|
+
name: string;
|
|
253
|
+
description: string;
|
|
254
|
+
model: _anvia_core_embeddings.EmbeddingModel;
|
|
255
|
+
search: Neo4jGraphSearchOptions;
|
|
256
|
+
traversal: Neo4jGraphTraversalOptions<Schema>;
|
|
257
|
+
retries?: RetrySetting | undefined;
|
|
258
|
+
}>;
|
|
259
|
+
type CreateNeo4jGraphSearchToolOptions<Schema extends Neo4jGraphSchema> = (CreateNeo4jGraphSearchToolBase<Schema> & Readonly<{
|
|
260
|
+
graph: AnyNeo4jKnowledgeGraph<Schema>;
|
|
261
|
+
evidence: Readonly<{
|
|
262
|
+
type: "none";
|
|
263
|
+
}>;
|
|
264
|
+
}>) | (CreateNeo4jGraphSearchToolBase<Schema> & Readonly<{
|
|
265
|
+
graph: ManagedNeo4jKnowledgeGraph<Schema>;
|
|
266
|
+
evidence: Readonly<{
|
|
267
|
+
type: "chunks";
|
|
268
|
+
maxChunks: number;
|
|
269
|
+
}>;
|
|
270
|
+
}>);
|
|
271
|
+
type Neo4jGraphChangeCounts = Readonly<{
|
|
272
|
+
created: number;
|
|
273
|
+
updated: number;
|
|
274
|
+
deleted: number;
|
|
275
|
+
unchanged: number;
|
|
276
|
+
}>;
|
|
277
|
+
type Neo4jGraphWriteResult = Readonly<{
|
|
278
|
+
documents: Neo4jGraphChangeCounts;
|
|
279
|
+
chunks: Neo4jGraphChangeCounts;
|
|
280
|
+
entities: Neo4jGraphChangeCounts;
|
|
281
|
+
relationships: Neo4jGraphChangeCounts;
|
|
282
|
+
mentions: Neo4jGraphChangeCounts;
|
|
283
|
+
}>;
|
|
284
|
+
type Neo4jGraphSearchTool = Tool<{
|
|
285
|
+
query: string;
|
|
286
|
+
}, Neo4jGraphContext>;
|
|
287
|
+
|
|
288
|
+
type SeedRegistration = Readonly<{
|
|
289
|
+
labels: readonly string[];
|
|
290
|
+
vectorIndex: Neo4jVectorIndex & Readonly<{
|
|
291
|
+
property: string;
|
|
292
|
+
}>;
|
|
293
|
+
fulltextIndex?: (Neo4jFulltextIndex & Readonly<{
|
|
294
|
+
properties: readonly string[];
|
|
295
|
+
}>) | undefined;
|
|
296
|
+
entryRelationshipType?: string | undefined;
|
|
297
|
+
}>;
|
|
298
|
+
type GraphTransaction = Pick<ManagedTransaction, "run">;
|
|
299
|
+
declare abstract class Neo4jKnowledgeGraphBase<Schema extends Neo4jGraphSchema = Neo4jGraphSchema, Evidence extends Neo4jGraphEvidenceCapability = Neo4jGraphEvidenceCapability> {
|
|
300
|
+
protected readonly owner: Neo4jClient;
|
|
301
|
+
readonly schema: Schema;
|
|
302
|
+
abstract readonly seeds: Readonly<Record<string, SeedRegistration>>;
|
|
303
|
+
abstract readonly evidenceCapability: Evidence;
|
|
304
|
+
protected constructor(owner: Neo4jClient, schema: Schema);
|
|
305
|
+
validate(options?: Neo4jGraphValidateOptions): Promise<void>;
|
|
306
|
+
query(text: string, parameters: Record<string, unknown>, abortSignal?: AbortSignal, access?: "read" | "write"): Promise<QueryResult>;
|
|
307
|
+
seed(name: string): SeedRegistration;
|
|
308
|
+
protected transaction<T>(abortSignal: AbortSignal | undefined, run: (transaction: GraphTransaction) => Promise<T>): Promise<T>;
|
|
309
|
+
private runTransaction;
|
|
310
|
+
private assertSupportedVersion;
|
|
311
|
+
}
|
|
312
|
+
declare class Neo4jKnowledgeGraph<Schema extends Neo4jGraphSchema> extends Neo4jKnowledgeGraphBase<Schema, "none"> {
|
|
313
|
+
readonly evidenceCapability: "none";
|
|
314
|
+
readonly seeds: Readonly<Record<string, SeedRegistration>>;
|
|
315
|
+
constructor(owner: Neo4jClient, options: Neo4jKnowledgeGraphOptions<Schema>);
|
|
316
|
+
}
|
|
317
|
+
declare class ManagedNeo4jKnowledgeGraph<Schema extends Neo4jGraphSchema> extends Neo4jKnowledgeGraphBase<Schema, "chunks"> {
|
|
318
|
+
readonly evidenceCapability: "chunks";
|
|
319
|
+
readonly seeds: Readonly<Record<string, SeedRegistration>>;
|
|
320
|
+
readonly resources: ManagedNeo4jKnowledgeGraphOptions<Schema>["resources"];
|
|
321
|
+
private readonly name;
|
|
322
|
+
constructor(owner: Neo4jClient, options: ManagedNeo4jKnowledgeGraphOptions<Schema>);
|
|
323
|
+
ensure(options: Neo4jGraphEnsureOptions): Promise<void>;
|
|
324
|
+
validate(options?: Neo4jGraphValidateOptions): Promise<void>;
|
|
325
|
+
replaceDocuments(options: ReplaceNeo4jDocumentsOptions<Schema>): Promise<Neo4jGraphWriteResult>;
|
|
326
|
+
deleteDocuments(options: DeleteNeo4jDocumentsOptions): Promise<Neo4jGraphWriteResult>;
|
|
327
|
+
private expectedConstraints;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
declare class Neo4jClient {
|
|
331
|
+
private readonly driver;
|
|
332
|
+
private readonly ownsDriver;
|
|
333
|
+
private closed;
|
|
334
|
+
readonly database: string | undefined;
|
|
335
|
+
constructor(options: Neo4jClientOptions);
|
|
336
|
+
managedKnowledgeGraph<Schema extends Neo4jGraphSchema>(options: ManagedNeo4jKnowledgeGraphOptions<Schema>): ManagedNeo4jKnowledgeGraph<Schema>;
|
|
337
|
+
knowledgeGraph<Schema extends Neo4jGraphSchema>(options: Neo4jKnowledgeGraphOptions<Schema>): Neo4jKnowledgeGraph<Schema>;
|
|
338
|
+
nativeDriver(): Driver;
|
|
339
|
+
close(): Promise<void>;
|
|
340
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
341
|
+
private assertOpen;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
declare function extractGraphFacts<Schema extends Neo4jGraphSchema, Model extends _anvia_core.CompletionModel>(options: ExtractGraphFactsOptions<Schema, Model>): Promise<ExtractGraphFactsResult<Schema>>;
|
|
345
|
+
declare class GraphFactConflictError extends Error {
|
|
346
|
+
readonly factKey: string;
|
|
347
|
+
readonly sourceChunkIds: readonly string[];
|
|
348
|
+
constructor(factKey: string, sourceChunkIds: readonly string[]);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
declare function retrieveGraphContext<Schema extends Neo4jGraphSchema>(options: RetrieveGraphContextOptions<Schema>): Promise<Neo4jGraphContext>;
|
|
352
|
+
|
|
353
|
+
declare function defineNeo4jGraphSchema<const Options extends Neo4jGraphSchemaOptions>(options: Options): Neo4jGraphSchema<Options>;
|
|
354
|
+
|
|
355
|
+
declare function createNeo4jGraphSearchTool<Schema extends Neo4jGraphSchema>(options: CreateNeo4jGraphSearchToolOptions<Schema>): Neo4jGraphSearchTool;
|
|
356
|
+
|
|
357
|
+
export { type CreateNeo4jGraphSearchToolOptions, type DeleteNeo4jDocumentsOptions, type ExistingNeo4jSeed, type ExtractGraphFactsOptions, type ExtractGraphFactsResult, GraphFactConflictError, ManagedNeo4jKnowledgeGraph, type ManagedNeo4jKnowledgeGraphOptions, Neo4jClient, type Neo4jClientOptions, type Neo4jFulltextIndex, type Neo4jGraphChangeCounts, type Neo4jGraphChunk, type Neo4jGraphConflict, type Neo4jGraphContext, type Neo4jGraphContextNode, type Neo4jGraphContextRelationship, type Neo4jGraphContextSeed, type Neo4jGraphDocument, type Neo4jGraphEnsureOptions, type Neo4jGraphEntity, type Neo4jGraphEvidence, type Neo4jGraphEvidenceCapability, type Neo4jGraphEvidenceOptions, type Neo4jGraphFacts, type Neo4jGraphMention, type Neo4jGraphRelationship, type Neo4jGraphSchema, type Neo4jGraphSchemaOptions, type Neo4jGraphSearchOptions, type Neo4jGraphSearchTool, type Neo4jGraphTraversalOptions, type Neo4jGraphValidateOptions, type Neo4jGraphWriteResult, type Neo4jHybridSearchOptions, Neo4jKnowledgeGraph, type Neo4jKnowledgeGraphOptions, type Neo4jNodeDefinition, type Neo4jNodeIdentity, type Neo4jOrphanEntityPolicy, type Neo4jProperties, type Neo4jPropertyPrimitive, type Neo4jPropertyValue, type Neo4jRelationshipDefinition, type Neo4jVectorIndex, type Neo4jVectorSearchOptions, type ReplaceNeo4jDocumentsOptions, type RetrieveGraphContextOptions, createNeo4jGraphSearchTool, defineNeo4jGraphSchema, extractGraphFacts, retrieveGraphContext };
|