Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
feat(bot):delete invalid tools, update search\grep tool's description (#929)
yeshion233338231487e6a43
diff --git a/bot/README.md b/bot/README.mdindex 9ba3e0ba1..46708024e 100644--- a/bot/README.md+++ b/bot/README.md@@ -221,7 +221,7 @@ Vikingbot provides 7 dedicated OpenViking tools:| `openviking_add_resource` | Add local files as OpenViking resources || `openviking_grep` | Search OpenViking resources using regular expressions || `openviking_glob` | Match OpenViking resources using glob patterns |-| `user_memory_search` | Search OpenViking user memory |+| `openviking_memory_commit` | Commit session to ov |### OpenViking Hooksdiff --git a/bot/README_CN.md b/bot/README_CN.mdindex d9b130687..209ebea0a 100644--- a/bot/README_CN.md+++ b/bot/README_CN.md@@ -224,7 +224,7 @@ Vikingbot 提供 7 个专用的 OpenViking 工具:| `openviking_add_resource` | 添加本地文件为 OpenViking 资源 || `openviking_grep` | 使用正则表达式搜索 OpenViking 资源 || `openviking_glob` | 使用 glob 模式匹配 OpenViking 资源 |-| `user_memory_search` | 搜索 OpenViking 用户记忆 |+| `openviking_memory_commit` | 提交session到Openviking|### OpenViking 钩子diff --git a/bot/eval/locomo/judge.py b/bot/eval/locomo/judge.pyindex ae93cc8e0..0b2e171f6 100644--- a/bot/eval/locomo/judge.py+++ b/bot/eval/locomo/judge.py@@ -131,6 +131,17 @@ async def main():# 并发处理semaphore = asyncio.Semaphore(args.parallel)+ file_lock = asyncio.Lock() # 用于同步文件写入++ async def save_results():+ """保存当前所有结果到CSV文件,使用临时文件+原子替换避免文件损坏"""+ async with file_lock:+ temp_file = f"{args.input}.tmp"+ with open(temp_file, "w", encoding="utf-8", newline="") as f:+ writer = csv.DictWriter(f, fieldnames=fieldnames)+ writer.writeheader()+ writer.writerows(rows)+ os.replace(temp_file, args.input)async def process_row(idx):async with semaphore:@@ -142,6 +153,11 @@ async def main():is_correct, reasoning = await grade_answer(client, args.model, question, gold, response)row["result"] = "CORRECT" if is_correct else "WRONG"row["reasoning"] = reasoning++ # 处理完一条就立即保存结果+ await save_results()+ print(f"Saved result for {idx + 1}/{total}: {row['result']}")+return idx, rowtasks = [process_row(idx) for idx in ungraded]@@ -152,13 +168,7 @@ async def main():total_graded = sum(1 for row in rows if row.get("result"))accuracy = correct / total_graded if total_graded > 0 else 0.0print(f"\nGrading completed: {correct}/{total_graded} correct, accuracy: {accuracy:.2%}")-- # 写回CSV- with open(args.input, "w", encoding="utf-8", newline="") as f:- writer = csv.DictWriter(f, fieldnames=fieldnames)- writer.writeheader()- writer.writerows(rows)- print(f"Results saved to {args.input}")+ print(f"All results saved to {args.input}")if __name__ == "__main__":diff --git a/bot/vikingbot/agent/tools/ov_file.py b/bot/vikingbot/agent/tools/ov_file.pyindex a48ad477d..8ccfdaa73 100644--- a/bot/vikingbot/agent/tools/ov_file.py+++ b/bot/vikingbot/agent/tools/ov_file.py@@ -4,15 +4,12 @@ import asynciofrom abc import ABCfrom pathlib import Pathfrom typing import Any, Optional, Union-import jsonimport httpxfrom loguru import loggerfrom vikingbot.agent.tools.base import Tool, ToolContextfrom vikingbot.openviking_mount.ov_server import VikingClient-from vikingbot.providers.litellm_provider import LiteLLMProvider-from vikingbot.config.loader import load_configclass OVFileTool(Tool, ABC):@@ -34,7 +31,7 @@ class VikingListTool(OVFileTool):@propertydef description(self) -> str:- return "List resources in a OpenViking path."+ return "List resources in a OpenViking folder path."@propertydef parameters(self) -> dict[str, Any]:@@ -88,7 +85,9 @@ class VikingSearchTool(OVFileTool):@propertydef description(self) -> str:- return "Search for resources in OpenViking using a query"+ return ("Using query to search for resources (knowledge, code, files, workflow, etc.) in OpenViking. "+ "This operation performs semantic retrieval, not full character matching. Please avoid repeated calls with similar queries as much as possible."+ "bad-case: after searching with ‘Nate Joanna dog playdate 3:00 pm', another search was performed using 'Nate Joanna dog playdate'.")@propertydef parameters(self) -> dict[str, Any]:@@ -194,7 +193,8 @@ class VikingGrepTool(OVFileTool):@propertydef description(self) -> str:- return "Search Viking resources using regex patterns (like grep). Supports multiple patterns to search concurrently."+ return ("Search Viking resources using regex patterns (like grep). Supports multiple patterns to search concurrently."+ "Please avoid repeated calls with similar queries as much as possible.")@propertydef parameters(self) -> dict[str, Any]:@@ -349,38 +349,6 @@ class VikingGlobTool(OVFileTool):except Exception as e:return f"Error searching Viking with glob: {str(e)}"--class VikingSearchUserMemoryTool(OVFileTool):- """Tool to search Viking user memories"""-- @property- def name(self) -> str:- return "user_memory_search"-- @property- def description(self) -> str:- return "Search for user memories in OpenViking using a query."-- @property- def parameters(self) -> dict[str, Any]:- return {- "type": "object",- "properties": {"query": {"type": "string", "description": "The search query"}},- "required": ["query"],- }-- async def execute(self, tool_context: ToolContext, query: str, **kwargs: Any) -> str:- try:- client = await self._get_client(tool_context)- results = await client.search_user_memory(query, tool_context.sender_id)-- if not results:- return f"No results found for query: {query}"- return str(results)- except Exception as e:- return f"Error searching Viking: {str(e)}"--class VikingMemoryCommitTool(OVFileTool):"""Tool to commit messages to OpenViking session."""@@ -430,197 +398,6 @@ class VikingMemoryCommitTool(OVFileTool):logger.exception(f"Error processing message: {e}")return f"Error committing to Viking: {str(e)}"-class VikingUserProfileTool(OVFileTool):- """Tool to commit messages to OpenViking session."""-- @property- def name(self) -> str:- return "openviking_user_profile_read"-- @property- def description(self) -> str:- return "Read user's profile details from OpenViking."-- @property- def parameters(self) -> dict[str, Any]:- return {}-- async def execute(- self,- tool_context: ToolContext,- messages: list[dict[str, Any]],- **kwargs: Any,- ) -> str:- try:- if not tool_context.sender_id:- return "Error committed, sender_id is required."- client = await self._get_client(tool_context)- session_id = tool_context.session_key.safe_name()- profile = await client.read_user_profile(tool_context.sender_id, session_id)- return f"{profile}"- except Exception as e:- logger.exception(f"Error processing message: {e}")- return f"Error read user profile: {str(e)}"---class VikingSearchUserMemoryToolV2(OVFileTool):- """Tool to search Viking user memories with enhanced query expansion and ranking."""-- def __init__(self):- super().__init__()- self._llm_provider = None- self._config = load_config()-- @property- def name(self) -> str:- return "user_memory_search_v2"-- @property- def description(self) -> str:- return "Search for user memories in OpenViking using a query with enhanced query expansion and relevance ranking. Do not use the openviking_search tool to retrieve similar queries again."-- @property- def parameters(self) -> dict[str, Any]:- return {- "type": "object",- "properties": {- "query": {"type": "string", "description": "The search query"},- "top_k": {"type": "integer", "description": "Number of top results to return", "default": 10},- },- "required": ["query"],- }-- async def _get_llm_provider(self):- """Get or initialize the LLM provider."""- if self._llm_provider is None:- llm_config = self._config.agents- self._llm_provider = LiteLLMProvider(- api_key=llm_config.api_key,- api_base=llm_config.api_base,- default_model=llm_config.model,- extra_headers=llm_config.extra_headers if llm_config else None,- provider_name=llm_config.provider,- )- return self._llm_provider-- async def _generate_search_queries(self, original_query: str) -> list[str]:- """Generate multiple search queries from the original query using LLM."""- llm = await self._get_llm_provider()-- system_prompt = """You are an expert in generating search queries. Based on the user's original query, split it into 3–5 different query phrases to retrieve the user's personal memories.-The generated queries should cover the intent of the original query from different perspectives, including synonyms, related concepts, various expressions, etc., and be as concise as possible.-Please return only a JSON array with no extra content. Example:-["Query 1", "Query 2", "Query 3"]"""-- messages = [- {"role": "system", "content": system_prompt},- {"role": "user", "content": f"query: {original_query}\n"}- ]-- try:- response = await llm.chat(messages, temperature=0.3, max_tokens=512)- content = response.content.strip()- # 尝试解析JSON- try:- queries = json.loads(content)- if isinstance(queries, list) and all(isinstance(q, str) for q in queries):- # 去重并保留原始查询- queries = list(set([original_query] + queries))- return queries[:5] # 最多5个查询- except json.JSONDecodeError:- # 如果解析失败,尝试提取数组部分- import re- match = re.search(r'\[.*\]', content, re.DOTALL)- if match:- try:- queries = json.loads(match.group())- if isinstance(queries, list) and all(isinstance(q, str) for q in queries):- queries = list(set([original_query] + queries))- return queries[:5]- except:- pass-- # 如果所有解析都失败,返回原始查询- return [original_query]-- except Exception as e:- logger.warning(f"Failed to generate search queries: {e}")- return [original_query]-- async def execute(self, tool_context: ToolContext, query: str, top_k: int = 10, **kwargs: Any) -> str:- try:- # 1. 生成扩展查询列表- search_queries = await self._generate_search_queries(query)- logger.info(f"Generated search queries: {search_queries}")-- # 2. 并发调用搜索接口- client = await self._get_client(tool_context)- user_id = tool_context.sender_id-- if not user_id:- return "Error: sender_id is required for memory search."-- # 检查用户是否存在- user_exists = await client._check_user_exists(user_id)- if not user_exists:- return f"No user found for id: {user_id}"-- uri_user_memory = f"viking://user/{user_id}/memories/"-- # 并发搜索- search_tasks = [- client.search(q, target_uri=uri_user_memory)- for q in search_queries- ]- search_results = await asyncio.gather(*search_tasks, return_exceptions=True)-- # 3. 合并结果并去重- all_memories = {}- for result in search_results:- if isinstance(result, Exception):- logger.warning(f"Search failed: {result}")- continue-- memories = result.get("memories", []) + result.get("resources", [])- for memory in memories:- uri = memory.get("uri", "")- if not uri:- continue-- score = memory.get("score", 0.0)- if uri not in all_memories or score > all_memories[uri].get("score", 0.0):- all_memories[uri] = memory-- # 4. 按相似度得分排序- sorted_memories = sorted(- all_memories.values(),- key=lambda x: x.get("score", 0.0),- reverse=True- )[:top_k]-- # 5. 构造结果摘要- if not sorted_memories:- return f"No results found for query: {query}"-- result_lines = [f"Found {len(sorted_memories)} relevant memories:"]- for i, memory in enumerate(sorted_memories, 1):- title = memory.get("title", memory.get("uri", "Untitled"))- abstract = memory.get("abstract", "")- score = memory.get("score", 0.0)- uri = memory.get("uri", "")-- result_lines.append(f"\n{i}. {title} (score: {score:.4f})")- if abstract:- result_lines.append(f" Abstract: {abstract}")- result_lines.append(f" URI: {uri}")-- return "\n".join(result_lines)-- except Exception as e:- logger.exception(f"Error in VikingSearchUserMemoryToolV2: {e}")- return f"Error searching Viking user memory: {str(e)}"--class VikingMultiReadTool(OVFileTool):"""Tool to read content from multiple Viking resources concurrently."""diff --git a/bot/workspace/TOOLS.md b/bot/workspace/TOOLS.mdindex 479aae3d6..9e5f2ae5f 100644--- a/bot/workspace/TOOLS.md+++ b/bot/workspace/TOOLS.md@@ -24,11 +24,6 @@ openviking_list(uri: str, recursive: bool = False) -> str```List all resources at a specified path.-### Search User Memories-```-user_memory_search(query: str) -> str-```-Search for user-related memories and events.### ⚠️ CRITICAL: Commit Memories and Events```