Skip to content

Commit

Permalink
fix: ensure KurtCache hashes the new forceSchemaConstrainedTokens
Browse files Browse the repository at this point in the history
… option

This ensures that changing the option will correctly cause a cache miss. But leaving it at the
default value of `false` won't change existing hashes because `false` is treated as "missing" in the
`mayHash` internal function.
  • Loading branch information
jemc committed Dec 10, 2024
1 parent c3a255d commit f3d86d6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
18 changes: 12 additions & 6 deletions packages/kurt-cache/spec/KurtCache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ const cacheDirRetain = `${cacheDir}-retain`
async function gen(
kurt: Kurt,
prompt: string,
schema?: KurtSchema<KurtSchemaInner>
schema?: KurtSchema<KurtSchemaInner>,
sampling?: KurtSamplingOptions
) {
const stream = schema
? kurt.generateStructuredData({ prompt, schema })
: kurt.generateNaturalLanguage({ prompt })
? kurt.generateStructuredData({ prompt, schema, sampling })
: kurt.generateNaturalLanguage({ prompt, sampling })
const result = await stream.result
return schema ? result.data : result.text
}
Expand All @@ -59,6 +60,7 @@ describe("KurtCache", () => {
return new StubAdapter([
["World ", random],
["bar ", random],
["bar2 ", random],
["never ", random],
])
})
Expand All @@ -71,13 +73,17 @@ describe("KurtCache", () => {
expect(await gen(kurt, `foo ${random}`)).toEqual(`bar ${random}`)
expect(await gen(kurt, `Hello ${random}`)).toEqual(`World ${random}`)
expect(await gen(kurt, `foo ${random}`)).toEqual(`bar ${random}`)
expect(await gen(kurt, `foo ${random}`)).toEqual(`bar ${random}`)
expect(
await gen(kurt, `foo ${random}`, undefined, {
forceSchemaConstrainedTokens: true,
})
).toEqual(`bar2 ${random}`)

// Expect that the adapter setup function was called just once.
expect(adapterFnCallCount).toEqual(1)

// Expect that the cache dir contains exactly two files.
expect(readdirSync(cacheDir)).toHaveLength(2)
// Expect that the cache dir contains exactly three files.
expect(readdirSync(cacheDir)).toHaveLength(3)
})

test("when cache hits, works without running the adapter fn", async () => {
Expand Down
10 changes: 8 additions & 2 deletions packages/kurt-cache/src/KurtCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ function hashSamplingOptions(digest: Hash, options: KurtSamplingOptions): Hash {
mayHash(digest, "maxOutputTokens", options.maxOutputTokens)
mayHash(digest, "temperature", options.temperature)
mayHash(digest, "topP", options.topP)
mayHash(
digest,
"forceSchemaConstrainedTokens",
options.forceSchemaConstrainedTokens
)
return digest
}

Expand Down Expand Up @@ -357,10 +362,11 @@ function hashSchema(digest: Hash, schema: KurtSchema<KurtSchemaInner>) {
function mayHash(
digest: Hash,
key: string,
value: string | number | undefined
value: string | number | boolean | undefined
) {
if (value === undefined) return
if (value === undefined || value === false) return
digest.update(key)
if (value === true) return
if (typeof value === "string") digest.update(value)
else digest.update(value.toString())
}

0 comments on commit f3d86d6

Please sign in to comment.