Skip to content

Commit b3bec17

Browse files
committed
Add search_courses_tool to demonstrate catalog view + RAG pattern
The notebook mentioned that the agent could 'search the full catalog' but didn't provide any tool to do so. Added a search_courses_tool that the agent can use to retrieve detailed course information when needed, demonstrating the pattern of using a high-level overview (catalog view) combined with on-demand detailed retrieval (RAG).
1 parent 3cc032a commit b3bec17

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

python-recipes/context-engineering/notebooks/section-4-optimizations/05_crafting_data_for_llms.ipynb

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,42 @@
365365
"\n",
366366
"catalog_view = redis_client.get(\"course_catalog_view\") or \"\"\n",
367367
"\n",
368+
"# Define a tool for searching courses\n",
369+
"from langchain_core.tools import tool\n",
370+
"\n",
371+
"@tool\n",
372+
"async def search_courses_tool(query: str, limit: int = 5) -> str:\n",
373+
" \"\"\"Search for courses by semantic similarity to the query.\n",
374+
" \n",
375+
" Args:\n",
376+
" query: Natural language description of what courses to find\n",
377+
" limit: Maximum number of courses to return (default: 5)\n",
378+
" \n",
379+
" Returns:\n",
380+
" Formatted string with course details\n",
381+
" \"\"\"\n",
382+
" courses = await course_manager.search_courses(query=query, limit=limit)\n",
383+
" if not courses:\n",
384+
" return \"No courses found matching that query.\"\n",
385+
" \n",
386+
" result = []\n",
387+
" for course in courses:\n",
388+
" result.append(f\"\"\"Course: {course.course_code} - {course.title}\n",
389+
"Department: {course.department}\n",
390+
"Description: {course.description}\n",
391+
"Credits: {course.credits} | Difficulty: {course.difficulty_level}\n",
392+
"Format: {course.format}\"\"\")\n",
393+
" return \"\\n\\n\".join(result)\n",
394+
"\n",
395+
"# Bind the tool to the LLM\n",
396+
"llm_with_tools = llm.bind_tools([search_courses_tool])\n",
397+
"\n",
368398
"system_prompt = f\"\"\"You are a class scheduling agent for Redis University.\n",
369399
"\n",
370400
"{catalog_view}\n",
371401
"\n",
372402
"Use this overview to help students understand what's available.\n",
373-
"For specific course details, you can search the full catalog.\n",
403+
"For specific course details, use the search_courses_tool to find detailed information.\n",
374404
"\"\"\"\n",
375405
"\n",
376406
"user_query = \"What departments offer courses? I'm interested in computer science.\"\n",
@@ -380,11 +410,13 @@
380410
" HumanMessage(content=user_query)\n",
381411
"]\n",
382412
"\n",
383-
"response = llm.invoke(messages)\n",
413+
"response = llm_with_tools.invoke(messages)\n",
384414
"\n",
385415
"print(f\"User: {user_query}\")\n",
386416
"print(f\"\\nAgent: {response.content}\")\n",
387-
"print(\"\\n✅ Agent has high-level overview of entire catalog!\")"
417+
"if response.tool_calls:\n",
418+
" print(f\"\\n🔧 Agent wants to use tools: {[tc['name'] for tc in response.tool_calls]}\")\n",
419+
"print(\"\\n✅ Agent has high-level overview and can search for details!\")"
388420
]
389421
},
390422
{

0 commit comments

Comments
 (0)