mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-06-28 02:53:30 +00:00
Update tools notebook to follow CustomTool->ClientTool change.
This commit is contained in:
parent
c91e3552a3
commit
43d9a20ee6
1 changed files with 372 additions and 358 deletions
|
@ -21,10 +21,13 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "2fbe7011",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-05-07T08:06:41.914323Z",
|
||||
"start_time": "2025-05-07T08:06:41.911153Z"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"import asyncio\n",
|
||||
"import json\n",
|
||||
|
@ -34,13 +37,12 @@
|
|||
"import nest_asyncio\n",
|
||||
"import requests\n",
|
||||
"from dotenv import load_dotenv\n",
|
||||
"from llama_stack_client import LlamaStackClient\n",
|
||||
"from llama_stack_client import AgentEventLogger, LlamaStackClient\n",
|
||||
"from llama_stack_client.lib.agents.agent import Agent\n",
|
||||
"from llama_stack_client.lib.agents.custom_tool import CustomTool\n",
|
||||
"from llama_stack_client.lib.agents.event_logger import EventLogger\n",
|
||||
"from llama_stack_client.lib.agents.client_tool import ClientTool\n",
|
||||
"from llama_stack_client.types import CompletionMessage\n",
|
||||
"from llama_stack_client.types.agent_create_params import AgentConfig\n",
|
||||
"from llama_stack_client.types.shared.tool_response_message import ToolResponseMessage\n",
|
||||
"from llama_stack_client.types.tool_def_param import Parameter\n",
|
||||
"\n",
|
||||
"# Allow asyncio to run in Jupyter Notebook\n",
|
||||
"nest_asyncio.apply()\n",
|
||||
|
@ -48,7 +50,9 @@
|
|||
"HOST = \"localhost\"\n",
|
||||
"PORT = 8321\n",
|
||||
"MODEL_NAME = \"meta-llama/Llama-3.2-3B-Instruct\"\n"
|
||||
]
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": 19
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
|
@ -64,14 +68,14 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "b4b3300c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"load_dotenv()\n",
|
||||
"BRAVE_SEARCH_API_KEY = os.environ[\"BRAVE_SEARCH_API_KEY\"]\n"
|
||||
]
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": null
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
|
@ -85,10 +89,13 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "62271ed2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-05-07T07:49:34.783948Z",
|
||||
"start_time": "2025-05-07T07:49:34.779274Z"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"class BraveSearch:\n",
|
||||
" def __init__(self, api_key: str) -> None:\n",
|
||||
|
@ -120,7 +127,9 @@
|
|||
" clean_response.append(cleaned)\n",
|
||||
"\n",
|
||||
" return {\"query\": query, \"top_k\": clean_response}\n"
|
||||
]
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": 10
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
|
@ -134,12 +143,15 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "92e75cf8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-05-07T08:04:34.126742Z",
|
||||
"start_time": "2025-05-07T08:04:34.122085Z"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"class WebSearchTool(CustomTool):\n",
|
||||
"class WebSearchTool(ClientTool):\n",
|
||||
" def __init__(self, api_key: str):\n",
|
||||
" self.api_key = api_key\n",
|
||||
" self.engine = BraveSearch(api_key)\n",
|
||||
|
@ -150,6 +162,16 @@
|
|||
" def get_description(self) -> str:\n",
|
||||
" return \"Search the web for a given query\"\n",
|
||||
"\n",
|
||||
" def get_params_definition(self) -> Dict[str, Parameter]:\n",
|
||||
" return {\n",
|
||||
" \"query\": Parameter(\n",
|
||||
" name=\"query\",\n",
|
||||
" parameter_type=\"str\",\n",
|
||||
" description=\"The query to search for\",\n",
|
||||
" required=True,\n",
|
||||
" )\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" async def run_impl(self, query: str):\n",
|
||||
" return await self.engine.search(query)\n",
|
||||
"\n",
|
||||
|
@ -192,7 +214,9 @@
|
|||
" f\" Description: {result.get('description', 'No Description')}\\n\\n\"\n",
|
||||
" )\n",
|
||||
" return formatted_result\n"
|
||||
]
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": 17
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
|
@ -206,16 +230,21 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "aaf5664f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-05-07T07:49:58.145145Z",
|
||||
"start_time": "2025-05-07T07:49:58.142695Z"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"async def execute_search(query: str):\n",
|
||||
" web_search_tool = WebSearchTool(api_key=BRAVE_SEARCH_API_KEY)\n",
|
||||
" result = await web_search_tool.run_impl(query)\n",
|
||||
" print(\"Search Results:\", result)\n"
|
||||
]
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": 12
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
|
@ -227,22 +256,27 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "5f22c4e2",
|
||||
"metadata": {},
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-05-07T07:50:07.783852Z",
|
||||
"start_time": "2025-05-07T07:50:07.258583Z"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"query = \"Latest developments in quantum computing\"\n",
|
||||
"asyncio.run(execute_search(query))\n"
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Search Results: {\"query\": \"Latest developments in quantum computing\", \"top_k\": [{\"title\": \"Quantum Computing | Latest News, Photos & Videos | WIRED\", \"url\": \"https://www.wired.com/tag/quantum-computing/\", \"description\": \"Find the <strong>latest</strong> <strong>Quantum</strong> <strong>Computing</strong> news from WIRED. See related science and technology articles, photos, slideshows and videos.\"}, {\"title\": \"Quantum Computing News -- ScienceDaily\", \"url\": \"https://www.sciencedaily.com/news/matter_energy/quantum_computing/\", \"description\": \"<strong>Quantum</strong> <strong>Computing</strong> News. Read the <strong>latest</strong> about the <strong>development</strong> <strong>of</strong> <strong>quantum</strong> <strong>computers</strong>.\"}]}\n"
|
||||
"Search Results: {\"query\": null, \"top_k\": []}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"query = \"Latest developments in quantum computing\"\n",
|
||||
"asyncio.run(execute_search(query))\n"
|
||||
]
|
||||
"execution_count": 13
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
|
@ -256,30 +290,8 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "9e704b01-f410-492f-8baf-992589b82803",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Created session_id=34d2978d-e299-4a2a-9219-4ffe2fb124a2 for Agent(8a68f2c3-2b2a-4f67-a355-c6d5b2451d6a)\n",
|
||||
"\u001b[30m\u001b[0m\u001b[33minference> \u001b[0m\u001b[33m[\u001b[0m\u001b[33mweb\u001b[0m\u001b[33m_search\u001b[0m\u001b[33m(query\u001b[0m\u001b[33m=\"\u001b[0m\u001b[33mlatest\u001b[0m\u001b[33m developments\u001b[0m\u001b[33m in\u001b[0m\u001b[33m quantum\u001b[0m\u001b[33m computing\u001b[0m\u001b[33m\")]\u001b[0m\u001b[97m\u001b[0m\n",
|
||||
"\u001b[32mCustomTool> Search Results with Citations:\n",
|
||||
"\n",
|
||||
"1. Quantum Computing | Latest News, Photos & Videos | WIRED\n",
|
||||
" URL: https://www.wired.com/tag/quantum-computing/\n",
|
||||
" Description: Find the <strong>latest</strong> <strong>Quantum</strong> <strong>Computing</strong> news from WIRED. See related science and technology articles, photos, slideshows and videos.\n",
|
||||
"\n",
|
||||
"2. Quantum Computing News -- ScienceDaily\n",
|
||||
" URL: https://www.sciencedaily.com/news/matter_energy/quantum_computing/\n",
|
||||
" Description: <strong>Quantum</strong> <strong>Computing</strong> News. Read the <strong>latest</strong> about the <strong>development</strong> <strong>of</strong> <strong>quantum</strong> <strong>computers</strong>.\n",
|
||||
"\n",
|
||||
"\u001b[0m\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"async def run_main(disable_safety: bool = False):\n",
|
||||
" # Initialize the Llama Stack client with the specified base URL\n",
|
||||
|
@ -325,13 +337,15 @@
|
|||
" )\n",
|
||||
"\n",
|
||||
" # Log and print the response from the agent asynchronously\n",
|
||||
" async for log in EventLogger().log(response):\n",
|
||||
" for log in AgentEventLogger().log(response):\n",
|
||||
" log.print()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Run the function asynchronously in a Jupyter Notebook cell\n",
|
||||
"await run_main(disable_safety=True)\n"
|
||||
]
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": null
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue