Implement forced tool selection in AIClient and OpenAIModel, enhancing tool invocation capabilities. Added methods for extracting forced tool names from user messages and updated logging to reflect forced tool usage. Improved error handling for timeout scenarios in message handling and model interactions, ensuring better user feedback and robustness.

This commit is contained in:
Mimikko-zeus
2026-03-03 14:25:21 +08:00
parent 7d7a4b8f54
commit 4a2666b1f2
5 changed files with 114 additions and 20 deletions

View File

@@ -37,3 +37,9 @@ def test_extract_forced_tool_name_ambiguous_prefix_returns_none():
forced = AIClient._extract_forced_tool_name(message, tools)
assert forced is None
def test_extract_prefix_limit_from_user_message():
assert AIClient._extract_prefix_limit("直接返回前100字") == 100
assert AIClient._extract_prefix_limit("前 256 字") == 256
assert AIClient._extract_prefix_limit("返回全文") is None

View File

@@ -313,9 +313,9 @@ def test_openai_model_falls_back_to_functions_for_legacy_sdk(monkeypatch):
sent = model.client.completions.last_params
assert model._supports_tools is False
assert model._supports_functions is True
assert sent["function_call"] == "auto"
assert isinstance(sent["functions"], list) and sent["functions"]
assert sent["functions"][0]["name"] == "demo_tool"
assert sent["tools"] == tools
assert sent["function_call"] is None
assert sent["functions"] is None
assert result.tool_calls is not None
assert result.tool_calls[0]["function"]["name"] == "demo_tool"
@@ -334,7 +334,8 @@ def test_openai_model_forces_function_call_for_legacy_sdk(monkeypatch):
)
sent = model.client.completions.last_params
assert sent["function_call"] == {"name": "demo_tool"}
assert sent["tool_choice"]["type"] == "function"
assert sent["tool_choice"]["function"]["name"] == "demo_tool"
def test_openai_model_formats_tool_messages_for_legacy_sdk(monkeypatch):