From 1ff142fc75297ef69ca3de3813c3342d61de3de3 Mon Sep 17 00:00:00 2001
From: erasernoob <1490910338@qq.com>
Date: Wed, 25 Dec 2024 22:31:08 +0800
Subject: [PATCH 1/4] update: merge the pullrequest
---
.../pom.xml | 65 ++++++++++
.../GoogleTranslateAutoConfiguration.java | 27 ++++
.../GoogleTranslateProperties.java | 38 ++++++
.../GoogleTranslateService.java | 118 ++++++++++++++++++
.../googletranslate/MainApplication.java | 12 ++
...ot.autoconfigure.AutoConfiguration.imports | 1 +
.../src/test/java/GoogleTranslateTest.java | 41 ++++++
.../translate/TranslateProperties.java | 1 -
pom.xml | 1 +
9 files changed, 303 insertions(+), 1 deletion(-)
create mode 100644 community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/pom.xml
create mode 100644 community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/src/main/java/com/alibaba/cloud/ai/functioncalling/googletranslate/GoogleTranslateAutoConfiguration.java
create mode 100644 community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/src/main/java/com/alibaba/cloud/ai/functioncalling/googletranslate/GoogleTranslateProperties.java
create mode 100644 community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/src/main/java/com/alibaba/cloud/ai/functioncalling/googletranslate/GoogleTranslateService.java
create mode 100644 community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/src/main/java/com/alibaba/cloud/ai/functioncalling/googletranslate/MainApplication.java
create mode 100644 community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/src/main/resources/META-INF.spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
create mode 100644 community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/src/test/java/GoogleTranslateTest.java
diff --git a/community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/pom.xml b/community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/pom.xml
new file mode 100644
index 000000000..334a2208b
--- /dev/null
+++ b/community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/pom.xml
@@ -0,0 +1,65 @@
+
+
+ 4.0.0
+
+ com.alibaba.cloud.ai
+ spring-ai-alibaba
+ ${revision}
+ ../../../pom.xml
+
+
+spring-ai-alibaba-starter-function-calling-googletranslate
+spring-ai-alibaba-starter-plugin-googletranslate
+Translate tool for Spring AI Alibaba
+
+
+
+ org.springframework.ai
+ spring-ai-spring-boot-autoconfigure
+
+
+
+ org.springframework.boot
+ spring-boot-configuration-processor
+ true
+
+
+
+ org.springframework.boot
+ spring-boot-starter-webflux
+
+
+
+ com.google.code.gson
+ gson
+
+
+ org.springframework.boot
+ spring-boot-test
+ test
+
+
+ org.springframework.ai
+ spring-ai-core
+ test
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.jetbrains
+ annotations
+ 24.0.1
+ test
+
+
+
+
+
+spring-ai-alibaba-plugin-googletranslate
+
+
\ No newline at end of file
diff --git a/community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/src/main/java/com/alibaba/cloud/ai/functioncalling/googletranslate/GoogleTranslateAutoConfiguration.java b/community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/src/main/java/com/alibaba/cloud/ai/functioncalling/googletranslate/GoogleTranslateAutoConfiguration.java
new file mode 100644
index 000000000..2d6431c44
--- /dev/null
+++ b/community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/src/main/java/com/alibaba/cloud/ai/functioncalling/googletranslate/GoogleTranslateAutoConfiguration.java
@@ -0,0 +1,27 @@
+package com.alibaba.cloud.ai.functioncalling.googletranslate;
+
+
+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 erasernoob
+ */
+@ConditionalOnClass({ GoogleTranslateService.class })
+@EnableConfigurationProperties(GoogleTranslateProperties.class)
+@ConditionalOnProperty(prefix = "spring.ai.alibaba.functioncalling.googletranlate", name = "enabled",
+ havingValue = "true")
+public class GoogleTranslateAutoConfiguration {
+
+ @Bean
+ @ConditionalOnMissingBean
+ @Description("Implement natural language translation capabilities.")
+ public GoogleTranslateService GoogleTranslateFunction(GoogleTranslateProperties properties) {
+ return new GoogleTranslateService(properties);
+ }
+}
diff --git a/community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/src/main/java/com/alibaba/cloud/ai/functioncalling/googletranslate/GoogleTranslateProperties.java b/community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/src/main/java/com/alibaba/cloud/ai/functioncalling/googletranslate/GoogleTranslateProperties.java
new file mode 100644
index 000000000..19f2e9753
--- /dev/null
+++ b/community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/src/main/java/com/alibaba/cloud/ai/functioncalling/googletranslate/GoogleTranslateProperties.java
@@ -0,0 +1,38 @@
+/*
+ * 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.
+ */
+
+package com.alibaba.cloud.ai.functioncalling.googletranslate;
+
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+/**
+ * @author erasernoob
+ */
+@ConfigurationProperties(prefix = "spring.ai.alibaba.plugin.googletranslate")
+public class GoogleTranslateProperties {
+
+ private String apiKey;
+
+ public String getApiKey() {
+ return apiKey;
+ }
+
+ public void setApiKey(String apiKey) {
+ this.apiKey = apiKey;
+ }
+}
diff --git a/community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/src/main/java/com/alibaba/cloud/ai/functioncalling/googletranslate/GoogleTranslateService.java b/community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/src/main/java/com/alibaba/cloud/ai/functioncalling/googletranslate/GoogleTranslateService.java
new file mode 100644
index 000000000..44e8185a7
--- /dev/null
+++ b/community/function-calling/spring-ai-alibaba-starter-function-calling-googletranslate/src/main/java/com/alibaba/cloud/ai/functioncalling/googletranslate/GoogleTranslateService.java
@@ -0,0 +1,118 @@
+/*
+ * 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.
+ */
+
+package com.alibaba.cloud.ai.functioncalling.googletranslate;
+import com.fasterxml.jackson.annotation.JsonClassDescription;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonPropertyDescription;
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpHeaders;
+import org.springframework.util.StringUtils;
+import org.springframework.web.reactive.function.client.WebClient;
+import org.springframework.web.util.UriComponentsBuilder;
+import reactor.core.publisher.Mono;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+
+/**
+ * @author erasernoob
+ */
+public class GoogleTranslateService implements Function {
+
+ private static final Logger log = LoggerFactory.getLogger(GoogleTranslateService.class);
+
+ private static final String TRANSLATE_HOST = "https://translation.googleapis.com";
+
+ private static final String TRANSLATE_PATH = "/language/translate/v2";
+
+ private final GoogleTranslateProperties properties;
+
+ private final WebClient webClient;
+
+ public GoogleTranslateService(GoogleTranslateProperties properties) {
+ assert StringUtils.hasText(properties.getApiKey());
+ this.properties = properties;
+ this.webClient = WebClient.builder()
+ .defaultHeader("Content-Type", "application/json")
+ .build();
+ }
+ @Override
+ public Response apply(Request request) {
+ if ( request == null || !StringUtils.hasText(properties.getApiKey()) ||
+ !StringUtils.hasText(request.text) || !StringUtils.hasText(request.targetLanguage)
+ ) {return null;}
+
+ String requestUrl = UriComponentsBuilder.fromHttpUrl(TRANSLATE_HOST + TRANSLATE_PATH)
+ .queryParam("key", properties.getApiKey())
+ .queryParam("target", request.targetLanguage)
+ .queryParam("q", request.text)
+ .queryParam("format", "text")
+ .toUriString();
+ try {
+ Mono responseMono = webClient.post().uri(requestUrl).retrieve().bodyToMono(String.class);
+
+ String responseData = responseMono.block();
+ assert responseData != null;
+ log.info("GoogleTranslation request: {}, response: {}", request.text, responseData);
+ return parseResponseData(responseData, request.text);
+ } catch (Exception e) {
+ log.error("Using the googleTranslate service failed due to {}", e.getMessage());
+ }
+ return null;
+ }
+
+ private Response parseResponseData(String responseData, String q) {
+ Gson gson = new Gson();
+ Map response = gson.fromJson(responseData,
+ new TypeToken