Skip to content

Commit a0680f1

Browse files
committed
Change to get_course_details tool that retrieves by course code
Replaced semantic search tool with a get_course_details tool that: - Takes a list of course codes (not natural language queries) - Can retrieve multiple courses in one call - Returns detailed information including prerequisites and instructor - Works with the catalog overview as a 'map' to find course codes
1 parent b3bec17 commit a0680f1

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -365,42 +365,48 @@
365365
"\n",
366366
"catalog_view = redis_client.get(\"course_catalog_view\") or \"\"\n",
367367
"\n",
368-
"# Define a tool for searching courses\n",
368+
"# Define a tool for retrieving course details by course code\n",
369369
"from langchain_core.tools import tool\n",
370+
"from typing import List\n",
370371
"\n",
371372
"@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",
373+
"async def get_course_details(course_codes: List[str]) -> str:\n",
374+
" \"\"\"Get detailed information about one or more courses by their course codes.\n",
374375
" \n",
375376
" Args:\n",
376-
" query: Natural language description of what courses to find\n",
377-
" limit: Maximum number of courses to return (default: 5)\n",
377+
" course_codes: List of course codes (e.g., ['CS101', 'MATH201'])\n",
378378
" \n",
379379
" Returns:\n",
380-
" Formatted string with course details\n",
380+
" Formatted string with detailed course information\n",
381381
" \"\"\"\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",
382+
" if not course_codes:\n",
383+
" return \"No course codes provided.\"\n",
385384
" \n",
386385
" result = []\n",
387-
" for course in courses:\n",
388-
" result.append(f\"\"\"Course: {course.course_code} - {course.title}\n",
386+
" for code in course_codes:\n",
387+
" course = await course_manager.get_course_by_code(code)\n",
388+
" if course:\n",
389+
" result.append(f\"\"\"Course: {course.course_code} - {course.title}\n",
389390
"Department: {course.department}\n",
390391
"Description: {course.description}\n",
391392
"Credits: {course.credits} | Difficulty: {course.difficulty_level}\n",
392-
"Format: {course.format}\"\"\")\n",
393+
"Format: {course.format}\n",
394+
"Instructor: {course.instructor}\n",
395+
"Prerequisites: {', '.join([p.course_code for p in course.prerequisites]) if course.prerequisites else 'None'}\"\"\")\n",
396+
" else:\n",
397+
" result.append(f\"Course {code}: Not found\")\n",
398+
" \n",
393399
" return \"\\n\\n\".join(result)\n",
394400
"\n",
395401
"# Bind the tool to the LLM\n",
396-
"llm_with_tools = llm.bind_tools([search_courses_tool])\n",
402+
"llm_with_tools = llm.bind_tools([get_course_details])\n",
397403
"\n",
398404
"system_prompt = f\"\"\"You are a class scheduling agent for Redis University.\n",
399405
"\n",
400406
"{catalog_view}\n",
401407
"\n",
402408
"Use this overview to help students understand what's available.\n",
403-
"For specific course details, use the search_courses_tool to find detailed information.\n",
409+
"When students ask about specific courses, use the get_course_details tool with the course codes from the overview above.\n",
404410
"\"\"\"\n",
405411
"\n",
406412
"user_query = \"What departments offer courses? I'm interested in computer science.\"\n",

0 commit comments

Comments
 (0)