wip converting queryLLM in flask_app to TS

This commit is contained in:
Ian Arawjo 2023-06-25 22:52:44 -04:00
parent d61bd922ca
commit 476e59365f
3 changed files with 1141 additions and 36 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,37 @@
import { Dict } from "./typing";
/**
* Singleton JSON cache that functions like a local filesystem in a Python backend,
* but where 'storageKeys' are used in place of filepaths.
*/
export default class StorageCache {
private static instance: StorageCache;
private data: Dict;
private constructor() {
// Initialize the singleton instance
this.data = {};
}
/** Gets the storage cache. Initializes it if the singleton instance does not yet exist. */
public static getInstance(): StorageCache {
if (!StorageCache.instance) {
StorageCache.instance = new StorageCache();
}
return StorageCache.instance;
}
private getCacheData(key: string): Dict {
return this.data[key] || {};
}
public static get(key: string): Dict {
return StorageCache.getInstance().getCacheData(key);
}
private storeCacheData(key: string, _data: any): void {
this.data[key] = _data;
}
public static store(key: string, data: any): void {
StorageCache.getInstance().storeCacheData(key, data);
}
}

View File

@ -9,6 +9,7 @@ import { PromptTemplate, PromptPermutationGenerator } from "./template";
import { LLM, RATE_LIMITS } from './models';
import { Dict, LLMResponseError, LLMResponseObject, LLMAPICall } from "./typing";
import { call_chatgpt, call_anthropic, call_google_palm, call_azure_openai, call_dalai, getEnumName, extract_responses, merge_response_objs } from "./utils";
import StorageCache from "./cache";
interface _IntermediateLLMResponseType {
prompt: PromptTemplate | string,
@ -17,42 +18,6 @@ interface _IntermediateLLMResponseType {
past_resp_obj?: LLMResponseObject | undefined,
}
/**
* Singleton JSON cache that functions like a local filesystem in a Python backend,
* but where 'storageKeys' are used in place of filepaths.
*/
class StorageCache {
private static instance: StorageCache;
private data: Dict;
private constructor() {
// Initialize the singleton instance
this.data = {};
}
/** Gets the storage cache. Initializes it if the singleton instance does not yet exist. */
public static getInstance(): StorageCache {
if (!StorageCache.instance) {
StorageCache.instance = new StorageCache();
}
return StorageCache.instance;
}
private getCacheData(key: string): Dict {
return this.data[key] || {};
}
public static get(key: string): Dict {
return StorageCache.getInstance().getCacheData(key);
}
private storeCacheData(key: string, _data: any): void {
this.data[key] = _data;
}
public static store(key: string, data: any): void {
StorageCache.getInstance().storeCacheData(key, data);
}
}
// From trincot @ SO: https://stackoverflow.com/a/76477994/1911342
// Functions equivalently to Python's asyncio 'as_completed' method,
// performing a Promise.race() but where all promises are yielded as they complete