-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathsearch_tools.py
More file actions
250 lines (200 loc) · 8.61 KB
/
Copy pathsearch_tools.py
File metadata and controls
250 lines (200 loc) · 8.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from abc import ABC, abstractmethod
from typing import Any, Dict, Optional, Protocol
from vector_store import SearchResults, VectorStore
class Tool(ABC):
"""Abstract base class for all tools"""
@abstractmethod
def get_tool_definition(self) -> Dict[str, Any]:
"""Return Anthropic tool definition for this tool"""
pass
@abstractmethod
def execute(self, **kwargs) -> str:
"""Execute the tool with given parameters"""
pass
class CourseSearchTool(Tool):
"""Tool for searching course content with semantic course name matching"""
def __init__(self, vector_store: VectorStore):
self.store = vector_store
self.last_sources = [] # Track sources from last search
self.last_source_links = [] # Track lesson links from last search
def get_tool_definition(self) -> Dict[str, Any]:
"""Return Anthropic tool definition for this tool"""
return {
"name": "search_course_content",
"description": "Search course materials with smart course name matching and lesson filtering",
"input_schema": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "What to search for in the course content",
},
"course_name": {
"type": "string",
"description": "Course title (partial matches work, e.g. 'MCP', 'Introduction')",
},
"lesson_number": {
"type": "integer",
"description": "Specific lesson number to search within (e.g. 1, 2, 3)",
},
},
"required": ["query"],
},
}
def execute(
self,
query: str,
course_name: Optional[str] = None,
lesson_number: Optional[int] = None,
) -> str:
"""
Execute the search tool with given parameters.
Args:
query: What to search for
course_name: Optional course filter
lesson_number: Optional lesson filter
Returns:
Formatted search results or error message
"""
# Use the vector store's unified search interface
results = self.store.search(
query=query, course_name=course_name, lesson_number=lesson_number
)
# Handle errors
if results.error:
return results.error
# Handle empty results
if results.is_empty():
filter_info = ""
if course_name:
filter_info += f" in course '{course_name}'"
if lesson_number:
filter_info += f" in lesson {lesson_number}"
return f"No relevant content found{filter_info}."
# Format and return results
return self._format_results(results)
def _format_results(self, results: SearchResults) -> str:
"""Format search results with course and lesson context"""
formatted = []
sources = [] # Track sources for the UI
source_links = [] # Track lesson links for the UI
for doc, meta in zip(results.documents, results.metadata):
course_title = meta.get("course_title", "unknown")
lesson_num = meta.get("lesson_number")
# Build context header
header = f"[{course_title}"
if lesson_num is not None:
header += f" - Lesson {lesson_num}"
header += "]"
# Track source for the UI
source = course_title
if lesson_num is not None:
source += f" - Lesson {lesson_num}"
sources.append(source)
# Get lesson link if available
lesson_link = None
if lesson_num is not None:
lesson_link = self.store.get_lesson_link(course_title, lesson_num)
source_links.append(lesson_link)
formatted.append(f"{header}\n{doc}")
# Store sources and links for retrieval
self.last_sources = sources
self.last_source_links = source_links
return "\n\n".join(formatted)
class CourseOutlineTool(Tool):
"""Tool for getting course outline and lesson structure"""
def __init__(self, vector_store: VectorStore):
self.store = vector_store
def get_tool_definition(self) -> Dict[str, Any]:
"""Return Anthropic tool definition for this tool"""
return {
"name": "get_course_outline",
"description": "Get course outline with title, link, and complete lesson list",
"input_schema": {
"type": "object",
"properties": {
"course_name": {
"type": "string",
"description": "Course title (partial matches work, e.g. 'MCP', 'Introduction')",
}
},
"required": ["course_name"],
},
}
def execute(self, course_name: str) -> str:
"""
Execute the outline tool to get course structure.
Args:
course_name: Course name to get outline for
Returns:
Formatted course outline or error message
"""
# Resolve course name using vector store's existing method
course_title = self.store._resolve_course_name(course_name)
if not course_title:
return f"No course found matching '{course_name}'"
# Get course metadata
try:
results = self.store.course_catalog.get(ids=[course_title])
if not results or not results["metadatas"]:
return f"Course metadata not found for '{course_title}'"
metadata = results["metadatas"][0]
# Parse lessons from JSON
import json
lessons_json = metadata.get("lessons_json")
if not lessons_json:
return f"No lesson information available for '{course_title}'"
lessons = json.loads(lessons_json)
# Format the outline
outline = []
outline.append(f"**Course Title:** {metadata.get('title', course_title)}")
outline.append(f"**Course Link:** {metadata.get('course_link', 'N/A')}")
outline.append(f"**Total Lessons:** {len(lessons)}")
outline.append("\n**Lesson Outline:**")
for lesson in lessons:
lesson_num = lesson.get("lesson_number", "N/A")
lesson_title = lesson.get("lesson_title", "N/A")
outline.append(f"Lesson {lesson_num}: {lesson_title}")
return "\n".join(outline)
except Exception as e:
return f"Error retrieving course outline: {str(e)}"
class ToolManager:
"""Manages available tools for the AI"""
def __init__(self):
self.tools = {}
def register_tool(self, tool: Tool):
"""Register any tool that implements the Tool interface"""
tool_def = tool.get_tool_definition()
tool_name = tool_def.get("name")
if not tool_name:
raise ValueError("Tool must have a 'name' in its definition")
self.tools[tool_name] = tool
def get_tool_definitions(self) -> list:
"""Get all tool definitions for Anthropic tool calling"""
return [tool.get_tool_definition() for tool in self.tools.values()]
def execute_tool(self, tool_name: str, **kwargs) -> str:
"""Execute a tool by name with given parameters"""
if tool_name not in self.tools:
return f"Tool '{tool_name}' not found"
return self.tools[tool_name].execute(**kwargs)
def get_last_sources(self) -> list:
"""Get sources from the last search operation"""
# Check all tools for last_sources attribute
for tool in self.tools.values():
if hasattr(tool, "last_sources") and tool.last_sources:
return tool.last_sources
return []
def get_last_source_links(self) -> list:
"""Get source links from the last search operation"""
# Check all tools for last_source_links attribute
for tool in self.tools.values():
if hasattr(tool, "last_source_links") and tool.last_source_links:
return tool.last_source_links
return []
def reset_sources(self):
"""Reset sources from all tools that track sources"""
for tool in self.tools.values():
if hasattr(tool, "last_sources"):
tool.last_sources = []
if hasattr(tool, "last_source_links"):
tool.last_source_links = []