Skip to content

Commit

Permalink
Added a cacheLanguageModel function
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpocock committed Dec 2, 2024
1 parent f736353 commit 505bfe3
Show file tree
Hide file tree
Showing 7 changed files with 511 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"dev": "turbo watch build lint dev",
"ci": "turbo build test lint",
"build": "turbo build",
"release": "pnpm run ci && changeset publish"
"release": "pnpm run ci && changeset publish",
"test-example": "cd packages/example && evalite"
},
"keywords": [],
"author": "Matt Pocock",
Expand Down
3 changes: 2 additions & 1 deletion packages/evalite/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/src/tests/playground
/src/tests/playground
evalite-report.jsonl
1 change: 1 addition & 0 deletions packages/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"./reporter": "./dist/reporter.js"
},
"dependencies": {
"unstorage": "^1.13.1",
"evalite": "workspace:*",
"ai": "^3.4.33",
"@ai-sdk/openai": "0.0.72",
Expand Down
17 changes: 15 additions & 2 deletions packages/example/src/basics.eval.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { openai } from "@ai-sdk/openai";
import { generateText } from "ai";
import { evalite, Levenshtein } from "evalite";
import { createStorage } from "unstorage";
import fsDriver from "unstorage/drivers/fs";
import { cacheLanguageModel } from "./cacheLanguageModel.js";

const storage = createStorage({
driver: (fsDriver as any)({
base: "./llm-cache.local",
}),
});

evalite("Test basics", {
data: async () => [
{
input: `What's the capital of France?`,
expected: `Lille.`,
expected: `Paris.`,
},
{
input: `What's the capital of Germany?`,
Expand All @@ -20,10 +29,14 @@ evalite("Test basics", {
input: `What's the capital of the United States?`,
expected: `Washington, D.C.`,
},
{
input: `What's the capital of Canada?`,
expected: `Ottawa.`,
},
],
task: async (input) => {
const result = await generateText({
model: openai("gpt-3.5-turbo"),
model: cacheLanguageModel(openai("gpt-3.5-turbo"), storage),
system: `
Answer the question concisely, in as few words as possible.
`,
Expand Down
45 changes: 45 additions & 0 deletions packages/example/src/cacheLanguageModel.ts
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;
},
},
});
};
7 changes: 7 additions & 0 deletions packages/example/vite.config.ts
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"],
},
});
Loading

0 comments on commit 505bfe3

Please sign in to comment.