修改:AI大模型接入-http接入

This commit is contained in:
huangge1199 2025-05-14 20:14:34 +08:00
parent 68f3f0d050
commit 3a3c4fc51c
3 changed files with 60 additions and 1 deletions

View File

@ -12,4 +12,6 @@ import com.alibaba.dashscope.exception.NoApiKeyException;
*/ */
public interface InvokeService { public interface InvokeService {
GenerationResult callWithMessage() throws NoApiKeyException, InputRequiredException; GenerationResult callWithMessage() throws NoApiKeyException, InputRequiredException;
String getMsgByHttp(String question);
} }

View File

@ -2,6 +2,9 @@ package com.huangge1199.aiagent.Service.impl;
import java.util.Arrays; 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.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam; import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult; import com.alibaba.dashscope.aigc.generation.GenerationResult;
@ -14,7 +17,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
* SdkAiInvoke * InvokeServiceImpl
* *
* @author huangge1199 * @author huangge1199
* @since 2025/5/14 12:27:47 * @since 2025/5/14 12:27:47
@ -46,4 +49,46 @@ public class InvokeServiceImpl implements InvokeService {
.build(); .build();
return gen.call(param); 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();
}
} }

View File

@ -11,6 +11,7 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping; 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -38,4 +39,15 @@ public class InvokeController {
return R.fail(e.getMessage()); return R.fail(e.getMessage());
} }
} }
@PostMapping("/http")
@Operation(summary = "http接入")
public R<String> httpAiInvoke(@RequestBody String question) {
try {
String result = invokeService.getMsgByHttp(question);
return R.ok(result);
} catch (ApiException e) {
return R.fail(e.getMessage());
}
}
} }