![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/2452e5297f4e4186ae1f2e3e37d9e7c0.png#pic_center)
后端
环境:springboot
ai版本:qwen-vl-chat-v1(免费)
application.properties
qwkey=你自己的apikey
public class QwController {
private final static String BANBEN = "qwen-vl-chat-v1";
@Value("${qwkey}")
private String apikey;
@PostMapping("/callWithMessage")
public Object callWithMessage(HttpServletRequest request) throws ApiException, NoApiKeyException, UploadFileException, JsonProcessingException {
Map dataMap = new HashMap();
String code = "000";
String messge = "成功";
String data = "";
Constants.apiKey = apikey;
MultiModalConversation conv = new MultiModalConversation();
ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> messageList = new ArrayList<>();
List<MultiModalConversationMessage> messages = new ArrayList<>();
String message = request.getParameter("message");
String memory = request.getParameter("memory");
String img = request.getParameter("img");
if (memory!=null&!"".equals(memory)) {
messageList = mapper.readValue(memory,
TypeFactory.defaultInstance().constructCollectionType(List.class, Map.class));
}
Map<String,Object> userMap = new HashMap<>();
userMap.put("role",Role.USER.getValue());
userMap.put("text",message);
userMap.put("img",img);
messageList.add(userMap);
for (Map<String,Object> map :messageList){
String role1 = map.get("role").toString();
String msg1 = map.get("text").toString();
String img1 = map.get("img").toString();
MultiModalMessageItemText userText = new MultiModalMessageItemText(msg1);
if (img1!=null&!"".equals(img1)){
messages.add(MultiModalConversationMessage.builder().role(role1)
.content(Arrays.asList(userText)).build());
}else {
MultiModalMessageItemImage userImg = new MultiModalMessageItemImage(img1);
messages.add( MultiModalConversationMessage.builder().role(Role.USER.getValue())
.content(Arrays.asList(userImg, userText)).build());
}
}
MultiModalConversationParam param = MultiModalConversationParam.builder()
.model(BANBEN)
.messages(messages)
.build();
param.setMessages(new ArrayList<Object>(messages));
MultiModalConversationResult result = conv.call(param);
Map<String,Object> assistentMap = new HashMap<>();
assistentMap.put("role",Role.ASSISTANT.getValue());
assistentMap.put("text",result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text").toString());
messageList.add(assistentMap);
dataMap.put("code",code);
dataMap.put("megges",result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text").toString());
dataMap.put("memory",messageList);
String resultString = JSONObject.toJSONString(dataMap, SerializerFeature.WriteMapNullValue);
return resultString;
}
Js 前端页面
function sendToServer(message) {
var memoryData = localStorage.getItem('memory');
$.ajax({
url: '../qw/callWithMessage',
method: 'POST',
data: {
"message":message,
"memory":memoryData
},
contentType: 'application/x-www-form-urlencoded',
success: function(response) {
var jsonResponse = JSON.parse(response);
const reply = jsonResponse.megges;
localStorage.setItem('memory', JSON.stringify(jsonResponse.memory));
appendMessage(reply, 'ai-message');
},
error: function(error) {
console.error('Error:', error);
}
});
}