@neural-tools/semantic-cache 0.1.3
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.md +80 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.js +146 -0
- package/package.json +33 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# Neural Tools License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Luke Amy. All rights reserved.
|
|
4
|
+
|
|
5
|
+
## License Agreement
|
|
6
|
+
|
|
7
|
+
This software is provided under a dual-license model:
|
|
8
|
+
|
|
9
|
+
### 1. Free Tier License (MIT)
|
|
10
|
+
|
|
11
|
+
The following components are licensed under the MIT License:
|
|
12
|
+
|
|
13
|
+
- Basic MCP generation functionality
|
|
14
|
+
- Claude command generation
|
|
15
|
+
- Core utilities and types
|
|
16
|
+
- Basic templates
|
|
17
|
+
- Documentation and examples
|
|
18
|
+
|
|
19
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of the free tier components to use, copy, modify, merge, publish, and distribute, subject to the following conditions:
|
|
20
|
+
|
|
21
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
22
|
+
|
|
23
|
+
### 2. Pro/Enterprise License (Proprietary)
|
|
24
|
+
|
|
25
|
+
The following features require a valid Pro or Enterprise license:
|
|
26
|
+
|
|
27
|
+
**Pro Features:**
|
|
28
|
+
- Vector database integration
|
|
29
|
+
- Semantic caching
|
|
30
|
+
- Fine-tuning workflows
|
|
31
|
+
- Cloud deployment templates (AWS/GCP)
|
|
32
|
+
- Premium templates and examples
|
|
33
|
+
- GitHub automation features
|
|
34
|
+
|
|
35
|
+
**Enterprise Features:**
|
|
36
|
+
- White-label support
|
|
37
|
+
- Custom integrations
|
|
38
|
+
- Priority support
|
|
39
|
+
- SLA guarantees
|
|
40
|
+
- Team collaboration features
|
|
41
|
+
|
|
42
|
+
These features are proprietary and may not be used without a valid license key purchased from neural-tools.dev.
|
|
43
|
+
|
|
44
|
+
### License Terms
|
|
45
|
+
|
|
46
|
+
1. **Free Tier**: You may use the free tier features for any purpose, including commercial use, under the MIT License terms.
|
|
47
|
+
|
|
48
|
+
2. **Pro/Enterprise**: You must purchase a license to access Pro or Enterprise features. Each license is:
|
|
49
|
+
- Per-user for individual licenses
|
|
50
|
+
- Per-organization for team/enterprise licenses
|
|
51
|
+
- Non-transferable without written consent
|
|
52
|
+
- Subject to the terms at neural-tools.dev/terms
|
|
53
|
+
|
|
54
|
+
3. **Source Code**: This repository is private. You may not:
|
|
55
|
+
- Redistribute the source code
|
|
56
|
+
- Create derivative works for redistribution
|
|
57
|
+
- Reverse engineer Pro/Enterprise features
|
|
58
|
+
- Remove or circumvent license checks
|
|
59
|
+
|
|
60
|
+
4. **Support**: Support is provided based on your license tier:
|
|
61
|
+
- Free: Community support only
|
|
62
|
+
- Pro: Email support (48-hour response)
|
|
63
|
+
- Enterprise: Priority support with SLA
|
|
64
|
+
|
|
65
|
+
### Warranty Disclaimer
|
|
66
|
+
|
|
67
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
68
|
+
|
|
69
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
70
|
+
|
|
71
|
+
### Contact
|
|
72
|
+
|
|
73
|
+
For licensing inquiries:
|
|
74
|
+
- Email: licensing@neural-tools.dev
|
|
75
|
+
- Website: https://neural-tools.dev/pricing
|
|
76
|
+
- Support: support@neural-tools.dev
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
**Last Updated:** January 2025
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export 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
|
+
export interface CacheEntry {
|
|
23
|
+
prompt: string;
|
|
24
|
+
response: string;
|
|
25
|
+
metadata?: Record<string, any>;
|
|
26
|
+
timestamp: number;
|
|
27
|
+
ttl?: number;
|
|
28
|
+
}
|
|
29
|
+
export 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
|
+
export declare function createSemanticCache(config?: SemanticCacheConfig): SemanticCache;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
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
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@neural-tools/semantic-cache",
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "Semantic caching for LLM responses",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"license": "SEE LICENSE IN ../../LICENSE.md",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/MacLeanLuke/ai-toolkit.git",
|
|
14
|
+
"directory": "packages/semantic-cache"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@neural-tools/core": "0.1.3",
|
|
18
|
+
"@neural-tools/vector-db": "0.1.3"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/node": "^20.11.5",
|
|
22
|
+
"typescript": "^5.3.3"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc",
|
|
29
|
+
"dev": "tsc --watch",
|
|
30
|
+
"clean": "rm -rf dist",
|
|
31
|
+
"test": "echo 'Tests coming soon'"
|
|
32
|
+
}
|
|
33
|
+
}
|