forked from phoenix-oss/llama-stack-mirror
This PR makes several core changes to the developer experience surrounding Llama Stack. Background: PR #92 introduced the notion of "routing" to the Llama Stack. It introduces three object types: (1) models, (2) shields and (3) memory banks. Each of these objects can be associated with a distinct provider. So you can get model A to be inferenced locally while model B, C can be inference remotely (e.g.) However, this had a few drawbacks: you could not address the provider instances -- i.e., if you configured "meta-reference" with a given model, you could not assign an identifier to this instance which you could re-use later. the above meant that you could not register a "routing_key" (e.g. model) dynamically and say "please use this existing provider I have already configured" for a new model. the terms "routing_table" and "routing_key" were exposed directly to the user. in my view, this is way too much overhead for a new user (which almost everyone is.) people come to the stack wanting to do ML and encounter a completely unexpected term. What this PR does: This PR structures the run config with only a single prominent key: - providers Providers are instances of configured provider types. Here's an example which shows two instances of the remote::tgi provider which are serving two different models. providers: inference: - provider_id: foo provider_type: remote::tgi config: { ... } - provider_id: bar provider_type: remote::tgi config: { ... } Secondly, the PR adds dynamic registration of { models | shields | memory_banks } to the API surface. The distribution still acts like a "routing table" (as previously) except that it asks the backing providers for a listing of these objects. For example it asks a TGI or Ollama inference adapter what models it is serving. Only the models that are being actually served can be requested by the user for inference. Otherwise, the Stack server will throw an error. When dynamically registering these objects, you can use the provider IDs shown above. Info about providers can be obtained using the Api.inspect set of endpoints (/providers, /routes, etc.) The above examples shows the correspondence between inference providers and models registry items. Things work similarly for the safety <=> shields and memory <=> memory_banks pairs. Registry: This PR also makes it so that Providers need to implement additional methods for registering and listing objects. For example, each Inference provider is now expected to implement the ModelsProtocolPrivate protocol (naming is not great!) which consists of two methods register_model list_models The goal is to inform the provider that a certain model needs to be supported so the provider can make any relevant backend changes if needed (or throw an error if the model cannot be supported.) There are many other cleanups included some of which are detailed in a follow-up comment.
173 lines
5.8 KiB
Python
173 lines
5.8 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 typing import Any, Dict, List, Optional
|
|
|
|
from llama_models.llama3.api.datatypes import * # noqa: F403
|
|
|
|
from llama_stack.apis.models import * # noqa: F403
|
|
from llama_stack.apis.shields import * # noqa: F403
|
|
from llama_stack.apis.memory_banks import * # noqa: F403
|
|
|
|
from llama_stack.distribution.datatypes import * # noqa: F403
|
|
|
|
|
|
def get_impl_api(p: Any) -> Api:
|
|
return p.__provider_spec__.api
|
|
|
|
|
|
async def register_object_with_provider(obj: RoutableObject, p: Any) -> None:
|
|
api = get_impl_api(p)
|
|
if api == Api.inference:
|
|
await p.register_model(obj)
|
|
elif api == Api.safety:
|
|
await p.register_shield(obj)
|
|
elif api == Api.memory:
|
|
await p.register_memory_bank(obj)
|
|
|
|
|
|
Registry = Dict[str, List[RoutableObjectWithProvider]]
|
|
|
|
|
|
# TODO: this routing table maintains state in memory purely. We need to
|
|
# add persistence to it when we add dynamic registration of objects.
|
|
class CommonRoutingTableImpl(RoutingTable):
|
|
def __init__(
|
|
self,
|
|
impls_by_provider_id: Dict[str, RoutedProtocol],
|
|
) -> None:
|
|
self.impls_by_provider_id = impls_by_provider_id
|
|
|
|
async def initialize(self) -> None:
|
|
self.registry: Registry = {}
|
|
|
|
def add_objects(objs: List[RoutableObjectWithProvider]) -> None:
|
|
for obj in objs:
|
|
if obj.identifier not in self.registry:
|
|
self.registry[obj.identifier] = []
|
|
|
|
self.registry[obj.identifier].append(obj)
|
|
|
|
for pid, p in self.impls_by_provider_id.items():
|
|
api = get_impl_api(p)
|
|
if api == Api.inference:
|
|
p.model_store = self
|
|
models = await p.list_models()
|
|
add_objects(
|
|
[ModelDefWithProvider(**m.dict(), provider_id=pid) for m in models]
|
|
)
|
|
|
|
elif api == Api.safety:
|
|
p.shield_store = self
|
|
shields = await p.list_shields()
|
|
add_objects(
|
|
[
|
|
ShieldDefWithProvider(**s.dict(), provider_id=pid)
|
|
for s in shields
|
|
]
|
|
)
|
|
|
|
elif api == Api.memory:
|
|
p.memory_bank_store = self
|
|
memory_banks = await p.list_memory_banks()
|
|
|
|
# do in-memory updates due to pesky Annotated unions
|
|
for m in memory_banks:
|
|
m.provider_id = pid
|
|
|
|
add_objects(memory_banks)
|
|
|
|
async def shutdown(self) -> None:
|
|
for p in self.impls_by_provider_id.values():
|
|
await p.shutdown()
|
|
|
|
def get_provider_impl(
|
|
self, routing_key: str, provider_id: Optional[str] = None
|
|
) -> Any:
|
|
if routing_key not in self.registry:
|
|
raise ValueError(f"`{routing_key}` not registered")
|
|
|
|
objs = self.registry[routing_key]
|
|
for obj in objs:
|
|
if not provider_id or provider_id == obj.provider_id:
|
|
return self.impls_by_provider_id[obj.provider_id]
|
|
|
|
raise ValueError(f"Provider not found for `{routing_key}`")
|
|
|
|
def get_object_by_identifier(
|
|
self, identifier: str
|
|
) -> Optional[RoutableObjectWithProvider]:
|
|
objs = self.registry.get(identifier, [])
|
|
if not objs:
|
|
return None
|
|
|
|
# kind of ill-defined behavior here, but we'll just return the first one
|
|
return objs[0]
|
|
|
|
async def register_object(self, obj: RoutableObjectWithProvider):
|
|
entries = self.registry.get(obj.identifier, [])
|
|
for entry in entries:
|
|
if entry.provider_id == obj.provider_id:
|
|
print(f"`{obj.identifier}` already registered with `{obj.provider_id}`")
|
|
return
|
|
|
|
if obj.provider_id not in self.impls_by_provider_id:
|
|
raise ValueError(f"Provider `{obj.provider_id}` not found")
|
|
|
|
p = self.impls_by_provider_id[obj.provider_id]
|
|
await register_object_with_provider(obj, p)
|
|
|
|
if obj.identifier not in self.registry:
|
|
self.registry[obj.identifier] = []
|
|
self.registry[obj.identifier].append(obj)
|
|
|
|
# TODO: persist this to a store
|
|
|
|
|
|
class ModelsRoutingTable(CommonRoutingTableImpl, Models):
|
|
async def list_models(self) -> List[ModelDefWithProvider]:
|
|
objects = []
|
|
for objs in self.registry.values():
|
|
objects.extend(objs)
|
|
return objects
|
|
|
|
async def get_model(self, identifier: str) -> Optional[ModelDefWithProvider]:
|
|
return self.get_object_by_identifier(identifier)
|
|
|
|
async def register_model(self, model: ModelDefWithProvider) -> None:
|
|
await self.register_object(model)
|
|
|
|
|
|
class ShieldsRoutingTable(CommonRoutingTableImpl, Shields):
|
|
async def list_shields(self) -> List[ShieldDef]:
|
|
objects = []
|
|
for objs in self.registry.values():
|
|
objects.extend(objs)
|
|
return objects
|
|
|
|
async def get_shield(self, shield_type: str) -> Optional[ShieldDefWithProvider]:
|
|
return self.get_object_by_identifier(shield_type)
|
|
|
|
async def register_shield(self, shield: ShieldDefWithProvider) -> None:
|
|
await self.register_object(shield)
|
|
|
|
|
|
class MemoryBanksRoutingTable(CommonRoutingTableImpl, MemoryBanks):
|
|
async def list_memory_banks(self) -> List[MemoryBankDefWithProvider]:
|
|
objects = []
|
|
for objs in self.registry.values():
|
|
objects.extend(objs)
|
|
return objects
|
|
|
|
async def get_memory_bank(
|
|
self, identifier: str
|
|
) -> Optional[MemoryBankDefWithProvider]:
|
|
return self.get_object_by_identifier(identifier)
|
|
|
|
async def register_memory_bank(
|
|
self, memory_bank: MemoryBankDefWithProvider
|
|
) -> None:
|
|
await self.register_object(memory_bank)
|