mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-06-28 02:53:30 +00:00
# What does this PR do? In short, provide a summary of what this PR does and why. Usually, the relevant context should be present in a linked issue. - [Currently redis as a kvstore is bugged, as the range method uses zrangebylex method. zrangebylex method is used when it is a sorted set but we are storing the value using .set method in the redis. This causes an error. Another issue is that zrangebylex method takes 3 args but only 2 are mentioned in the range method. This causes a runtime error. That method has been replaced with the current implementation in the PR ] Addresses issue (#520 ) ## Test Plan Please describe: - tests you ran to verify your changes with result summaries. - provide instructions so it can be reproduced. `python llama_stack/apis/agents/client.py localhost 8001 tools_llama_3_1 meta-llama/Llama-3.1-70B-Instruct` <img width="1711" alt="Screenshot 2024-11-25 at 2 59 55 PM" src="https://github.com/user-attachments/assets/c2551555-bc73-4427-b09b-c86d6deb2956"> <img width="634" alt="Screenshot 2024-11-25 at 3 00 33 PM" src="https://github.com/user-attachments/assets/a087718f-fc2a-424b-b096-4ecad08a07bf"> Have used redis in the run.yaml file as well for the persistence_store. Also enable_session_persistence turned to True for this test. Have also tested this in a jupyter notebook to make sure the current flow does not work through multiple turns in the same session. ## Sources Please link relevant resources if necessary. ## Before submitting - [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case). - [x] Ran pre-commit to handle lint / formatting issues. - [x] Read the [contributor guideline](https://github.com/meta-llama/llama-stack/blob/main/CONTRIBUTING.md), Pull Request section? - [ ] Updated relevant documentation. - [ ] Wrote necessary unit or integration tests.
74 lines
2.3 KiB
Python
74 lines
2.3 KiB
Python
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
# All rights reserved.
|
|
#
|
|
# This source code is licensed under the terms described in the LICENSE file in
|
|
# the root directory of this source tree.
|
|
|
|
from datetime import datetime
|
|
from typing import List, Optional
|
|
|
|
from redis.asyncio import Redis
|
|
|
|
from ..api import KVStore
|
|
from ..config import RedisKVStoreConfig
|
|
|
|
|
|
class RedisKVStoreImpl(KVStore):
|
|
def __init__(self, config: RedisKVStoreConfig):
|
|
self.config = config
|
|
|
|
async def initialize(self) -> None:
|
|
self.redis = Redis.from_url(self.config.url)
|
|
|
|
def _namespaced_key(self, key: str) -> str:
|
|
if not self.config.namespace:
|
|
return key
|
|
return f"{self.config.namespace}:{key}"
|
|
|
|
async def set(
|
|
self, key: str, value: str, expiration: Optional[datetime] = None
|
|
) -> None:
|
|
key = self._namespaced_key(key)
|
|
await self.redis.set(key, value)
|
|
if expiration:
|
|
await self.redis.expireat(key, expiration)
|
|
|
|
async def get(self, key: str) -> Optional[str]:
|
|
key = self._namespaced_key(key)
|
|
value = await self.redis.get(key)
|
|
if value is None:
|
|
return None
|
|
ttl = await self.redis.ttl(key)
|
|
return value
|
|
|
|
async def delete(self, key: str) -> None:
|
|
key = self._namespaced_key(key)
|
|
await self.redis.delete(key)
|
|
|
|
async def range(self, start_key: str, end_key: str) -> List[str]:
|
|
start_key = self._namespaced_key(start_key)
|
|
end_key = self._namespaced_key(end_key)
|
|
cursor = 0
|
|
pattern = start_key + "*" # Match all keys starting with start_key prefix
|
|
matching_keys = []
|
|
while True:
|
|
cursor, keys = await self.redis.scan(cursor, match=pattern, count=1000)
|
|
|
|
for key in keys:
|
|
key_str = key.decode("utf-8") if isinstance(key, bytes) else key
|
|
if start_key <= key_str <= end_key:
|
|
matching_keys.append(key)
|
|
|
|
if cursor == 0:
|
|
break
|
|
|
|
# Then fetch all values in a single MGET call
|
|
if matching_keys:
|
|
values = await self.redis.mget(matching_keys)
|
|
return [
|
|
value.decode("utf-8") if isinstance(value, bytes) else value
|
|
for value in values
|
|
if value is not None
|
|
]
|
|
|
|
return []
|