做一个问答界面,然后调用AI大模型的接口,将问题输入给AI,结果返回到帆软报表界面
package com.fr.function;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
//调用AI模型工具类
public class DeepSeekAIClientWithHttpClient {
//AI模型接口
private static final String API_URL = "https://api.deepseek.com/v1/chat/completions";
//接口秘钥
private static final String API_KEY = "your_api_key_here";
public static String askDeepSeekAI(String question) throws Exception {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(API_URL);
// 设置请求头
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Authorization", "Bearer " + API_KEY);
// 构建请求体
JSONObject requestBody = new JSONObject();
requestBody.put("model", "deepseek-chat");
JSONObject message = new JSONObject();
message.put("role", "user");
message.put("content", question);
requestBody.put("messages", new JSONObject[] {message});
requestBody.put("temperature", 0.7);
httpPost.setEntity(new StringEntity(requestBody.toString()));
// 发送请求并处理响应
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
// 解析JSON响应
JSONObject jsonResponse = new JSONObject(responseString);
return jsonResponse.getJSONArray("choices")
.getJSONObject(0)
.getJSONObject("message")
.getString("content");
}
}
}
}
package com.fr.function;
import com.fr.script.AbstractFunction;
import com.fr.log.FineLoggerFactory;
import java.io.File;
//帆软自定义函数,调用这个函数,返回AI模型回答结果
public class GetFileAbsPath extends AbstractFunction {
public Object run(Object[] args) {
//用户输入问题
String para = args[0].toString().trim();
//返回AI模型回答结果
return DeepSeekAIClientWithHttpClient(para);
}
}