mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 11:14:04 +00:00
(docs) redis cache
This commit is contained in:
parent
f71f34e680
commit
4b798624dc
1 changed files with 64 additions and 4 deletions
|
@ -1,11 +1,11 @@
|
||||||
import Tabs from '@theme/Tabs';
|
import Tabs from '@theme/Tabs';
|
||||||
import TabItem from '@theme/TabItem';
|
import TabItem from '@theme/TabItem';
|
||||||
|
|
||||||
# Caching - In-Memory, Redis, s3
|
# Caching - In-Memory, Redis, s3, Redis Semantic Cache
|
||||||
|
|
||||||
[**See Code**](https://github.com/BerriAI/litellm/blob/main/litellm/caching.py)
|
[**See Code**](https://github.com/BerriAI/litellm/blob/main/litellm/caching.py)
|
||||||
|
|
||||||
## Initialize Cache - In Memory, Redis, s3 Bucket
|
## Initialize Cache - In Memory, Redis, s3 Bucket, Redis Semantic Cache
|
||||||
|
|
||||||
|
|
||||||
<Tabs>
|
<Tabs>
|
||||||
|
@ -18,7 +18,7 @@ pip install redis
|
||||||
```
|
```
|
||||||
|
|
||||||
For the hosted version you can setup your own Redis DB here: https://app.redislabs.com/
|
For the hosted version you can setup your own Redis DB here: https://app.redislabs.com/
|
||||||
### Quick Start
|
|
||||||
```python
|
```python
|
||||||
import litellm
|
import litellm
|
||||||
from litellm import completion
|
from litellm import completion
|
||||||
|
@ -55,7 +55,7 @@ Set AWS environment variables
|
||||||
AWS_ACCESS_KEY_ID = "AKI*******"
|
AWS_ACCESS_KEY_ID = "AKI*******"
|
||||||
AWS_SECRET_ACCESS_KEY = "WOl*****"
|
AWS_SECRET_ACCESS_KEY = "WOl*****"
|
||||||
```
|
```
|
||||||
### Quick Start
|
|
||||||
```python
|
```python
|
||||||
import litellm
|
import litellm
|
||||||
from litellm import completion
|
from litellm import completion
|
||||||
|
@ -80,6 +80,66 @@ response2 = completion(
|
||||||
</TabItem>
|
</TabItem>
|
||||||
|
|
||||||
|
|
||||||
|
<TabItem value="redis-sem" label="redis-semantic cache">
|
||||||
|
|
||||||
|
Install redis
|
||||||
|
```shell
|
||||||
|
pip install redisvl==0.0.7
|
||||||
|
```
|
||||||
|
|
||||||
|
For the hosted version you can setup your own Redis DB here: https://app.redislabs.com/
|
||||||
|
|
||||||
|
```python
|
||||||
|
import litellm
|
||||||
|
from litellm import completion
|
||||||
|
from litellm.caching import Cache
|
||||||
|
|
||||||
|
random_number = random.randint(
|
||||||
|
1, 100000
|
||||||
|
) # add a random number to ensure it's always adding / reading from cache
|
||||||
|
|
||||||
|
print("testing semantic caching")
|
||||||
|
litellm.cache = Cache(
|
||||||
|
type="redis-semantic",
|
||||||
|
host=os.environ["REDIS_HOST"],
|
||||||
|
port=os.environ["REDIS_PORT"],
|
||||||
|
password=os.environ["REDIS_PASSWORD"],
|
||||||
|
similarity_threshold=0.8,
|
||||||
|
redis_semantic_cache_embedding_model="text-embedding-ada-002", # this model is passed to litellm.embedding(), any litellm.embedding() model is supported here
|
||||||
|
)
|
||||||
|
response1 = completion(
|
||||||
|
model="gpt-3.5-turbo",
|
||||||
|
messages=[
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": f"write a one sentence poem about: {random_number}",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
max_tokens=20,
|
||||||
|
)
|
||||||
|
print(f"response1: {response1}")
|
||||||
|
|
||||||
|
random_number = random.randint(1, 100000)
|
||||||
|
|
||||||
|
response2 = completion(
|
||||||
|
model="gpt-3.5-turbo",
|
||||||
|
messages=[
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": f"write a one sentence poem about: {random_number}",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
max_tokens=20,
|
||||||
|
)
|
||||||
|
print(f"response2: {response1}")
|
||||||
|
assert response1.id == response2.id
|
||||||
|
# response1 == response2, response 1 is cached
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<TabItem value="in-mem" label="in memory cache">
|
<TabItem value="in-mem" label="in memory cache">
|
||||||
|
|
||||||
### Quick Start
|
### Quick Start
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue