kikocis/llama-stack public
Changesets · Branches · Tags · Compare
| Changeset | 5ec25afa3bd57d86f7ca8ec058215cb9a333f32de0b6cdc09601d2c167de34fd |
|---|---|
| Author | Hardik Shah <hjshah@meta.com> · 2024-09-10 12:34 -07:00 |
| Committer | GitHub <noreply@github.com> · 2024-09-10 12:34 -07:00 |
| Parents | 371459e1ee27 |
7 files changed
modified llama_toolchain/agentic_system/api/api.py +10 −2
| @@ -41,11 +41,19 @@ | ||
| 41 | 41 | output_shields: Optional[List[ShieldDefinition]] = Field(default_factory=list) |
| 42 | 42 | |
| 43 | 43 | |
| 44 | +class SearchEngineType(Enum): | |
| 45 | + bing = "bing" | |
| 46 | + brave = "brave" | |
| 47 | + | |
| 48 | + | |
| 44 | 49 | @json_schema_type |
| 45 | -class BraveSearchToolDefinition(ToolDefinitionCommon): | |
| 50 | +class SearchToolDefinition(ToolDefinitionCommon): | |
| 51 | + # NOTE: brave_search is just a placeholder since model always uses | |
| 52 | + # brave_search as tool call name | |
| 46 | 53 | type: Literal[AgenticSystemTool.brave_search.value] = ( |
| 47 | 54 | AgenticSystemTool.brave_search.value |
| 48 | 55 | ) |
| 56 | + engine: SearchEngineType = SearchEngineType.brave | |
| 49 | 57 | remote_execution: Optional[RestAPIExecutionConfig] = None |
| 50 | 58 | |
| 51 | 59 | |
| @@ -163,7 +171,7 @@ | ||
| 163 | 171 | |
| 164 | 172 | AgenticSystemToolDefinition = Annotated[ |
| 165 | 173 | Union[ |
| 166 | - BraveSearchToolDefinition, | |
| 174 | + SearchToolDefinition, | |
| 167 | 175 | WolframAlphaToolDefinition, |
| 168 | 176 | PhotogenToolDefinition, |
| 169 | 177 | CodeInterpreterToolDefinition, |
modified llama_toolchain/agentic_system/client.py +1 −1
| @@ -134,7 +134,7 @@ | ||
| 134 | 134 | api = AgenticSystemClient(f"http://{host}:{port}") |
| 135 | 135 | |
| 136 | 136 | tool_definitions = [ |
| 137 | - BraveSearchToolDefinition(), | |
| 137 | + SearchToolDefinition(engine=SearchEngineType.bing), | |
| 138 | 138 | WolframAlphaToolDefinition(), |
| 139 | 139 | CodeInterpreterToolDefinition(), |
| 140 | 140 | ] |
modified llama_toolchain/agentic_system/meta_reference/agent_instance.py +1 −1
| @@ -710,7 +710,7 @@ | ||
| 710 | 710 | def _get_tools(self) -> List[ToolDefinition]: |
| 711 | 711 | ret = [] |
| 712 | 712 | for t in self.agent_config.tools: |
| 713 | - if isinstance(t, BraveSearchToolDefinition): | |
| 713 | + if isinstance(t, SearchToolDefinition): | |
| 714 | 714 | ret.append(ToolDefinition(tool_name=BuiltinTool.brave_search)) |
| 715 | 715 | elif isinstance(t, WolframAlphaToolDefinition): |
| 716 | 716 | ret.append(ToolDefinition(tool_name=BuiltinTool.wolfram_alpha)) |
modified llama_toolchain/agentic_system/meta_reference/agentic_system.py +10 −8
| @@ -15,9 +15,9 @@ | ||
| 15 | 15 | from llama_toolchain.safety.api import Safety |
| 16 | 16 | from llama_toolchain.agentic_system.api import * # noqa: F403 |
| 17 | 17 | from llama_toolchain.tools.builtin import ( |
| 18 | - BraveSearchTool, | |
| 19 | 18 | CodeInterpreterTool, |
| 20 | 19 | PhotogenTool, |
| 20 | + SearchTool, | |
| 21 | 21 | WolframAlphaTool, |
| 22 | 22 | ) |
| 23 | 23 | from llama_toolchain.tools.safety import with_safety |
| @@ -62,17 +62,19 @@ | ||
| 62 | 62 | if not key: |
| 63 | 63 | raise ValueError("Wolfram API key not defined in config") |
| 64 | 64 | tool = WolframAlphaTool(key) |
| 65 | - elif isinstance(tool_defn, BraveSearchToolDefinition): | |
| 66 | - key = self.config.brave_search_api_key | |
| 65 | + elif isinstance(tool_defn, SearchToolDefinition): | |
| 66 | + key = None | |
| 67 | + if tool_defn.engine == SearchEngineType.brave: | |
| 68 | + key = self.config.brave_search_api_key | |
| 69 | + elif tool_defn.engine == SearchEngineType.bing: | |
| 70 | + key = self.config.bing_search_api_key | |
| 67 | 71 | if not key: |
| 68 | - raise ValueError("Brave API key not defined in config") | |
| 69 | - tool = BraveSearchTool(key) | |
| 72 | + raise ValueError("API key not defined in config") | |
| 73 | + tool = SearchTool(tool_defn.engine, key) | |
| 70 | 74 | elif isinstance(tool_defn, CodeInterpreterToolDefinition): |
| 71 | 75 | tool = CodeInterpreterTool() |
| 72 | 76 | elif isinstance(tool_defn, PhotogenToolDefinition): |
| 73 | - tool = PhotogenTool( | |
| 74 | - dump_dir=tempfile.mkdtemp(), | |
| 75 | - ) | |
| 77 | + tool = PhotogenTool(dump_dir=tempfile.mkdtemp()) | |
| 76 | 78 | else: |
| 77 | 79 | continue |
| 78 | 80 |
modified llama_toolchain/agentic_system/meta_reference/config.py +1 −0
| @@ -11,4 +11,5 @@ | ||
| 11 | 11 | |
| 12 | 12 | class MetaReferenceImplConfig(BaseModel): |
| 13 | 13 | brave_search_api_key: Optional[str] = None |
| 14 | + bing_search_api_key: Optional[str] = None | |
| 14 | 15 | wolfram_api_key: Optional[str] = None |
modified llama_toolchain/common/deployment_types.py +4 −4
| @@ -5,7 +5,7 @@ | ||
| 5 | 5 | # the root directory of this source tree. |
| 6 | 6 | |
| 7 | 7 | from enum import Enum |
| 8 | -from typing import Dict, Optional | |
| 8 | +from typing import Any, Dict, Optional | |
| 9 | 9 | |
| 10 | 10 | from llama_models.llama3.api.datatypes import URL |
| 11 | 11 | |
| @@ -26,6 +26,6 @@ | ||
| 26 | 26 | class RestAPIExecutionConfig(BaseModel): |
| 27 | 27 | url: URL |
| 28 | 28 | method: RestAPIMethod |
| 29 | - params: Optional[Dict[str, str]] = None | |
| 30 | - headers: Optional[Dict[str, str]] = None | |
| 31 | - body: Optional[Dict[str, str]] = None | |
| 29 | + params: Optional[Dict[str, Any]] = None | |
| 30 | + headers: Optional[Dict[str, Any]] = None | |
| 31 | + body: Optional[Dict[str, Any]] = None |
modified llama_toolchain/tools/builtin.py +60 −2
| @@ -83,14 +83,72 @@ | ||
| 83 | 83 | raise NotImplementedError() |
| 84 | 84 | |
| 85 | 85 | |
| 86 | -class BraveSearchTool(SingleMessageBuiltinTool): | |
| 87 | - def __init__(self, api_key: str) -> None: | |
| 86 | +class SearchTool(SingleMessageBuiltinTool): | |
| 87 | + def __init__(self, engine: SearchEngineType, api_key: str, **kwargs) -> None: | |
| 88 | 88 | self.api_key = api_key |
| 89 | + if engine == SearchEngineType.bing: | |
| 90 | + self.engine = BingSearch(api_key, **kwargs) | |
| 91 | + elif engine == SearchEngineType.brave: | |
| 92 | + self.engine = BraveSearch(api_key, **kwargs) | |
| 93 | + else: | |
| 94 | + raise ValueError(f"Unknown search engine: {engine}") | |
| 89 | 95 | |
| 90 | 96 | def get_name(self) -> str: |
| 91 | 97 | return BuiltinTool.brave_search.value |
| 92 | 98 | |
| 93 | 99 | async def run_impl(self, query: str) -> str: |
| 100 | + return await self.engine.search(query) | |
| 101 | + | |
| 102 | + | |
| 103 | +class BingSearch: | |
| 104 | + def __init__(self, api_key: str, top_k: int = 3, **kwargs) -> None: | |
| 105 | + self.api_key = api_key | |
| 106 | + self.top_k = top_k | |
| 107 | + | |
| 108 | + async def search(self, query: str) -> str: | |
| 109 | + url = "https://api.bing.microsoft.com/v7.0/search" | |
| 110 | + headers = { | |
| 111 | + "Ocp-Apim-Subscription-Key": self.api_key, | |
| 112 | + } | |
| 113 | + params = { | |
| 114 | + "count": self.top_k, | |
| 115 | + "textDecorations": True, | |
| 116 | + "textFormat": "HTML", | |
| 117 | + "q": query, | |
| 118 | + } | |
| 119 | + | |
| 120 | + response = requests.get(url=url, params=params, headers=headers) | |
| 121 | + response.raise_for_status() | |
| 122 | + clean = self._clean_response(response.json()) | |
| 123 | + return json.dumps(clean) | |
| 124 | + | |
| 125 | + def _clean_response(self, search_response): | |
| 126 | + clean_response = [] | |
| 127 | + query = search_response["queryContext"]["originalQuery"] | |
| 128 | + if "webPages" in search_response: | |
| 129 | + pages = search_response["webPages"]["value"] | |
| 130 | + for p in pages: | |
| 131 | + selected_keys = {"name", "url", "snippet"} | |
| 132 | + clean_response.append( | |
| 133 | + {k: v for k, v in p.items() if k in selected_keys} | |
| 134 | + ) | |
| 135 | + if "news" in search_response: | |
| 136 | + clean_news = [] | |
| 137 | + news = search_response["news"]["value"] | |
| 138 | + for n in news: | |
| 139 | + selected_keys = {"name", "url", "description"} | |
| 140 | + clean_news.append({k: v for k, v in n.items() if k in selected_keys}) | |
| 141 | + | |
| 142 | + clean_response.append(clean_news) | |
| 143 | + | |
| 144 | + return {"query": query, "top_k": clean_response} | |
| 145 | + | |
| 146 | + | |
| 147 | +class BraveSearch: | |
| 148 | + def __init__(self, api_key: str) -> None: | |
| 149 | + self.api_key = api_key | |
| 150 | + | |
| 151 | + async def search(self, query: str) -> str: | |
| 94 | 152 | url = "https://api.search.brave.com/res/v1/web/search" |
| 95 | 153 | headers = { |
| 96 | 154 | "X-Subscription-Token": self.api_key, |