Skip to content

Commit 68710cb

Browse files
author
料料
committed
create agent
1 parent 679431b commit 68710cb

File tree

5 files changed

+747
-1
lines changed

5 files changed

+747
-1
lines changed

alias/src/alias/agent/agents/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from alias.agent.agents._alias_agent_base import AliasAgentBase
3+
from alias.agent.agents._qa_agent import QAAgent
34
from alias.agent.agents._meta_planner import MetaPlanner
45
from alias.agent.agents._browser_agent import BrowserAgent
56
from alias.agent.agents._react_worker import ReActWorker
@@ -11,6 +12,7 @@
1112
DataScienceAgent,
1213
init_ds_toolkit,
1314
)
15+
from alias.agent.agents._qa_agent import QAAgent
1416

1517
__all__ = [
1618
"AliasAgentBase",
@@ -19,6 +21,7 @@
1921
"ReActWorker",
2022
"DeepResearchAgent",
2123
"DataScienceAgent",
24+
"QAAgent",
2225
"init_ds_toolkit",
2326
"init_dr_toolkit",
2427
]

alias/src/alias/agent/agents/_alias_agent_base.py

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import json
44
import time
55
import traceback
6-
from typing import Any, Optional, Literal
6+
from typing import Any, List, Optional, Literal
77

88
from loguru import logger
99

@@ -24,6 +24,111 @@
2424

2525

2626
class AliasAgentBase(ReActAgent):
27+
@classmethod
28+
async def create(
29+
cls,
30+
name: str,
31+
model: str = 'qwen3-max',
32+
system_prompt: Optional[str] = None,
33+
tools: Optional[List[str]] = None,
34+
worker_full_toolkit: Optional[AliasToolkit] = None,
35+
use_long_term_memory_service: bool = False,
36+
) -> "AliasAgentBase":
37+
"""
38+
Create an AliasAgentBase instance with default configuration.
39+
40+
This is a convenience factory method that sets up the agent with
41+
appropriate defaults. Tools are registered via share_tools from
42+
worker_full_toolkit when both tools and worker_full_toolkit are provided.
43+
44+
Args:
45+
name: The unique identifier name for the agent instance.
46+
model: The model name (e.g., "qwen3-max", "qwen-vl-max").
47+
Must be a key in MODEL_FORMATTER_MAPPING from run.py.
48+
system_prompt: The system prompt. If None, uses default prompt.
49+
tools: List of tool names to register in the agent's toolkit.
50+
Used together with worker_full_toolkit; tools are copied via share_tools.
51+
worker_full_toolkit: Source toolkit containing all tools. When provided
52+
together with tools, the specified tools are shared into the agent's toolkit.
53+
use_long_term_memory_service: Whether to enable long-term memory service.
54+
55+
Returns:
56+
A configured AliasAgentBase instance.
57+
"""
58+
from agentscope.memory import InMemoryMemory
59+
from alias.agent.mock import MockSessionService
60+
from alias.agent.run import MODEL_FORMATTER_MAPPING
61+
from alias.agent.tools.share_tools import share_tools
62+
from datetime import datetime
63+
64+
time_str = datetime.now().strftime("%Y%m%d%H%M%S")
65+
if model not in MODEL_FORMATTER_MAPPING:
66+
raise ValueError(
67+
f"Unknown model name: {model}. "
68+
f"Available models: {list(MODEL_FORMATTER_MAPPING.keys())}",
69+
)
70+
71+
model_instance, formatter = MODEL_FORMATTER_MAPPING[model]
72+
session_service = MockSessionService(
73+
use_long_term_memory_service=use_long_term_memory_service,
74+
)
75+
76+
# Initialize long-term memory if enabled (same as run.py)
77+
long_term_memory = None
78+
if use_long_term_memory_service:
79+
from alias.server.clients.memory_client import MemoryClient
80+
from alias.agent.memory.longterm_memory import AliasLongTermMemory
81+
82+
if await MemoryClient.is_available():
83+
long_term_memory = AliasLongTermMemory(
84+
session_service=session_service,
85+
)
86+
logger.info(
87+
"Long-term memory service is available and initialized",
88+
)
89+
else:
90+
logger.warning(
91+
"use_long_term_memory_service is True, but memory "
92+
"service is not available. Long-term memory will not "
93+
"be used. Please check if the memory service is "
94+
"running.",
95+
)
96+
97+
# Build toolkit: use worker_full_toolkit's sandbox if provided, then share_tools
98+
if worker_full_toolkit is not None:
99+
toolkit = AliasToolkit(
100+
sandbox=worker_full_toolkit.sandbox,
101+
add_all=False,
102+
)
103+
if tools:
104+
# Validate that each requested tool exists in worker_full_toolkit
105+
for tool_name in tools:
106+
if tool_name not in worker_full_toolkit.tools:
107+
raise ValueError(
108+
f"Tool '{tool_name}' is not available in worker_full_toolkit. "
109+
)
110+
share_tools(worker_full_toolkit, toolkit, tools)
111+
logger.info(f"Shared tools into agent toolkit: {tools}")
112+
else:
113+
toolkit = AliasToolkit(sandbox=None, add_all=False)
114+
115+
# Create agent instance
116+
agent = cls(
117+
name=name,
118+
model=model_instance,
119+
formatter=formatter,
120+
memory=InMemoryMemory(),
121+
toolkit=toolkit,
122+
session_service=session_service,
123+
state_saving_dir=f"./agent-states/run-{time_str}",
124+
sys_prompt=system_prompt,
125+
max_iters=10,
126+
long_term_memory=long_term_memory,
127+
long_term_memory_mode="both",
128+
)
129+
130+
return agent
131+
27132
def __init__(
28133
self,
29134
name: str,

0 commit comments

Comments
 (0)