-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathmoss_crewai.py
More file actions
317 lines (239 loc) · 10.4 KB
/
Copy pathmoss_crewai.py
File metadata and controls
317 lines (239 loc) · 10.4 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import asyncio
import uuid
from typing import Any
from crewai.tools import BaseTool
from moss import DocumentInfo, GetDocumentsOptions, MossClient, MutationOptions, QueryOptions
from pydantic import BaseModel, Field, PrivateAttr
class MossBaseTool(BaseTool):
"""Base class for all Moss tools. Handles shared client and sync wrapper."""
_client: Any = PrivateAttr()
def __init__(self, client: MossClient, **kwargs: Any) -> None:
super().__init__(**kwargs)
self._client = client
def _run(self, *args: Any, **kwargs: Any) -> str:
"""Synchronous execution — wraps _arun() for CrewAI's @abstractmethod requirement."""
try:
return asyncio.run(self._arun(*args, **kwargs))
except RuntimeError as e:
if "cannot be called from a running event loop" in str(e):
raise RuntimeError(
f"{self.__class__.__name__}._run() cannot be called from a "
"running event loop. Use async mode or call from a standard script."
) from e
raise
class MossSearchInput(BaseModel):
"""Input schema for MossSearchTool."""
query: str = Field(description="The search query text")
class MossSearchTool(MossBaseTool):
"""Semantic search tool powered by Moss."""
name: str = "moss_search"
description: str = (
"Search a knowledge base using Moss semantic search. "
"Returns the most relevant documents for a given query."
)
args_schema: type[BaseModel] = MossSearchInput
index_name: str = Field(description="Name of the Moss index to search")
top_k: int = Field(default=5, description="Number of results to return")
alpha: float = Field(
default=0.8,
description="Hybrid search balance (0=keyword, 1=semantic)",
)
_index_loaded: bool = PrivateAttr(default=False)
async def _ensure_loaded(self) -> None:
if not self._index_loaded:
await self._client.load_index(self.index_name)
self._index_loaded = True
async def _arun(self, query: str) -> str:
"""Async search against Moss index."""
await self._ensure_loaded()
results = await self._client.query(
self.index_name,
query,
QueryOptions(top_k=self.top_k, alpha=self.alpha),
)
if not results.docs:
return "No relevant information found."
return "\n\n".join(
f"Result {i + 1} (score: {doc.score:.2f}):\n{doc.text}"
for i, doc in enumerate(results.docs)
)
# --- Document Management ---
class MossAddDocsInput(BaseModel):
"""Input schema for MossAddDocsTool."""
texts: list[str] = Field(description="List of text documents to add")
ids: list[str] | None = Field(
default=None,
description="Optional document IDs (auto-generated if omitted)",
)
upsert: bool = Field(
default=False,
description="If True, update existing documents with the same ID instead of failing",
)
class MossAddDocsTool(MossBaseTool):
"""Add documents to a Moss index."""
name: str = "moss_add_docs"
description: str = "Add text documents to a Moss semantic search index."
args_schema: type[BaseModel] = MossAddDocsInput
index_name: str = Field(description="Name of the Moss index")
async def _arun(
self, texts: list[str], ids: list[str] | None = None, upsert: bool = False
) -> str:
"""Async add documents to Moss index."""
docs = [
DocumentInfo(
id=ids[i] if ids and i < len(ids) else str(uuid.uuid4()),
text=text,
)
for i, text in enumerate(texts)
]
options = MutationOptions(upsert=True) if upsert else None
await self._client.add_docs(self.index_name, docs, options)
action = "upserted" if upsert else "added"
return f"Successfully {action} {len(docs)} documents."
class MossDeleteDocsInput(BaseModel):
"""Input schema for MossDeleteDocsTool."""
doc_ids: list[str] = Field(description="List of document IDs to delete")
class MossDeleteDocsTool(MossBaseTool):
"""Delete documents from a Moss index by their IDs."""
name: str = "moss_delete_docs"
description: str = "Delete specific documents from a Moss index by their IDs."
args_schema: type[BaseModel] = MossDeleteDocsInput
index_name: str = Field(description="Name of the Moss index")
async def _arun(self, doc_ids: list[str]) -> str:
"""Async delete documents from Moss index."""
await self._client.delete_docs(self.index_name, doc_ids)
return f"Successfully deleted {len(doc_ids)} documents."
class MossGetDocsInput(BaseModel):
"""Input schema for MossGetDocsTool."""
doc_ids: list[str] | None = Field(
default=None,
description="Optional list of document IDs to retrieve. If omitted, returns all documents.",
)
class MossGetDocsTool(MossBaseTool):
"""Retrieve documents from a Moss index."""
name: str = "moss_get_docs"
description: str = (
"Retrieve documents from a Moss index. "
"Can fetch specific documents by ID or all documents."
)
args_schema: type[BaseModel] = MossGetDocsInput
index_name: str = Field(description="Name of the Moss index")
async def _arun(self, doc_ids: list[str] | None = None) -> str:
"""Async retrieve documents from Moss index."""
options = None
if doc_ids:
options = GetDocumentsOptions(doc_ids=doc_ids)
docs = await self._client.get_docs(self.index_name, options)
if not docs:
return "No documents found."
lines = []
for doc in docs:
text_preview = doc.text[:150] + "..." if len(doc.text) > 150 else doc.text
lines.append(f"[{doc.id}] {text_preview}")
return f"Retrieved {len(docs)} documents:\n" + "\n".join(lines)
# --- Index Management ---
class MossGetIndexInput(BaseModel):
"""Input schema for MossGetIndexTool."""
index_name: str = Field(description="Name of the index to get info for")
class MossGetIndexTool(MossBaseTool):
"""Get detailed information about a specific Moss index."""
name: str = "moss_get_index"
description: str = "Get information about a specific Moss index, including document count and status."
args_schema: type[BaseModel] = MossGetIndexInput
async def _arun(self, index_name: str) -> str:
"""Async get info about a specific Moss index."""
info = await self._client.get_index(index_name)
name = getattr(info, "name", index_name)
doc_count = getattr(info, "doc_count", "?")
status = getattr(info, "status", "?")
return f"Index '{name}': {doc_count} docs, status: {status}"
class MossListIndexesInput(BaseModel):
"""Input schema for MossListIndexesTool."""
pass # No input needed
class MossListIndexesTool(MossBaseTool):
"""List all available Moss indexes."""
name: str = "moss_list_indexes"
description: str = (
"List all available indexes in the Moss project with their details."
)
args_schema: type[BaseModel] = MossListIndexesInput
async def _arun(self) -> str:
"""Async list all Moss indexes."""
indexes = await self._client.list_indexes()
if not indexes:
return "No indexes found."
lines = []
for idx in indexes:
name = getattr(idx, "name", "unknown")
doc_count = getattr(idx, "doc_count", "?")
status = getattr(idx, "status", "?")
lines.append(f"- {name} ({doc_count} docs, status: {status})")
return "Indexes:\n" + "\n".join(lines)
class MossCreateIndexInput(BaseModel):
"""Input schema for MossCreateIndexTool."""
index_name: str = Field(description="Name for the new index")
texts: list[str] = Field(description="List of text documents to index")
ids: list[str] | None = Field(
default=None,
description="Optional document IDs (auto-generated if omitted)",
)
class MossCreateIndexTool(MossBaseTool):
"""Create a new Moss index with documents."""
name: str = "moss_create_index"
description: str = (
"Create a new Moss semantic search index and populate it with documents."
)
args_schema: type[BaseModel] = MossCreateIndexInput
async def _arun(
self, index_name: str, texts: list[str], ids: list[str] | None = None
) -> str:
"""Async create a new Moss index."""
docs = [
DocumentInfo(
id=ids[i] if ids and i < len(ids) else str(uuid.uuid4()),
text=text,
)
for i, text in enumerate(texts)
]
await self._client.create_index(index_name, docs)
return f"Successfully created index '{index_name}' with {len(docs)} documents."
class MossDeleteIndexInput(BaseModel):
"""Input schema for MossDeleteIndexTool."""
index_name: str = Field(description="Name of the index to delete")
class MossDeleteIndexTool(MossBaseTool):
"""Delete a Moss index and all its data."""
name: str = "moss_delete_index"
description: str = (
"Delete a Moss index and all its documents. This action is irreversible."
)
args_schema: type[BaseModel] = MossDeleteIndexInput
async def _arun(self, index_name: str) -> str:
"""Async delete a Moss index."""
await self._client.delete_index(index_name)
return f"Successfully deleted index '{index_name}'."
# --- Factory ---
def moss_tools(
client: MossClient,
index_name: str,
top_k: int = 5,
alpha: float = 0.8,
) -> list[BaseTool]:
"""Create all Moss tools with a shared MossClient.
Args:
client: A shared MossClient instance.
index_name: Default index name for document/search tools.
top_k: Default number of search results.
alpha: Hybrid search balance.
Returns: [search, add_docs, delete_docs, get_docs, get_index,
list_indexes, create_index, delete_index]
"""
return [
MossSearchTool(client=client, index_name=index_name, top_k=top_k, alpha=alpha),
MossAddDocsTool(client=client, index_name=index_name),
MossDeleteDocsTool(client=client, index_name=index_name),
MossGetDocsTool(client=client, index_name=index_name),
MossGetIndexTool(client=client),
MossListIndexesTool(client=client),
MossCreateIndexTool(client=client),
MossDeleteIndexTool(client=client),
]