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

Feature/gaodemap #133

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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;
Expand All @@ -27,14 +26,13 @@
* @author YunLong
*/
@Configuration
@ConditionalOnClass(DingTalkService.class)
@EnableConfigurationProperties(DingTalkProperties.class)
@ConditionalOnProperty(prefix = "spring.ai.alibaba.plugin.dingtalk", name = "enabled", havingValue = "true")
public class DingTalkConfiguration {

@Bean
@ConditionalOnMissingBean
@Description("Send DingTalk group chat messages using a custom robot")
@ConditionalOnProperty(prefix = "spring.ai.alibaba.plugin.dingtalk", name = "enabled", havingValue = "true")
public DingTalkService dingTalkGroupSendMessageByCustomRobotFunction(DingTalkProperties dingTalkProperties) {
return new DingTalkService(dingTalkProperties);
}
Expand Down
84 changes: 84 additions & 0 deletions community/plugins/spring-ai-alibaba-starter-plugin-gaode/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Copyright 2023-2024 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 http://maven.apache.org/maven-v4_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-plugin-gaode</artifactId>
<name>spring-ai-alibaba-starter-plugin-gaode</name>
<description>Gao De Map tool for Spring AI Alibaba</description>

<properties>

</properties>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<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.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.alibaba.cloud.ai.plugin.gaode;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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.
*/
import com.alibaba.cloud.ai.plugin.gaode.function.WeatherSearchFunction;
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 YunLong
*/
@Configuration
@EnableConfigurationProperties(GaoDeProperties.class)
@ConditionalOnProperty(prefix = "spring.ai.alibaba.plugin.gaode", name = "enabled", havingValue = "true")
public class GaoDeConfiguration {

@Bean
@ConditionalOnMissingBean
@Description("Get weather information according to address.")
public WeatherSearchFunction gaoDeGetAddressWeatherFunction(GaoDeProperties gaoDeProperties) {
return new WeatherSearchFunction(gaoDeProperties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.alibaba.cloud.ai.plugin.gaode;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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.
*/
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* @author YunLong
*/
@ConfigurationProperties(prefix = "spring.ai.alibaba.plugin.gaode")
public class GaoDeProperties {

// Official Document Address: https://lbs.amap.com/api/webservice/summary
private String webApiKey;

public GaoDeProperties() {}

public GaoDeProperties(String webApiKey) {
this.webApiKey = webApiKey;
}

public String getWebApiKey() {
return webApiKey;
}

public void setWebApiKey(String webApiKey) {
this.webApiKey = webApiKey;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.alibaba.cloud.ai.plugin.gaode.function;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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.
*/
import com.alibaba.cloud.ai.plugin.gaode.GaoDeProperties;
import com.alibaba.cloud.ai.plugin.gaode.service.WebService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonClassDescription;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;

import java.util.function.Function;

/**
* @author YunLong
*/
public class WeatherSearchFunction implements Function<WeatherSearchFunction.Request, WeatherSearchFunction.Response> {

private final WebService webService;

public WeatherSearchFunction(GaoDeProperties gaoDeProperties) {
this.webService = new WebService(gaoDeProperties);
}

@Override
public Response apply(Request request) {

String responseBody = webService.getAddressCityCode(request.address);

String adcode = "";

try {
JSONObject jsonObject = JSON.parseObject(responseBody);
JSONArray geocodesArray = jsonObject.getJSONArray("geocodes");
if (geocodesArray != null && !geocodesArray.isEmpty()) {
JSONObject firstGeocode = geocodesArray.getJSONObject(0);
adcode = firstGeocode.getString("adcode");
}
} catch (Exception e){
return new Response("Error occurred while processing the request.");
}

String weather = webService.getWeather(adcode);

return new Response(weather);
}

@JsonClassDescription("Get the weather conditions for a specified address.")
public record Request(
@JsonProperty(required = true, value = "address")
@JsonPropertyDescription("The address") String address) {
}

public record Response(String message) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.alibaba.cloud.ai.plugin.gaode.service;

import com.alibaba.cloud.ai.plugin.gaode.GaoDeProperties;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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.
*/
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

/**
* @author YunLong
*/

public class WebService {

private final String baseUrl = "https://restapi.amap.com/v3";

private final GaoDeProperties gaoDeProperties;

private final HttpClient httpClient;

public WebService(GaoDeProperties gaoDeProperties){
this.gaoDeProperties = gaoDeProperties;

this.httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.build();

if (Objects.isNull(gaoDeProperties.getWebApiKey())){
throw new RuntimeException("Please configure your GaoDe API key in the application.yml file.");
}
}

/**
* Geographic/Inverse Geocoding
* @param address
* @return https://lbs.amap.com/api/webservice/guide/api/georegeo#s2
*/
public String getAddressCityCode(String address) {

String path = String.format("/geocode/geo?key=%s&address=%s", gaoDeProperties.getWebApiKey(), address);

HttpRequest httpRequest = createGetRequest(path);

CompletableFuture<HttpResponse<String>> responseFuture = httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString());

HttpResponse<String> response = responseFuture.join();

if (response.statusCode() != 200){
throw new RuntimeException("Failed to get address city code");
}

return response.body();
}


/**
* Weather Information
* @param cityCode
* @return https://lbs.amap.com/api/webservice/guide/api/weatherinfo#s0
*/
public String getWeather(String cityCode) {
String path = String.format("/weather/weatherInfo?key=%s&city=%s&extensions=%s", gaoDeProperties.getWebApiKey(), cityCode, "all");

HttpRequest httpRequest = createGetRequest(path);

CompletableFuture<HttpResponse<String>> responseFuture = httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString());

HttpResponse<String> response = responseFuture.join();

if (response.statusCode() != 200){
throw new RuntimeException("Failed to get weather information");
}

return response.body();
}

private HttpRequest createGetRequest(String path) {
URI uri = URI.create(baseUrl + path);

return HttpRequest.newBuilder()
.uri(uri)
.GET()
.build();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.alibaba.cloud.ai.plugin.gaode.GaoDeConfiguration
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<module>community/plugins/spring-ai-alibaba-starter-plugin-baidusearch</module>
<module>community/plugins/spring-ai-alibaba-starter-plugin-bingsearch</module>
<module>community/plugins/spring-ai-alibaba-starter-plugin-dingtalk</module>
<module>community/plugins/spring-ai-alibaba-starter-plugin-gaode</module>
</modules>

<properties>
Expand Down
6 changes: 6 additions & 0 deletions spring-ai-alibaba-examples/function-calling-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
<version>${spring-ai-alibaba.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-plugin-gaode</artifactId>
<version>${spring-ai-alibaba.version}</version>
</dependency>

</dependencies>

<build>
Expand Down
Loading