Skip to content

Commit 448670a

Browse files
authored
Merge pull request #10 from wstever/main
feat:Add OpenAI LLMs chat example
2 parents 2831000 + a0fca27 commit 448670a

File tree

13 files changed

+528
-0
lines changed

13 files changed

+528
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Spring AI Alibaba OpenAI Chat Example
2+
3+
This is a chat client example for use OpenAI LLMs by Spring AI Alibaba.
4+
5+
TODO: add more info for ChatClient.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Copyright 2023-2024 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
19+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xmlns="http://maven.apache.org/POM/4.0.0"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
24+
<parent>
25+
<groupId>com.alibaba.cloud.ai</groupId>
26+
<artifactId>openai</artifactId>
27+
<version>${revision}</version>
28+
<relativePath>../pom.xml</relativePath>
29+
</parent>
30+
31+
<artifactId>openai-chat-client</artifactId>
32+
<version>${revision}</version>
33+
<packaging>jar</packaging>
34+
35+
<description>Spring AI Alibaba OpenAI Chat Client Example</description>
36+
<name>Spring AI Alibaba OpenAI Chat Client Examples</name>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-maven-plugin</artifactId>
43+
<version>${spring-boot.version}</version>
44+
</plugin>
45+
<plugin>
46+
<groupId>org.apache.maven.plugins</groupId>
47+
<artifactId>maven-deploy-plugin</artifactId>
48+
<version>${maven-deploy-plugin.version}</version>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
53+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.alibaba.cloud.ai.example.chat.openai;
19+
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
23+
/**
24+
* @Author: wst
25+
* @Date: 2024-12-16
26+
*/
27+
28+
@SpringBootApplication
29+
public class OpenAiChatClientApplication {
30+
31+
public static void main(String[] args) {
32+
33+
SpringApplication.run(OpenAiChatClientApplication.class, args);
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.alibaba.cloud.ai.example.chat.openai.controller;
2+
3+
import jakarta.servlet.http.HttpServletResponse;
4+
import org.springframework.ai.openai.OpenAiChatOptions;
5+
import reactor.core.publisher.Flux;
6+
7+
import org.springframework.ai.chat.client.ChatClient;
8+
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
9+
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
10+
import org.springframework.ai.chat.memory.InMemoryChatMemory;
11+
import org.springframework.ai.chat.model.ChatModel;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.PathVariable;
14+
import org.springframework.web.bind.annotation.RequestMapping;
15+
import org.springframework.web.bind.annotation.RestController;
16+
17+
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
18+
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;
19+
20+
/**
21+
* @Author: wst
22+
* @Date: 2024-12-16
23+
*/
24+
25+
@RestController
26+
@RequestMapping("/openai/chat-client")
27+
public class OpenAiClientController {
28+
29+
private static final String DEFAULT_PROMPT = "你好,介绍下你自己!";
30+
31+
private final ChatClient openAiChatClient;
32+
33+
private final ChatModel chatModel;
34+
35+
public OpenAiClientController(ChatModel chatModel) {
36+
37+
this.chatModel = chatModel;
38+
39+
// 构造时,可以设置 ChatClient 的参数
40+
// {@link org.springframework.ai.chat.client.ChatClient};
41+
this.openAiChatClient = ChatClient.builder(chatModel)
42+
// 实现 Chat Memory 的 Advisor
43+
// 在使用 Chat Memory 时,需要指定对话 ID,以便 Spring AI 处理上下文。
44+
.defaultAdvisors(
45+
new MessageChatMemoryAdvisor(new InMemoryChatMemory())
46+
)
47+
// 实现 Logger 的 Advisor
48+
.defaultAdvisors(
49+
new SimpleLoggerAdvisor()
50+
)
51+
// 设置 ChatClient 中 ChatModel 的 Options 参数
52+
.defaultOptions(
53+
OpenAiChatOptions.builder()
54+
.withTopP(0.7)
55+
.build()
56+
)
57+
.build();
58+
}
59+
60+
// 也可以使用如下的方式注入 ChatClient
61+
// public OpenAIChatClientController(ChatClient.Builder chatClientBuilder) {
62+
//
63+
// this.openAiChatClient = chatClientBuilder.build();
64+
// }
65+
66+
/**
67+
* ChatClient 简单调用
68+
*/
69+
@GetMapping("/simple/chat")
70+
public String simpleChat() {
71+
72+
return openAiChatClient.prompt(DEFAULT_PROMPT).call().content();
73+
}
74+
75+
/**
76+
* ChatClient 流式调用
77+
*/
78+
@GetMapping("/stream/chat")
79+
public Flux<String> streamChat(HttpServletResponse response) {
80+
81+
response.setCharacterEncoding("UTF-8");
82+
return openAiChatClient.prompt(DEFAULT_PROMPT).stream().content();
83+
}
84+
85+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
server:
2+
port: 10002
3+
4+
spring:
5+
application:
6+
name: spring-ai-alibaba-openai-chat-client-example
7+
8+
ai:
9+
openai:
10+
api-key: ${OPENAI_API_KEY}
11+
base-url: https://api.openai-hk.com
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Spring AI Alibaba OpenAI Chat Example
2+
3+
This is a chat model example for use OpenAI LLMs by Spring AI Alibaba.
4+
5+
TODO: add more info for ChatModel. or blogs or docs.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Copyright 2023-2024 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
19+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xmlns="http://maven.apache.org/POM/4.0.0"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
24+
<parent>
25+
<groupId>com.alibaba.cloud.ai</groupId>
26+
<artifactId>openai</artifactId>
27+
<version>${revision}</version>
28+
<relativePath>../pom.xml</relativePath>
29+
</parent>
30+
31+
<artifactId>openai-chat-model</artifactId>
32+
<version>${revision}</version>
33+
<packaging>jar</packaging>
34+
35+
<description>Spring AI Alibaba OpenAI Chat Model Example</description>
36+
<name>Spring AI Alibaba OpenAI Chat Model Examples</name>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-maven-plugin</artifactId>
43+
<version>${spring-boot.version}</version>
44+
</plugin>
45+
<plugin>
46+
<groupId>org.apache.maven.plugins</groupId>
47+
<artifactId>maven-deploy-plugin</artifactId>
48+
<version>${maven-deploy-plugin.version}</version>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
53+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.alibaba.cloud.ai.example.chat.openai;
19+
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
23+
/**
24+
* @Author: wst
25+
* @Date: 2024-12-16
26+
*/
27+
28+
@SpringBootApplication
29+
public class OpenAiChatModelApplication {
30+
31+
public static void main(String[] args) {
32+
33+
SpringApplication.run(OpenAiChatModelApplication.class, args);
34+
}
35+
36+
}

0 commit comments

Comments
 (0)