Skip to main content

缓存

LangChain为聊天模型提供了一个可选的缓存层。这对于两个原因很有用:

如果您经常多次请求相同的完成,它可以通过减少您对LLM提供商的API调用次数来节省您的费用。它可以通过减少您对LLM提供商的API调用次数来加快应用程序的速度。

import langchain
from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI()

内存缓存

from langchain.cache import InMemoryCache
langchain.llm_cache = InMemoryCache()

# 第一次调用时,它还没有在缓存中,所以需要更长的时间
llm.predict("Tell me a joke")

CPU times: user 35.9 ms, sys: 28.6 ms, total: 64.6 ms
Wall time: 4.83 s

"\n\n为什么自行车不能自己站起来?因为它太累了!"

# 第二次调用时,它已经在缓存中,所以速度更快
llm.predict("Tell me a joke")

CPU times: user 238 µs, sys: 143 µs, total: 381 µs
Wall time: 1.76 ms

'\n\n为什么鸡会过马路?\n\n为了到达另一边。'

SQLite缓存

rm .langchain.db

# 我们可以使用SQLite缓存做同样的事情
from langchain.cache import SQLiteCache
langchain.llm_cache = SQLiteCache(database_path=".langchain.db")

# 第一次调用时,它还没有在缓存中,所以需要更长的时间
llm.predict("Tell me a joke")

CPU times: user 17 ms, sys: 9.76 ms, total: 26.7 ms
Wall time: 825 ms

'\n\n为什么鸡会过马路?\n\n为了到达另一边。'

# 第二次调用时,它已经在缓存中,所以速度更快
llm.predict("Tell me a joke")

CPU times: user 2.46 ms, sys: 1.23 ms, total: 3.7 ms
Wall time: 2.67 ms

'\n\n为什么鸡会过马路?\n\n为了到达另一边。'