-
Notifications
You must be signed in to change notification settings - Fork 18
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
f736353
commit 505bfe3
Showing
7 changed files
with
511 additions
and
4 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
/src/tests/playground | ||
/src/tests/playground | ||
evalite-report.jsonl |
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
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,45 @@ | ||
import { | ||
experimental_wrapLanguageModel, | ||
type LanguageModelV1, | ||
type LanguageModelV1CallOptions, | ||
} from "ai"; | ||
import { createHash } from "node:crypto"; | ||
import { type Storage } from "unstorage"; | ||
|
||
const createKey = (params: LanguageModelV1CallOptions) => { | ||
return createHash("sha256").update(JSON.stringify(params)).digest("hex"); | ||
}; | ||
|
||
const createResultFromCachedObject = ( | ||
obj: any | ||
): Awaited<ReturnType<LanguageModelV1["doGenerate"]>> => { | ||
if (obj?.response?.timestamp) { | ||
obj.response.timestamp = new Date(obj.response.timestamp); | ||
} | ||
return obj as any; | ||
}; | ||
|
||
export const cacheLanguageModel = ( | ||
model: LanguageModelV1, | ||
storage: Storage | ||
) => { | ||
return experimental_wrapLanguageModel({ | ||
model, | ||
middleware: { | ||
wrapGenerate: async (opts) => { | ||
const key = createKey(opts.params); | ||
|
||
const resultFromCache = await storage.get(key); | ||
|
||
if (resultFromCache && typeof resultFromCache === "object") { | ||
return createResultFromCachedObject(resultFromCache); | ||
} | ||
const generated = await opts.doGenerate(); | ||
|
||
await storage.set(key, JSON.stringify(generated)); | ||
|
||
return generated; | ||
}, | ||
}, | ||
}); | ||
}; |
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,7 @@ | ||
import { defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({ | ||
test: { | ||
setupFiles: ["dotenv/config"], | ||
}, | ||
}); |
Oops, something went wrong.