|
365 | 365 | "\n", |
366 | 366 | "catalog_view = redis_client.get(\"course_catalog_view\") or \"\"\n", |
367 | 367 | "\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", |
368 | 398 | "system_prompt = f\"\"\"You are a class scheduling agent for Redis University.\n", |
369 | 399 | "\n", |
370 | 400 | "{catalog_view}\n", |
371 | 401 | "\n", |
372 | 402 | "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", |
374 | 404 | "\"\"\"\n", |
375 | 405 | "\n", |
376 | 406 | "user_query = \"What departments offer courses? I'm interested in computer science.\"\n", |
|
380 | 410 | " HumanMessage(content=user_query)\n", |
381 | 411 | "]\n", |
382 | 412 | "\n", |
383 | | - "response = llm.invoke(messages)\n", |
| 413 | + "response = llm_with_tools.invoke(messages)\n", |
384 | 414 | "\n", |
385 | 415 | "print(f\"User: {user_query}\")\n", |
386 | 416 | "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!\")" |
388 | 420 | ] |
389 | 421 | }, |
390 | 422 | { |
|
0 commit comments