feat: 支持配置 system 消息内容格式
- 为 OpenAI 兼容模型增加字符串与文本数组两种 system content 策略 - 在 AgentScope 模型工厂统一安装兼容 formatter 并补充测试
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package com.easyagents.agent.runtime.agentscope;
|
||||
|
||||
import com.easyagents.agent.runtime.model.AgentGenerationOptions;
|
||||
import com.easyagents.agent.runtime.model.AgentModelProviderType;
|
||||
import com.easyagents.agent.runtime.model.AgentModelSpec;
|
||||
import com.easyagents.agent.runtime.model.AgentSystemContentFormat;
|
||||
import io.agentscope.core.formatter.openai.dto.OpenAIContentPart;
|
||||
import io.agentscope.core.formatter.openai.dto.OpenAIMessage;
|
||||
import io.agentscope.core.message.Msg;
|
||||
import io.agentscope.core.message.MsgRole;
|
||||
import io.agentscope.core.model.OpenAIChatModel;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Agent OpenAI Chat 消息格式兼容测试。
|
||||
*/
|
||||
public class AgentOpenAIChatFormatterTest {
|
||||
|
||||
/**
|
||||
* 验证 system 文本转换为 text 内容块数组,普通 user 文本保持字符串。
|
||||
*/
|
||||
@Test
|
||||
public void shouldConvertOnlySystemTextToContentParts() {
|
||||
AgentOpenAIChatFormatter formatter = new AgentOpenAIChatFormatter();
|
||||
List<OpenAIMessage> messages = formatter.format(List.of(
|
||||
Msg.builder().role(MsgRole.SYSTEM).textContent("system prompt").build(),
|
||||
Msg.builder().role(MsgRole.USER).textContent("hello").build()));
|
||||
|
||||
Assert.assertTrue(messages.get(0).getContent() instanceof List<?>);
|
||||
List<OpenAIContentPart> contentParts = messages.get(0).getContentAsList();
|
||||
Assert.assertEquals(1, contentParts.size());
|
||||
Assert.assertEquals("text", contentParts.get(0).getType());
|
||||
Assert.assertEquals("system prompt", contentParts.get(0).getText());
|
||||
Assert.assertEquals("hello", messages.get(1).getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证内容块数组策略会安装 EasyAgents 的 OpenAI Formatter。
|
||||
*
|
||||
* @throws Exception 反射读取 Formatter 失败时抛出
|
||||
*/
|
||||
@Test
|
||||
public void textPartsPolicyShouldInstallAgentOpenAIFormatter() throws Exception {
|
||||
AgentModelSpec spec = new AgentModelSpec();
|
||||
spec.setProviderType(AgentModelProviderType.OPENAI_COMPATIBLE);
|
||||
spec.setModelName("vlm-test");
|
||||
spec.setBaseUrl("http://model.example.com/v1");
|
||||
spec.setApiKey("test-key");
|
||||
spec.setSystemContentFormat(AgentSystemContentFormat.TEXT_PARTS);
|
||||
|
||||
OpenAIChatModel model = (OpenAIChatModel) new AgentScopeModelFactory()
|
||||
.create(spec, new AgentGenerationOptions());
|
||||
Field formatterField = OpenAIChatModel.class.getDeclaredField("formatter");
|
||||
formatterField.setAccessible(true);
|
||||
|
||||
Assert.assertTrue(formatterField.get(model) instanceof AgentOpenAIChatFormatter);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user