-
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.
Merge pull request #323 from xiaohai-78/main-base-newsplugin
add Express Delivery function calling
- Loading branch information
Showing
7 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100/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,61 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright 2024-2025 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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.alibaba.cloud.ai</groupId> | ||
<artifactId>spring-ai-alibaba</artifactId> | ||
<version>${revision}</version> | ||
<relativePath>../../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>spring-ai-alibaba-starter-function-calling-kuaidi100</artifactId> | ||
<name>spring-ai-alibaba-starter-function-calling-kuaidi100</name> | ||
<description>Sina News tool for Spring AI Alibaba</description> | ||
<url>https://github.com/alibaba/spring-ai-alibaba</url> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.springframework.ai</groupId> | ||
<artifactId>spring-ai-spring-boot-autoconfigure</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-configuration-processor</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-annotations</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.github.kuaidi100-api</groupId> | ||
<artifactId>sdk</artifactId> | ||
<version>1.0.15</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<finalName>spring-ai-alibaba-starter-function-calling-kuaidi100</finalName> | ||
</build> | ||
</project> |
43 changes: 43 additions & 0 deletions
43
.../main/java/com/alibaba/cloud/ai/functioncalling/kuaidi100/Kuaidi100AutoConfiguration.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,43 @@ | ||
/* | ||
* Copyright 2024-2025 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.functioncalling.kuaidi100; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Description; | ||
|
||
/** | ||
* @Author: XiaoYunTao | ||
* @Date: 2024/12/18 | ||
*/ | ||
@Configuration | ||
@ConditionalOnClass(Kuaidi100AutoConfiguration.class) | ||
@ConditionalOnProperty(prefix = "spring.ai.alibaba.functioncalling.kuaidi100", name = "enabled", havingValue = "true") | ||
@EnableConfigurationProperties(Kuaidi100Properties.class) | ||
public class Kuaidi100AutoConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
@Description("Query courier tracking information") | ||
public Kuaidi100Service queryTrackFunction(Kuaidi100Properties kuaidi100Properties) { | ||
return new Kuaidi100Service(kuaidi100Properties); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...i100/src/main/java/com/alibaba/cloud/ai/functioncalling/kuaidi100/Kuaidi100Exception.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,28 @@ | ||
/* | ||
* Copyright 2024-2025 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.functioncalling.kuaidi100; | ||
|
||
/** | ||
* @Author: XiaoYunTao | ||
* @Date: 2024/12/26 | ||
*/ | ||
public class Kuaidi100Exception extends RuntimeException { | ||
|
||
public Kuaidi100Exception(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
...100/src/main/java/com/alibaba/cloud/ai/functioncalling/kuaidi100/Kuaidi100Properties.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,59 @@ | ||
/* | ||
* Copyright 2024-2025 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.functioncalling.kuaidi100; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
/** | ||
* @Author: XiaoYunTao | ||
* @Date: 2024/12/25 | ||
*/ | ||
@ConfigurationProperties(prefix = "spring.ai.alibaba.functioncalling.kuaidi100") | ||
public class Kuaidi100Properties { | ||
|
||
/** | ||
* 授权key <a href="https://api.kuaidi100.com/manager/v2/myinfo/enterprise">获取授权key</a> | ||
*/ | ||
private String key; | ||
|
||
/** | ||
* customer | ||
* <a href="https://api.kuaidi100.com/manager/v2/myinfo/enterprise">获取customer</a> | ||
*/ | ||
private String customer; | ||
|
||
public Kuaidi100Properties(String key, String customer) { | ||
this.key = key; | ||
this.customer = customer; | ||
} | ||
|
||
public void setKey(String key) { | ||
this.key = key; | ||
} | ||
|
||
public void setCustomer(String customer) { | ||
this.customer = customer; | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
public String getCustomer() { | ||
return customer; | ||
} | ||
|
||
} |
104 changes: 104 additions & 0 deletions
104
...idi100/src/main/java/com/alibaba/cloud/ai/functioncalling/kuaidi100/Kuaidi100Service.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,104 @@ | ||
/* | ||
* Copyright 2024-2025 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.functioncalling.kuaidi100; | ||
|
||
import com.fasterxml.jackson.annotation.JsonClassDescription; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonPropertyDescription; | ||
import com.google.gson.Gson; | ||
import com.kuaidi100.sdk.api.AutoNum; | ||
import com.kuaidi100.sdk.api.QueryTrack; | ||
import com.kuaidi100.sdk.request.AutoNumReq; | ||
import com.kuaidi100.sdk.request.QueryTrackParam; | ||
import com.kuaidi100.sdk.request.QueryTrackReq; | ||
import com.kuaidi100.sdk.response.QueryTrackResp; | ||
import com.kuaidi100.sdk.utils.SignUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.function.Function; | ||
|
||
/** | ||
* @Author: XiaoYunTao | ||
* @Date: 2024/12/25 | ||
*/ | ||
public class Kuaidi100Service implements Function<Kuaidi100Service.Request, QueryTrackResp> { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(Kuaidi100Service.class); | ||
|
||
private final Gson gson = new Gson(); | ||
|
||
private final AutoNum autoNum = new AutoNum(); | ||
|
||
final Kuaidi100Properties kuaidi100Properties; | ||
|
||
public Kuaidi100Service(Kuaidi100Properties kuaidi100Properties) { | ||
this.kuaidi100Properties = kuaidi100Properties; | ||
} | ||
|
||
@Override | ||
public QueryTrackResp apply(Kuaidi100Service.Request request) { | ||
QueryTrackResp queryTrackResp; | ||
try { | ||
queryTrackResp = queryTrack(request.num()); | ||
logger.debug("queryTrackResp: {}", queryTrackResp); | ||
} | ||
catch (Exception e) { | ||
logger.error("Error occurred while querying track!", e); | ||
throw new Kuaidi100Exception("Error querying track.", e); | ||
} | ||
return queryTrackResp; | ||
} | ||
|
||
private QueryTrackResp queryTrack(String num) throws Exception { | ||
String key = kuaidi100Properties.getKey(); | ||
String customer = kuaidi100Properties.getCustomer(); | ||
|
||
QueryTrackParam queryTrackParam = createQueryTrackParam(num, key); | ||
String param = gson.toJson(queryTrackParam); | ||
|
||
QueryTrackReq queryTrackReq = createQueryTrackReq(customer, param, key); | ||
return new QueryTrack().queryTrack(queryTrackReq); | ||
} | ||
|
||
private QueryTrackParam createQueryTrackParam(String num, String key) throws Exception { | ||
AutoNumReq autoNumReq = new AutoNumReq(); | ||
autoNumReq.setNum(num); | ||
autoNumReq.setKey(key); | ||
String company = autoNum.getFirstComByNum(autoNumReq); | ||
|
||
QueryTrackParam queryTrackParam = new QueryTrackParam(); | ||
queryTrackParam.setCom(company); | ||
queryTrackParam.setNum(num); | ||
return queryTrackParam; | ||
} | ||
|
||
private QueryTrackReq createQueryTrackReq(String customer, String param, String key) { | ||
QueryTrackReq queryTrackReq = new QueryTrackReq(); | ||
queryTrackReq.setParam(param); | ||
queryTrackReq.setCustomer(customer); | ||
queryTrackReq.setSign(SignUtils.querySign(param, key, customer)); | ||
return queryTrackReq; | ||
} | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@JsonClassDescription("Methods for real-time courier tracking") | ||
public record Request( | ||
@JsonProperty(required = true, value = "num") @JsonPropertyDescription("tracking number") String num) { | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
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 @@ | ||
com.alibaba.cloud.ai.functioncalling.kuaidi100.Kuaidi100AutoConfiguration |
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