31 lines
982 B
Java
31 lines
982 B
Java
package cn.hezhaohui.webhook.service;
|
|
|
|
import cn.hezhaohui.webhook.constant.AIConstant;
|
|
import cn.hutool.http.HttpRequest;
|
|
import cn.hutool.http.HttpResponse;
|
|
import cn.hutool.http.HttpUtil;
|
|
import cn.hutool.json.JSONUtil;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
@Service
|
|
public class AIService {
|
|
public String getAIResponse(String prompt) {
|
|
Map<String, Object> body = new HashMap<>();
|
|
body.put("model", AIConstant.AI_MODEL);
|
|
body.put("messages", List.of(Map.of("role", "user", "content", prompt)));
|
|
|
|
String jsonBody = JSONUtil.toJsonStr(body);
|
|
|
|
HttpResponse response = HttpRequest.post(AIConstant.AI_URL)
|
|
.header("Content-Type", "application/json")
|
|
.header("Authorization", "Bearer " + AIConstant.AI_TOKEN)
|
|
.body(jsonBody)
|
|
.execute();
|
|
return "AI response: " + response.body();
|
|
}
|
|
}
|