-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add provider API for listing and inspecting provider info #1429
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 .providers import * # noqa: F401 F403 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# 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 List, Protocol, runtime_checkable | ||
|
||
from pydantic import BaseModel | ||
|
||
from llama_stack.distribution.datatypes import Provider | ||
from llama_stack.schema_utils import json_schema_type, webmethod | ||
|
||
|
||
@json_schema_type | ||
class ProviderInfo(BaseModel): | ||
api: str | ||
provider_id: str | ||
provider_type: str | ||
|
||
|
||
class GetProviderResponse(BaseModel): | ||
data: Provider | None | ||
|
||
|
||
class ListProvidersResponse(BaseModel): | ||
data: List[ProviderInfo] | ||
|
||
|
||
@runtime_checkable | ||
class Providers(Protocol): | ||
""" | ||
Providers API for inspecting, listing, and modifying providers and their configurations. | ||
""" | ||
|
||
@webmethod(route="/providers", method="GET") | ||
async def list_providers(self) -> ListProvidersResponse: ... | ||
|
||
@webmethod(route="/providers/{provider_id}", method="GET") | ||
async def inspect_provider(self, provider_id: str) -> GetProviderResponse: ... | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# 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 pydantic import BaseModel | ||
|
||
from llama_stack.apis.providers import GetProviderResponse, ListProvidersResponse, ProviderInfo, Providers | ||
|
||
from .datatypes import StackRunConfig | ||
from .stack import redact_sensitive_fields | ||
|
||
|
||
class ProviderImplConfig(BaseModel): | ||
run_config: StackRunConfig | ||
|
||
|
||
async def get_provider_impl(config, deps): | ||
impl = ProviderImpl(config, deps) | ||
await impl.initialize() | ||
return impl | ||
|
||
|
||
class ProviderImpl(Providers): | ||
|
||
def __init__(self, config, deps): | ||
self.config = config | ||
self.deps = deps | ||
|
||
async def initialize(self) -> None: | ||
pass | ||
|
||
async def list_providers(self) -> ListProvidersResponse: | ||
run_config = self.config.run_config | ||
ret = [] | ||
for api, providers in run_config.providers.items(): | ||
ret.extend( | ||
[ | ||
ProviderInfo( | ||
api=api, | ||
provider_id=p.provider_id, | ||
provider_type=p.provider_type, | ||
) | ||
for p in providers | ||
] | ||
) | ||
|
||
return ListProvidersResponse(data=ret) | ||
|
||
async def inspect_provider(self, provider_id: str) -> GetProviderResponse: | ||
run_config = self.config.run_config | ||
safe_config = StackRunConfig(**redact_sensitive_fields(run_config.model_dump())) | ||
ret = None | ||
for _, providers in safe_config.providers.items(): | ||
for p in providers: | ||
if p.provider_id == provider_id: | ||
ret = p | ||
|
||
return GetProviderResponse(data=ret) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# 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. |
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.
shouldn't this return ProviderInfo?
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.
ProviderInfo
doesn't contain aconfig
, so I created a new type for that.inspect
returns detailed info about a provider.list
returnsProviderInfo