-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: yuluo-yx <[email protected]>
- Loading branch information
Showing
31 changed files
with
600 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 的使用。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
spring-ai-alibaba-examples/usercase-example/generate-sql-queries/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
13 changes: 13 additions & 0 deletions
13
...se-example/generate-sql-queries/src/main/java/com/alibaba/cloud/ai/sql/AIApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...generate-sql-queries/src/main/java/com/alibaba/cloud/ai/sql/controller/SQLController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
...e-example/generate-sql-queries/src/main/java/com/alibaba/cloud/ai/sql/entity/Request.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |
6 changes: 6 additions & 0 deletions
6
...-example/generate-sql-queries/src/main/java/com/alibaba/cloud/ai/sql/entity/Response.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { } |
53 changes: 53 additions & 0 deletions
53
...ample/generate-sql-queries/src/main/java/com/alibaba/cloud/ai/sql/service/SQLService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...alibaba-examples/usercase-example/generate-sql-queries/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
30 changes: 30 additions & 0 deletions
30
spring-ai-alibaba-examples/usercase-example/generate-sql-queries/src/main/resources/data.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
INSERT INTO TBL_USER (username, email, password) | ||
VALUES | ||
('user1', '[email protected]', 'password1'), | ||
('user2', '[email protected]', 'password2'), | ||
('user3', '[email protected]', 'password3'), | ||
('user4', '[email protected]', 'password4'), | ||
('user5', '[email protected]', 'password5'), | ||
('user6', '[email protected]', 'password6'), | ||
('user7', '[email protected]', 'password7'), | ||
('user8', '[email protected]', 'password8'), | ||
('user9', '[email protected]', 'password9'), | ||
('user10', '[email protected]', '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'); |
11 changes: 11 additions & 0 deletions
11
...a-examples/usercase-example/generate-sql-queries/src/main/resources/prompt/sql-queries.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
17 changes: 17 additions & 0 deletions
17
...g-ai-alibaba-examples/usercase-example/generate-sql-queries/src/main/resources/schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.