Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSPP: Completed Spring Cloud Alibaba's Spring AI model support for Tongyi series models #35

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions spring-ai-alibaba-examples/README-zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Spring AI Alibaba Example

该目录包含演示 Spring AI Alibaba 的基本和高级用法的示例。

这里的所有示例都被设计为独立的 maven 项目,可以独立复制和导入。因此建议将整个示例目录或每个特定示例子目录作为单独的 Maven 项目导入,
以体验 Spring AI Alibaba 开发框架的能力。

* Client API 示例:此 Example 主要演示使用 Client API(高级 API) 调用模型;
* Model API 示例:此 Example 主要演示使用 Model API(低级 API) 调用模型;
* 函数调用示例:此 Example 主要演示如何使用 Function Calling 增强 LLM 能力;
* 结构化输出示例:此 Example 主要演示如何使用 Structured Output ,将 LLM 输出转为 Java Bean;
* Prompt 示例:此 Example 主要演示如何使用 Prompt Template 构建动态 Prompt 等,其他 Prompt 用法;
* RAG 示例:此 Example 演示 RAG(检索增强)应用的构建示例。
* Flight Booking Playground:一个高级示例,同时展示了 Prompt 模板、Function Calling、Chat Memory 和 RAG 的使用。
21 changes: 12 additions & 9 deletions spring-ai-alibaba-examples/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
This directory contains examples demonstrating basic and advanced usages of Spring AI Alibaba.
## Spring AI Alibaba Example

All the examples here are designed to be independent maven projects that can be copied and imported independently. So it's recommended to import the whole example directory or each specific example sub-directory as a separate maven project.
This directory contains examples that demonstrate basic and advanced usage of Spring AI Alibaba.

* Hello World
* Chat Model
* Function Calling
* Structured Output
* Prompt
* RAG
* Flight Booking Playground, an advanced example showcasing usage of prompt template, function calling, chat memory and rag at the same time.
All examples here are designed as independent Maven projects that can be copied and imported independently. Therefore, it is recommended to import the entire example directory or each specific example subdirectory as a separate Maven project,
to experience the capabilities of the Spring AI Alibaba development framework.

* Client API Example: This Example mainly demonstrates the use of Client API (advanced API) to call the model;
* Model API Example: This Example mainly demonstrates the use of Model API (low-level API) to call the model;
* Function Calling Example: This Example mainly demonstrates how to use Function Calling to enhance LLM capabilities;
* Structured Output Example: This Example mainly demonstrates how to use Structured Output to convert LLM output to Java Bean;
* Prompt Example: This Example mainly demonstrates how to use Prompt Template to build dynamic Prompt, etc., and other Prompt usage;
* RAG Example: This Example demonstrates the construction example of RAG (retrieval enhancement) application.
* Flight Booking Playground: An advanced example that also demonstrates the use of Prompt templates, Function Calling, Chat Memory, and RAG.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>helloworld-example</artifactId>
<artifactId>client-api-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloworld-example</name>
<description>Demo project for Spring AI Alibaba</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>chatmodel-example</artifactId>

<artifactId>module-api-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>chatmodel-example</name>
<description>Demo project for Spring AI Alibaba</description>
Expand Down
5 changes: 3 additions & 2 deletions spring-ai-alibaba-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
</properties>

<modules>
<module>helloworld-example</module>
<module>chatmodel-example</module>
<module>client-api-example</module>
<module>module-api-example</module>
<module>prompt-example</module>
<module>function-calling-example</module>
<module>rag-example</module>
<module>output-parser-example</module>
<module>playground-flight-booking</module>
<module>usercase-example</module>
</modules>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<description>Generate SQL Queries</description>
<artifactId>generate-sql-queries</artifactId>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven-deploy-plugin.version>3.1.1</maven-deploy-plugin.version>

