feat: 完善模型能力识别与验证
- 自动识别模型类型、视觉、推理和工具能力并保留手动覆盖 - 使用 AgentScope 工具与视觉探测并统一管理端配置反馈
This commit is contained in:
@@ -15,9 +15,12 @@ import com.easyagents.agent.runtime.model.AgentModelSpec;
|
||||
import io.agentscope.core.message.ContentBlock;
|
||||
import io.agentscope.core.message.Msg;
|
||||
import io.agentscope.core.message.TextBlock;
|
||||
import io.agentscope.core.message.ToolUseBlock;
|
||||
import io.agentscope.core.model.ChatResponse;
|
||||
import io.agentscope.core.model.ExecutionConfig;
|
||||
import io.agentscope.core.model.GenerateOptions;
|
||||
import io.agentscope.core.model.ToolChoice;
|
||||
import io.agentscope.core.model.ToolSchema;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -30,8 +33,12 @@ import tech.easyflow.common.web.exceptions.BusinessException;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 使用 AgentScope 真实运行链路验证 Chat Model 与 VLM 连通性。
|
||||
@@ -40,11 +47,13 @@ import java.util.Map;
|
||||
public class AgentScopeChatModelConnectivityVerifier implements ChatModelConnectivityVerifier {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(AgentScopeChatModelConnectivityVerifier.class);
|
||||
private static final String PROBE_TOOL_NAME = "easyflow_capability_probe";
|
||||
private static final String PROBE_NONCE_FIELD = "nonce";
|
||||
private static final String PROBE_IMAGE_FIELD = "imageCode";
|
||||
private static final String VERIFICATION_SYSTEM_PROMPT = "You are a model connectivity verification assistant.";
|
||||
/** 为未识别关闭思考扩展参数的推理模型保留足够的小额输出预算。 */
|
||||
private static final int MAX_TOKENS = 256;
|
||||
private static final int MAX_TOKENS = 128;
|
||||
private static final Duration DEFAULT_PHASE_TIMEOUT = Duration.ofSeconds(60);
|
||||
private static final String VERIFICATION_SYSTEM_PROMPT =
|
||||
"You are verifying model connectivity. Follow the user's instruction exactly.";
|
||||
|
||||
/** AgentScope 模型工厂。 */
|
||||
private final AgentModelFactory<io.agentscope.core.model.Model> modelFactory;
|
||||
@@ -52,12 +61,15 @@ public class AgentScopeChatModelConnectivityVerifier implements ChatModelConnect
|
||||
private final AgentScopeMessageAdapter messageAdapter;
|
||||
/** 单阶段最大等待时间。 */
|
||||
private final Duration phaseTimeout;
|
||||
/** 为每次工具能力探测生成随机校验值。 */
|
||||
private final Supplier<String> nonceSupplier;
|
||||
|
||||
/**
|
||||
* 使用生产运行时组件创建验证器。
|
||||
*/
|
||||
public AgentScopeChatModelConnectivityVerifier() {
|
||||
this(new AgentScopeModelFactory(), new AgentScopeMessageAdapter(), DEFAULT_PHASE_TIMEOUT);
|
||||
this(new AgentScopeModelFactory(), new AgentScopeMessageAdapter(), DEFAULT_PHASE_TIMEOUT,
|
||||
() -> UUID.randomUUID().toString());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,60 +83,122 @@ public class AgentScopeChatModelConnectivityVerifier implements ChatModelConnect
|
||||
AgentModelFactory<io.agentscope.core.model.Model> modelFactory,
|
||||
AgentScopeMessageAdapter messageAdapter,
|
||||
Duration phaseTimeout) {
|
||||
this.modelFactory = modelFactory;
|
||||
this.messageAdapter = messageAdapter;
|
||||
this.phaseTimeout = phaseTimeout;
|
||||
this(modelFactory, messageAdapter, phaseTimeout, () -> UUID.randomUUID().toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 依次验证非流式基础连接与流式响应能力。
|
||||
* 使用可控随机值创建验证器,供能力探测测试使用。
|
||||
*
|
||||
* @param modelFactory AgentScope 模型工厂
|
||||
* @param messageAdapter 消息适配器
|
||||
* @param phaseTimeout 单阶段最大等待时间
|
||||
* @param nonceSupplier 探测随机值生成器
|
||||
*/
|
||||
AgentScopeChatModelConnectivityVerifier(
|
||||
AgentModelFactory<io.agentscope.core.model.Model> modelFactory,
|
||||
AgentScopeMessageAdapter messageAdapter,
|
||||
Duration phaseTimeout,
|
||||
Supplier<String> nonceSupplier) {
|
||||
this.modelFactory = modelFactory;
|
||||
this.messageAdapter = messageAdapter;
|
||||
this.phaseTimeout = phaseTimeout;
|
||||
this.nonceSupplier = nonceSupplier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用一次非流式 Chat 请求优先同时验证连接、视觉与工具调用能力。
|
||||
*
|
||||
* <p>当兼容接口明确拒绝工具参数时,追加一次不带工具的连接兜底请求,
|
||||
* 避免将可用的普通对话模型误判为连接失败。</p>
|
||||
*
|
||||
* @param model 已补齐供应商默认配置的模型
|
||||
* @return 双阶段验证结果
|
||||
* @throws BusinessException 非流式基础连接失败时抛出
|
||||
* @return 连接和工具能力验证结果
|
||||
* @throws BusinessException 连接或视觉验证失败时抛出
|
||||
*/
|
||||
@Override
|
||||
public ChatModelVerificationResult verify(Model model) {
|
||||
AgentModelSpec modelSpec = AgentModelSpecMapper.fromModel(model);
|
||||
String effectiveHttpVersion = AgentHttpTransportProvider.resolveEffectivePolicy(
|
||||
modelSpec.getHttpVersionPolicy(), modelSpec.getBaseUrl()).name();
|
||||
AgentMessage verificationMessage = buildVerificationMessage(modelSpec.isSupportImage());
|
||||
String nonce = nonceSupplier.get();
|
||||
|
||||
try {
|
||||
verifyPhase(modelSpec, verificationMessage, false);
|
||||
} catch (BusinessException exception) {
|
||||
LOG.error("AgentScope model base connectivity verification failed, modelId={}, httpPolicy={}",
|
||||
model.getId(), effectiveHttpVersion, exception);
|
||||
throw exception;
|
||||
boolean supportTool = verifyProbeRequest(modelSpec, nonce);
|
||||
return ChatModelVerificationResult.passed(effectiveHttpVersion, supportTool);
|
||||
} catch (Exception exception) {
|
||||
LOG.error("AgentScope model base connectivity verification failed, modelId={}, httpPolicy={}",
|
||||
if (isToolCapabilityRejection(exception)) {
|
||||
LOG.info("Model endpoint rejected tool probe, fallback to plain connectivity, modelId={}",
|
||||
model.getId());
|
||||
try {
|
||||
verifyPlainRequest(modelSpec);
|
||||
return ChatModelVerificationResult.passed(effectiveHttpVersion, false);
|
||||
} catch (Exception fallbackException) {
|
||||
LOG.error("AgentScope model fallback connectivity verification failed, modelId={}, httpPolicy={}",
|
||||
model.getId(), effectiveHttpVersion, fallbackException);
|
||||
throw new BusinessException(
|
||||
400, 1, "模型连接验证失败,请查看后端日志", fallbackException);
|
||||
}
|
||||
}
|
||||
LOG.error("AgentScope model connectivity verification failed, modelId={}, httpPolicy={}",
|
||||
model.getId(), effectiveHttpVersion, exception);
|
||||
throw new BusinessException(400, 1, "模型基础连接验证失败,请查看后端日志", exception);
|
||||
}
|
||||
|
||||
try {
|
||||
verifyPhase(modelSpec, verificationMessage, true);
|
||||
return ChatModelVerificationResult.passed(effectiveHttpVersion);
|
||||
} catch (Exception exception) {
|
||||
LOG.warn("AgentScope model streaming verification failed, modelId={}, httpPolicy={}",
|
||||
model.getId(), effectiveHttpVersion, exception);
|
||||
return ChatModelVerificationResult.streamingUnavailable(effectiveHttpVersion);
|
||||
if (exception instanceof BusinessException businessException) {
|
||||
throw businessException;
|
||||
}
|
||||
throw new BusinessException(400, 1, "模型连接验证失败,请查看后端日志", exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行一次指定流式模式的模型请求并校验响应。
|
||||
* 执行携带无副作用工具 Schema 的能力探测请求。
|
||||
*
|
||||
* @param modelSpec 运行时模型声明
|
||||
* @param nonce 本次探测随机值
|
||||
* @return 模型是否正确返回指定工具调用
|
||||
* @throws BusinessException 响应为空或视觉识别失败时抛出
|
||||
*/
|
||||
private boolean verifyProbeRequest(AgentModelSpec modelSpec, String nonce) {
|
||||
AgentMessage message = buildProbeMessage(modelSpec.isSupportImage(), nonce);
|
||||
List<ChatResponse> responses = request(
|
||||
modelSpec,
|
||||
message,
|
||||
List.of(buildProbeToolSchema(modelSpec.isSupportImage())),
|
||||
new ToolChoice.Specific(PROBE_TOOL_NAME));
|
||||
boolean validToolCall = hasValidProbeToolCall(
|
||||
responses, nonce, modelSpec.isSupportImage());
|
||||
validateProbeResponse(modelSpec.isSupportImage(), responses, validToolCall);
|
||||
return validToolCall;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行不带工具的兼容性兜底请求。
|
||||
*
|
||||
* @param modelSpec 运行时模型声明
|
||||
* @throws BusinessException 响应为空或视觉识别失败时抛出
|
||||
*/
|
||||
private void verifyPlainRequest(AgentModelSpec modelSpec) {
|
||||
List<ChatResponse> responses = request(
|
||||
modelSpec,
|
||||
buildPlainVerificationMessage(modelSpec.isSupportImage()),
|
||||
List.of(),
|
||||
null);
|
||||
validateTextResponse(modelSpec.isSupportImage(), aggregateText(responses));
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用统一低成本参数执行一次非流式模型请求。
|
||||
*
|
||||
* @param modelSpec 运行时模型声明
|
||||
* @param verificationMessage 验证消息
|
||||
* @param stream 是否启用流式响应
|
||||
* @throws BusinessException 响应为空或 VLM 图片识别错误时抛出
|
||||
* @param tools 工具 Schema
|
||||
* @param toolChoice 工具选择策略
|
||||
* @return 模型响应片段
|
||||
*/
|
||||
private void verifyPhase(AgentModelSpec modelSpec,
|
||||
AgentMessage verificationMessage,
|
||||
boolean stream) {
|
||||
private List<ChatResponse> request(AgentModelSpec modelSpec,
|
||||
AgentMessage verificationMessage,
|
||||
List<ToolSchema> tools,
|
||||
ToolChoice toolChoice) {
|
||||
AgentGenerationOptions generationOptions = new AgentGenerationOptions();
|
||||
generationOptions.setStream(stream);
|
||||
generationOptions.setStream(false);
|
||||
generationOptions.setThinkingEnabled(false);
|
||||
disableOpenAiCompatibleThinking(modelSpec, generationOptions);
|
||||
generationOptions.setMaxTokens(MAX_TOKENS);
|
||||
@@ -132,24 +206,23 @@ public class AgentScopeChatModelConnectivityVerifier implements ChatModelConnect
|
||||
|
||||
List<Msg> messages = List.of(
|
||||
messageAdapter.toMsg(AgentMessage.text(
|
||||
AgentMessageRole.SYSTEM,
|
||||
VERIFICATION_SYSTEM_PROMPT)),
|
||||
AgentMessageRole.SYSTEM, VERIFICATION_SYSTEM_PROMPT)),
|
||||
messageAdapter.toMsg(verificationMessage));
|
||||
GenerateOptions requestOptions = GenerateOptions.builder()
|
||||
.stream(stream)
|
||||
GenerateOptions.Builder requestBuilder = GenerateOptions.builder()
|
||||
.stream(false)
|
||||
.maxTokens(MAX_TOKENS)
|
||||
.executionConfig(ExecutionConfig.builder()
|
||||
.timeout(phaseTimeout)
|
||||
.maxAttempts(1)
|
||||
.build())
|
||||
.build();
|
||||
List<ChatResponse> responses = agentScopeModel
|
||||
.stream(messages, List.of(), requestOptions)
|
||||
.build());
|
||||
if (toolChoice != null) {
|
||||
requestBuilder.toolChoice(toolChoice);
|
||||
}
|
||||
return agentScopeModel
|
||||
.stream(messages, tools, requestBuilder.build())
|
||||
.timeout(phaseTimeout)
|
||||
.collectList()
|
||||
.block(phaseTimeout.plusSeconds(1));
|
||||
String responseText = aggregateText(responses);
|
||||
validateResponse(modelSpec.isSupportImage(), responseText);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -182,19 +255,47 @@ public class AgentScopeChatModelConnectivityVerifier implements ChatModelConnect
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建文字模型或 VLM 的最小验证消息。
|
||||
* 创建同时验证连接、视觉和工具调用的消息。
|
||||
*
|
||||
* @param supportImage 是否验证图片理解能力
|
||||
* @return 验证消息
|
||||
* @param nonce 本次探测随机值
|
||||
* @return 工具能力探测消息
|
||||
*/
|
||||
private AgentMessage buildVerificationMessage(boolean supportImage) {
|
||||
private AgentMessage buildProbeMessage(boolean supportImage, String nonce) {
|
||||
String prompt = supportImage
|
||||
? "请调用 " + PROBE_TOOL_NAME + " 工具,将 nonce 设置为“" + nonce
|
||||
+ "”,并将图片中的验证码设置为 imageCode。不要直接回答。"
|
||||
: "请调用 " + PROBE_TOOL_NAME + " 工具,并将 nonce 设置为“" + nonce
|
||||
+ "”。不要直接回答。";
|
||||
return buildVerificationMessage(prompt, supportImage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建不带工具的兼容性兜底消息。
|
||||
*
|
||||
* @param supportImage 是否验证图片理解能力
|
||||
* @return 普通连接验证消息
|
||||
*/
|
||||
private AgentMessage buildPlainVerificationMessage(boolean supportImage) {
|
||||
String prompt = supportImage
|
||||
? "请直接输出图片中的内容,不要补充其他内容。"
|
||||
: "请直接回复“你好”,不要补充其他内容。";
|
||||
return buildVerificationMessage(prompt, supportImage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建文字或 VLM 验证消息。
|
||||
*
|
||||
* @param prompt 验证提示词
|
||||
* @param supportImage 是否附加验证图片
|
||||
* @return AgentScope 消息
|
||||
*/
|
||||
private AgentMessage buildVerificationMessage(String prompt, boolean supportImage) {
|
||||
if (!supportImage) {
|
||||
return AgentMessage.text(
|
||||
AgentMessageRole.USER,
|
||||
"请直接回复“你好”,不要补充其他内容。");
|
||||
return AgentMessage.text(AgentMessageRole.USER, prompt);
|
||||
}
|
||||
List<AgentContentBlock> blocks = new ArrayList<>();
|
||||
blocks.add(new AgentTextBlock("请直接输出图片中的内容,不要补充其他内容。"));
|
||||
blocks.add(new AgentTextBlock(prompt));
|
||||
AgentMediaBlock image = new AgentMediaBlock("image");
|
||||
image.setMimeType("image/png");
|
||||
image.setData(Base64.getEncoder().encodeToString(VlmVerificationImage.pngBytes()));
|
||||
@@ -205,6 +306,111 @@ public class AgentScopeChatModelConnectivityVerifier implements ChatModelConnect
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建只声明参数、不绑定执行逻辑的探测工具 Schema。
|
||||
*
|
||||
* @param supportImage 是否包含图片验证码参数
|
||||
* @return 探测工具 Schema
|
||||
*/
|
||||
private ToolSchema buildProbeToolSchema(boolean supportImage) {
|
||||
Map<String, Object> properties = new LinkedHashMap<>();
|
||||
properties.put(PROBE_NONCE_FIELD, Map.of(
|
||||
"type", "string",
|
||||
"description", "原样返回用户提供的 nonce"));
|
||||
List<String> required = new ArrayList<>();
|
||||
required.add(PROBE_NONCE_FIELD);
|
||||
if (supportImage) {
|
||||
properties.put(PROBE_IMAGE_FIELD, Map.of(
|
||||
"type", "string",
|
||||
"description", "图片中的验证码"));
|
||||
required.add(PROBE_IMAGE_FIELD);
|
||||
}
|
||||
return ToolSchema.builder()
|
||||
.name(PROBE_TOOL_NAME)
|
||||
.description("验证模型是否能生成结构化工具调用")
|
||||
.parameters(Map.of(
|
||||
"type", "object",
|
||||
"properties", properties,
|
||||
"required", required))
|
||||
.strict(false)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断响应是否包含参数正确的探测工具调用。
|
||||
*
|
||||
* @param responses AgentScope 响应片段
|
||||
* @param nonce 本次探测随机值
|
||||
* @param supportImage 是否同时验证图片
|
||||
* @return 工具名和参数均正确返回 true
|
||||
*/
|
||||
private boolean hasValidProbeToolCall(List<ChatResponse> responses,
|
||||
String nonce,
|
||||
boolean supportImage) {
|
||||
if (responses == null) {
|
||||
return false;
|
||||
}
|
||||
for (ChatResponse response : responses) {
|
||||
if (response == null || response.getContent() == null) {
|
||||
continue;
|
||||
}
|
||||
for (ContentBlock block : response.getContent()) {
|
||||
if (!(block instanceof ToolUseBlock toolUse)
|
||||
|| !PROBE_TOOL_NAME.equals(toolUse.getName())
|
||||
|| toolUse.getInput() == null
|
||||
|| !nonce.equals(String.valueOf(toolUse.getInput().get(PROBE_NONCE_FIELD)))) {
|
||||
continue;
|
||||
}
|
||||
if (!supportImage || VlmVerificationImage.VERIFICATION_CODE.equals(
|
||||
normalizeVerificationText(String.valueOf(
|
||||
toolUse.getInput().get(PROBE_IMAGE_FIELD))))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验探测响应是否足以证明连接和视觉能力。
|
||||
*
|
||||
* @param supportImage 是否验证图片理解能力
|
||||
* @param responses AgentScope 响应片段
|
||||
* @param validToolCall 是否包含正确工具调用
|
||||
* @throws BusinessException 响应为空或图片识别错误时抛出
|
||||
*/
|
||||
private void validateProbeResponse(boolean supportImage,
|
||||
List<ChatResponse> responses,
|
||||
boolean validToolCall) {
|
||||
if (validToolCall) {
|
||||
return;
|
||||
}
|
||||
String responseText = aggregateText(responses);
|
||||
if (responseText != null && !responseText.isBlank()) {
|
||||
validateTextResponse(supportImage, responseText);
|
||||
return;
|
||||
}
|
||||
if (hasAnyContent(responses) && !supportImage) {
|
||||
return;
|
||||
}
|
||||
throw new BusinessException("模型未返回有效内容");
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断模型是否返回任意内容块。
|
||||
*
|
||||
* @param responses AgentScope 响应片段
|
||||
* @return 存在内容块返回 true
|
||||
*/
|
||||
private boolean hasAnyContent(List<ChatResponse> responses) {
|
||||
if (responses == null) {
|
||||
return false;
|
||||
}
|
||||
return responses.stream()
|
||||
.filter(response -> response != null && response.getContent() != null)
|
||||
.anyMatch(response -> !response.getContent().isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* 聚合流式响应中的全部文本增量。
|
||||
*
|
||||
@@ -236,7 +442,7 @@ public class AgentScopeChatModelConnectivityVerifier implements ChatModelConnect
|
||||
* @param responseText 聚合后的响应文本
|
||||
* @throws BusinessException 响应为空或图片识别结果不匹配时抛出
|
||||
*/
|
||||
private void validateResponse(boolean supportImage, String responseText) {
|
||||
private void validateTextResponse(boolean supportImage, String responseText) {
|
||||
if (responseText == null || responseText.isBlank()) {
|
||||
throw new BusinessException("模型未返回有效内容");
|
||||
}
|
||||
@@ -278,4 +484,34 @@ public class AgentScopeChatModelConnectivityVerifier implements ChatModelConnect
|
||||
? normalized
|
||||
: normalized.substring(0, 160) + "...";
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断异常是否明确来自工具或工具选择参数不兼容。
|
||||
*
|
||||
* @param exception 模型调用异常
|
||||
* @return 工具参数被拒绝返回 true
|
||||
*/
|
||||
private boolean isToolCapabilityRejection(Throwable exception) {
|
||||
Throwable current = exception;
|
||||
while (current != null) {
|
||||
String message = current.getMessage();
|
||||
if (message != null) {
|
||||
String normalized = message.toLowerCase(Locale.ROOT);
|
||||
if (normalized.contains("tool_choice")
|
||||
|| normalized.contains("tool choice")
|
||||
|| normalized.contains("tool_call")
|
||||
|| normalized.contains("tool call")
|
||||
|| normalized.contains("tools parameter")
|
||||
|| normalized.contains("function calling")
|
||||
|| (normalized.contains("tools")
|
||||
&& (normalized.contains("unsupported")
|
||||
|| normalized.contains("not support")
|
||||
|| normalized.contains("invalid")))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
current = current.getCause();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,10 @@ import io.agentscope.core.message.ImageBlock;
|
||||
import io.agentscope.core.message.Msg;
|
||||
import io.agentscope.core.message.MsgRole;
|
||||
import io.agentscope.core.message.TextBlock;
|
||||
import io.agentscope.core.message.ToolUseBlock;
|
||||
import io.agentscope.core.model.ChatResponse;
|
||||
import io.agentscope.core.model.GenerateOptions;
|
||||
import io.agentscope.core.model.ToolChoice;
|
||||
import io.agentscope.core.model.ToolSchema;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
@@ -27,101 +29,102 @@ import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* AgentScope 双阶段模型连通性验证测试。
|
||||
* AgentScope 单次优先模型连接与工具能力验证测试。
|
||||
*/
|
||||
public class AgentScopeChatModelConnectivityVerifierTest {
|
||||
|
||||
/**
|
||||
* 验证非流式与流式阶段按顺序执行并返回通过状态。
|
||||
*/
|
||||
@Test
|
||||
public void shouldPassWhenBothPhasesReturnText() {
|
||||
RecordingModelFactory factory = new RecordingModelFactory(
|
||||
Flux.just(response("你好")),
|
||||
Flux.just(response("你"), response("好")));
|
||||
AgentScopeChatModelConnectivityVerifier verifier = verifier(factory);
|
||||
|
||||
ChatModelVerificationResult result = verifier.verify(model(false));
|
||||
|
||||
Assert.assertEquals(ModelVerificationStatus.PASSED, result.getStatus());
|
||||
Assert.assertEquals("HTTP_1_1", result.getEffectiveHttpVersion());
|
||||
Assert.assertEquals(List.of(false, true), factory.getFactoryStreams());
|
||||
Assert.assertEquals(List.of(false, true), factory.getRequestStreams());
|
||||
Assert.assertEquals(List.of(MsgRole.SYSTEM, MsgRole.USER), factory.getMessageRoles().get(0));
|
||||
Assert.assertEquals(List.of(false, false), factory.getEnableThinkingValues());
|
||||
Assert.assertEquals(List.of(false, false), factory.getChatTemplateThinkingValues());
|
||||
}
|
||||
private static final String TEST_NONCE = "probe-nonce";
|
||||
|
||||
/**
|
||||
* 验证基础连接通过而流式阶段失败时返回部分通过结果。
|
||||
* 验证一次请求正确返回工具调用时同时确认连接和工具能力。
|
||||
*/
|
||||
@Test
|
||||
public void shouldReturnPartialWhenStreamingPhaseFails() {
|
||||
public void shouldPassConnectionAndToolProbeInOneRequest() {
|
||||
RecordingModelFactory factory = new RecordingModelFactory(
|
||||
Flux.just(response("你好")),
|
||||
Flux.error(new IllegalStateException("stream failed")));
|
||||
Flux.just(toolResponse(TEST_NONCE, null)));
|
||||
|
||||
ChatModelVerificationResult result = verifier(factory).verify(model(false));
|
||||
|
||||
Assert.assertEquals(ModelVerificationStatus.PARTIAL, result.getStatus());
|
||||
Assert.assertEquals(ModelVerificationStatus.PASSED, result.getStatus());
|
||||
Assert.assertEquals(ModelVerificationStatus.PASSED, result.getNonStreaming());
|
||||
Assert.assertEquals(ModelVerificationStatus.FAILED, result.getStreaming());
|
||||
Assert.assertTrue(result.getMessage().contains("流式响应不可用"));
|
||||
Assert.assertEquals(ModelVerificationStatus.SKIPPED, result.getStreaming());
|
||||
Assert.assertEquals(Boolean.TRUE, result.getSupportTool());
|
||||
Assert.assertEquals("验证通过", result.getMessage());
|
||||
Assert.assertEquals(List.of(false), factory.getFactoryStreams());
|
||||
Assert.assertEquals(List.of(false), factory.getRequestStreams());
|
||||
Assert.assertEquals(List.of(1), factory.getToolCounts());
|
||||
Assert.assertTrue(factory.getToolChoices().get(0) instanceof ToolChoice.Specific);
|
||||
Assert.assertEquals(MsgRole.SYSTEM, factory.getMessageBatches().get(0).get(0).getRole());
|
||||
Assert.assertEquals(MsgRole.USER, factory.getMessageBatches().get(0).get(1).getRole());
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证基础连接失败时立即终止且返回业务失败。
|
||||
* 验证模型返回普通文本时连接通过但工具能力保持关闭。
|
||||
*/
|
||||
@Test
|
||||
public void shouldStopWhenNonStreamingPhaseFails() {
|
||||
public void shouldPassConnectionAndMarkToolUnsupportedWhenTextReturned() {
|
||||
RecordingModelFactory factory = new RecordingModelFactory(
|
||||
Flux.just(textResponse("你好")));
|
||||
|
||||
ChatModelVerificationResult result = verifier(factory).verify(model(false));
|
||||
|
||||
Assert.assertEquals(ModelVerificationStatus.PASSED, result.getStatus());
|
||||
Assert.assertEquals(Boolean.FALSE, result.getSupportTool());
|
||||
Assert.assertEquals(List.of(1), factory.getToolCounts());
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证接口明确拒绝工具参数时仅追加一次普通连接兜底。
|
||||
*/
|
||||
@Test
|
||||
public void shouldFallbackToPlainRequestWhenToolChoiceIsRejected() {
|
||||
RecordingModelFactory factory = new RecordingModelFactory(
|
||||
Flux.error(new IllegalArgumentException("tool_choice is unsupported")),
|
||||
Flux.just(textResponse("你好")));
|
||||
|
||||
ChatModelVerificationResult result = verifier(factory).verify(model(false));
|
||||
|
||||
Assert.assertEquals(ModelVerificationStatus.PASSED, result.getStatus());
|
||||
Assert.assertEquals(Boolean.FALSE, result.getSupportTool());
|
||||
Assert.assertEquals(List.of(1, 0), factory.getToolCounts());
|
||||
Assert.assertTrue(factory.getToolChoices().get(0) instanceof ToolChoice.Specific);
|
||||
Assert.assertNull(factory.getToolChoices().get(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证普通连接异常不会被工具兼容兜底掩盖。
|
||||
*/
|
||||
@Test
|
||||
public void shouldFailWhenConnectivityRequestFails() {
|
||||
RecordingModelFactory factory = new RecordingModelFactory(
|
||||
Flux.error(new IllegalStateException("connection failed")));
|
||||
|
||||
try {
|
||||
verifier(factory).verify(model(false));
|
||||
Assert.fail("Expected base connectivity verification failure");
|
||||
Assert.fail("Expected connectivity verification failure");
|
||||
} catch (BusinessException exception) {
|
||||
Assert.assertTrue(exception.getMessage().contains("基础连接验证失败"));
|
||||
Assert.assertTrue(exception.getMessage().contains("连接验证失败"));
|
||||
Assert.assertFalse(exception.getMessage().contains("connection failed"));
|
||||
}
|
||||
Assert.assertEquals(List.of(false), factory.getFactoryStreams());
|
||||
Assert.assertEquals(List.of(1), factory.getToolCounts());
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证流式阶段超时被归类为部分可用且不暴露底层异常。
|
||||
* 验证 VLM 在同一次工具调用中返回图片验证码。
|
||||
*/
|
||||
@Test
|
||||
public void shouldReturnPartialWhenStreamingPhaseTimesOut() {
|
||||
public void shouldVerifyVlmAndToolCallInOneRequest() {
|
||||
RecordingModelFactory factory = new RecordingModelFactory(
|
||||
Flux.just(response("你好")),
|
||||
Flux.never());
|
||||
AgentScopeChatModelConnectivityVerifier verifier = new AgentScopeChatModelConnectivityVerifier(
|
||||
factory,
|
||||
new AgentScopeMessageAdapter(),
|
||||
Duration.ofMillis(20));
|
||||
|
||||
ChatModelVerificationResult result = verifier.verify(model(false));
|
||||
|
||||
Assert.assertEquals(ModelVerificationStatus.PARTIAL, result.getStatus());
|
||||
Assert.assertEquals("连接成功,流式响应不可用,可关闭智能体的模型流式响应。",
|
||||
result.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证 VLM 使用 Base64 图片,并能聚合流式文本增量。
|
||||
*/
|
||||
@Test
|
||||
public void shouldVerifyVlmImageAndAggregateStreamingChunks() {
|
||||
RecordingModelFactory factory = new RecordingModelFactory(
|
||||
Flux.just(response("图片中的内容是“" + VlmVerificationImage.VERIFICATION_CODE + "”。")),
|
||||
Flux.just(response("识别结果:58"), response("39")));
|
||||
Flux.just(toolResponse(TEST_NONCE, VlmVerificationImage.VERIFICATION_CODE)));
|
||||
|
||||
ChatModelVerificationResult result = verifier(factory).verify(model(true));
|
||||
|
||||
Assert.assertEquals(ModelVerificationStatus.PASSED, result.getStatus());
|
||||
Msg message = factory.getMessages().get(0);
|
||||
Assert.assertEquals(Boolean.TRUE, result.getSupportTool());
|
||||
Msg message = factory.getMessageBatches().get(0).get(1);
|
||||
ImageBlock image = message.getContent().stream()
|
||||
.filter(ImageBlock.class::isInstance)
|
||||
.map(ImageBlock.class::cast)
|
||||
@@ -154,7 +157,7 @@ public class AgentScopeChatModelConnectivityVerifierTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建使用测试工厂的验证器。
|
||||
* 创建使用固定随机值的验证器。
|
||||
*
|
||||
* @param factory 记录型模型工厂
|
||||
* @return 验证器
|
||||
@@ -163,54 +166,73 @@ public class AgentScopeChatModelConnectivityVerifierTest {
|
||||
return new AgentScopeChatModelConnectivityVerifier(
|
||||
factory,
|
||||
new AgentScopeMessageAdapter(),
|
||||
Duration.ofSeconds(2));
|
||||
Duration.ofSeconds(2),
|
||||
() -> TEST_NONCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建单个文本响应片段。
|
||||
* 创建文本响应。
|
||||
*
|
||||
* @param text 文本内容
|
||||
* @return AgentScope 响应
|
||||
*/
|
||||
private ChatResponse response(String text) {
|
||||
private ChatResponse textResponse(String text) {
|
||||
return ChatResponse.builder()
|
||||
.content(List.of(TextBlock.builder().text(text).build()))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 按阶段返回预设响应并记录调用参数的模型工厂。
|
||||
* 创建探测工具调用响应。
|
||||
*
|
||||
* @param nonce 随机校验值
|
||||
* @param imageCode 图片验证码
|
||||
* @return AgentScope 响应
|
||||
*/
|
||||
private ChatResponse toolResponse(String nonce, String imageCode) {
|
||||
Map<String, Object> input = imageCode == null
|
||||
? Map.of("nonce", nonce)
|
||||
: Map.of("nonce", nonce, "imageCode", imageCode);
|
||||
return ChatResponse.builder()
|
||||
.content(List.of(ToolUseBlock.builder()
|
||||
.id("call-probe")
|
||||
.name("easyflow_capability_probe")
|
||||
.input(input)
|
||||
.build()))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 按调用顺序返回预设响应并记录真实请求参数的模型工厂。
|
||||
*/
|
||||
private static final class RecordingModelFactory
|
||||
implements AgentModelFactory<io.agentscope.core.model.Model> {
|
||||
|
||||
/** 每个阶段的预设响应。 */
|
||||
private final List<Flux<ChatResponse>> phaseResponses;
|
||||
/** 每次调用的预设响应。 */
|
||||
private final List<Flux<ChatResponse>> responses;
|
||||
/** 模型工厂收到的流式参数。 */
|
||||
private final List<Boolean> factoryStreams = new ArrayList<>();
|
||||
/** 模型请求收到的流式参数。 */
|
||||
private final List<Boolean> requestStreams = new ArrayList<>();
|
||||
/** 模型请求收到的消息。 */
|
||||
private final List<Msg> messages = new ArrayList<>();
|
||||
/** 各阶段请求消息的角色顺序。 */
|
||||
private final List<List<MsgRole>> messageRoles = new ArrayList<>();
|
||||
/** OpenAI-compatible 请求中的思考开关。 */
|
||||
private final List<Object> enableThinkingValues = new ArrayList<>();
|
||||
/** GPUStack 模板参数中的思考开关。 */
|
||||
private final List<Object> chatTemplateThinkingValues = new ArrayList<>();
|
||||
/** 模型请求收到的消息批次。 */
|
||||
private final List<List<Msg>> messageBatches = new ArrayList<>();
|
||||
/** 每次请求携带的工具数量。 */
|
||||
private final List<Integer> toolCounts = new ArrayList<>();
|
||||
/** 每次请求使用的工具选择策略。 */
|
||||
private final List<ToolChoice> toolChoices = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 创建记录型模型工厂。
|
||||
*
|
||||
* @param phaseResponses 每个阶段的预设响应
|
||||
* @param responses 每次调用的预设响应
|
||||
*/
|
||||
@SafeVarargs
|
||||
private RecordingModelFactory(Flux<ChatResponse>... phaseResponses) {
|
||||
this.phaseResponses = List.of(phaseResponses);
|
||||
private RecordingModelFactory(Flux<ChatResponse>... responses) {
|
||||
this.responses = List.of(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建当前验证阶段的测试模型。
|
||||
* 创建当前验证请求使用的模型。
|
||||
*
|
||||
* @param modelSpec 模型声明
|
||||
* @param generationOptions 生成参数
|
||||
@@ -220,19 +242,12 @@ public class AgentScopeChatModelConnectivityVerifierTest {
|
||||
public io.agentscope.core.model.Model create(
|
||||
AgentModelSpec modelSpec,
|
||||
AgentGenerationOptions generationOptions) {
|
||||
int phaseIndex = factoryStreams.size();
|
||||
int requestIndex = factoryStreams.size();
|
||||
factoryStreams.add(Boolean.TRUE.equals(generationOptions.getStream()));
|
||||
enableThinkingValues.add(
|
||||
generationOptions.getAdditionalBodyParams().get("enable_thinking"));
|
||||
Object templateOptions = generationOptions.getAdditionalBodyParams()
|
||||
.get("chat_template_kwargs");
|
||||
chatTemplateThinkingValues.add(templateOptions instanceof java.util.Map<?, ?> map
|
||||
? map.get("enable_thinking")
|
||||
: null);
|
||||
Flux<ChatResponse> responses = phaseResponses.get(phaseIndex);
|
||||
Flux<ChatResponse> response = responses.get(requestIndex);
|
||||
return new io.agentscope.core.model.Model() {
|
||||
/**
|
||||
* 返回预设响应并记录真实请求参数。
|
||||
* 返回预设响应并记录请求参数。
|
||||
*
|
||||
* @param inputMessages 模型消息
|
||||
* @param tools 工具声明
|
||||
@@ -245,9 +260,10 @@ public class AgentScopeChatModelConnectivityVerifierTest {
|
||||
List<ToolSchema> tools,
|
||||
GenerateOptions options) {
|
||||
requestStreams.add(Boolean.TRUE.equals(options.getStream()));
|
||||
messages.add(inputMessages.get(inputMessages.size() - 1));
|
||||
messageRoles.add(inputMessages.stream().map(Msg::getRole).toList());
|
||||
return responses;
|
||||
messageBatches.add(List.copyOf(inputMessages));
|
||||
toolCounts.add(tools.size());
|
||||
toolChoices.add(options.getToolChoice());
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -263,7 +279,7 @@ public class AgentScopeChatModelConnectivityVerifierTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取工厂流式参数记录。
|
||||
* 获取模型工厂流式参数。
|
||||
*
|
||||
* @return 流式参数列表
|
||||
*/
|
||||
@@ -272,7 +288,7 @@ public class AgentScopeChatModelConnectivityVerifierTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求流式参数记录。
|
||||
* 获取请求流式参数。
|
||||
*
|
||||
* @return 流式参数列表
|
||||
*/
|
||||
@@ -281,39 +297,30 @@ public class AgentScopeChatModelConnectivityVerifierTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求消息记录。
|
||||
* 获取每次请求的验证消息批次。
|
||||
*
|
||||
* @return 消息列表
|
||||
* @return 验证消息批次
|
||||
*/
|
||||
private List<Msg> getMessages() {
|
||||
return messages;
|
||||
private List<List<Msg>> getMessageBatches() {
|
||||
return messageBatches;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取各阶段请求消息的角色顺序。
|
||||
* 获取工具数量。
|
||||
*
|
||||
* @return 消息角色顺序
|
||||
* @return 工具数量列表
|
||||
*/
|
||||
private List<List<MsgRole>> getMessageRoles() {
|
||||
return messageRoles;
|
||||
private List<Integer> getToolCounts() {
|
||||
return toolCounts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 OpenAI-compatible 请求中的思考开关。
|
||||
* 获取工具选择策略。
|
||||
*
|
||||
* @return 各阶段思考开关
|
||||
* @return 工具选择策略列表
|
||||
*/
|
||||
private List<Object> getEnableThinkingValues() {
|
||||
return enableThinkingValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 GPUStack 模板参数中的思考开关。
|
||||
*
|
||||
* @return 各阶段模板思考开关
|
||||
*/
|
||||
private List<Object> getChatTemplateThinkingValues() {
|
||||
return chatTemplateThinkingValues;
|
||||
private List<ToolChoice> getToolChoices() {
|
||||
return toolChoices;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user