Skip to content

Commit

Permalink
feat: Adapting the spring-ai to the TongYI model (alibaba#3659)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuluo-yx authored Mar 28, 2024
1 parent b0023fd commit 41b1fd4
Show file tree
Hide file tree
Showing 24 changed files with 1,766 additions and 1 deletion.
24 changes: 24 additions & 0 deletions spring-cloud-alibaba-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<spring.context.support.version>1.0.11</spring.context.support.version>
<!-- Apache RocketMQ -->
<rocketmq.version>5.1.4</rocketmq.version>

<!-- Spring AI -->
<spring.ai.version>0.8.0</spring.ai.version>
<dashscope-sdk-java.version>2.10.1</dashscope-sdk-java.version>

<!-- Maven Plugin Versions -->
<maven-source-plugin.version>3.2.1</maven-source-plugin.version>
<maven-javadoc-plugin.version>3.1.1</maven-javadoc-plugin.version>
Expand All @@ -42,6 +47,19 @@
<version>${nacos.client.version}</version>
</dependency>

<!-- Spring AI & dashscope SDK-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dashscope-sdk-java</artifactId>
<version>${dashscope-sdk-java.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-core</artifactId>
<version>${spring.ai.version}</version>
</dependency>

<!-- Sentinel -->
<dependency>
<groupId>com.alibaba.csp</groupId>
Expand Down Expand Up @@ -198,6 +216,12 @@
<version>${revision}</version>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-ai</artifactId>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
Expand Down
1 change: 1 addition & 0 deletions spring-cloud-alibaba-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<module>integrated-example/integrated-praise-consumer</module>
<module>integrated-example/integrated-common</module>
<module>integrated-example/integrated-frontend</module>
<module>spring-cloud-ai-example</module>
</modules>


Expand Down
106 changes: 106 additions & 0 deletions spring-cloud-alibaba-examples/spring-cloud-ai-example/README-en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Spring Cloud Alibaba AI Example

## Project description

The Spring Cloud Alibaba AI module is based on [Spring AI 0.8.0](https://docs.spring.io/spring-ai/reference/0.8-SNAPSHOT/index.html) the project API to complete the access of the general series of large models. This project demonstrates how to use `spring-cloud-starter-alibaba-ai` the Spring Cloud microservice application to integrate with the generic family model.

[model service dashscope](https://help.aliyun.com/zh/dashscope/) It is a big model application service launched by Alibaba. Based on the concept of "Model-as-a-Service" (MaaS), Lingji Model Service provides a variety of model services including model reasoning and model fine-tuning training through standardized APIs around AI models in various fields.

- Current completion of spring-ai

## Application access

### Access `spring-cloud-starter-alibaba-ai`

1. Add the following dependencies to the project POM. XML:


```xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-ai</artifactId>
</dependency>
```

2. Add the following configuration to the application. Yml configuration file:


```yaml
spring:
cloud:
ai:
tongyi:
chat:
options:
# api_key is invalied.
api-key: sk-a3d73b1709bf4a178c28ed7c8b3b5a45
system-user: "You are a helpful assistant."
```
3. Add the following code:
```java
controller:

@GetMapping("/example")
public Map<String, String> completion(
@RequestParam(value = "message", defaultValue = "Tell me a joke")
String message
) {

return tongyiService.completion(message);
}

service:

@Resource
private MessageManager msgManager;

private final ChatClient chatClient;

@Autowired
public TongYiServiceImpl(ChatClient chatClient) {

this.chatClient = chatClient;
}

@Override
public Map<String, String> completion(String message) {

Message userMsg = Message.builder()
.role(Role.USER.getValue())
.content(message)
.build();
msgManager.add(userMsg);

return Map.of(message, chatClient.call(message));
}
```

4. Start the application

This Example project supports the following two startup methods:

1. IDE direct startup: find the main class `TongYiApplication` and execute the main method to start the application.
2. Start after packaging and compiling: First `mvn clean package`, compile and package the project, and then enter the `target` folder to `java -jar spring-cloud-ai-example.jar` start the application.

## Validate

Browser address bar input: `http://localhost:8080/ai/example`

The following response is returned:


```json
{
"Tell me a joke": "Sure, here's a classic one for you:\n\nWhy was the math book sad?\n\nBecause it had too many problems.\n\nI hope that made you smile! If you're looking for more, just let me know."
}
```

## Configuration item description

https://help.aliyun.com/zh/dashscope/developer-reference/api-details



104 changes: 104 additions & 0 deletions spring-cloud-alibaba-examples/spring-cloud-ai-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Spring Cloud Alibaba AI Example

## 项目说明

Spring Cloud Alibaba AI 模块基于 [Spring AI 0.8.0](https://docs.spring.io/spring-ai/reference/0.8-SNAPSHOT/index.html) 项目 API 完成通义系列大模型的接入。本项目演示如何使用 `spring-cloud-starter-alibaba-ai` 完成 Spring Cloud 微服务应用与通义系列模型的整合。

[模型服务灵积](https://help.aliyun.com/zh/dashscope/) 是阿里巴巴推出的一个大模型应用服务。灵积模型服务建立在“模型即服务”(Model-as-a-Service,MaaS)的理念基础之上,围绕AI各领域模型,通过标准化的API提供包括模型推理、模型微调训练在内的多种模型服务。

- 目前之完成 spring-ai

## 应用接入

### 接入 `spring-cloud-starter-alibaba-ai`

1. 在项目 pom.xml 中加入以下依赖:

```xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-ai</artifactId>
</dependency>
```

2. 在 application.yml 配置文件中加入以下配置:

```yaml
spring:
cloud:
ai:
tongyi:
chat:
options:
# api_key is invalied.
api-key: sk-a3d73b1709bf4a178c28ed7c8b3b5a45
system-user: "You are a helpful assistant."
```
3. 添加如下代码:
```java
controller:

@GetMapping("/example")
public Map<String, String> completion(
@RequestParam(value = "message", defaultValue = "Tell me a joke")
String message
) {

return tongyiService.completion(message);
}

service:

@Resource
private MessageManager msgManager;

private final ChatClient chatClient;

@Autowired
public TongYiServiceImpl(ChatClient chatClient) {

this.chatClient = chatClient;
}

@Override
public Map<String, String> completion(String message) {

Message userMsg = Message.builder()
.role(Role.USER.getValue())
.content(message)
.build();
msgManager.add(userMsg);

return Map.of(message, chatClient.call(message));
}
```

4. 启动应用

本 Example 项目支持如下两种启动方式:

1. IDE 直接启动:找到主类 `TongYiApplication`,执行 main 方法启动应用。
2. 打包编译后启动:首先执行 `mvn clean package` 将工程编译打包,进入 `target` 文件夹执行 `java -jar spring-cloud-ai-example.jar` 启动应用。

## 验证

浏览器地址栏输入:`http://localhost:8080/ai/example`

返回如下响应:

```json
{
"Tell me a joke": "Sure, here's a classic one for you:\n\nWhy was the math book sad?\n\nBecause it had too many problems.\n\nI hope that made you smile! If you're looking for more, just let me know."
}
```

## 配置项说明

https://help.aliyun.com/zh/dashscope/developer-reference/api-details

##



62 changes: 62 additions & 0 deletions spring-cloud-alibaba-examples/spring-cloud-ai-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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-example</artifactId>
<name>Spring Cloud Starter Alibaba AI Example</name>
<description>Example demonstrating how to use 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>

</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
* @since 2023.0.0.0
*/

@SpringBootApplication
public class TongYiApplication {

public static void main(String[] args) {

SpringApplication.run(TongYiApplication.class);
}

}
Loading

0 comments on commit 41b1fd4

Please sign in to comment.