Skip to content

Commit

Permalink
feat: format the name of baidu map function calling and replace the f…
Browse files Browse the repository at this point in the history
…astjson with jackson
  • Loading branch information
CoderSerio committed Dec 24, 2024
1 parent 83908e0 commit c14ff4b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
<artifactId>jackson-annotations</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-spring-boot-autoconfigure</artifactId>
Expand All @@ -46,12 +51,6 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
@ConditionalOnClass(MapSearchService.class)
@EnableConfigurationProperties(BaiDuMapProperties.class)
@ConditionalOnProperty(prefix = "spring.ai.alibaba.functioncalling.baidumap", name = "enabled", havingValue = "true")
public class BaiDuMapConfiguration {
public class BaiDuMapAutoConfiguration {

@Bean
@ConditionalOnMissingBean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@

package com.alibaba.cloud.ai.functioncalling.baidumap;

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 com.fasterxml.jackson.databind.ObjectMapper;

import java.util.function.Function;

Expand All @@ -33,23 +31,25 @@ public class MapSearchService implements Function<MapSearchService.Request, MapS

private final MapTools mapTools;

private final ObjectMapper objectMapper;

public MapSearchService(BaiDuMapProperties baiDuMapProperties) {
this.mapTools = new MapTools(baiDuMapProperties);
this.objectMapper = new ObjectMapper();
}

@Override
public Response apply(Request request) {
try {
JSONObject jsonObject = new JSONObject();
var jsonObject = new com.fasterxml.jackson.databind.node.ObjectNode(objectMapper.getNodeFactory());

String addressCityCodeResponse = mapTools.getAddressCityCode(request.address);
JSONObject cityCodeJson = JSON.parseObject(addressCityCodeResponse);
JSONArray districtsArray = cityCodeJson.getJSONArray("districts");
var cityCodeJson = objectMapper.readTree(addressCityCodeResponse);
var districtsArray = cityCodeJson.path("districts");

if (districtsArray != null && !districtsArray.isEmpty()) {
for (int i = 0; i < districtsArray.size(); i++) {
JSONObject district = districtsArray.getJSONObject(i);
String adcode = district.getString("adcode");
if (districtsArray.isArray() && districtsArray.size() > 0) {
for (var district : districtsArray) {
String adcode = district.path("adcode").asText();

if (adcode != null && !adcode.isEmpty()) {
String weather = mapTools.getWeather(adcode);
Expand All @@ -58,9 +58,9 @@ public Response apply(Request request) {
}

String facilityInformationJson = mapTools.getFacilityInformation(request.address, request.facilityType);
JSONArray facilityResults = JSON.parseObject(facilityInformationJson).getJSONArray("results");
if (facilityResults != null) {
jsonObject.put("facilityInformation", facilityResults.toJSONString());
var facilityResults = objectMapper.readTree(facilityInformationJson).path("results");
if (facilityResults.isArray()) {
jsonObject.set("facilityInformation", facilityResults);
}
else {
jsonObject.put("facilityInformation", "No facility information found.");
Expand All @@ -70,7 +70,7 @@ public Response apply(Request request) {
return new Response("No districts found in the response.");
}

return new Response(jsonObject.toJSONString());
return new Response(objectMapper.writeValueAsString(jsonObject));
}
catch (Exception e) {
return new Response("Error occurred while processing the request: " + e.getMessage());
Expand All @@ -88,4 +88,4 @@ public record Request(
public record Response(String message) {
}

}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
com.alibaba.cloud.ai.functioncalling.baidumap.BaiDuMapConfiguration
com.alibaba.cloud.ai.functioncalling.baidumap.BaiDuMapAutoConfiguration

0 comments on commit c14ff4b

Please sign in to comment.