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,44 @@
<?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 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,28 @@
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,13 @@
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,44 @@
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,89 @@
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 @@ -53,6 +53,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/document-readers/github-document-reader</module>
<module>community/document-readers/poi-document-reader</module>
Expand Down
6 changes: 6 additions & 0 deletions spring-ai-alibaba-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@
<artifactId>micrometer-observation-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
xiaohai-78 marked this conversation as resolved.
Show resolved Hide resolved
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-function-calling-kuaidi100</artifactId>
<version>${revision}</version>
<scope>test</scope>
</dependency>
</dependencies>

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

import com.alibaba.cloud.ai.dashscope.DashscopeAiTestConfiguration;
import com.alibaba.cloud.ai.functioncalling.kuaidi100.Kuaidi100AutoConfiguration;
import com.alibaba.cloud.ai.functioncalling.kuaidi100.Kuaidi100Properties;
import com.alibaba.cloud.ai.functioncalling.kuaidi100.Kuaidi100Service;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
* @Author: XiaoYunTao
* @Date: 2024/12/26
*/
@SpringBootTest(classes = { Kuaidi100AutoConfiguration.class, DashscopeAiTestConfiguration.class })
@EnabledIfEnvironmentVariable(named = "DASHSCOPE_API_KEY", matches = ".+")
public class kuaidi100Test {

@Autowired
ChatModel dashscopeChatModel;

@Autowired
Kuaidi100Properties kuaidi100Properties;

@Test
public void test() {
ChatClient chatClient = ChatClient.builder(dashscopeChatModel).build();
String content = chatClient.prompt()
.function("queryTrackFunction", "查询快递", new Kuaidi100Service(kuaidi100Properties))
.user("查询一下单号为:,的快递")
.call()
.content();
System.out.println(content);
}

}
Loading