langchain icon indicating copy to clipboard operation
langchain copied to clipboard

Add caching support to BaseChatModel

Open asadovsky opened this issue 1 year ago • 5 comments

Looks like BaseLLM supports caching (via langchain.llm_cache), but BaseChatModel does not.

asadovsky avatar Mar 13 '23 20:03 asadovsky

Here is a suboptimal workaround that I came across: https://gist.github.com/BlackHC/49a37aaa6e9f3e31928596ce477897ad

Note that it only works for generate, not agenerate, and only for ChatOpenAI.

asadovsky avatar Mar 19 '23 03:03 asadovsky

Working on it - will be done today or tomorrow

UmerHA avatar May 18 '23 12:05 UmerHA

Amazing! Any news on this? @UmerHA

kaikun213 avatar May 22 '23 09:05 kaikun213

Yes: I'm working on it (https://github.com/UmerHA/langchain/tree/1644-BaseChatModel-Caching), but it got delayed as I had to fix another PR first.

UmerHA avatar May 22 '23 10:05 UmerHA

Amazing! Any news on this? @UmerHA

aaaand-its-done-r7d5cl

https://github.com/hwchase17/langchain/pull/5089 :)

UmerHA avatar May 22 '23 12:05 UmerHA

@UmerHA Does this work for agenerate? Still getting: ValueError: GPTCache only supports caching of normal LLM generations, got <class 'langchain.schema.ChatGeneration'>

gururise avatar Jun 25 '23 20:06 gururise

@gururise Hey, the lc team adapted the pr and now it doesn't, but it was a conscious decision from their pov. I assume the reasoning is that some caches only make sense with single-message inputs. GPTCache would be one of them.

UmerHA avatar Jun 27 '23 08:06 UmerHA

@gururise Hey, the lc team adapted the pr and now it doesn't, but it was a conscious decision from their pov. I assume the reasoning is that some caches only make sense with single-message inputs. GPTCache would be one of them.

Could you please elaborate a bit on that? Why won't caching make sense for a chat model?

pors avatar Jun 27 '23 09:06 pors

Thanks for the merge! I have been looking into RedisCache and noticed the cache throwing KeyError 'message' when cache value would be available in the cache.

The reason seems to be, that it is not intended for use with ChatGenerations. There are ValueErrors in the code making statements along those lines. The limitation is fine, I will subclass and override. However I wanted to come back to the PR as the intended ValueError is never thrown. ChatGeneration is a subclass of Generation and therefor not hitting:

            if not isinstance(gen, Generation):
                raise ValueError(
                    "RedisCache only supports caching of normal LLM generations, "
                    f"got {type(gen)}"
                )

jakobsa avatar Jul 04 '23 09:07 jakobsa