-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Added support for mongoDB KV store #543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
23df7db
Added support for mongoDB KV store
c2f3a7f
fixed port and collection_name issue
fdda9f3
removed extra spaces and fix collection_name
8e79f53
removed unused imports
a585e08
removed run.yaml
fced5ec
Merge branch 'meta-llama:main' into main
shrinitg 54e48d5
Merge branch 'meta-llama:main' into main
shrinitg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# 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 .mongodb import MongoDBKVStoreImpl |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# 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. | ||
|
||
import logging | ||
from datetime import datetime | ||
from typing import List, Optional | ||
|
||
from pymongo import MongoClient | ||
|
||
from llama_stack.providers.utils.kvstore import KVStore, MongoDBKVStoreConfig | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class MongoDBKVStoreImpl(KVStore): | ||
def __init__(self, config: MongoDBKVStoreConfig): | ||
self.config = config | ||
self.conn = None | ||
self.collection = None | ||
|
||
async def initialize(self) -> None: | ||
try: | ||
conn_creds = { | ||
"host": self.config.host, | ||
"port": self.config.port, | ||
"username": self.config.user, | ||
"password": self.config.password, | ||
} | ||
conn_creds = {k: v for k, v in conn_creds.items() if v is not None} | ||
self.conn = MongoClient(**conn_creds) | ||
self.collection = self.conn[self.config.db][self.config.collection_name] | ||
except Exception as e: | ||
log.exception("Could not connect to MongoDB database server") | ||
raise RuntimeError("Could not connect to MongoDB database server") from e | ||
|
||
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) | ||
update_query = {"$set": {"value": value, "expiration": expiration}} | ||
self.collection.update_one({"key": key}, update_query, upsert=True) | ||
|
||
async def get(self, key: str) -> Optional[str]: | ||
key = self._namespaced_key(key) | ||
query = {"key": key} | ||
result = self.collection.find_one(query, {"value": 1, "_id": 0}) | ||
return result["value"] if result else None | ||
|
||
async def delete(self, key: str) -> None: | ||
key = self._namespaced_key(key) | ||
self.collection.delete_one({"key": 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) | ||
query = { | ||
"key": {"$gte": start_key, "$lt": end_key}, | ||
} | ||
cursor = self.collection.find(query, {"value": 1, "_id": 0}).sort("key", 1) | ||
return [doc["value"] for doc in cursor] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should also add a
**kwargs
parameter to sample run config (since thedistro_codegen.py
script invokes it with a "distro" parameter)