-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3dab603
commit f1902e7
Showing
5 changed files
with
200 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Prompt } from '../prompt-engineering/prompt'; | ||
import { sharedCache } from './sharedcache'; | ||
|
||
export class PromptCacheManager { | ||
public static getPromptCacheKey({ | ||
id, | ||
name, | ||
version | ||
}: { | ||
id?: string; | ||
name?: string; | ||
version?: number; | ||
}): string { | ||
if (id) { | ||
return id; | ||
} else if (name && typeof version === 'number') { | ||
return `${name}:${version}`; | ||
} else if (name) { | ||
return name; | ||
} | ||
throw new Error('Either id or name must be provided'); | ||
} | ||
|
||
public static putPrompt(prompt: Prompt): void { | ||
sharedCache.put(prompt.id, prompt); | ||
sharedCache.put(prompt.name, prompt); | ||
sharedCache.put(`${prompt.name}:${prompt.version}`, prompt); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const cache: Map<string, any> = new Map(); | ||
|
||
class SharedCache { | ||
private static instance: SharedCache; | ||
|
||
public constructor() { | ||
if (SharedCache.instance) { | ||
throw new Error('SharedCache can only be created once'); | ||
} | ||
SharedCache.instance = this; | ||
} | ||
|
||
public getInstance(): SharedCache { | ||
return this; | ||
} | ||
|
||
public getCache(): Map<string, any> { | ||
return cache; | ||
} | ||
|
||
public get(key: string): any { | ||
return cache.get(key); | ||
} | ||
|
||
public put(key: string, value: any): void { | ||
cache.set(key, value); | ||
} | ||
|
||
public clear(): void { | ||
cache.clear(); | ||
} | ||
} | ||
|
||
export const sharedCache = new SharedCache(); | ||
|
||
export default sharedCache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { API } from '../src/api'; | ||
import { PromptCacheManager } from '../src/cache/prompt-cache-manager'; | ||
import { sharedCache } from '../src/cache/sharedcache'; | ||
import { Prompt, PromptConstructor } from '../src/prompt-engineering/prompt'; | ||
|
||
describe('Cache', () => { | ||
let api: API; | ||
let mockPrompt: Prompt; | ||
|
||
beforeAll(() => { | ||
api = {} as API; | ||
}); | ||
|
||
beforeEach(() => { | ||
sharedCache.clear(); | ||
|
||
const mockPromptData: PromptConstructor = { | ||
id: 'test-id', | ||
type: 'CHAT', | ||
createdAt: '2023-01-01T00:00:00Z', | ||
name: 'test-name', | ||
version: 1, | ||
metadata: {}, | ||
items: [], | ||
templateMessages: [{ role: 'user', content: 'Hello', uuid: '123' }], | ||
provider: 'test-provider', | ||
settings: { | ||
provider: 'test-provider', | ||
model: 'test-model', | ||
frequency_penalty: 0, | ||
max_tokens: 100, | ||
presence_penalty: 0, | ||
temperature: 0.7, | ||
top_p: 1 | ||
}, | ||
variables: [] | ||
}; | ||
mockPrompt = new Prompt(api, mockPromptData); | ||
}); | ||
|
||
describe('PromptCacheManager', () => { | ||
describe('getPromptCacheKey', () => { | ||
it('should return id when provided', () => { | ||
const key = PromptCacheManager.getPromptCacheKey({ | ||
id: 'test-id', | ||
name: 'test-name', | ||
version: 1 | ||
}); | ||
expect(key).toBe('test-id'); | ||
}); | ||
|
||
it('should return name:version when id not provided but name and version are', () => { | ||
const key = PromptCacheManager.getPromptCacheKey({ | ||
name: 'test-name', | ||
version: 1 | ||
}); | ||
expect(key).toBe('test-name:1'); | ||
}); | ||
|
||
it('should return name when only name provided', () => { | ||
const key = PromptCacheManager.getPromptCacheKey({ name: 'test-name' }); | ||
expect(key).toBe('test-name'); | ||
}); | ||
|
||
it('should throw error when neither id nor name provided', () => { | ||
expect(() => | ||
PromptCacheManager.getPromptCacheKey({ version: 0 }) | ||
).toThrow('Either id or name must be provided'); | ||
}); | ||
}); | ||
|
||
describe('putPrompt', () => { | ||
it('should store prompt with multiple keys', () => { | ||
PromptCacheManager.putPrompt(mockPrompt); | ||
|
||
expect(sharedCache.get('test-id')).toEqual(mockPrompt); | ||
expect(sharedCache.get('test-name')).toEqual(mockPrompt); | ||
expect(sharedCache.get('test-name:1')).toEqual(mockPrompt); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('SharedCache', () => { | ||
it('should return undefined for non-existent key', () => { | ||
const value = sharedCache.get('non-existent'); | ||
expect(value).toBeUndefined(); | ||
}); | ||
|
||
it('should store and retrieve values', () => { | ||
sharedCache.put('test-key', 'test-value'); | ||
expect(sharedCache.get('test-key')).toBe('test-value'); | ||
}); | ||
|
||
it('should clear all values', () => { | ||
sharedCache.put('key1', 'value1'); | ||
sharedCache.put('key2', 'value2'); | ||
|
||
sharedCache.clear(); | ||
|
||
expect(sharedCache.get('key1')).toBeUndefined(); | ||
expect(sharedCache.get('key2')).toBeUndefined(); | ||
}); | ||
|
||
it('should maintain singleton behavior', () => { | ||
const instance1 = sharedCache; | ||
const instance2 = sharedCache; | ||
|
||
instance1.put('test', 'value'); | ||
expect(instance2.get('test')).toBe('value'); | ||
expect(instance1).toBe(instance2); | ||
}); | ||
|
||
it('should expose cache map', () => { | ||
sharedCache.put('test', 'value'); | ||
const cacheMap = sharedCache.getCache(); | ||
|
||
expect(cacheMap instanceof Map).toBe(true); | ||
expect(cacheMap.get('test')).toBe('value'); | ||
}); | ||
}); | ||
}); |