feat: 新增xbot agent核心代码
新增agent/app/xbot模块,包含: - agent.py: agent核心逻辑 - config.py: 配置管理 - session.py: 会话管理 - memory.py: 记忆管理 - loop.py: 循环任务 - adapter.py: 适配器 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,9 +6,16 @@ from typing import Any, Optional
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from .loop import AgentLoop
|
from .loop import AgentLoop
|
||||||
from .memory import MemoryConsolidator
|
|
||||||
from .session import SessionManager
|
from .session import SessionManager
|
||||||
from .adapter import XBotLLMAdapter, LLMResponse
|
from .adapter import XBotLLMAdapter, LLMResponse
|
||||||
|
from . import config
|
||||||
|
|
||||||
|
# 尝试导入 simplemem
|
||||||
|
try:
|
||||||
|
from simplemem import SimpleMemSystem
|
||||||
|
HAS_SIMPLEMEM = True
|
||||||
|
except ImportError:
|
||||||
|
HAS_SIMPLEMEM = False
|
||||||
|
|
||||||
|
|
||||||
class SimpleToolRegistry:
|
class SimpleToolRegistry:
|
||||||
@@ -84,6 +91,8 @@ class XBotAgent:
|
|||||||
base_url: Optional[str] = None,
|
base_url: Optional[str] = None,
|
||||||
workspace: Optional[Path] = None,
|
workspace: Optional[Path] = None,
|
||||||
context_window_tokens: int = 200000,
|
context_window_tokens: int = 200000,
|
||||||
|
embedding_model: Optional[str] = None,
|
||||||
|
embedding_base_url: Optional[str] = None,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
初始化 XBot Agent
|
初始化 XBot Agent
|
||||||
@@ -101,9 +110,15 @@ class XBotAgent:
|
|||||||
self.name = name
|
self.name = name
|
||||||
self.role_description = role_description
|
self.role_description = role_description
|
||||||
|
|
||||||
# 创建工作目录
|
# 使用配置文件的默认值
|
||||||
|
if api_key is None:
|
||||||
|
api_key = config.API_KEY
|
||||||
|
if base_url is None:
|
||||||
|
base_url = config.BASE_URL
|
||||||
if workspace is None:
|
if workspace is None:
|
||||||
workspace = Path(os.getenv("XAGENT_WORKSPACE", "./xbot_workspace"))
|
workspace = Path(config.WORKSPACE)
|
||||||
|
|
||||||
|
# 创建工作目录
|
||||||
self.workspace = workspace
|
self.workspace = workspace
|
||||||
self.workspace.mkdir(parents=True, exist_ok=True)
|
self.workspace.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
@@ -130,14 +145,35 @@ class XBotAgent:
|
|||||||
# 创建会话管理器
|
# 创建会话管理器
|
||||||
self.sessions = SessionManager(self.workspace)
|
self.sessions = SessionManager(self.workspace)
|
||||||
|
|
||||||
# 创建内存压缩器
|
# 创建 SimpleMem 记忆系统
|
||||||
self.memory = MemoryConsolidator(
|
if HAS_SIMPLEMEM and api_key and config.ENABLE_SIMPLEMEM:
|
||||||
workspace=self.workspace,
|
# 使用配置文件的 embedding 设置
|
||||||
provider=self.provider,
|
emb_model = embedding_model or config.EMBEDDING_MODEL
|
||||||
model=model,
|
emb_base = embedding_base_url or config.EMBEDDING_BASE_URL or base_url
|
||||||
sessions=self.sessions,
|
|
||||||
context_window_tokens=context_window_tokens,
|
self.memory = SimpleMemSystem(
|
||||||
)
|
api_key=api_key,
|
||||||
|
base_url=emb_base,
|
||||||
|
model=model,
|
||||||
|
embedding_model=emb_model,
|
||||||
|
db_path=str(self.workspace / "memory_db"),
|
||||||
|
clear_db=False,
|
||||||
|
# 并行处理配置
|
||||||
|
enable_parallel_processing=config.ENABLE_PARALLEL_PROCESSING,
|
||||||
|
max_parallel_workers=config.MAX_PARALLEL_WORKERS,
|
||||||
|
enable_parallel_retrieval=config.ENABLE_PARALLEL_RETRIEVAL,
|
||||||
|
max_retrieval_workers=config.MAX_RETRIEVAL_WORKERS,
|
||||||
|
enable_planning=config.ENABLE_PLANNING,
|
||||||
|
enable_reflection=config.ENABLE_REFLECTION,
|
||||||
|
max_reflection_rounds=config.MAX_REFLECTION_ROUNDS,
|
||||||
|
)
|
||||||
|
self._use_simplemem = True
|
||||||
|
print(f"SimpleMem initialized with embedding: {emb_model}, base_url: {emb_base}")
|
||||||
|
else:
|
||||||
|
self.memory = None
|
||||||
|
self._use_simplemem = False
|
||||||
|
if not api_key:
|
||||||
|
print("Warning: No API key provided, SimpleMem will be disabled")
|
||||||
|
|
||||||
def _register_default_tools(self) -> None:
|
def _register_default_tools(self) -> None:
|
||||||
"""注册默认工具"""
|
"""注册默认工具"""
|
||||||
@@ -191,6 +227,17 @@ class XBotAgent:
|
|||||||
|
|
||||||
请根据用户的问题回答,并使用 Markdown 格式输出。"""
|
请根据用户的问题回答,并使用 Markdown 格式输出。"""
|
||||||
|
|
||||||
|
# 如果使用 SimpleMem,检索相关记忆
|
||||||
|
memory_context = ""
|
||||||
|
if self._use_simplemem and self.memory:
|
||||||
|
try:
|
||||||
|
memory_context = self.memory.ask(user_input)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Memory retrieval error: {e}")
|
||||||
|
|
||||||
|
if memory_context:
|
||||||
|
system_prompt += f"\n\n相关记忆:\n{memory_context}"
|
||||||
|
|
||||||
# 获取历史消息
|
# 获取历史消息
|
||||||
history = session.get_history(max_messages=50)
|
history = session.get_history(max_messages=50)
|
||||||
|
|
||||||
@@ -210,8 +257,14 @@ class XBotAgent:
|
|||||||
session.messages.append(m)
|
session.messages.append(m)
|
||||||
self.sessions.save(session)
|
self.sessions.save(session)
|
||||||
|
|
||||||
# 尝试内存压缩
|
# 保存到 SimpleMem 记忆
|
||||||
await self.memory.maybe_consolidate_by_tokens(session)
|
if self._use_simplemem and self.memory and final_content:
|
||||||
|
try:
|
||||||
|
self.memory.add_dialogue("User", user_input, datetime.now().isoformat())
|
||||||
|
self.memory.add_dialogue(self.name, final_content, datetime.now().isoformat())
|
||||||
|
self.memory.finalize()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Memory save error: {e}")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"content": final_content or "No response",
|
"content": final_content or "No response",
|
||||||
|
|||||||
74
agent/app/xbot/config.py
Normal file
74
agent/app/xbot/config.py
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
"""
|
||||||
|
XBot 配置文件
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ==================== LLM 配置 ====================
|
||||||
|
|
||||||
|
# 默认 LLM 提供商
|
||||||
|
DEFAULT_PROVIDER = "openai"
|
||||||
|
|
||||||
|
# 默认模型
|
||||||
|
DEFAULT_MODEL = "gpt-4"
|
||||||
|
|
||||||
|
# API Key(建议使用环境变量)
|
||||||
|
import os
|
||||||
|
API_KEY = os.getenv("OPENAI_API_KEY", "")
|
||||||
|
|
||||||
|
# Base URL
|
||||||
|
BASE_URL = os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1")
|
||||||
|
|
||||||
|
|
||||||
|
# ==================== SimpleMem 记忆配置 ====================
|
||||||
|
|
||||||
|
# 是否启用 SimpleMem
|
||||||
|
ENABLE_SIMPLEMEM = True
|
||||||
|
|
||||||
|
# Embedding 模型
|
||||||
|
# 推荐: text-embedding-3-small, text-embedding-3-large, text-embedding-ada-002
|
||||||
|
# 或使用 Qwen: Qwen/Qwen3-Embedding-0.6B
|
||||||
|
EMBEDDING_MODEL = os.getenv("EMBEDDING_MODEL", "text-embedding-3-small")
|
||||||
|
|
||||||
|
# Embedding 服务的 Base URL(可选,默认使用 BASE_URL)
|
||||||
|
EMBEDDING_BASE_URL = os.getenv("EMBEDDING_BASE_URL", "")
|
||||||
|
|
||||||
|
|
||||||
|
# ==================== 并行处理配置 ====================
|
||||||
|
|
||||||
|
# 是否启用并行处理
|
||||||
|
ENABLE_PARALLEL_PROCESSING = True
|
||||||
|
MAX_PARALLEL_WORKERS = 8
|
||||||
|
|
||||||
|
# 是否启用并行检索
|
||||||
|
ENABLE_PARALLEL_RETRIEVAL = True
|
||||||
|
MAX_RETRIEVAL_WORKERS = 4
|
||||||
|
|
||||||
|
# 是否启用规划
|
||||||
|
ENABLE_PLANNING = True
|
||||||
|
|
||||||
|
# 是否启用反思
|
||||||
|
ENABLE_REFLECTION = True
|
||||||
|
MAX_REFLECTION_ROUNDS = 2
|
||||||
|
|
||||||
|
|
||||||
|
# ==================== 工作目录 ====================
|
||||||
|
|
||||||
|
# 工作目录(用于存储会话和记忆)
|
||||||
|
WORKSPACE = os.getenv("XAGENT_WORKSPACE", "./xbot_workspace")
|
||||||
|
|
||||||
|
# 上下文窗口大小
|
||||||
|
CONTEXT_WINDOW_TOKENS = 200000
|
||||||
|
|
||||||
|
|
||||||
|
# ==================== Agent 配置 ====================
|
||||||
|
|
||||||
|
# 默认 Agent 配置
|
||||||
|
DEFAULT_AGENTS = {
|
||||||
|
1: {
|
||||||
|
"name": "数据分析助手",
|
||||||
|
"role_description": "你是一个专业的数据分析助手,擅长分析数据、生成报告。",
|
||||||
|
},
|
||||||
|
2: {
|
||||||
|
"name": "代码审查助手",
|
||||||
|
"role_description": "你是一个专业的代码审查助手,擅长审查代码、发现bug。",
|
||||||
|
},
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user