<!-- Spring AI -->
<spring-ai-alibaba.version>1.0.0-M2</spring-ai-alibaba.version>
</properties>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</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>
</repositories>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.alibaba.cloud.ai.sql;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AIApplication {

public static void main(String[] args) {

SpringApplication.run(AIApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alibaba.cloud.ai.sql.controller;

import com.alibaba.cloud.ai.sql.entity.Request;
import com.alibaba.cloud.ai.sql.entity.Response;
import com.alibaba.cloud.ai.sql.service.SQLService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Objects;

@RestController
@RequestMapping("/ai/sql")
public class SQLController {

@Resource
private SQLService sqlService;

@GetMapping
public Response sql(Request request) throws IOException {

Response response = sqlService.sql(request);

if (Objects.isNull(response)) {
throw new RuntimeException("SQL Example throw exception");
}

return response;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.alibaba.cloud.ai.sql.entity;

public record Request(String text) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.alibaba.cloud.ai.sql.entity;

import java.util.List;
import java.util.Map;

public record Response(String sqlQuery, List<Map<String, Object>> results) { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.alibaba.cloud.ai.sql.service;

import com.alibaba.cloud.ai.sql.entity.Request;
import com.alibaba.cloud.ai.sql.entity.Response;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.charset.Charset;

@Service
public class SQLService {

@Value("classpath:schema.sql")
private Resource ddlResource;

@Value("classpath:/prompt/sql-queries.st")
private Resource sqlPromptTemplateResource;

private final ChatClient aiClient;
private final JdbcTemplate jdbcTemplate;

public SQLService(ChatClient.Builder aiClientBuilder, JdbcTemplate jdbcTemplate) {
this.aiClient = aiClientBuilder.build();
this.jdbcTemplate = jdbcTemplate;
}

public Response sql(Request request) throws IOException {
String schema = ddlResource.getContentAsString(Charset.defaultCharset());

String query = aiClient.prompt()
.advisors(new SimpleLoggerAdvisor())
.user(userSpec -> userSpec
.text(sqlPromptTemplateResource)
.param("question", request.text())
.param("ddl", schema)
)
.call()
.content();

if (query.toLowerCase().startsWith("select")) {

return new Response(query, jdbcTemplate.queryForList(query));
}

throw new RuntimeException(query);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
server:
port: 8081

spring:
application:
name: generate-sql-queries

ai:
dashscope:
api-key: ${AI_DASHSCOPE_API_KEY}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
INSERT INTO TBL_USER (username, email, password)
VALUES
('user1', 'user1@example.com', 'password1'),
('user2', 'user2@example.com', 'password2'),
('user3', 'user3@example.com', 'password3'),
('user4', 'user4@example.com', 'password4'),
('user5', 'user5@example.com', 'password5'),
('user6', 'user6@example.com', 'password6'),
('user7', 'user7@example.com', 'password7'),
('user8', 'user8@example.com', 'password8'),
('user9', 'user9@example.com', 'password9'),
('user10', 'user10@example.com', 'password10');

INSERT INTO TBL_ACCOUNT (accountNumber, user_id, balance, openDate)
VALUES
('ACC001', 1, 1000.00, '2024-07-09'),
('ACC002', 1, 500.00, '2024-07-10'),
('ACC003', 2, 1500.00, '2024-07-09'),
('ACC004', 2, 200.00, '2024-07-10'),
('ACC005', 3, 800.00, '2024-07-09'),
('ACC006', 4, 3000.00, '2024-07-09'),
('ACC007', 4, 100.00, '2024-07-10'),
('ACC008', 5, 250.00, '2024-07-09'),
('ACC009', 6, 1800.00, '2024-07-09'),
('ACC010', 6, 700.00, '2024-07-10'),
('ACC011', 7, 500.00, '2024-07-09'),
('ACC012', 8, 1200.00, '2024-07-09'),
('ACC013', 9, 900.00, '2024-07-09'),
('ACC014', 9, 300.00, '2024-07-10'),
('ACC015', 10, 2000.00, '2024-07-09');
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Given the DDL in the DDL section, write an SQL query that answers the asked question in the QUESTION section.
Only produce select queries. Do not append any text or markup in the start or end of response.
Remove the markups such as ``` , sql , \n as well.
If the question would result in an insert, update, or delete, or if the query would alter the DDL in any way, say that the operation isn't supported.
If the question can't be answered, say that the DDL doesn't support answering that question.

QUESTION
{question}

DDL
{ddl}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
create table TBL_USER (
id int not null auto_increment,
username varchar(255) not null,
email varchar(255) not null,
password varchar(255) not null,
primary key (id)
);

create table TBL_ACCOUNT (
id int not null auto_increment,
accountNumber varchar(255) not null,
user_id int not null,
balance decimal(10, 2) not null,
openDate date not null,
primary key (id),
foreign key (user_id) references TBL_USER(id)
);
16 changes: 16 additions & 0 deletions spring-ai-alibaba-examples/usercase-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>

<description>User Cases Examples</description>
<artifactId>usercase-example</artifactId>
<packaging>pom</packaging>

<modules>
<module>text-classifier</module>
<module>generate-sql-queries</module>
</modules>

</project>
Loading