ai: add chat message context manager example & update Daschope SDK to 2.15.1 (#3803)

Signed-off-by: yuluo-yx <yuluo08290126@gmail.com>
pull/3804/head
YuLuo 9 months ago committed by GitHub
parent 6c26a49ec3
commit 34459161ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -28,7 +28,7 @@
<!-- Spring AI -->
<spring.ai.version>1.0.0-M1</spring.ai.version>
<dashscope-sdk-java.version>2.14.0</dashscope-sdk-java.version>
<dashscope-sdk-java.version>2.15.1</dashscope-sdk-java.version>
<!-- scheduling -->
<shedlock.version>4.23.0</shedlock.version>

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2023-2024 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-alibaba-examples</artifactId>
<groupId>com.alibaba.cloud</groupId>
<version>${revision}</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-ai-chat-msg-context-example</artifactId>
<name>Spring Cloud Starter Alibaba AI Chat Message Context Holder Example</name>
<description>Example for Chat Message Context Holder By Spring Cloud Alibaba AI</description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-ai</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
</project>

@ -0,0 +1,35 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.example.tongyi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
*/
@SpringBootApplication
public class ChatMsgApplication {
public static void main(String[] args) {
SpringApplication.run(ChatMsgApplication.class, args);
}
}

@ -0,0 +1,47 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.example.tongyi.context;
import java.util.List;
import org.springframework.ai.chat.messages.Message;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
*/
public interface MessageContextHolder {
/**
* Th default session id key.
* Can use session_id request_id &etc.
*/
String SCA_SESSION_ID = "SCA_SESSION_ID";
void addMsg(String sessionId, Message msg);
void removeMsg(String sessionId);
List<Message> getMsg(String sessionId);
default String getSCASessionId() {
return SCA_SESSION_ID;
}
}

@ -0,0 +1,76 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.example.tongyi.context.defaults;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import com.alibaba.cloud.ai.example.tongyi.context.MessageContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.stereotype.Component;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
*/
@Component
public class MemoryMessageContextHolder implements MessageContextHolder {
private static final Logger log = LoggerFactory.getLogger(MemoryMessageContextHolder.class);
private final Map<String, List<Message>> msgContextHolderMap = new HashMap<>();
@Override
public void addMsg(String sessionId, Message msg) {
msgContextHolderMap.computeIfAbsent(sessionId, k -> new ArrayList<>());
log.info("addMsg: sessionId={}, msg={}", sessionId, msg);
}
@Override
public void removeMsg(String sessionId) {
msgContextHolderMap.remove(sessionId);
}
@Override
public List<Message> getMsg(String sessionId) {
return new ArrayList<>(msgContextHolderMap.getOrDefault(sessionId, new ArrayList<>()));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("MessageContextHolderImpl{");
StringJoiner joiner = new StringJoiner(", ", "{", "}");
for (Map.Entry<String, List<Message>> entry : msgContextHolderMap.entrySet()) {
joiner.add(entry.getKey() + "=" + entry.getValue().toString());
}
sb.append("msgContextHolderMap=").append(joiner);
sb.append('}');
return sb.toString();
}
}

@ -0,0 +1,52 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.example.tongyi.context.defaults;
import java.util.List;
import com.alibaba.cloud.ai.example.tongyi.context.MessageContextHolder;
import org.springframework.ai.chat.messages.Message;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
*/
//@Component
public class RedisMessageContextHolder implements MessageContextHolder {
@Override
public void addMsg(String sessionId, Message msg) {
System.out.println("RedisMessageContextHolder addMsg");
}
@Override
public void removeMsg(String sessionId) {
System.out.println("RedisMessageContextHolder removeMsg");
}
@Override
public List<Message> getMsg(String sessionId) {
System.out.println("RedisMessageContextHolder getMsg");
return null;
}
}

@ -0,0 +1,45 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.example.tongyi.controller;
import com.alibaba.cloud.ai.example.tongyi.service.ChatMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
*/
@RestController
@RequestMapping("/chat")
public class ChatMsgController {
@Autowired
private ChatMsgService msgService;
@GetMapping("/msg")
public String completion(@RequestParam String message) {
return msgService.completion(message);
}
}

@ -0,0 +1,69 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.example.tongyi.service;
import com.alibaba.cloud.ai.example.tongyi.context.MessageContextHolder;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
*/
@Service
public class ChatMsgService {
private final ChatModel chatModel;
private final MessageContextHolder messageContextHolder;
@Autowired
public ChatMsgService(ChatModel chatModel, MessageContextHolder messageContextHolder) {
this.chatModel = chatModel;
this.messageContextHolder = messageContextHolder;
}
public String completion(String message) {
// create chat prompt
Prompt prompt = new Prompt(new UserMessage(message));
// collect user message
messageContextHolder.addMsg(
messageContextHolder.getSCASessionId(),
prompt.getInstructions().get(0)
);
ChatResponse resp = chatModel.call(prompt);
// collect model response
messageContextHolder.addMsg(
messageContextHolder.getSCASessionId(),
resp.getResult().getOutput()
);
return resp.getResult().getOutput().getContent();
}
}

@ -0,0 +1,26 @@
#
# Copyright 2023-2024 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
server:
port: 8084
spring:
application:
name: tongyi-example
# please setting api-key. suggestion by environment variable.
# Note: api-key is invalid, please apply for a new one.
# export SPRING_CLOUD_AI_TONGYI_API_KEY=sk-a3d73b1709bf4a178c28ed7c8b3b5a345

@ -53,6 +53,7 @@
<module>integrated-example/integrated-frontend</module>
<module>ai-example/spring-cloud-ai-example</module>
<module>ai-example/spring-cloud-ai-rag-example</module>
<module>ai-example/spring-cloud-ai-chat-msg-context-example</module>
<module>spring-cloud-scheduling-example</module>
</modules>

@ -91,6 +91,12 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<repositories>

@ -34,7 +34,6 @@ import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.audio.asr.transcription.Transcription;
import com.alibaba.dashscope.audio.tts.SpeechSynthesizer;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.embeddings.TextEmbedding;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.ApiKey;
@ -58,7 +57,6 @@ import org.springframework.context.annotation.Scope;
@AutoConfiguration
@ConditionalOnClass({
MessageManager.class,
TongYiChatModel.class,
TongYiImagesModel.class,
TongYiAudioSpeechModel.class,
@ -83,14 +81,6 @@ public class TongYiAutoConfiguration {
return new Generation();
}
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
public MessageManager msgManager() {
return new MessageManager(10);
}
@Bean
@Scope("prototype")
@ConditionalOnMissingBean

@ -15,18 +15,17 @@
*/
package com.alibaba.cloud.ai.tongyi.chat;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import com.alibaba.cloud.ai.tongyi.common.exception.TongYiException;
import com.alibaba.dashscope.aigc.conversation.ConversationParam;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationOutput;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
@ -51,7 +50,6 @@ import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.ModelOptionsUtils;
import org.springframework.ai.model.function.AbstractFunctionCallSupport;
import org.springframework.ai.model.function.FunctionCallbackContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
@ -85,12 +83,6 @@ public class TongYiChatModel extends
*/
private TongYiChatOptions defaultOptions;
/**
* User role message manager.
*/
@Autowired
private MessageManager msgManager;
/**
* Initializes an instance of the TongYiChatClient.
* @param generation DashScope generation client.
@ -144,19 +136,7 @@ public class TongYiChatModel extends
public ChatResponse call(Prompt prompt) {
ConversationParam params = toTongYiChatParams(prompt);
// TongYi models context loader.
com.alibaba.dashscope.common.Message message = new com.alibaba.dashscope.common.Message();
message.setRole(Role.USER.getValue());
message.setContent(prompt.getContents());
msgManager.add(message);
params.setMessages(msgManager.get());
logger.trace("TongYi ConversationOptions: {}", params);
GenerationResult chatCompletions = this.callWithFunctionSupport(params);
logger.trace("TongYi ConversationOptions: {}", params);
msgManager.add(chatCompletions);
List<org.springframework.ai.chat.model.Generation> generations =
chatCompletions
@ -183,7 +163,8 @@ public class TongYiChatModel extends
ConversationParam tongYiChatParams = toTongYiChatParams(prompt);
// See https://help.aliyun.com/zh/dashscope/developer-reference/api-details?spm=a2c4g.11186623.0.0.655fc11aRR0jj7#b9ad0a10cfhpe
// tongYiChatParams.setIncrementalOutput(true);
// enable incremental output
tongYiChatParams.setIncrementalOutput(true);
try {
genRes = generation.streamCall(tongYiChatParams);
@ -226,7 +207,9 @@ public class TongYiChatModel extends
Set<String> functionsForThisRequest = new HashSet<>();
List<com.alibaba.dashscope.common.Message> tongYiMessage = prompt.getInstructions().stream()
List<com.alibaba.dashscope.common.Message> tongYiMessage = prompt
.getInstructions()
.stream()
.map(this::fromSpringAIMessage)
.toList();

@ -25,8 +25,6 @@ import org.springframework.ai.chat.metadata.PromptMetadata;
import org.springframework.ai.chat.metadata.Usage;
import org.springframework.util.Assert;
/**
* {@link ChatResponseMetadata} implementation for {@literal Alibaba DashScope}.
*

@ -27,8 +27,6 @@ import org.springframework.ai.model.ResponseMetadata;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>

@ -18,18 +18,15 @@ package com.alibaba.cloud.ai.tongyi.metadata.audio;
import java.util.HashMap;
import javax.annotation.Nullable;
import com.alibaba.dashscope.audio.asr.transcription.TranscriptionResult;
import com.google.gson.JsonObject;
import org.springframework.ai.chat.metadata.EmptyRateLimit;
import org.springframework.ai.chat.metadata.RateLimit;
import org.springframework.ai.model.ResponseMetadata;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>

Loading…
Cancel
Save