feat: raise configurable chat tool call limit
This commit is contained in:
@@ -218,8 +218,9 @@ def test_sse_persists_messages_and_tool_failure(session_factory, monkeypatch):
|
||||
def tool_call(self, messages, tools):
|
||||
calls.append(messages)
|
||||
if len(calls) == 1:
|
||||
return {"role": "assistant", "content": None, "tool_calls": [{"id": "call1", "type": "function",
|
||||
"function": {"name": "list_courses", "arguments": "{}"}}]}
|
||||
return {"role": "assistant", "content": None, "tool_calls": [
|
||||
{"id": f"call{index}", "type": "function",
|
||||
"function": {"name": "list_courses", "arguments": "{}"}} for index in range(9)]}
|
||||
return {"role": "assistant", "content": "已列出课程版本。", "tool_calls": []}
|
||||
monkeypatch.setattr(chat_api, "client_for_chat", lambda db: FakeClient())
|
||||
def override_db():
|
||||
@@ -231,9 +232,10 @@ def test_sse_persists_messages_and_tool_failure(session_factory, monkeypatch):
|
||||
created = client.post("/api/conversations", json={"title": "测试对话"}).json()
|
||||
with client.stream("POST", f"/api/conversations/{created['id']}/messages", json={"content": "列出课程"}) as response:
|
||||
body = "".join(response.iter_text())
|
||||
assert "event: tool" in body and "event: message" in body
|
||||
assert body.count("event: tool") == 9 and "event: message" in body
|
||||
restored = client.get(f"/api/conversations/{created['id']}").json()
|
||||
assert [m["role"] for m in restored["messages"]] == ["user", "assistant"]
|
||||
assert len(restored["messages"][1]["tool_calls"]) == 9
|
||||
assert restored["runs"][0]["status"] == "completed"
|
||||
monkeypatch.setattr(chat_api, "client_for_chat", lambda db: (_ for _ in ()).throw(ModelError("不支持工具调用")))
|
||||
with client.stream("POST", f"/api/conversations/{created['id']}/messages", json={"content": "再试"}) as response:
|
||||
@@ -251,6 +253,35 @@ def test_sse_persists_messages_and_tool_failure(session_factory, monkeypatch):
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
|
||||
def test_failed_tool_calls_count_toward_limit(monkeypatch):
|
||||
class RepeatingClient:
|
||||
def tool_call(self, messages, tools):
|
||||
return {"role": "assistant", "content": None, "tool_calls": [
|
||||
{"id": f"call{len(messages)}", "type": "function",
|
||||
"function": {"name": "unknown_tool", "arguments": "{}"}}]}
|
||||
|
||||
conversation = SimpleNamespace(id=1, course_version_id=None, batch_id=None, project_id=None, updated_at=None)
|
||||
run = SimpleNamespace(id=1, status="running", error=None, finished_at=None)
|
||||
class FakeSession:
|
||||
def __enter__(self):
|
||||
return self
|
||||
def __exit__(self, *_args):
|
||||
pass
|
||||
def get(self, model, _id):
|
||||
return conversation if model is Conversation else run
|
||||
def scalars(self, _query):
|
||||
return SimpleNamespace(all=lambda: [])
|
||||
def commit(self):
|
||||
pass
|
||||
|
||||
monkeypatch.setattr(chat_api, "client_for_chat", lambda db: RepeatingClient())
|
||||
monkeypatch.setattr(chat_api, "get_settings", lambda: SimpleNamespace(chat_max_tool_calls=9))
|
||||
body = "".join(chat_api._run_agent(FakeSession, 1, 1, "循环调用工具"))
|
||||
assert body.count("event: tool") == 9
|
||||
assert "本次消息工具调用超过上限 9 次" in body
|
||||
assert run.status == "failed"
|
||||
|
||||
|
||||
def test_native_tool_calls_unsupported_endpoint():
|
||||
transport = httpx.MockTransport(lambda request: httpx.Response(400, json={"error": "unsupported"}))
|
||||
client = DeepSeekClient(api_key="test-key", transport=transport)
|
||||
|
||||
@@ -10,6 +10,7 @@ from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from .actions import action_dict, confirm, prepare, require_scope
|
||||
from .agent_tools import execute, tool_specs
|
||||
from .config import get_settings
|
||||
from .db import get_db
|
||||
from .llm import ModelError
|
||||
from .models import AgentRun, ChatMessage, Conversation, PendingAction, Project, ScoringBatch, now
|
||||
@@ -113,7 +114,10 @@ def _run_agent(factory, conversation_id, run_id, user_text):
|
||||
"project_id": conversation.project_id}, ensure_ascii=False)}]
|
||||
messages.extend({"role": m.role, "content": m.content[:4000]} for m in history)
|
||||
yield _event("run", {"id": run.id, "status": "running"})
|
||||
for round_number in range(4):
|
||||
max_tool_calls = get_settings().chat_max_tool_calls
|
||||
attempted_calls = 0
|
||||
# One extra model response lets the agent answer after its last allowed tool call.
|
||||
for _ in range(max_tool_calls + 1):
|
||||
reply = client.tool_call(messages, tool_specs())
|
||||
calls = reply.get("tool_calls") or []
|
||||
if not calls:
|
||||
@@ -121,16 +125,17 @@ def _run_agent(factory, conversation_id, run_id, user_text):
|
||||
if not isinstance(content, str) or not content.strip():
|
||||
raise ModelError("对话模型未返回文字或工具调用")
|
||||
db.add(ChatMessage(conversation_id=conversation_id, role="assistant", content=content,
|
||||
citations=citations[:20], tool_calls=used[:8]))
|
||||
citations=citations[:20], tool_calls=used))
|
||||
run.status, run.finished_at = "completed", now()
|
||||
conversation.updated_at = now()
|
||||
db.commit()
|
||||
completed = True
|
||||
yield _event("message", {"content": content, "citations": citations[:20], "tool_calls": used[:8]})
|
||||
yield _event("message", {"content": content, "citations": citations[:20], "tool_calls": used})
|
||||
yield _event("done", {"run_id": run.id})
|
||||
return
|
||||
if len(calls) + len(used) > 8:
|
||||
raise ModelError("单轮工具调用超过上限 8 次")
|
||||
if attempted_calls + len(calls) > max_tool_calls:
|
||||
raise ModelError(f"本次消息工具调用超过上限 {max_tool_calls} 次")
|
||||
attempted_calls += len(calls)
|
||||
messages.append({"role": "assistant", "content": reply.get("content"), "tool_calls": calls})
|
||||
for call in calls:
|
||||
name = call.get("function", {}).get("name", "")
|
||||
@@ -155,7 +160,7 @@ def _run_agent(factory, conversation_id, run_id, user_text):
|
||||
messages.append({"role": "tool", "tool_call_id": call_id,
|
||||
"content": json.dumps({"error": error[:500]}, ensure_ascii=False)})
|
||||
yield _event("tool", {"name": name, "status": "failed", "error": error[:500]})
|
||||
raise ModelError("模型工具调用轮数超过上限 4 次")
|
||||
raise ModelError(f"本次消息模型调用超过上限 {max_tool_calls + 1} 轮")
|
||||
except Exception as exc:
|
||||
run.status, run.error, run.finished_at = "failed", str(exc)[:500], now()
|
||||
db.commit()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
from pydantic import Field
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
@@ -23,6 +24,7 @@ class Settings(BaseSettings):
|
||||
model_timeout_seconds: int = 120
|
||||
model_concurrency: int = 2
|
||||
model_retries: int = 2
|
||||
chat_max_tool_calls: int = Field(default=32, ge=1, le=64)
|
||||
|
||||
|
||||
@lru_cache
|
||||
|
||||
Reference in New Issue
Block a user