Skip to content

Commit 53f0f9b

Browse files
authored
Merge pull request #71 from springaialibaba/0308-yuluo/add-examples
feat: add more example
2 parents 29a624e + 9b3e1a4 commit 53f0f9b

File tree

24 files changed

+600
-0
lines changed

24 files changed

+600
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 使用 Spring AI Alibaba 总结文本内容
2+
3+
使用示例
4+
5+
```shell
6+
curl -X POST -F file='./saa.pdf' http://localhost:10091/summarize
7+
```

spring-ai-alibaba-sql-example/pom.xml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<parent>
24+
<groupId>com.alibaba.cloud.ai</groupId>
25+
<artifactId>spring-ai-alibaba-examples</artifactId>
26+
<version>${revision}</version>
27+
<relativePath>../pom.xml</relativePath>
28+
</parent>
29+
30+
<artifactId>spring-ai-alibaba-text-summarizer-example</artifactId>
31+
<version>0.0.1-SNAPSHOT</version>
32+
<name>Spring AI Alibaba Text Summarizer Example</name>
33+
<description>Spring AI Alibaba Text Summarizer Example</description>
34+
35+
<properties>
36+
<maven.compiler.source>17</maven.compiler.source>
37+
<maven.compiler.target>17</maven.compiler.target>
38+
<maven-deploy-plugin.version>3.1.1</maven-deploy-plugin.version>
39+
</properties>
40+
41+
<dependencies>
42+
<dependency>
43+
<groupId>com.alibaba.cloud.ai</groupId>
44+
<artifactId>spring-ai-alibaba-starter</artifactId>
45+
<version>${spring-ai-alibaba.version}</version>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-web</artifactId>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>org.springframework.ai</groupId>
55+
<artifactId>spring-ai-tika-document-reader</artifactId>
56+
</dependency>
57+
</dependencies>
58+
59+
<build>
60+
<plugins>
61+
<plugin>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-maven-plugin</artifactId>
64+
</plugin>
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-deploy-plugin</artifactId>
68+
<version>${maven-deploy-plugin.version}</version>
69+
<configuration>
70+
<skip>true</skip>
71+
</configuration>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
76+
<repositories>
77+
<repository>
78+
<id>spring-milestones</id>
79+
<name>Spring Milestones</name>
80+
<url>https://repo.spring.io/milestone</url>
81+
<snapshots>
82+
<enabled>false</enabled>
83+
</snapshots>
84+
</repository>
85+
</repositories>
86+
87+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.alibaba.example.summarizer;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SQLApplication {
8+
9+
public static void main(String[] args) {
10+
11+
SpringApplication.run(SQLApplication.class, args);
12+
}
13+
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.alibaba.example.summarizer;
2+
3+
public class SQLGenerationException extends RuntimeException {
4+
5+
public SQLGenerationException(String response) {
6+
super(response);
7+
}
8+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.alibaba.example.summarizer;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ProblemDetail;
5+
import org.springframework.web.bind.annotation.ExceptionHandler;
6+
import org.springframework.web.bind.annotation.RestControllerAdvice;
7+
8+
@RestControllerAdvice
9+
public class SQLGenerationExceptionHandler {
10+
11+
@ExceptionHandler(SQLGenerationException.class)
12+
public ProblemDetail handle(SQLGenerationException ex) {
13+
14+
return ProblemDetail.forStatusAndDetail(
15+
HttpStatus.EXPECTATION_FAILED,
16+
ex.getMessage()
17+
);
18+
}
19+
20+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.alibaba.example.summarizer.controller;
2+
3+
import java.io.IOException;
4+
import java.nio.charset.Charset;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import com.alibaba.example.summarizer.SQLGenerationException;
9+
10+
import org.springframework.ai.chat.client.ChatClient;
11+
import org.springframework.beans.factory.annotation.Value;
12+
import org.springframework.core.io.Resource;
13+
import org.springframework.jdbc.core.JdbcTemplate;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.RequestBody;
16+
import org.springframework.web.bind.annotation.RestController;
17+
18+
@RestController
19+
public class SQLController {
20+
21+
@Value("classpath:/schema.sql")
22+
private Resource ddlResource;
23+
24+
@Value("classpath:/sql-prompt-template.st")
25+
private Resource sqlPromptTemplateResource;
26+
27+
private final ChatClient chatClient;
28+
private final JdbcTemplate jdbcTemplate;
29+
30+
public SQLController(
31+
ChatClient.Builder aiClientBuilder,
32+
JdbcTemplate jdbcTemplate
33+
) {
34+
35+
this.chatClient = aiClientBuilder.build();
36+
this.jdbcTemplate = jdbcTemplate;
37+
}
38+
39+
@PostMapping(path = "/sql")
40+
public Answer sql(@RequestBody SqlRequest sqlRequest) throws IOException {
41+
42+
String schema = ddlResource.getContentAsString(Charset.defaultCharset());
43+
44+
String query = chatClient.prompt()
45+
.user(userSpec -> userSpec
46+
.text(sqlPromptTemplateResource)
47+
.param("question", sqlRequest.question())
48+
.param("ddl", schema)
49+
)
50+
.call()
51+
.content();
52+
53+
assert query != null;
54+
if (query.toLowerCase().startsWith("select")) {
55+
56+
return new Answer(
57+
query,
58+
jdbcTemplate.queryForList(query)
59+
);
60+
}
61+
62+
throw new SQLGenerationException(query);
63+
}
64+
65+
public record SqlRequest(String question) { }
66+
67+
public record Answer(String sqlQuery, List<Map<String, Object>> results) { }
68+
69+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
server:
2+
port: 10091
3+
4+
spring:
5+
application:
6+
name: spring-ai-alibaba-text-summarizer-example
7+
8+
ai:
9+
dashscope:
10+
api-key: ${AI_DASHSCOPE_API_KEY}
11+
12+
servlet:
13+
multipart:
14+
max-file-size: 5MB

spring-ai-alibaba-sql-example/src/main/resources/data.sql

Whitespace-only changes.

spring-ai-alibaba-sql-example/src/main/resources/schema.sql

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Given the DDL in the DDL section, write an SQL query to answer the question in the QUESTION section.
2+
Only produce select queries. If the question would result in an insert, update,
3+
or delete, or if the query would alter the DDL in any way, say that the operation
4+
isn't supported. If the question can't be answered, say that the DDL doesn't support
5+
answering that question.
6+
7+
Answer with the raw SQL query only; no markdown or other punctuation that isn't part of the query itself.
8+
9+
10+
QUESTION
11+
{question}
12+
13+
DDL
14+
{ddl}

0 commit comments

Comments
 (0)