当前位置: 首页 > wzjs >正文

网站权限设计方案今天的军事新闻

网站权限设计方案,今天的军事新闻,广东营销型网站建设报价,wordpress二级目录伪静态Spring Boot 集成国内AI,包含文心一言、通义千问和讯飞星火平台实战教程 一、项目结构二、添加Maven依赖三、配置API密钥 (application.yml)四、配置类1. AI配置类 (AiProperties.java)2. 启用配置类 (AiConfig.java) 五、服务层实现1. 文心一言服务 (WenxinService…

Spring Boot 集成国内AI,包含文心一言、通义千问和讯飞星火平台实战教程

  • 一、项目结构
  • 二、添加Maven依赖
  • 三、配置API密钥 (application.yml)
  • 四、配置类
    • 1. AI配置类 (AiProperties.java)
    • 2. 启用配置类 (AiConfig.java)
  • 五、服务层实现
    • 1. 文心一言服务 (WenxinService.java)
    • 2. 通义千问服务 (QianwenService.java)
    • 3. 讯飞星火服务 (XinghuoService.java)
  • 六、统一控制器 (AiController.java)
  • 七、安全增强配置
    • 1. 添加API密钥保护(自定义Filter)
    • 2. 添加Rate Limiting(使用Resilience4j)
  • 八、应用入口 (AiIntegrationApplication.java)
  • 九、测试示例
  • 十、最佳实践建议

Spring Boot集成国内主流AI平台的详细实现方案,包含文心一言、通义千问和讯飞星火的对接代码,助力快速构建智能应用。

一、项目结构

ai-integration-demo/
├── src/main/java
│   ├── com/example/ai
│   │   ├── config                # 配置类
│   │   ├── controller            # API控制器
│   │   ├── service               # 服务层
│   │   │   ├── impl              # 服务实现
│   │   ├── dto                   # 数据传输对象
├── resources
│   ├── application.yml           # 配置文件

二、添加Maven依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- HTTP客户端 --><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>4.11.0</version></dependency><!-- JSON处理 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency><!-- 配置处理 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency>
</dependencies>

三、配置API密钥 (application.yml)

ai:wenxin:api-key: YOUR_WENXIN_API_KEYsecret-key: YOUR_WENXIN_SECRET_KEYapi-url: https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completionsqianwen:api-key: YOUR_QIANWEN_API_KEYapi-url: https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generationxinghuo:api-key: YOUR_XINGHUO_API_KEYsecret: YOUR_XINGHUO_SECRETappid: YOUR_XINGHUO_APPIDapi-url: https://spark-api.xf-yun.com/v3.5/chat

四、配置类

1. AI配置类 (AiProperties.java)

@ConfigurationProperties(prefix = "ai")
@Data
public class AiProperties {private Wenxin wenxin;private Qianwen qianwen;private Xinghuo xinghuo;@Datapublic static class Wenxin {private String apiKey;private String secretKey;private String apiUrl;}@Datapublic static class Qianwen {private String apiKey;private String apiUrl;}@Datapublic static class Xinghuo {private String apiKey;private String secret;private String appid;private String apiUrl;}
}

2. 启用配置类 (AiConfig.java)

@Configuration
@EnableConfigurationProperties(AiProperties.class)
public class AiConfig {@Beanpublic OkHttpClient okHttpClient() {return new OkHttpClient();}
}

五、服务层实现

1. 文心一言服务 (WenxinService.java)

@Service
@RequiredArgsConstructor
public class WenxinService {private final AiProperties aiProperties;private final OkHttpClient okHttpClient;// 获取AccessTokenprivate String getAccessToken() {String url = "https://aip.baidubce.com/oauth/2.0/token?"+ "grant_type=client_credentials"+ "&client_id=" + aiProperties.getWenxin().getApiKey()+ "&client_secret=" + aiProperties.getWenxin().getSecretKey();Request request = new Request.Builder().url(url).get().build();try (Response response = okHttpClient.newCall(request).execute()) {String responseBody = response.body().string();ObjectMapper objectMapper = new ObjectMapper();JsonNode rootNode = objectMapper.readTree(responseBody);return rootNode.get("access_token").asText();} catch (Exception e) {throw new RuntimeException("获取文心一言Token失败", e);}}public String chatCompletion(String prompt) {String accessToken = getAccessToken();String url = aiProperties.getWenxin().getApiUrl() + "?access_token=" + accessToken;JSONObject body = new JSONObject();body.put("messages", new JSONArray().put(new JSONObject().put("role", "user").put("content", prompt)));Request request = new Request.Builder().url(url).post(RequestBody.create(body.toString(), MediaType.get("application/json"))).build();try (Response response = okHttpClient.newCall(request).execute()) {if (!response.isSuccessful()) {throw new RuntimeException("文心一言API请求失败: " + response);}String responseBody = response.body().string();JSONObject jsonResponse = new JSONObject(responseBody);return jsonResponse.getJSONObject("result").getString("content");} catch (Exception e) {throw new RuntimeException("调用文心一言API出错", e);}}
}

2. 通义千问服务 (QianwenService.java)

@Service
@RequiredArgsConstructor
public class QianwenService {private final AiProperties aiProperties;private final OkHttpClient okHttpClient;public String generateText(String prompt) {JSONObject body = new JSONObject();body.put("model", "qwen-turbo");JSONObject input = new JSONObject();input.put("prompt", prompt);body.put("input", input);JSONObject parameters = new JSONObject();parameters.put("temperature", 0.85);parameters.put("top_p", 0.8);parameters.put("max_tokens", 1500);body.put("parameters", parameters);Request request = new Request.Builder().url(aiProperties.getQianwen().getApiUrl()).header("Authorization", "Bearer " + aiProperties.getQianwen().getApiKey()).post(RequestBody.create(body.toString(), MediaType.get("application/json"))).build();try (Response response = okHttpClient.newCall(request).execute()) {if (!response.isSuccessful()) {throw new RuntimeException("通义千问API请求失败: " + response);}String responseBody = response.body().string();JSONObject jsonResponse = new JSONObject(responseBody);return jsonResponse.getJSONObject("output").getString("text");} catch (Exception e) {throw new RuntimeException("调用通义千问API出错", e);}}
}

3. 讯飞星火服务 (XinghuoService.java)

@Service
@RequiredArgsConstructor
public class XinghuoService {private final AiProperties aiProperties;private final OkHttpClient okHttpClient;public String chat(String prompt) {try {// 构造鉴权URLString authUrl = generateAuthUrl();// 构造请求体JSONObject body = new JSONObject();JSONObject header = new JSONObject();header.put("app_id", aiProperties.getXinghuo().getAppid());JSONObject parameter = new JSONObject();JSONObject chat = new JSONObject();chat.put("domain", "generalv3.5");chat.put("temperature", 0.5);chat.put("max_tokens", 4096);parameter.put("chat", chat);JSONObject payload = new JSONObject();JSONObject message = new JSONObject();JSONArray text = new JSONArray();text.put(new JSONObject().put("role", "user").put("content", prompt));message.put("text", text);payload.put("message", message);body.put("header", header);body.put("parameter", parameter);body.put("payload", payload);// 发送请求Request request = new Request.Builder().url(authUrl).post(RequestBody.create(body.toString(), MediaType.get("application/json"))).build();try (Response response = okHttpClient.newCall(request).execute()) {if (!response.isSuccessful()) {throw new RuntimeException("讯飞星火API请求失败: " + response);}String responseBody = response.body().string();JSONObject jsonResponse = new JSONObject(responseBody);return extractContent(jsonResponse);}} catch (Exception e) {throw new RuntimeException("调用讯飞星火API出错", e);}}// 生成带鉴权信息的URLprivate String generateAuthUrl() throws ParseException, InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException {String apiUrl = aiProperties.getXinghuo().getApiUrl();String host = new URL(apiUrl).getHost();String path = new URL(apiUrl).getPath();// 创建日期对象SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);sdf.setTimeZone(TimeZone.getTimeZone("GMT"));String date = sdf.format(new Date());// 构造签名String signatureOrigin = "host: " + host + "\n";signatureOrigin += "date: " + date + "\n";signatureOrigin += "POST " + path + " HTTP/1.1";Mac mac = Mac.getInstance("hmacsha256");SecretKeySpec secretKeySpec = new SecretKeySpec(aiProperties.getXinghuo().getSecret().getBytes("UTF-8"), "hmacsha256");mac.init(secretKeySpec);byte[] signatureSha = mac.doFinal(signatureOrigin.getBytes("UTF-8"));String signature = Base64.getEncoder().encodeToString(signatureSha);// 构造授权头String authorization = String.format("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"",aiProperties.getXinghuo().getApiKey(), "hmac-sha256", "host date request-line", signature);return apiUrl + "?authorization=" + Base64.getEncoder().encodeToString(authorization.getBytes("UTF-8"))+ "&date=" + URLEncoder.encode(date, "UTF-8")+ "&host=" + URLEncoder.encode(host, "UTF-8");}// 提取响应内容private String extractContent(JSONObject response) {JSONObject payload = response.getJSONObject("payload");JSONObject message = payload.getJSONObject("message");JSONArray text = message.getJSONArray("text");StringBuilder result = new StringBuilder();for (int i = 0; i < text.length(); i++) {JSONObject textObj = text.getJSONObject(i);if (textObj.has("content")) {result.append(textObj.getString("content"));}}return result.toString();}
}

六、统一控制器 (AiController.java)

@RestController
@RequestMapping("/api/ai")
@RequiredArgsConstructor
public class AiController {private final WenxinService wenxinService;private final QianwenService qianwenService;private final XinghuoService xinghuoService;@PostMapping("/wenxin")public ResponseEntity<String> wenxinChat(@RequestBody @Valid AiRequest request) {return ResponseEntity.ok(wenxinService.chatCompletion(request.getPrompt()));}@PostMapping("/qianwen")public ResponseEntity<String> qianwenGenerate(@RequestBody @Valid AiRequest request) {return ResponseEntity.ok(qianwenService.generateText(request.getPrompt()));}@PostMapping("/xinghuo")public ResponseEntity<String> xinghuoChat(@RequestBody @Valid AiRequest request) {return ResponseEntity.ok(xinghuoService.chat(request.getPrompt()));}@Datastatic class AiRequest {@NotBlank(message = "提示语不能为空")private String prompt;}
}

七、安全增强配置

1. 添加API密钥保护(自定义Filter)

@Component
@RequiredArgsConstructor
public class ApiKeyFilter extends OncePerRequestFilter {private final AiProperties aiProperties;@Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {String clientId = request.getHeader("X-API-CLIENT-ID");String clientSecret = request.getHeader("X-API-CLIENT-SECRET");// 验证客户端凭证if (!isValidCredentials(clientId, clientSecret)) {response.sendError(HttpStatus.UNAUTHORIZED.value(), "无效的API凭证");return;}filterChain.doFilter(request, response);}private boolean isValidCredentials(String clientId, String clientSecret) {// 这里应该是从数据库或配置中读取验证信息// 简化示例:使用配置中的文心API密钥做演示return clientId != null && clientSecret != null && clientId.equals("demo-app") && clientSecret.equals(aiProperties.getWenxin().getApiKey());}
}

2. 添加Rate Limiting(使用Resilience4j)

@Configuration
public class RateLimiterConfig {@Beanpublic RateLimiter wenxinRateLimiter() {return RateLimiter.of("wenxin-limiter", RateLimiterConfig.custom().limitRefreshPeriod(Duration.ofSeconds(10)).limitForPeriod(5).timeoutDuration(Duration.ofSeconds(5)).build());}
}// 在控制器中使用
@RestController
@RequestMapping("/api/ai")
public class AiController {private final RateLimiter wenxinRateLimiter;@PostMapping("/wenxin")@RateLimiter(name = "wenxin-limiter")public ResponseEntity<String> wenxinChat(@RequestBody AiRequest request) {// ...}
}

八、应用入口 (AiIntegrationApplication.java)

@SpringBootApplication
public class AiIntegrationApplication {public static void main(String[] args) {SpringApplication.run(AiIntegrationApplication.class, args);}
}

九、测试示例

使用cURL测试:

# 通义千问测试
curl -X POST http://localhost:8080/api/ai/qianwen \-H "Content-Type: application/json" \-d '{"prompt": "用100字介绍Spring Boot"}'# 文心一言测试
curl -X POST http://localhost:8080/api/ai/wenxin \-H "Content-Type: application/json" \-d '{"prompt": "用Java写一个快速排序算法"}'# 讯飞星火测试
curl -X POST http://localhost:8080/api/ai/xinghuo \-H "Content-Type: application/json" \-d '{"prompt": "如何做好电商运营"}'

十、最佳实践建议

  1. 异步处理:使用@Async注解异步调用AI接口,避免阻塞
  2. 缓存结果:对常见问题的结果进行缓存,减少API调用
  3. 错误重试:实现指数退避重试机制处理临时错误
  4. 流量控制:针对不同AI平台设置不同的QPS限制
  5. 统一接口:创建统一的AI门面服务,提供平台无关的调用
@Service
public class AiFacadeService {private enum AiProvider { WENXIN, QIANWEN, XINGHUO }private final WenxinService wenxinService;private final QianwenService qianwenService;private final XinghuoService xinghuoService;public String unifiedChat(String prompt) {// 简单轮询策略AiProvider[] providers = AiProvider.values();AiProvider provider = providers[(int)(System.currentTimeMillis() % providers.length)];switch (provider) {case WENXIN: return wenxinService.chatCompletion(prompt);case QIANWEN: return qianwenService.generateText(prompt);case XINGHUO: return xinghuoService.chat(prompt);default: throw new IllegalStateException("未知的AI提供商");}}
}

本项目提供了完整的企业级Spring Boot集成国内主流AI平台的实现方案,可根据实际需求进行扩展和优化。

http://www.dtcms.com/wzjs/832052.html

相关文章:

  • 创建一个网站的步骤是加盟类网站建设
  • 微信公众号怎么做链接网站吗福州网站设计大概多少钱
  • 网站建设意见征求汇报婚礼策划网站模板中文
  • 罗湖网站制作公司淘宝网站代做
  • 网站设计作品如何做外围网站的代理
  • 有那些可以自己做壁纸的网站北京单页营销型网站
  • 纯静态网站 后台wordpress自动注册
  • 企业门户网站建设思路网站建设续费是那些
  • 网站策划书结尾开发银行助学贷款系统
  • 西安专业做淘宝网站的公司营销型网站和传统网站区别
  • 外贸网站推广平台排名投标网招标网
  • 天津市城乡建设网站家装网站模板下载
  • index网站制作人被备案了会有什么后果
  • 网站负责人核验照外贸产品网站建设
  • 做网站开什么端口广告公司创意广告语
  • 成都网站优化排名建设通官网app下载
  • 金坛市常州网络推广福建企业seo推广
  • 临潼区建设局网站一起做业官方网站
  • 58同城济南网站建设如何做互联网营销
  • 兖矿东华建设网站免费网站空间免备案
  • 类似饿了么的网站怎么做.net 网站开发教程
  • ui设计的网站物语系列短篇资源WordPress
  • 常用的网站类型有哪些类型有哪些类型有哪些金融网站设计方向
  • 周口网站建设网站新闻置顶怎么做
  • 抖音营销网站建设价格网站建设教程试题
  • 建设网站包括哪些费用学校网站建设考评办法
  • 长春火车站附近美食对象存储 做视频网站
  • 济南建设银行网站帮做网站的
  • 分辨率大于1920的网站怎么做兰州网站建设优化制作公司
  • 做搜狗手机网站快速排长春九台建设局网站