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

add Express Delivery function calling #323

Merged
merged 11 commits into from
Jan 13, 2025
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>
xiaohai-78 marked this conversation as resolved.
Show resolved Hide resolved

<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>
xiaohai-78 marked this conversation as resolved.
Show resolved Hide resolved

</dependencies>

<build>
<finalName>spring-ai-alibaba-starter-function-calling-kuaidi100</finalName>
xiaohai-78 marked this conversation as resolved.
Show resolved Hide resolved
</build>
</project>
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);
}

}
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);
}

}
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;
}

}
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) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.alibaba.cloud.ai.functioncalling.kuaidi100.Kuaidi100AutoConfiguration
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<module>community/function-calling/spring-ai-alibaba-starter-function-calling-sinanews</module>
<module>community/function-calling/spring-ai-alibaba-starter-function-calling-toutiaonews</module>
<module>community/function-calling/spring-ai-alibaba-starter-function-calling-yuque</module>
<module>community/function-calling/spring-ai-alibaba-starter-function-calling-kuaidi100</module>
<module>community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate</module>
<module>community/function-calling/spring-ai-alibaba-starter-function-calling-alitranslate</module>

Expand Down
Loading