Add Redis cache implementation (#397)

I'm using a hash function for the key just to make sure its length
doesn't get out of hand, otherwise the implementation is quite similar.
This commit is contained in:
Samantha Whitmore
2022-12-22 11:31:27 -06:00
committed by GitHub
parent ff03242fa0
commit 6bc8ae63ef
4 changed files with 317 additions and 190 deletions

View File

@@ -60,8 +60,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 30 ms, sys: 10.8 ms, total: 40.8 ms\n",
"Wall time: 983 ms\n"
"CPU times: user 30.6 ms, sys: 9.95 ms, total: 40.5 ms\n",
"Wall time: 730 ms\n"
]
},
{
@@ -91,8 +91,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 65 µs, sys: 1 µs, total: 66 µs\n",
"Wall time: 70.1 µs\n"
"CPU times: user 71 µs, sys: 3 µs, total: 74 µs\n",
"Wall time: 78.9 µs\n"
]
},
{
@@ -142,8 +142,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 6.76 ms, sys: 2.6 ms, total: 9.36 ms\n",
"Wall time: 7.86 ms\n"
"CPU times: user 5.27 ms, sys: 2.36 ms, total: 7.63 ms\n",
"Wall time: 6.68 ms\n"
]
},
{
@@ -167,14 +167,16 @@
"cell_type": "code",
"execution_count": 8,
"id": "5bf2f6fd",
"metadata": {},
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2.52 ms, sys: 1.47 ms, total: 3.99 ms\n",
"Wall time: 2.98 ms\n"
"CPU times: user 3.05 ms, sys: 1.1 ms, total: 4.16 ms\n",
"Wall time: 5.58 ms\n"
]
},
{
@@ -194,6 +196,90 @@
"llm(\"Tell me a joke\")"
]
},
{
"cell_type": "markdown",
"id": "278ad7ae",
"metadata": {},
"source": [
"### Redis Cache"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "39f6eb0b",
"metadata": {},
"outputs": [],
"source": [
"# We can do the same thing with a Redis cache\n",
"# (make sure your local Redis instance is running first before running this example)\n",
"from redis import Redis\n",
"from langchain.cache import RedisCache\n",
"langchain.llm_cache = RedisCache(redis_=Redis())"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "28920749",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 6.75 ms, sys: 3.14 ms, total: 9.89 ms\n",
"Wall time: 716 ms\n"
]
},
{
"data": {
"text/plain": [
"'\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side!'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"# The first time, it is not yet in cache, so it should take longer\n",
"llm(\"Tell me a joke\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "94bf9415",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 1.66 ms, sys: 1.92 ms, total: 3.57 ms\n",
"Wall time: 7.56 ms\n"
]
},
{
"data": {
"text/plain": [
"'\\n\\nWhy did the chicken cross the road?\\n\\nTo get to the other side!'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"# The second time it is, so it goes faster\n",
"llm(\"Tell me a joke\")"
]
},
{
"cell_type": "markdown",
"id": "934943dc",
@@ -459,7 +545,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.8"
"version": "3.10.4"
}
},
"nbformat": 4,