-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ai: add chat message context manager example & update Daschope SDK to…
… 2.15.1 (#3803) Signed-off-by: yuluo-yx <yuluo08290126@gmail.com>
- Loading branch information
Showing
16 changed files
with
446 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-chat-msg-context-example/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
35 changes: 35 additions & 0 deletions
35
...context-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/ChatMsgApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
...ample/src/main/java/com/alibaba/cloud/ai/example/tongyi/context/MessageContextHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
...java/com/alibaba/cloud/ai/example/tongyi/context/defaults/MemoryMessageContextHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
.../java/com/alibaba/cloud/ai/example/tongyi/context/defaults/RedisMessageContextHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...ample/src/main/java/com/alibaba/cloud/ai/example/tongyi/controller/ChatMsgController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
Oops, something went wrong.