@flowrag/core 1.0.2 → 1.4.0
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 +53 -0
- package/dist/index.d.mts +46 -1
- package/dist/index.mjs +46 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -4
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# @flowrag/core
|
|
2
|
+
|
|
3
|
+
Interfaces, schema definition, and types for FlowRAG.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @flowrag/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Schema Definition
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { defineSchema } from '@flowrag/core';
|
|
17
|
+
|
|
18
|
+
const schema = defineSchema({
|
|
19
|
+
entityTypes: ['SERVICE', 'DATABASE', 'PROTOCOL'],
|
|
20
|
+
relationTypes: ['USES', 'PRODUCES', 'CONSUMES'],
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
schema.isValidEntityType('SERVICE'); // true
|
|
24
|
+
schema.normalizeEntityType('UNKNOWN'); // 'Other'
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Custom Fields
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
const schema = defineSchema({
|
|
31
|
+
entityTypes: ['SERVICE', 'DATABASE'],
|
|
32
|
+
relationTypes: ['USES', 'PRODUCES'],
|
|
33
|
+
entityFields: {
|
|
34
|
+
status: { type: 'enum', values: ['active', 'deprecated'], default: 'active' },
|
|
35
|
+
},
|
|
36
|
+
relationFields: {
|
|
37
|
+
syncType: { type: 'enum', values: ['sync', 'async'] },
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Interfaces
|
|
43
|
+
|
|
44
|
+
- `KVStorage` — key-value storage (documents, chunks, cache)
|
|
45
|
+
- `VectorStorage` — vector embeddings for semantic search
|
|
46
|
+
- `GraphStorage` — knowledge graph (entities, relations, traversal)
|
|
47
|
+
- `Embedder` — text to vector embeddings
|
|
48
|
+
- `LLMExtractor` — entity/relation extraction via LLM
|
|
49
|
+
- `Reranker` — post-retrieval result reranking
|
|
50
|
+
|
|
51
|
+
## License
|
|
52
|
+
|
|
53
|
+
MIT
|
package/dist/index.d.mts
CHANGED
|
@@ -11,15 +11,28 @@ interface Embedder {
|
|
|
11
11
|
}
|
|
12
12
|
//#endregion
|
|
13
13
|
//#region src/schema.d.ts
|
|
14
|
+
/** Custom field definition */
|
|
15
|
+
interface FieldDefinition {
|
|
16
|
+
type: "string" | "enum";
|
|
17
|
+
values?: string[];
|
|
18
|
+
default?: string;
|
|
19
|
+
filterable?: boolean;
|
|
20
|
+
}
|
|
14
21
|
/** Schema configuration input */
|
|
15
22
|
interface SchemaConfig<E extends readonly string[] = readonly string[], R extends readonly string[] = readonly string[]> {
|
|
16
23
|
entityTypes: E;
|
|
17
24
|
relationTypes: R;
|
|
25
|
+
documentFields?: Record<string, FieldDefinition>;
|
|
26
|
+
entityFields?: Record<string, FieldDefinition>;
|
|
27
|
+
relationFields?: Record<string, FieldDefinition>;
|
|
18
28
|
}
|
|
19
29
|
/** Resolved schema with validation */
|
|
20
30
|
interface Schema<E extends readonly string[] = readonly string[], R extends readonly string[] = readonly string[]> {
|
|
21
31
|
entityTypes: E;
|
|
22
32
|
relationTypes: R;
|
|
33
|
+
documentFields: Record<string, FieldDefinition>;
|
|
34
|
+
entityFields: Record<string, FieldDefinition>;
|
|
35
|
+
relationFields: Record<string, FieldDefinition>;
|
|
23
36
|
isValidEntityType: (type: string) => boolean;
|
|
24
37
|
isValidRelationType: (type: string) => boolean;
|
|
25
38
|
normalizeEntityType: (type: string) => E[number] | "Other";
|
|
@@ -48,6 +61,7 @@ interface DocumentMetadata {
|
|
|
48
61
|
title?: string;
|
|
49
62
|
createdAt: Date;
|
|
50
63
|
updatedAt: Date;
|
|
64
|
+
fields?: Record<string, unknown>;
|
|
51
65
|
[key: string]: unknown;
|
|
52
66
|
}
|
|
53
67
|
/** Chunk - a piece of a document */
|
|
@@ -65,6 +79,7 @@ interface Entity {
|
|
|
65
79
|
type: string;
|
|
66
80
|
description: string;
|
|
67
81
|
sourceChunkIds: Id[];
|
|
82
|
+
fields?: Record<string, unknown>;
|
|
68
83
|
}
|
|
69
84
|
/** Relation - an edge in the knowledge graph */
|
|
70
85
|
interface Relation {
|
|
@@ -75,6 +90,7 @@ interface Relation {
|
|
|
75
90
|
description: string;
|
|
76
91
|
keywords: string[];
|
|
77
92
|
sourceChunkIds: Id[];
|
|
93
|
+
fields?: Record<string, unknown>;
|
|
78
94
|
}
|
|
79
95
|
/** Vector record for storage */
|
|
80
96
|
interface VectorRecord {
|
|
@@ -104,6 +120,7 @@ interface ExtractedEntity {
|
|
|
104
120
|
name: string;
|
|
105
121
|
type: string;
|
|
106
122
|
description: string;
|
|
123
|
+
fields?: Record<string, unknown>;
|
|
107
124
|
}
|
|
108
125
|
/** Extracted relation from LLM */
|
|
109
126
|
interface ExtractedRelation {
|
|
@@ -112,6 +129,7 @@ interface ExtractedRelation {
|
|
|
112
129
|
type: string;
|
|
113
130
|
description: string;
|
|
114
131
|
keywords: string[];
|
|
132
|
+
fields?: Record<string, unknown>;
|
|
115
133
|
}
|
|
116
134
|
/** Result of entity extraction */
|
|
117
135
|
interface ExtractionResult {
|
|
@@ -127,6 +145,27 @@ interface LLMExtractor {
|
|
|
127
145
|
extractEntities(content: string, knownEntities: string[], schema: Schema): Promise<ExtractionResult>;
|
|
128
146
|
}
|
|
129
147
|
//#endregion
|
|
148
|
+
//#region src/interfaces/reranker.d.ts
|
|
149
|
+
/**
|
|
150
|
+
* Reranker interface for FlowRAG
|
|
151
|
+
*/
|
|
152
|
+
/** A scored document to rerank */
|
|
153
|
+
interface RerankDocument {
|
|
154
|
+
id: string;
|
|
155
|
+
content: string;
|
|
156
|
+
score: number;
|
|
157
|
+
}
|
|
158
|
+
/** Result after reranking */
|
|
159
|
+
interface RerankResult {
|
|
160
|
+
id: string;
|
|
161
|
+
score: number;
|
|
162
|
+
index: number;
|
|
163
|
+
}
|
|
164
|
+
/** Reranker - re-scores search results for better relevance */
|
|
165
|
+
interface Reranker {
|
|
166
|
+
rerank(query: string, documents: RerankDocument[], limit?: number): Promise<RerankResult[]>;
|
|
167
|
+
}
|
|
168
|
+
//#endregion
|
|
130
169
|
//#region src/interfaces/storage.d.ts
|
|
131
170
|
/** KV Storage - for documents, chunks, and cache */
|
|
132
171
|
interface KVStorage {
|
|
@@ -156,5 +195,11 @@ interface GraphStorage {
|
|
|
156
195
|
deleteRelation(id: Id): Promise<void>;
|
|
157
196
|
}
|
|
158
197
|
//#endregion
|
|
159
|
-
|
|
198
|
+
//#region src/prompt.d.ts
|
|
199
|
+
/**
|
|
200
|
+
* Build the standard entity extraction prompt used by all LLM providers.
|
|
201
|
+
*/
|
|
202
|
+
declare function buildExtractionPrompt(content: string, knownEntities: string[], schema: Schema): string;
|
|
203
|
+
//#endregion
|
|
204
|
+
export { Chunk, Document, DocumentMetadata, Embedder, Entity, EntityFilter, ExtractedEntity, ExtractedRelation, ExtractionResult, FieldDefinition, GraphStorage, Id, KVStorage, LLMExtractor, QueryMode, Relation, RelationDirection, RerankDocument, RerankResult, Reranker, Schema, SchemaConfig, SearchResult, VectorFilter, VectorRecord, VectorStorage, buildExtractionPrompt, defineSchema };
|
|
160
205
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,47 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
|
|
3
|
+
//#region src/prompt.ts
|
|
4
|
+
/**
|
|
5
|
+
* Build the standard entity extraction prompt used by all LLM providers.
|
|
6
|
+
*/
|
|
7
|
+
function buildExtractionPrompt(content, knownEntities, schema) {
|
|
8
|
+
const entityTypes = schema.entityTypes.join(", ");
|
|
9
|
+
const relationTypes = schema.relationTypes.join(", ");
|
|
10
|
+
const knownEntitiesList = knownEntities.length > 0 ? `\n\nKnown entities to reference: ${knownEntities.join(", ")}` : "";
|
|
11
|
+
const entityFieldsDef = Object.keys(schema.entityFields).length > 0 ? `\n\nEntity custom fields: ${JSON.stringify(schema.entityFields)}` : "";
|
|
12
|
+
const relationFieldsDef = Object.keys(schema.relationFields).length > 0 ? `\n\nRelation custom fields: ${JSON.stringify(schema.relationFields)}` : "";
|
|
13
|
+
return `Extract entities and relations from the following content.
|
|
14
|
+
|
|
15
|
+
Entity types: ${entityTypes}
|
|
16
|
+
Relation types: ${relationTypes}${knownEntitiesList}${entityFieldsDef}${relationFieldsDef}
|
|
17
|
+
|
|
18
|
+
Content:
|
|
19
|
+
${content}
|
|
20
|
+
|
|
21
|
+
Return a JSON object with this structure:
|
|
22
|
+
{
|
|
23
|
+
"entities": [
|
|
24
|
+
{
|
|
25
|
+
"name": "entity name",
|
|
26
|
+
"type": "entity type from the list above, or 'Other' if not matching",
|
|
27
|
+
"description": "brief description of the entity"${entityFieldsDef ? ",\n \"fields\": {}" : ""}
|
|
28
|
+
}
|
|
29
|
+
],
|
|
30
|
+
"relations": [
|
|
31
|
+
{
|
|
32
|
+
"source": "source entity name",
|
|
33
|
+
"target": "target entity name",
|
|
34
|
+
"type": "relation type from the list above",
|
|
35
|
+
"description": "description of the relationship",
|
|
36
|
+
"keywords": ["keyword1", "keyword2"]${relationFieldsDef ? ",\n \"fields\": {}" : ""}
|
|
37
|
+
}
|
|
38
|
+
]
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
Focus on technical entities and their relationships. Be precise and avoid duplicates.${entityFieldsDef || relationFieldsDef ? "\nInclude a \"fields\" object in each entity/relation with the custom field values when applicable." : ""}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
3
45
|
//#region src/schema.ts
|
|
4
46
|
/**
|
|
5
47
|
* Schema definition for FlowRAG
|
|
@@ -19,6 +61,9 @@ function defineSchema(config) {
|
|
|
19
61
|
return {
|
|
20
62
|
entityTypes: config.entityTypes,
|
|
21
63
|
relationTypes: config.relationTypes,
|
|
64
|
+
documentFields: config.documentFields ?? {},
|
|
65
|
+
entityFields: config.entityFields ?? {},
|
|
66
|
+
relationFields: config.relationFields ?? {},
|
|
22
67
|
isValidEntityType: (type) => entitySet.has(type),
|
|
23
68
|
isValidRelationType: (type) => relationSet.has(type),
|
|
24
69
|
normalizeEntityType: (type) => entitySet.has(type) ? type : "Other",
|
|
@@ -27,5 +72,5 @@ function defineSchema(config) {
|
|
|
27
72
|
}
|
|
28
73
|
|
|
29
74
|
//#endregion
|
|
30
|
-
export { defineSchema };
|
|
75
|
+
export { buildExtractionPrompt, defineSchema };
|
|
31
76
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/schema.ts"],"sourcesContent":["/**\n * Schema definition for FlowRAG\n */\n\nimport { z } from 'zod';\n\n/** Schema configuration input */\nexport interface SchemaConfig<\n E extends readonly string[] = readonly string[],\n R extends readonly string[] = readonly string[],\n> {\n entityTypes: E;\n relationTypes: R;\n}\n\n/** Resolved schema with validation */\nexport interface Schema<\n E extends readonly string[] = readonly string[],\n R extends readonly string[] = readonly string[],\n> {\n entityTypes: E;\n relationTypes: R;\n isValidEntityType: (type: string) => boolean;\n isValidRelationType: (type: string) => boolean;\n normalizeEntityType: (type: string) => E[number] | 'Other';\n normalizeRelationType: (type: string) => R[number] | 'Other';\n}\n\nconst schemaConfigSchema = z.object({\n entityTypes: z.array(z.string()).min(1),\n relationTypes: z.array(z.string()).min(1),\n});\n\n/**\n * Define a schema for entity and relation types.\n * Types are suggestions - if LLM extracts a type not in the list, it falls back to 'Other'.\n */\nexport function defineSchema<const E extends readonly string[], const R extends readonly string[]>(\n config: SchemaConfig<E, R>,\n): Schema<E, R> {\n schemaConfigSchema.parse(config);\n\n const entitySet = new Set<string>(config.entityTypes);\n const relationSet = new Set<string>(config.relationTypes);\n\n return {\n entityTypes: config.entityTypes,\n relationTypes: config.relationTypes,\n isValidEntityType: (type: string) => entitySet.has(type),\n isValidRelationType: (type: string) => relationSet.has(type),\n normalizeEntityType: (type: string) => (entitySet.has(type) ? (type as E[number]) : 'Other'),\n normalizeRelationType: (type: string) =>\n relationSet.has(type) ? (type as R[number]) : 'Other',\n };\n}\n"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/prompt.ts","../src/schema.ts"],"sourcesContent":["import type { Schema } from './schema.js';\n\n/**\n * Build the standard entity extraction prompt used by all LLM providers.\n */\nexport function buildExtractionPrompt(\n content: string,\n knownEntities: string[],\n schema: Schema,\n): string {\n const entityTypes = schema.entityTypes.join(', ');\n const relationTypes = schema.relationTypes.join(', ');\n const knownEntitiesList =\n knownEntities.length > 0 ? `\\n\\nKnown entities to reference: ${knownEntities.join(', ')}` : '';\n\n const entityFieldsDef =\n Object.keys(schema.entityFields).length > 0\n ? `\\n\\nEntity custom fields: ${JSON.stringify(schema.entityFields)}`\n : '';\n const relationFieldsDef =\n Object.keys(schema.relationFields).length > 0\n ? `\\n\\nRelation custom fields: ${JSON.stringify(schema.relationFields)}`\n : '';\n\n const fieldsInstruction =\n entityFieldsDef || relationFieldsDef\n ? '\\nInclude a \"fields\" object in each entity/relation with the custom field values when applicable.'\n : '';\n\n return `Extract entities and relations from the following content.\n\nEntity types: ${entityTypes}\nRelation types: ${relationTypes}${knownEntitiesList}${entityFieldsDef}${relationFieldsDef}\n\nContent:\n${content}\n\nReturn a JSON object with this structure:\n{\n \"entities\": [\n {\n \"name\": \"entity name\",\n \"type\": \"entity type from the list above, or 'Other' if not matching\",\n \"description\": \"brief description of the entity\"${entityFieldsDef ? ',\\n \"fields\": {}' : ''}\n }\n ],\n \"relations\": [\n {\n \"source\": \"source entity name\",\n \"target\": \"target entity name\",\n \"type\": \"relation type from the list above\",\n \"description\": \"description of the relationship\",\n \"keywords\": [\"keyword1\", \"keyword2\"]${relationFieldsDef ? ',\\n \"fields\": {}' : ''}\n }\n ]\n}\n\nFocus on technical entities and their relationships. Be precise and avoid duplicates.${fieldsInstruction}`;\n}\n","/**\n * Schema definition for FlowRAG\n */\n\nimport { z } from 'zod';\n\n/** Custom field definition */\nexport interface FieldDefinition {\n type: 'string' | 'enum';\n values?: string[];\n default?: string;\n filterable?: boolean;\n}\n\n/** Schema configuration input */\nexport interface SchemaConfig<\n E extends readonly string[] = readonly string[],\n R extends readonly string[] = readonly string[],\n> {\n entityTypes: E;\n relationTypes: R;\n documentFields?: Record<string, FieldDefinition>;\n entityFields?: Record<string, FieldDefinition>;\n relationFields?: Record<string, FieldDefinition>;\n}\n\n/** Resolved schema with validation */\nexport interface Schema<\n E extends readonly string[] = readonly string[],\n R extends readonly string[] = readonly string[],\n> {\n entityTypes: E;\n relationTypes: R;\n documentFields: Record<string, FieldDefinition>;\n entityFields: Record<string, FieldDefinition>;\n relationFields: Record<string, FieldDefinition>;\n isValidEntityType: (type: string) => boolean;\n isValidRelationType: (type: string) => boolean;\n normalizeEntityType: (type: string) => E[number] | 'Other';\n normalizeRelationType: (type: string) => R[number] | 'Other';\n}\n\nconst schemaConfigSchema = z.object({\n entityTypes: z.array(z.string()).min(1),\n relationTypes: z.array(z.string()).min(1),\n});\n\n/**\n * Define a schema for entity and relation types.\n * Types are suggestions - if LLM extracts a type not in the list, it falls back to 'Other'.\n */\nexport function defineSchema<const E extends readonly string[], const R extends readonly string[]>(\n config: SchemaConfig<E, R>,\n): Schema<E, R> {\n schemaConfigSchema.parse(config);\n\n const entitySet = new Set<string>(config.entityTypes);\n const relationSet = new Set<string>(config.relationTypes);\n\n return {\n entityTypes: config.entityTypes,\n relationTypes: config.relationTypes,\n documentFields: config.documentFields ?? {},\n entityFields: config.entityFields ?? {},\n relationFields: config.relationFields ?? {},\n isValidEntityType: (type: string) => entitySet.has(type),\n isValidRelationType: (type: string) => relationSet.has(type),\n normalizeEntityType: (type: string) => (entitySet.has(type) ? (type as E[number]) : 'Other'),\n normalizeRelationType: (type: string) =>\n relationSet.has(type) ? (type as R[number]) : 'Other',\n };\n}\n"],"mappings":";;;;;;AAKA,SAAgB,sBACd,SACA,eACA,QACQ;CACR,MAAM,cAAc,OAAO,YAAY,KAAK,KAAK;CACjD,MAAM,gBAAgB,OAAO,cAAc,KAAK,KAAK;CACrD,MAAM,oBACJ,cAAc,SAAS,IAAI,oCAAoC,cAAc,KAAK,KAAK,KAAK;CAE9F,MAAM,kBACJ,OAAO,KAAK,OAAO,aAAa,CAAC,SAAS,IACtC,6BAA6B,KAAK,UAAU,OAAO,aAAa,KAChE;CACN,MAAM,oBACJ,OAAO,KAAK,OAAO,eAAe,CAAC,SAAS,IACxC,+BAA+B,KAAK,UAAU,OAAO,eAAe,KACpE;AAON,QAAO;;gBAEO,YAAY;kBACV,gBAAgB,oBAAoB,kBAAkB,kBAAkB;;;EAGxF,QAAQ;;;;;;;;wDAQ8C,kBAAkB,4BAA0B,GAAG;;;;;;;;;4CAS3D,oBAAoB,4BAA0B,GAAG;;;;;uFA3BzF,mBAAmB,oBACf,wGACA;;;;;;;;ACeR,MAAM,qBAAqB,EAAE,OAAO;CAClC,aAAa,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,IAAI,EAAE;CACvC,eAAe,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,IAAI,EAAE;CAC1C,CAAC;;;;;AAMF,SAAgB,aACd,QACc;AACd,oBAAmB,MAAM,OAAO;CAEhC,MAAM,YAAY,IAAI,IAAY,OAAO,YAAY;CACrD,MAAM,cAAc,IAAI,IAAY,OAAO,cAAc;AAEzD,QAAO;EACL,aAAa,OAAO;EACpB,eAAe,OAAO;EACtB,gBAAgB,OAAO,kBAAkB,EAAE;EAC3C,cAAc,OAAO,gBAAgB,EAAE;EACvC,gBAAgB,OAAO,kBAAkB,EAAE;EAC3C,oBAAoB,SAAiB,UAAU,IAAI,KAAK;EACxD,sBAAsB,SAAiB,YAAY,IAAI,KAAK;EAC5D,sBAAsB,SAAkB,UAAU,IAAI,KAAK,GAAI,OAAqB;EACpF,wBAAwB,SACtB,YAAY,IAAI,KAAK,GAAI,OAAqB;EACjD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flowrag/core",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "📐 The heart of FlowRAG. Defines all TypeScript interfaces (Storage, Embedder, LLMExtractor), the Zod-based schema system for defining entity/relation types, and the orchestration pipeline that coordinates indexing and querying. This package contains only contracts and types, no concrete implementations.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"interfaces",
|
|
@@ -28,14 +28,12 @@
|
|
|
28
28
|
".": "./dist/index.mjs",
|
|
29
29
|
"./package.json": "./package.json"
|
|
30
30
|
},
|
|
31
|
-
"main": "./dist/index.mjs",
|
|
32
|
-
"module": "./dist/index.mjs",
|
|
33
31
|
"types": "./dist/index.d.mts",
|
|
34
32
|
"files": [
|
|
35
33
|
"dist"
|
|
36
34
|
],
|
|
37
35
|
"dependencies": {
|
|
38
|
-
"zod": "^4.3.
|
|
36
|
+
"zod": "^4.3.6"
|
|
39
37
|
},
|
|
40
38
|
"engines": {
|
|
41
39
|
"node": ">=20"
|