import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/**
* date: 2025/3/15
*/
public class DeepSeek {
private String apiKey = "向官网申请一个API密钥";
private String apiUrl = "https://api.deepseek.com/chat/completions";//请求网站
public String getFoodInfo(String foodName) throws IOException {
//发送请求的内容
String gs = foodName+",你给我该食物的名称,热量,营养成分,好处,坏处,图片地址。如 草莓 32/49(卡路里/克) " +
"碳水化合物:约8克,主要为天然糖。膳食纤维:约2克,有助于消化和饱腹感。维生素C:约98%的每日推荐摄入量,支持免疫系统和皮肤健康。" +
"锰:约24%的每日推荐摄入量,有助于骨骼健康和代谢功能。抗氧化剂:如花青素,有助于减少炎症和氧化应激。 " +
"增强免疫力:高维生素C含量有助于增强免疫系统。支持心脏健康:抗氧化剂有助于降低胆固醇和血压。促进消化:纤维有助于预防便秘。 " +
"农药残留:草莓常被列为农药残留较高的水果之一,建议清洗干净或选择有机草莓。过敏反应:部分人可能对草莓过敏。 " +
"/static/images/strawberry.jpg ,每个属性用一个@键隔开";
// 创建 OkHttpClient 实例,设置超时时间
OkHttpClient client = new OkHttpClient().newBuilder()
.readTimeout(30, TimeUnit.SECONDS) // 读取超时
.connectTimeout(30, TimeUnit.SECONDS) // 连接超时
.build();
// 构建请求体(JSON 格式)
MediaType mediaType = MediaType.parse("application/json");
String jsonBody = "{\n" +
" \"messages\": [\n" +
" {\n" +
" \"content\": \"You are a helpful assistant\",\n" +
" \"role\": \"system\"\n" +
" },\n" +
" {\n" +
" \"content\": \""+gs+"\",\n" +
" \"role\": \"user\"\n" +
" }\n" +
" ],\n" +
" \"model\": \"deepseek-chat\",\n" +
" \"frequency_penalty\": 0,\n" +
" \"max_tokens\": 2048,\n" +
" \"presence_penalty\": 0,\n" +
" \"response_format\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"stop\": null,\n" +
" \"stream\": false,\n" +
" \"stream_options\": null,\n" +
" \"temperature\": 1,\n" +
" \"top_p\": 1,\n" +
" \"tools\": null,\n" +
" \"tool_choice\": \"none\",\n" +
" \"logprobs\": false,\n" +
" \"top_logprobs\": null\n" +
"}";
RequestBody body = RequestBody.create(mediaType, jsonBody);
System.out.println("key -- >"+apiKey +"\nurl -- >"+apiUrl);
// 构建请求
Request request = new Request.Builder()
.url(apiUrl)
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.addHeader("Authorization", "Bearer " + apiKey) // 添加认证头
.build();
// 发送请求并处理响应
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
String responseBody = response.body().string();
JSONObject jsonObject = JSON.parseObject(responseBody);
// System.out.println("Response: " + responseBody);
// System.out.println(jsonObject.toJSONString());
//提取 choices 数组
JSONArray choicesArray = jsonObject.getJSONArray("choices");
// 遍历 choices 数组
for (int i = 0; i < choicesArray.size(); i++) {
JSONObject choiceObject = choicesArray.getJSONObject(i);
JSONObject messageObject = choiceObject.getJSONObject("message");
// 提取 content 属性
String content = messageObject.getString("content");
System.out.println("Content: " + content);
//我们最后想要的结果
return content;
}
} else {
System.out.println("Request failed with code: " + response.code());
System.out.println("Error message: " + response.body().string());
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
下面是申请密钥的步骤