Skip to content

Commit

Permalink
ai: add chat message context manager example & update Daschope SDK to…
Browse files Browse the repository at this point in the history
… 2.15.1 (#3803)

Signed-off-by: yuluo-yx <yuluo08290126@gmail.com>
  • Loading branch information
yuluo-yx committed Jul 16, 2024
1 parent 6c26a49 commit 3445916
Show file tree
Hide file tree
Showing 16 changed files with 446 additions and 42 deletions.
2 changes: 1 addition & 1 deletion spring-cloud-alibaba-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
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>
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);
}

}
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;
}

}
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();
}
}
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;
}

}
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);
}

}
Loading

0 comments on commit 3445916

Please sign in to comment.