@neural-tools/semantic-cache 0.1.4 → 0.1.5

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,64 @@
1
+ interface SemanticCacheConfig {
2
+ /**
3
+ * Similarity threshold (0-1). Higher = more strict matching.
4
+ * Default: 0.95
5
+ */
6
+ similarityThreshold?: number;
7
+ /**
8
+ * Time to live in seconds. 0 = never expire.
9
+ * Default: 3600 (1 hour)
10
+ */
11
+ ttl?: number;
12
+ /**
13
+ * Vector store provider
14
+ * Default: 'local'
15
+ */
16
+ provider?: 'local' | 'pinecone' | 'qdrant' | 'chromadb';
17
+ /**
18
+ * Vector database configuration
19
+ */
20
+ vectorDBConfig?: any;
21
+ }
22
+ interface CacheEntry {
23
+ prompt: string;
24
+ response: string;
25
+ metadata?: Record<string, any>;
26
+ timestamp: number;
27
+ ttl?: number;
28
+ }
29
+ declare class SemanticCache {
30
+ private vectorStore;
31
+ private config;
32
+ private initialized;
33
+ constructor(config?: SemanticCacheConfig);
34
+ /**
35
+ * Initialize the semantic cache
36
+ */
37
+ initialize(): Promise<void>;
38
+ /**
39
+ * Get a cached response for a prompt
40
+ */
41
+ get(prompt: string): Promise<string | null>;
42
+ /**
43
+ * Set a cache entry
44
+ */
45
+ set(prompt: string, response: string, metadata?: Record<string, any>): Promise<void>;
46
+ /**
47
+ * Clear all cache entries
48
+ */
49
+ clear(): Promise<void>;
50
+ /**
51
+ * Clean up expired entries
52
+ */
53
+ cleanup(): Promise<number>;
54
+ /**
55
+ * Close the cache connection
56
+ */
57
+ close(): Promise<void>;
58
+ }
59
+ /**
60
+ * Create a semantic cache instance
61
+ */
62
+ declare function createSemanticCache(config?: SemanticCacheConfig): SemanticCache;
63
+
64
+ export { type CacheEntry, SemanticCache, type SemanticCacheConfig, createSemanticCache };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export interface SemanticCacheConfig {
1
+ interface SemanticCacheConfig {
2
2
  /**
3
3
  * Similarity threshold (0-1). Higher = more strict matching.
4
4
  * Default: 0.95
@@ -19,14 +19,14 @@ export interface SemanticCacheConfig {
19
19
  */
20
20
  vectorDBConfig?: any;
21
21
  }
22
- export interface CacheEntry {
22
+ interface CacheEntry {
23
23
  prompt: string;
24
24
  response: string;
25
25
  metadata?: Record<string, any>;
26
26
  timestamp: number;
27
27
  ttl?: number;
28
28
  }
29
- export declare class SemanticCache {
29
+ declare class SemanticCache {
30
30
  private vectorStore;
31
31
  private config;
32
32
  private initialized;
@@ -59,4 +59,6 @@ export declare class SemanticCache {
59
59
  /**
60
60
  * Create a semantic cache instance
61
61
  */
62
- export declare function createSemanticCache(config?: SemanticCacheConfig): SemanticCache;
62
+ declare function createSemanticCache(config?: SemanticCacheConfig): SemanticCache;
63
+
64
+ export { type CacheEntry, SemanticCache, type SemanticCacheConfig, createSemanticCache };
package/dist/index.js CHANGED
@@ -1,146 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SemanticCache = void 0;
4
- exports.createSemanticCache = createSemanticCache;
5
- const vector_db_1 = require("@neural-tools/vector-db");
6
- const core_1 = require("@neural-tools/core");
7
- class SemanticCache {
8
- vectorStore = null;
9
- config;
10
- initialized = false;
11
- constructor(config = {}) {
12
- this.config = {
13
- similarityThreshold: config.similarityThreshold || 0.95,
14
- ttl: config.ttl || 3600,
15
- provider: config.provider || 'local',
16
- vectorDBConfig: config.vectorDBConfig || {}
17
- };
18
- }
19
- /**
20
- * Initialize the semantic cache
21
- */
22
- async initialize() {
23
- if (this.initialized)
24
- return;
25
- // Check feature access for non-local providers
26
- if (this.config.provider !== 'local') {
27
- await (0, core_1.requireFeature)('semantic-cache', 'Semantic Cache');
28
- }
29
- this.vectorStore = await (0, vector_db_1.createVectorStore)({
30
- provider: this.config.provider,
31
- ...this.config.vectorDBConfig
32
- });
33
- await this.vectorStore.connect();
34
- this.initialized = true;
35
- }
36
- /**
37
- * Get a cached response for a prompt
38
- */
39
- async get(prompt) {
40
- if (!this.initialized) {
41
- await this.initialize();
42
- }
43
- if (!this.vectorStore) {
44
- throw new Error('Vector store not initialized');
45
- }
46
- // Create embedding for the prompt
47
- const embedding = await (0, vector_db_1.createEmbedding)(prompt);
48
- // Query for similar prompts
49
- const results = await this.vectorStore.query(embedding, {
50
- topK: 1,
51
- includeMetadata: true
52
- });
53
- if (results.length === 0) {
54
- return null;
55
- }
56
- const bestMatch = results[0];
57
- // Check similarity threshold
58
- if (bestMatch.score < this.config.similarityThreshold) {
59
- return null;
60
- }
61
- // Check if expired
62
- const entry = bestMatch.metadata;
63
- if (entry.ttl && entry.ttl > 0) {
64
- const age = Date.now() - entry.timestamp;
65
- if (age > entry.ttl * 1000) {
66
- // Entry expired, delete it
67
- await this.vectorStore.delete([bestMatch.id]);
68
- return null;
69
- }
70
- }
71
- return entry.response;
72
- }
73
- /**
74
- * Set a cache entry
75
- */
76
- async set(prompt, response, metadata) {
77
- if (!this.initialized) {
78
- await this.initialize();
79
- }
80
- if (!this.vectorStore) {
81
- throw new Error('Vector store not initialized');
82
- }
83
- // Create embedding for the prompt
84
- const embedding = await (0, vector_db_1.createEmbedding)(prompt);
85
- // Create cache entry
86
- const entry = {
87
- prompt,
88
- response,
89
- metadata,
90
- timestamp: Date.now(),
91
- ttl: this.config.ttl
92
- };
93
- // Store in vector database
94
- const id = `cache-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
95
- await this.vectorStore.upsert([
96
- {
97
- id,
98
- values: embedding,
99
- metadata: entry
100
- }
101
- ]);
102
- }
103
- /**
104
- * Clear all cache entries
105
- */
106
- async clear() {
107
- if (!this.initialized) {
108
- await this.initialize();
109
- }
110
- if (!this.vectorStore) {
111
- throw new Error('Vector store not initialized');
112
- }
113
- await this.vectorStore.deleteNamespace('default');
114
- }
115
- /**
116
- * Clean up expired entries
117
- */
118
- async cleanup() {
119
- if (!this.initialized) {
120
- await this.initialize();
121
- }
122
- if (!this.vectorStore) {
123
- throw new Error('Vector store not initialized');
124
- }
125
- // This is a simplified cleanup - in production, you'd want to
126
- // query all vectors and check their TTL
127
- // For now, we'll return 0 as a placeholder
128
- return 0;
129
- }
130
- /**
131
- * Close the cache connection
132
- */
133
- async close() {
134
- if (this.vectorStore) {
135
- await this.vectorStore.disconnect();
136
- }
137
- this.initialized = false;
138
- }
139
- }
140
- exports.SemanticCache = SemanticCache;
141
- /**
142
- * Create a semantic cache instance
143
- */
144
- function createSemanticCache(config) {
145
- return new SemanticCache(config);
146
- }
1
+ "use strict";var c=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var u=(e,t)=>{for(var r in t)c(e,r,{get:t[r],enumerable:!0})},v=(e,t,r,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of m(t))!f.call(e,i)&&i!==r&&c(e,i,{get:()=>t[i],enumerable:!(o=d(t,i))||o.enumerable});return e};var g=e=>v(c({},"__esModule",{value:!0}),e);var p={};u(p,{SemanticCache:()=>s,createSemanticCache:()=>w});module.exports=g(p);var n=require("@neural-tools/vector-db"),h=require("@neural-tools/core"),s=class{vectorStore=null;config;initialized=!1;constructor(t={}){this.config={similarityThreshold:t.similarityThreshold||.95,ttl:t.ttl||3600,provider:t.provider||"local",vectorDBConfig:t.vectorDBConfig||{}}}async initialize(){this.initialized||(this.config.provider!=="local"&&await(0,h.requireFeature)("semantic-cache","Semantic Cache"),this.vectorStore=await(0,n.createVectorStore)({provider:this.config.provider,...this.config.vectorDBConfig}),await this.vectorStore.connect(),this.initialized=!0)}async get(t){if(this.initialized||await this.initialize(),!this.vectorStore)throw new Error("Vector store not initialized");let r=await(0,n.createEmbedding)(t),o=await this.vectorStore.query(r,{topK:1,includeMetadata:!0});if(o.length===0)return null;let i=o[0];if(i.score<this.config.similarityThreshold)return null;let a=i.metadata;return a.ttl&&a.ttl>0&&Date.now()-a.timestamp>a.ttl*1e3?(await this.vectorStore.delete([i.id]),null):a.response}async set(t,r,o){if(this.initialized||await this.initialize(),!this.vectorStore)throw new Error("Vector store not initialized");let i=await(0,n.createEmbedding)(t),a={prompt:t,response:r,metadata:o,timestamp:Date.now(),ttl:this.config.ttl},l=`cache-${Date.now()}-${Math.random().toString(36).substr(2,9)}`;await this.vectorStore.upsert([{id:l,values:i,metadata:a}])}async clear(){if(this.initialized||await this.initialize(),!this.vectorStore)throw new Error("Vector store not initialized");await this.vectorStore.deleteNamespace("default")}async cleanup(){if(this.initialized||await this.initialize(),!this.vectorStore)throw new Error("Vector store not initialized");return 0}async close(){this.vectorStore&&await this.vectorStore.disconnect(),this.initialized=!1}};function w(e){return new s(e)}0&&(module.exports={SemanticCache,createSemanticCache});
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import{createVectorStore as l,createEmbedding as c}from"@neural-tools/vector-db";import{requireFeature as h}from"@neural-tools/core";var a=class{vectorStore=null;config;initialized=!1;constructor(t={}){this.config={similarityThreshold:t.similarityThreshold||.95,ttl:t.ttl||3600,provider:t.provider||"local",vectorDBConfig:t.vectorDBConfig||{}}}async initialize(){this.initialized||(this.config.provider!=="local"&&await h("semantic-cache","Semantic Cache"),this.vectorStore=await l({provider:this.config.provider,...this.config.vectorDBConfig}),await this.vectorStore.connect(),this.initialized=!0)}async get(t){if(this.initialized||await this.initialize(),!this.vectorStore)throw new Error("Vector store not initialized");let o=await c(t),r=await this.vectorStore.query(o,{topK:1,includeMetadata:!0});if(r.length===0)return null;let e=r[0];if(e.score<this.config.similarityThreshold)return null;let i=e.metadata;return i.ttl&&i.ttl>0&&Date.now()-i.timestamp>i.ttl*1e3?(await this.vectorStore.delete([e.id]),null):i.response}async set(t,o,r){if(this.initialized||await this.initialize(),!this.vectorStore)throw new Error("Vector store not initialized");let e=await c(t),i={prompt:t,response:o,metadata:r,timestamp:Date.now(),ttl:this.config.ttl},s=`cache-${Date.now()}-${Math.random().toString(36).substr(2,9)}`;await this.vectorStore.upsert([{id:s,values:e,metadata:i}])}async clear(){if(this.initialized||await this.initialize(),!this.vectorStore)throw new Error("Vector store not initialized");await this.vectorStore.deleteNamespace("default")}async cleanup(){if(this.initialized||await this.initialize(),!this.vectorStore)throw new Error("Vector store not initialized");return 0}async close(){this.vectorStore&&await this.vectorStore.disconnect(),this.initialized=!1}};function u(n){return new a(n)}export{a as SemanticCache,u as createSemanticCache};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neural-tools/semantic-cache",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Semantic caching for LLM responses",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -13,7 +13,7 @@
13
13
  "url": "https://github.com/MacLeanLuke/neural-tools.git",
14
14
  "directory": "packages/semantic-cache"
15
15
  },
16
- "homepage": "https://neural-tools.com",
16
+ "homepage": "https://neural-tools.com/docs/semantic-cache.html",
17
17
  "bugs": {
18
18
  "url": "https://github.com/MacLeanLuke/neural-tools/issues"
19
19
  },
@@ -27,8 +27,8 @@
27
27
  "vector-search"
28
28
  ],
29
29
  "dependencies": {
30
- "@neural-tools/core": "0.1.4",
31
- "@neural-tools/vector-db": "0.1.4"
30
+ "@neural-tools/core": "0.1.5",
31
+ "@neural-tools/vector-db": "0.1.5"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/node": "^20.11.5",
@@ -38,8 +38,8 @@
38
38
  "dist"
39
39
  ],
40
40
  "scripts": {
41
- "build": "tsc",
42
- "dev": "tsc --watch",
41
+ "build": "tsup",
42
+ "dev": "tsup --watch",
43
43
  "clean": "rm -rf dist",
44
44
  "test": "echo 'Tests coming soon'"
45
45
  }