agentscope-ai / agentscope-ai/agentscope
[Bug]:agentscope对阿里的api适配及其不好经常报错
- 主要语言
- Python
- 星标
- 31.6k
- 派生
- 3.5k
- 平均合并
- 1 天 16 小时
- 30 天内合并 PR
- 103
描述
**Describe the bug**
我不知道是不是我代码问题,我调用ollama与deepseek接口就不会报错400,但是切换到阿里的api接口就经常这样报错400,在使用ollama与阿里api的时候我均使用过OpenAIChatModel、DashScopeChatModel、OllamaChatModel参数只有在使用阿里的api会报错400有时候调用工具返回的是xml格式。报错类型很多。
**To Reproduce**
Steps to reproduce the behavior:
def build_chat_model(model_config: dict) -> ChatModelBase:
"""根据配置创建对应的 ChatModel。"""
model_type = normalize_model_type(model_config)
generation_config = dict(model_config.get("generation_config", {}))
stream = model_config.get("stream", False)
base_url = _get_base_url(model_config)
thinking_config = _get_thinking_config(model_config, model_type)
if model_type == "openai":
client_kwargs = dict(model_config.get("client_kwargs", {}))
if base_url and "base_url" not in client_kwargs:
client_kwargs["base_url"] = base_url
model = OpenAIChatModel(
model_name=model_config["model_name"],
api_key=model_config.get("api_key"),
stream=stream,
reasoning_effort=thinking_config,
organization=model_config.get("organization"),
client_kwargs=client_kwargs or None,
generate_kwargs=generation_config,
)
elif model_type == "dashscope":
model = DashScopeChatModel(
model_name=model_config["model_name"],
api_key=model_config["api_key"],
stream=stream,
enable_thinking=thinking_config,
multimodality=model_config.get("multimodality"),
base_http_api_url=base_url,
generate_kwargs=generation_config,
)
elif model_type == "anthropic":
client_kwargs = dict(model_config.get("client_kwargs", {}))
max_tokens = model_config.get(
"max_tokens",
generation_config.get("max_tokens", 2048),
)
model = AnthropicChatModel(
model_name=model_config["model_name"],
api_key=model_config.get("api_key"),
max_tokens=max_tokens,
stream=stream,
thinking=thinking_config,
client_kwargs=client_kwargs or None,
generate_kwargs=generation_config,
)
elif model_type == "gemini":
client_kwargs = dict(model_config.get("client_kwargs", {}))
model = GeminiChatModel(
model_name=model_config["model_name"],
api_key=model_config["api_key"],
stream=stream,
thinking_config=thinking_config,
client_kwargs=client_kwargs or None,
generate_kwargs=generation_config,
)
else: # ollama
client_kwargs = dict(model_config.get("client_kwargs", {}))
# 禁用连接复用
if "limits" not in client_kwargs:
client_kwargs["limits"] = httpx.Limits(max_connections=1, max_keepalive_connections=0)
ollama_options = dict(model_config.get("options", {}))
# 映射 generation_config 到 ollama options
if "temperature" in generation_config and "temperature" not in ollama_options:
ollama_options["temperature"] = generation_config["temperature"]
if "max_tokens" in generation_config and "num_predict" not in ollama_options:
ollama_options["num_ctx"] = generation_config["max_tokens"]
model = OllamaChatModel(
model_name=model_config["model_name"],
stream=stream,
options=ollama_options or None,
keep_alive=model_config.get("keep_alive", "-1m"),
enable_thinking=thinking_config,
host=base_url,
client_kwargs=client_kwargs or None,
)
logger.info("[ModelFactory] 已按 %s 类型初始化模型", model_type)
return model
class CustomCompressionSummary(BaseModel):
"""自定义压缩摘要结构。"""
main_topic: str = Field(
max_length=200,
description="对话的主题"
)
key_points: str = Field(
max_length=400,
description="讨论的重要观点"
)
pending_tasks: str = Field(
max_length=200,
description="待完成的任务"
)
COMPRESSION_PROMPT = (
"请总结上述对话,并严格以 JSON 形式输出,"
"不要输出 Markdown、标题、代码块、xml或额外说明。"
"输出字段必须为 main_topic、key_points、pending_tasks。"
"对应的每个字段都必须是字符串类型,不要输出数组,整数,字典类型数据"
"重点关注主题、关键讨论点和待完成任务。"
)
SUMMARY_TEMPLATE = (
"对话摘要:\n"
"主题:{main_topic}\n\n"
"关键观点:\n{key_points}\n\n"
"待完成任务:\n{pending_tasks}"
""
)
def create_compression_config(
model_config: dict,
enable: bool = True,
trigger_threshold: int = 10000,
keep_recent: int = 3,
) -> ReActAgent.CompressionConfig:
"""创建 ReActAgent 的消息压缩配置。"""
token_counter = build_token_counter(model_config)
return ReActAgent.CompressionConfig(
enable=enable,
agent_token_counter=token_counter,
trigger_threshold=trigger_threshold,
keep_recent=keep_recent,
compression_prompt=COMPRESSION_PROMPT,
summary_template=SUMMARY_TEMPLATE,
summary_schema=CustomCompressionSummary,
)
def build_token_counter(model_config: dict) -> TokenCounterBase:
"""根据模型配置创建 token counter。"""
model_type = normalize_model_type(model_config)
if model_type == "openai":
return OpenAITokenCounter(model_name=model_config["model_name"])
if model_type == "anthropic":
return AnthropicTokenCounter(
model_name=model_config["model_name"],
api_key=model_config.get("api_key"),
**dict(model_config.get("client_kwargs", {})),
)
if model_type == "gemini":
return GeminiTokenCounter(
model_name=model_config["model_name"],
api_key=model_config.get("api_key"),
**dict(model_config.get("client_kwargs", {})),
)
# DashScope / Ollama: 优先使用 HuggingFace,否则回退 CharTokenCounter
tokenizer_model_name = model_config.get("tokenizer_model_name")
if tokenizer_model_name:
return _build_huggingface_token_counter(
{
"pretrained_model_name_or_path": tokenizer_model_name,
"use_mirror": model_config.get("use_mirror", False),
"use_fast": model_config.get("use_fast", False),
"trust_remote_code": model_config.get("trust_remote_code", False),
"kwargs": dict(model_config.get("tokenizer_kwargs", {})),
}
)
logger.warning(
"[ModelFactory] %s 类型未配置专用 token counter,已回退到 CharTokenCounter",
model_type,
)
return CharTokenCounter()
def build_agent(
self,
name: str = "Assistant",
sys_prompt: Optional[str] = None,
memory: Optional[MemoryBase] = None,
) -> ReActAgent:
"""
构建 Agent 实例
Args:
name: Agent 名称
sys_prompt: 自定义系统提示词(可选)
memory: Memory 实例(可选),默认使用初始化时创建的 memory
Returns:
ReActAgent: Agent 实例
"""
agent_memory = memory or self.memory
final_sys_prompt = sys_prompt or DEFAULT_AGENT_SYS_PROMPT
self.agent = ReActAgent(
name=name,
model=self.model,
formatter=build_chat_formatter(self.model_config),
toolkit=self.toolkit,
sys_prompt=final_sys_prompt,
memory=agent_memory,
max_iters=25,
parallel_tool_calls=False,
compression_config=self.create_compression_config(
enable=self.memory_config.get("enable_compression", True),
trigger_threshold=self.memory_config.get("compression_threshold", 10000),
keep_recent=self.memory_config.get("keep_recent_messages", 3)
)
)
logger.info(f"[AgentBuilder] Agent '{name}' 创建完成")
return self.agent
MODEL_CONFIG = {
"type": "dashscope",
"model_name": "glm-4.7",
"api_key": "sk-xxx",
"base_url": "https://dashscope.aliyuncs.com/api/v1",
"stream": True,
"thinking": True, # DashScope: bool
"generation_config": {
"temperature": 0.7,
"max_tokens": 131072,
}
}
以上是我的主要代码片段
**Error messages**
报错内容包括不限于:InternalError.Algo.InvalidParameter: The tool_choice parameter does not support being set to required or object in thinking mode
Environment :
AgentScope Version: [1.0.18]
Python Version: [3.10.0]
OS: [windows]
贡献指南
评估
这个 Issue 还没有评估数据。