This commit is contained in:
Heiko W. Rupp 2025-06-27 11:39:51 +02:00 committed by GitHub
commit e85cbc102f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -21,10 +21,13 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2,
"id": "2fbe7011", "id": "2fbe7011",
"metadata": {}, "metadata": {
"outputs": [], "ExecuteTime": {
"end_time": "2025-05-07T08:06:41.914323Z",
"start_time": "2025-05-07T08:06:41.911153Z"
}
},
"source": [ "source": [
"import asyncio\n", "import asyncio\n",
"import json\n", "import json\n",
@ -34,13 +37,12 @@
"import nest_asyncio\n", "import nest_asyncio\n",
"import requests\n", "import requests\n",
"from dotenv import load_dotenv\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.agent import Agent\n",
"from llama_stack_client.lib.agents.custom_tool import CustomTool\n", "from llama_stack_client.lib.agents.client_tool import ClientTool\n",
"from llama_stack_client.lib.agents.event_logger import EventLogger\n",
"from llama_stack_client.types import CompletionMessage\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.shared.tool_response_message import ToolResponseMessage\n",
"from llama_stack_client.types.tool_def_param import Parameter\n",
"\n", "\n",
"# Allow asyncio to run in Jupyter Notebook\n", "# Allow asyncio to run in Jupyter Notebook\n",
"nest_asyncio.apply()\n", "nest_asyncio.apply()\n",
@ -48,7 +50,9 @@
"HOST = \"localhost\"\n", "HOST = \"localhost\"\n",
"PORT = 8321\n", "PORT = 8321\n",
"MODEL_NAME = \"meta-llama/Llama-3.2-3B-Instruct\"\n" "MODEL_NAME = \"meta-llama/Llama-3.2-3B-Instruct\"\n"
] ],
"outputs": [],
"execution_count": 19
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -64,14 +68,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3,
"id": "b4b3300c", "id": "b4b3300c",
"metadata": {}, "metadata": {},
"outputs": [],
"source": [ "source": [
"load_dotenv()\n", "load_dotenv()\n",
"BRAVE_SEARCH_API_KEY = os.environ[\"BRAVE_SEARCH_API_KEY\"]\n" "BRAVE_SEARCH_API_KEY = os.environ[\"BRAVE_SEARCH_API_KEY\"]\n"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -85,10 +89,13 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4,
"id": "62271ed2", "id": "62271ed2",
"metadata": {}, "metadata": {
"outputs": [], "ExecuteTime": {
"end_time": "2025-05-07T07:49:34.783948Z",
"start_time": "2025-05-07T07:49:34.779274Z"
}
},
"source": [ "source": [
"class BraveSearch:\n", "class BraveSearch:\n",
" def __init__(self, api_key: str) -> None:\n", " def __init__(self, api_key: str) -> None:\n",
@ -120,7 +127,9 @@
" clean_response.append(cleaned)\n", " clean_response.append(cleaned)\n",
"\n", "\n",
" return {\"query\": query, \"top_k\": clean_response}\n" " return {\"query\": query, \"top_k\": clean_response}\n"
] ],
"outputs": [],
"execution_count": 10
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -134,12 +143,15 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5,
"id": "92e75cf8", "id": "92e75cf8",
"metadata": {}, "metadata": {
"outputs": [], "ExecuteTime": {
"end_time": "2025-05-07T08:04:34.126742Z",
"start_time": "2025-05-07T08:04:34.122085Z"
}
},
"source": [ "source": [
"class WebSearchTool(CustomTool):\n", "class WebSearchTool(ClientTool):\n",
" def __init__(self, api_key: str):\n", " def __init__(self, api_key: str):\n",
" self.api_key = api_key\n", " self.api_key = api_key\n",
" self.engine = BraveSearch(api_key)\n", " self.engine = BraveSearch(api_key)\n",
@ -150,6 +162,16 @@
" def get_description(self) -> str:\n", " def get_description(self) -> str:\n",
" return \"Search the web for a given query\"\n", " return \"Search the web for a given query\"\n",
"\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", " async def run_impl(self, query: str):\n",
" return await self.engine.search(query)\n", " return await self.engine.search(query)\n",
"\n", "\n",
@ -192,7 +214,9 @@
" f\" Description: {result.get('description', 'No Description')}\\n\\n\"\n", " f\" Description: {result.get('description', 'No Description')}\\n\\n\"\n",
" )\n", " )\n",
" return formatted_result\n" " return formatted_result\n"
] ],
"outputs": [],
"execution_count": 17
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -206,16 +230,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6,
"id": "aaf5664f", "id": "aaf5664f",
"metadata": {}, "metadata": {
"outputs": [], "ExecuteTime": {
"end_time": "2025-05-07T07:49:58.145145Z",
"start_time": "2025-05-07T07:49:58.142695Z"
}
},
"source": [ "source": [
"async def execute_search(query: str):\n", "async def execute_search(query: str):\n",
" web_search_tool = WebSearchTool(api_key=BRAVE_SEARCH_API_KEY)\n", " web_search_tool = WebSearchTool(api_key=BRAVE_SEARCH_API_KEY)\n",
" result = await web_search_tool.run_impl(query)\n", " result = await web_search_tool.run_impl(query)\n",
" print(\"Search Results:\", result)\n" " print(\"Search Results:\", result)\n"
] ],
"outputs": [],
"execution_count": 12
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -227,22 +256,27 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7,
"id": "5f22c4e2", "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": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "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": [ "execution_count": 13
"query = \"Latest developments in quantum computing\"\n",
"asyncio.run(execute_search(query))\n"
]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -256,30 +290,8 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15,
"id": "9e704b01-f410-492f-8baf-992589b82803", "id": "9e704b01-f410-492f-8baf-992589b82803",
"metadata": {}, "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": [ "source": [
"async def run_main(disable_safety: bool = False):\n", "async def run_main(disable_safety: bool = False):\n",
" # Initialize the Llama Stack client with the specified base URL\n", " # Initialize the Llama Stack client with the specified base URL\n",
@ -325,13 +337,15 @@
" )\n", " )\n",
"\n", "\n",
" # Log and print the response from the agent asynchronously\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", " log.print()\n",
"\n", "\n",
"\n", "\n",
"# Run the function asynchronously in a Jupyter Notebook cell\n", "# Run the function asynchronously in a Jupyter Notebook cell\n",
"await run_main(disable_safety=True)\n" "await run_main(disable_safety=True)\n"
] ],
"outputs": [],
"execution_count": null
} }
], ],
"metadata": { "metadata": {