@eucoder/rag 0.2.0 → 0.3.1

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,114 @@
1
+ export interface QdrantFilter {
2
+ [key: string]: unknown;
3
+ }
4
+
5
+ export interface QdrantSearchResult {
6
+ id: string;
7
+ score: number;
8
+ payload?: Record<string, unknown>;
9
+ }
10
+
11
+ export class QdrantVectorStore {
12
+ private baseUrl: string = "";
13
+ private apiKey?: string;
14
+
15
+ connect(url: string, apiKey?: string): void {
16
+ this.baseUrl = url.replace(/\/$/, "");
17
+ this.apiKey = apiKey;
18
+ }
19
+
20
+ private headers(): Record<string, string> {
21
+ const h: Record<string, string> = { "Content-Type": "application/json" };
22
+ if (this.apiKey) h["api-key"] = this.apiKey;
23
+ return h;
24
+ }
25
+
26
+ private async request(path: string, init?: RequestInit): Promise<unknown> {
27
+ const res = await fetch(`${this.baseUrl}${path}`, {
28
+ ...init,
29
+ headers: { ...this.headers(), ...((init?.headers as Record<string, string>) || {}) },
30
+ });
31
+ if (!res.ok) {
32
+ const text = await res.text().catch(() => "unknown error");
33
+ throw new Error(`Qdrant error ${res.status}: ${text}`);
34
+ }
35
+ if (res.status === 204) return undefined;
36
+ return res.json();
37
+ }
38
+
39
+ async addVectors(
40
+ ids: string[],
41
+ vectors: number[][],
42
+ payloads: Record<string, unknown>[]
43
+ ): Promise<void> {
44
+ const dims = vectors[0]?.length ?? 0;
45
+ if (!dims || ids.length !== vectors.length || vectors.length !== payloads.length) {
46
+ throw new Error("Invalid addVectors arguments: mismatched lengths or empty vectors");
47
+ }
48
+
49
+ const collection = "eucode_chunks";
50
+
51
+ try {
52
+ await this.request(`/collections/${collection}`);
53
+ } catch (e: any) {
54
+ if (e.message?.includes("404") || e.message?.includes("Not found")) {
55
+ await this.request(`/collections/${collection}`, {
56
+ method: "PUT",
57
+ body: JSON.stringify({
58
+ vectors: { size: dims, distance: "Cosine" },
59
+ }),
60
+ });
61
+ } else {
62
+ throw e;
63
+ }
64
+ }
65
+
66
+ await this.request(`/collections/${collection}/points?wait=true`, {
67
+ method: "PUT",
68
+ body: JSON.stringify({
69
+ points: ids.map((id, i) => ({
70
+ id,
71
+ vector: vectors[i],
72
+ payload: payloads[i],
73
+ })),
74
+ }),
75
+ });
76
+ }
77
+
78
+ async search(
79
+ vector: number[],
80
+ topK: number,
81
+ filter?: QdrantFilter
82
+ ): Promise<QdrantSearchResult[]> {
83
+ const collection = "eucode_chunks";
84
+ const body: Record<string, unknown> = {
85
+ vector,
86
+ limit: topK,
87
+ with_payload: true,
88
+ };
89
+ if (filter) {
90
+ body.filter = filter;
91
+ }
92
+
93
+ const result = (await this.request(`/collections/${collection}/points/search`, {
94
+ method: "POST",
95
+ body: JSON.stringify(body),
96
+ })) as {
97
+ result?: Array<{
98
+ id: string | number;
99
+ score: number;
100
+ payload?: Record<string, unknown>;
101
+ }>;
102
+ };
103
+
104
+ return (result.result ?? []).map((r) => ({
105
+ id: String(r.id),
106
+ score: r.score,
107
+ payload: r.payload,
108
+ }));
109
+ }
110
+
111
+ async deleteCollection(name: string): Promise<void> {
112
+ await this.request(`/collections/${name}`, { method: "DELETE" });
113
+ }
114
+ }
package/src/types.ts CHANGED
@@ -88,6 +88,20 @@ export interface KnowledgeGraph {
88
88
  getConnectedEntities(entityId: string, depth?: number): Entity[];
89
89
  }
90
90
 
91
+ export interface AsyncKnowledgeGraph {
92
+ addEntity(entity: Entity): Promise<void>;
93
+ addRelation(relation: Relation): Promise<void>;
94
+ getEntity(id: string): Promise<Entity | undefined>;
95
+ getRelations(entityId: string): Promise<Relation[]>;
96
+ getConnectedEntities(entityId: string, depth?: number): Promise<Entity[]>;
97
+ searchEntities(query: { name?: string; type?: string }): Promise<Entity[]>;
98
+ deleteEntity(entityId: string): Promise<void>;
99
+ deleteRelation(relationId: string): Promise<void>;
100
+ getStats(): Promise<{ entityCount: number; relationCount: number }>;
101
+ clear(): Promise<void>;
102
+ close(): Promise<void>;
103
+ }
104
+
91
105
  export interface EntityExtractor {
92
106
  extract(text: string): Promise<Entity[]>;
93
107
  }