From 3a3c4fc51c21e0bc553e0a75ff1d7b1189d2d98c Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Wed, 14 May 2025 20:14:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9AAI=E5=A4=A7=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E6=8E=A5=E5=85=A5-http=E6=8E=A5=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aiagent/Service/InvokeService.java | 2 + .../Service/impl/InvokeServiceImpl.java | 47 ++++++++++++++++++- .../controller/invoke/InvokeController.java | 12 +++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/huangge1199/aiagent/Service/InvokeService.java b/src/main/java/com/huangge1199/aiagent/Service/InvokeService.java index 3fc2e73..e758c00 100644 --- a/src/main/java/com/huangge1199/aiagent/Service/InvokeService.java +++ b/src/main/java/com/huangge1199/aiagent/Service/InvokeService.java @@ -12,4 +12,6 @@ import com.alibaba.dashscope.exception.NoApiKeyException; */ public interface InvokeService { GenerationResult callWithMessage() throws NoApiKeyException, InputRequiredException; + + String getMsgByHttp(String question); } diff --git a/src/main/java/com/huangge1199/aiagent/Service/impl/InvokeServiceImpl.java b/src/main/java/com/huangge1199/aiagent/Service/impl/InvokeServiceImpl.java index b44370b..f2b910b 100644 --- a/src/main/java/com/huangge1199/aiagent/Service/impl/InvokeServiceImpl.java +++ b/src/main/java/com/huangge1199/aiagent/Service/impl/InvokeServiceImpl.java @@ -2,6 +2,9 @@ package com.huangge1199.aiagent.Service.impl; import java.util.Arrays; +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import cn.hutool.json.JSONObject; import com.alibaba.dashscope.aigc.generation.Generation; import com.alibaba.dashscope.aigc.generation.GenerationParam; import com.alibaba.dashscope.aigc.generation.GenerationResult; @@ -14,7 +17,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; /** - * SdkAiInvoke + * InvokeServiceImpl * * @author huangge1199 * @since 2025/5/14 12:27:47 @@ -46,4 +49,46 @@ public class InvokeServiceImpl implements InvokeService { .build(); return gen.call(param); } + + @Override + public String getMsgByHttp(String question) { + // API URL + String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation"; + + // 构造请求数据 + JSONObject requestBody = new JSONObject(); + requestBody.put("model", "qwen-plus"); + + // 构建 messages 数组 + JSONObject systemMessage = new JSONObject(); + systemMessage.put("role", "system"); + systemMessage.put("content", "You are a helpful assistant."); + + JSONObject userMessage = new JSONObject(); + userMessage.put("role", "user"); + userMessage.put("content", question); + + // 将 messages 数组放入 input 对象 + JSONObject input = new JSONObject(); + input.put("messages", new Object[]{systemMessage, userMessage}); + requestBody.put("input", input); + + // 构建 parameters 对象 + JSONObject parameters = new JSONObject(); + parameters.put("result_format", "message"); + requestBody.put("parameters", parameters); + + // 将请求体转换为字符串 + String jsonData = requestBody.toString(); + + // 发送请求 + HttpResponse response = HttpRequest.post(url) + .header("Authorization", "Bearer " + baiLianKey) + .header("Content-Type", "application/json") + .body(jsonData) + .execute(); + + // 获取响应内容 + return response.body(); + } } diff --git a/src/main/java/com/huangge1199/aiagent/controller/invoke/InvokeController.java b/src/main/java/com/huangge1199/aiagent/controller/invoke/InvokeController.java index b4bcba7..9794a30 100644 --- a/src/main/java/com/huangge1199/aiagent/controller/invoke/InvokeController.java +++ b/src/main/java/com/huangge1199/aiagent/controller/invoke/InvokeController.java @@ -11,6 +11,7 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -38,4 +39,15 @@ public class InvokeController { return R.fail(e.getMessage()); } } + + @PostMapping("/http") + @Operation(summary = "http接入") + public R httpAiInvoke(@RequestBody String question) { + try { + String result = invokeService.getMsgByHttp(question); + return R.ok(result); + } catch (ApiException e) { + return R.fail(e.getMessage()); + } + } }