130 字
1 分钟
调用Deepseek接口
毕设上准备加入点AI相关的创新点,原本想用协同过滤算法的,但是感觉还是大模型更吸引眼球,就调用了一下API玩一玩。
配置文件key和url。创建config.properties配置文件
key = ***url = https://api.deepseek.com/chat/completions打印返回体
public class TypewriterEffect { public static void printWord(String text,int delay){ for(int i = 0; i从配置文件中读取key和url。添加配置文件的目的是为了充分解耦。
private static String API_KEY; private static String API_URL; //从配置文件中获取key和url static { Properties properties = new Properties(); try{ InputStream is = DeepSeekClient.class.getClassLoader().getResourceAsStream("config.properties"); properties.load(is); API_KEY = properties.getProperty("key"); API_URL = properties.getProperty("url"); }catch(Exception e){ throw new RuntimeException(e); } }运行类
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input = ""; System.out.println("请提问"); while(true){ String question = scanner.next(); if(question.equals("exit")){ break; } ask(question);//询问 System.out.println(); } }请求响应类的结构
//定义内部类请求响应结构 static class Message{ private String content; private String role; public Message(String content, String role) { this.content = content; this.role = role; } public String getContent() { return content; } }请求体
//请求体 static class ChatRequest{ private String model; private List messages; private double temperature; private int max_tokens; public ChatRequest(String model, List messages, double temperature, int max_tokens) { this.model = model; this.messages = messages; this.temperature = temperature; this.max_tokens = max_tokens; } }响应体
//响应体 static class ChatResponse{ private List choices; public List getChoices() { return choices; } static class Choice{ private Message message; public Message getMessage() { return message; } } }询问方法
public static void ask(String content) { //创建消息列表 List messages = new ArrayList<>(); messages.add(new Message(content,"user")); ChatRequest requestBody = new ChatRequest( "deepseek-chat",//配置模型 messages, 0.7, 1000 ); System.out.println("正在提交问题"); long start = System.currentTimeMillis(); //发送请求 String response = sendRequest(requestBody); long endTime = System.currentTimeMillis(); System.out.println("思考时间: "+ (endTime - start) + "毫秒");//计算响应时间 TypewriterEffect.printWord(response,0);//在控制栏打印结果 }发送请求方法
private static String sendRequest(ChatRequest requestBody){ HttpClient client = HttpClient.newHttpClient(); Gson gson = new Gson(); //将ChatRequest 对象中封装的数据转为JSON格式 String requestBodyJson = gson.toJson(requestBody); try{ HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(API_URL))//配置url .header("Content-Type","application/json") .header("Authorization","Bearer "+API_KEY)//配置key .POST(HttpRequest.BodyPublishers.ofString(requestBodyJson))//将返回数据转成json格式 .build(); System.out.println("问题以提交,正在思考中"); HttpResponse response = client.send(request,HttpResponse.BodyHandlers.ofString()); if(response.statusCode() == 200){ ChatResponse chatResponse = gson.fromJson(response.body(),ChatResponse.class); return chatResponse.getChoices().get(0).getMessage().getContent(); }else{ return "请求失败,状态码: " + response.statusCode()+" ,响应: "+response.body(); } } catch (Exception e) { e.printStackTrace(); return "请求异常:" + e.getMessage(); } } 分享
如果这篇文章对你有帮助,欢迎分享给更多人!
部分信息可能已经过时
相关文章 智能推荐
1
DeepSeek大模型推荐功能实现
AI 应用 舍友给了一个deepseek的KEY,本着不浪费的原则,顺便学习新知识,就在我的电子商城里加入了AI智能推荐。
2
JAVA学习笔记–JVM(1)
后端开发 JVM:Java虚拟机,.class代码运行的地方
3
JAVA学习笔记–反射
后端开发 反射机制被视作动态语言的关键,允许程序在执行期间借助ReflectionAPI获取任何类的内部信息,并直接操作类的内部属性方法。
4
Mybatis-Plus学习笔记(2)
后端开发 温馨提示:服务层实现的时候,需要让YourService 继承ServiceImpl(因为这个原因,测了两个小时…)
5
Mybatis-Plus学习笔记(1)
后端开发 特性






