Skip to content

Commit

Permalink
Fix: Upstash Redis lookup parse value bug (langchain-ai#2765)
Browse files Browse the repository at this point in the history
* fix: bug in upstash redis lookup

* nit

* fix: /* eslint-disable no-process-env */

* chore: lint

* fix: type get result as StoredGeneration

* fix: proper types on non int test

---------

Co-authored-by: jacoblee93 <[email protected]>
  • Loading branch information
bracesproul and jacoblee93 authored Oct 2, 2023
1 parent 89a9aaf commit 23b3199
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
38 changes: 38 additions & 0 deletions langchain/src/cache/tests/upstash_redis.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* eslint-disable no-process-env */
import { ChatOpenAI } from "../../chat_models/openai.js";
import { UpstashRedisCache } from "../upstash_redis.js";

/**
* This test is a result of the `lookup` method trying to parse an
* incorrectly typed value Before it was being typed as a string,
* whereas in reality it was a JSON object.
*/
test.skip("UpstashRedisCache does not parse non string cached values", async () => {
if (
!process.env.UPSTASH_REDIS_REST_URL ||
!process.env.UPSTASH_REDIS_REST_TOKEN ||
!process.env.OPENAI_API_KEY
) {
throw new Error(
"Missing Upstash Redis REST URL // REST TOKEN or OpenAI API key"
);
}
const upstashRedisCache = new UpstashRedisCache({
config: {
url: process.env.UPSTASH_REDIS_REST_URL,
token: process.env.UPSTASH_REDIS_REST_TOKEN,
},
});

const chat = new ChatOpenAI({
temperature: 0,
cache: upstashRedisCache,
maxTokens: 10,
});

const prompt = "is the sky blue";
const result1 = await chat.predict(prompt);
const result2 = await chat.predict(prompt);

expect(result1).toEqual(result2);
});
5 changes: 3 additions & 2 deletions langchain/src/cache/tests/upstash_redis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { test, expect, jest } from "@jest/globals";
import hash from "object-hash";

import { UpstashRedisCache } from "../upstash_redis.js";
import { StoredGeneration } from "../../schema/index.js";

const sha256 = (str: string) => hash(str);

test("UpstashRedisCache", async () => {
const redis = {
get: jest.fn(async (key: string) => {
get: jest.fn(async (key: string): Promise<StoredGeneration | null> => {
if (key === sha256("foo_bar_0")) {
return JSON.stringify({ text: "baz" });
return { text: "baz" };
}
return null;
}),
Expand Down
8 changes: 4 additions & 4 deletions langchain/src/cache/upstash_redis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Redis, type RedisConfigNodejs } from "@upstash/redis";

import { BaseCache, Generation } from "../schema/index.js";
import { BaseCache, Generation, StoredGeneration } from "../schema/index.js";
import {
deserializeStoredGeneration,
getCacheKey,
Expand Down Expand Up @@ -46,14 +46,14 @@ export class UpstashRedisCache extends BaseCache {
public async lookup(prompt: string, llmKey: string) {
let idx = 0;
let key = getCacheKey(prompt, llmKey, String(idx));
let value: string | null = await this.redisClient.get(key);
let value = await this.redisClient.get<StoredGeneration | null>(key);
const generations: Generation[] = [];

while (value) {
generations.push(deserializeStoredGeneration(JSON.parse(value)));
generations.push(deserializeStoredGeneration(value));
idx += 1;
key = getCacheKey(prompt, llmKey, String(idx));
value = await this.redisClient.get(key);
value = await this.redisClient.get<StoredGeneration | null>(key);
}

return generations.length > 0 ? generations : null;
Expand Down

0 comments on commit 23b3199

Please sign in to comment.