自定义 QueryTransformer 查询转换器(第三方API)

今天花了点时间实现了AI超级智能体教程中的扩展点用第三方api翻译提示词,下面是实现过程,如果对大家有用请点点免费的赞赞,谢谢啦 (≧ω≦)/

我这里用到的api是腾讯的,因为免费٩(๑❛ᴗ❛๑)۶

一、依赖引入

xml
复制代码
<!-- 腾讯云机器翻译sdk --> <dependency> <groupId>com.tencentcloudapi</groupId> <artifactId>tencentcloud-sdk-java-tmt</artifactId> <version>3.1.1195</version> </dependency>

二、实现

由于并非主要流程所以没有进行二次封装了

java
复制代码
/** * @version v1.0.0 * @belongsProject: feng-ai-agent * @belongsPackage: com.qcdfz.fengaiagent.rag * @author: fgh * @description: 查询翻译转换器,自动将查询内容翻译为目标语言 * @createTime: 2025-05-13 11:17 */ @Slf4j public class TencentTranslationQueryTransformer { private final String secretId; private final String secretKey; private final String region; private final String sourceLang; private final String targetLang; private TencentTranslationQueryTransformer(Builder builder) { Assert.notNull(builder.secretId,"SecretId 不能为空"); Assert.notNull(builder.secretId,"secretKey 不能为空"); Assert.notNull(builder.targetLang,"目标语言不能为空"); this.secretId = builder.secretId; this.secretKey = builder.secretKey; this.region = builder.region; this.sourceLang = builder.sourceLang; this.targetLang = builder.targetLang; } public static Builder builder() { return new Builder(); } /** * Builder模式 */ public static final class Builder { private String secretId; private String secretKey; private String region = "ap-guangzhou" ; private String sourceLang = "auto"; private String targetLang; public Builder secretId(String secretId) { this.secretId = secretId; return this; } public Builder secretKey(String secretKey) { this.secretKey = secretKey; return this; } public Builder region(String region) { this.region = region; return this; } public Builder sourceLang(String sourceLang) { this.sourceLang = sourceLang; return this; } public Builder targetLang(String targetLang) { this.targetLang = targetLang; return this; } public TencentTranslationQueryTransformer build() { return new TencentTranslationQueryTransformer(this); } } /** * 翻译查询内容 * @param query 原始查询 * @return 翻译后的查询 */ public String transform(String query) { if (!StringUtils.hasText(query)) { return query; } try { Credential cred = new Credential(secretId, secretKey); HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint("tmt.tencentcloudapi.com"); ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); TmtClient client = new TmtClient(cred, region, clientProfile); TextTranslateRequest req = new TextTranslateRequest(); req.setSourceText(query); req.setSource(sourceLang); req.setTarget(targetLang); req.setProjectId(0L); TextTranslateResponse resp = client.TextTranslate(req); return resp.getTargetText(); } catch (TencentCloudSDKException e) { log.error("调用腾讯云翻译API失败: {}", e.getMessage(), e); // 失败时返回原文 return query; } } }

支持语言枚举类

java
复制代码
/** * @version v1.0.0 * @belongsProject: feng-ai-agent * @belongsPackage: com.qcdfz.fengaiagent.enums * @author: fgh * @description: 腾讯云机器翻译支持的目标语言枚举 * @createTime: 2025-05-13 12:01 */ public enum TencentTranslateTargetLangEnum { /** 简体中文 */ ZH("zh", "简体中文"), /** 繁体中文 */ ZH_TW("zh-TW", "繁体中文"), /** 英语 */ EN("en", "英语"), /** 日语 */ JA("ja", "日语"), /** 韩语 */ KO("ko", "韩语"), /** 法语 */ FR("fr", "法语"), /** 西班牙语 */ ES("es", "西班牙语"), /** 意大利语 */ IT("it", "意大利语"), /** 德语 */ DE("de", "德语"), /** 土耳其语 */ TR("tr", "土耳其语"), /** 俄语 */ RU("ru", "俄语"), /** 葡萄牙语 */ PT("pt", "葡萄牙语"), /** 越南语 */ VI("vi", "越南语"), /** 印尼语 */ ID("id", "印尼语"), /** 泰语 */ TH("th", "泰语"), /** 马来语 */ MS("ms", "马来语"), /** 阿拉伯语 */ AR("ar", "阿拉伯语"), /** 印地语 */ HI("hi", "印地语"); private final String value; private final String text; TencentTranslateTargetLangEnum(String value, String text) { this.value = value; this.text = text; } public String getValue() { return value; } public String getText() { return text; } }

三、使用方法

java
复制代码
@SpringBootTest class TencentTranslationQueryTransformerTest { @Resource GlobalApiKeyProperties globalApiKeyProperties; @Test void transform() { TencentTranslationQueryTransformer build = TencentTranslationQueryTransformer.builder() .secretId(globalApiKeyProperties.getTencent().getSecretId()) .secretKey(globalApiKeyProperties.getTencent().getSecretKey()) .targetLang(TencentTranslateTargetLangEnum.EN.getValue()) .build(); String transform = build.transform("你好我是程序员:青春的疯子"); Assertions.assertNotNull(transform); } }

四、效果

0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
青春的疯子
下载 APP