mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-06-28 02:53:30 +00:00
This PR adds a new Tools api and adds two tool runtime providers: brave and MCP. Test plan: ``` curl -X POST 'http://localhost:5000/alpha/toolgroups/register' \ -H 'Content-Type: application/json' \ -d '{ "tool_group_id": "simple_tool", "tool_group": { "type": "model_context_protocol", "endpoint": {"uri": "http://localhost:56000/sse"} }, "provider_id": "model-context-protocol" }' curl -X POST 'http://localhost:5000/alpha/toolgroups/register' \ -H 'Content-Type: application/json' \ -d '{ "tool_group_id": "search", "provider_id": "brave-search", "tool_group": { "type": "user_defined", "tools": [ { "name": "brave_search", "description": "A web search tool", "parameters": [ { "name": "query", "parameter_type": "string", "description": "The query to search" } ], "metadata": {}, "tool_prompt_format": "json" } ] } }' curl -X GET http://localhost:5000/alpha/tools/list | jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 662 100 662 0 0 333k 0 --:--:-- --:--:-- --:--:-- 646k [ { "identifier": "brave_search", "provider_resource_id": "brave_search", "provider_id": "brave-search", "type": "tool", "tool_group": "search", "description": "A web search tool", "parameters": [ { "name": "query", "parameter_type": "string", "description": "The query to search" } ], "metadata": {}, "tool_prompt_format": "json" }, { "identifier": "fetch", "provider_resource_id": "fetch", "provider_id": "model-context-protocol", "type": "tool", "tool_group": "simple_tool", "description": "Fetches a website and returns its content", "parameters": [ { "name": "url", "parameter_type": "string", "description": "URL to fetch" } ], "metadata": { "endpoint": "http://localhost:56000/sse" }, "tool_prompt_format": "json" } ] curl -X POST 'http://localhost:5000/alpha/tool-runtime/invoke' \ -H 'Content-Type: application/json' \ -d '{ "tool_name": "fetch", "args": { "url": "http://google.com/" } }' curl -X POST 'http://localhost:5000/alpha/tool-runtime/invoke' \ -H 'Content-Type: application/json' -H 'X-LlamaStack-ProviderData: {"api_key": "<KEY>"}' \ -d '{ "tool_name": "brave_search", "args": { "query": "who is meta ceo" } }' ```
72 lines
2 KiB
Python
72 lines
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.
|
|
|
|
from typing import Any
|
|
|
|
from llama_stack.distribution.datatypes import * # noqa: F403
|
|
from llama_stack.distribution.store import DistributionRegistry
|
|
|
|
from .routing_tables import (
|
|
DatasetsRoutingTable,
|
|
EvalTasksRoutingTable,
|
|
MemoryBanksRoutingTable,
|
|
ModelsRoutingTable,
|
|
ScoringFunctionsRoutingTable,
|
|
ShieldsRoutingTable,
|
|
ToolGroupsRoutingTable,
|
|
)
|
|
|
|
|
|
async def get_routing_table_impl(
|
|
api: Api,
|
|
impls_by_provider_id: Dict[str, RoutedProtocol],
|
|
_deps,
|
|
dist_registry: DistributionRegistry,
|
|
) -> Any:
|
|
api_to_tables = {
|
|
"memory_banks": MemoryBanksRoutingTable,
|
|
"models": ModelsRoutingTable,
|
|
"shields": ShieldsRoutingTable,
|
|
"datasets": DatasetsRoutingTable,
|
|
"scoring_functions": ScoringFunctionsRoutingTable,
|
|
"eval_tasks": EvalTasksRoutingTable,
|
|
"tool_groups": ToolGroupsRoutingTable,
|
|
}
|
|
|
|
if api.value not in api_to_tables:
|
|
raise ValueError(f"API {api.value} not found in router map")
|
|
|
|
impl = api_to_tables[api.value](impls_by_provider_id, dist_registry)
|
|
await impl.initialize()
|
|
return impl
|
|
|
|
|
|
async def get_auto_router_impl(api: Api, routing_table: RoutingTable, _deps) -> Any:
|
|
from .routers import (
|
|
DatasetIORouter,
|
|
EvalRouter,
|
|
InferenceRouter,
|
|
MemoryRouter,
|
|
SafetyRouter,
|
|
ScoringRouter,
|
|
ToolRuntimeRouter,
|
|
)
|
|
|
|
api_to_routers = {
|
|
"memory": MemoryRouter,
|
|
"inference": InferenceRouter,
|
|
"safety": SafetyRouter,
|
|
"datasetio": DatasetIORouter,
|
|
"scoring": ScoringRouter,
|
|
"eval": EvalRouter,
|
|
"tool_runtime": ToolRuntimeRouter,
|
|
}
|
|
if api.value not in api_to_routers:
|
|
raise ValueError(f"API {api.value} not found in router map")
|
|
|
|
impl = api_to_routers[api.value](routing_table)
|
|
await impl.initialize()
|
|
return impl
|