mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-06-28 02:53:30 +00:00
This is yet another of those large PRs (hopefully we will have less and less of them as things mature fast). This one introduces substantial improvements and some simplifications to the stack. Most important bits: * Agents reference implementation now has support for session / turn persistence. The default implementation uses sqlite but there's also support for using Redis. * We have re-architected the structure of the Stack APIs to allow for more flexible routing. The motivating use cases are: - routing model A to ollama and model B to a remote provider like Together - routing shield A to local impl while shield B to a remote provider like Bedrock - routing a vector memory bank to Weaviate while routing a keyvalue memory bank to Redis * Support for provider specific parameters to be passed from the clients. A client can pass data using `x_llamastack_provider_data` parameter which can be type-checked and provided to the Adapter implementations.
49 lines
1.2 KiB
Python
49 lines
1.2 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.
|
|
|
|
import json
|
|
import threading
|
|
from typing import Any, Dict, Optional
|
|
|
|
from .utils.dynamic import instantiate_class_type
|
|
|
|
_THREAD_LOCAL = threading.local()
|
|
|
|
|
|
def get_request_provider_data() -> Any:
|
|
return getattr(_THREAD_LOCAL, "provider_data", None)
|
|
|
|
|
|
def set_request_provider_data(headers: Dict[str, str], validator_class: Optional[str]):
|
|
if not validator_class:
|
|
return
|
|
|
|
keys = [
|
|
"X-LlamaStack-ProviderData",
|
|
"x-llamastack-providerdata",
|
|
]
|
|
for key in keys:
|
|
val = headers.get(key, None)
|
|
if val:
|
|
break
|
|
|
|
if not val:
|
|
return
|
|
|
|
try:
|
|
val = json.loads(val)
|
|
except json.JSONDecodeError:
|
|
print("Provider data not encoded as a JSON object!", val)
|
|
return
|
|
|
|
validator = instantiate_class_type(validator_class)
|
|
try:
|
|
provider_data = validator(**val)
|
|
except Exception as e:
|
|
print("Error parsing provider data", e)
|
|
return
|
|
|
|
_THREAD_LOCAL.provider_data = provider_data
|