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
parent
6c26a49ec3
commit
34459161ae
@ -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
|
Loading…
Reference in New Issue