springboot对接deepseek & sse流式输出 & 多轮对话推理demo & 接入豆包/千帆/讯飞
链接
Vue版本AI对话组件 - 直接用这个组件优化界面
semi-ui-vue聊天组件 - 可以用这个组件优化界面
SSE(Server-Sent Events),解密AI流式输出和呈现
Hi-Dream-Blog - 参考这个博客,可以在后台将markdown语法转为html。
markdown-it基本使用 - 可以参考这个,可以在前端将md语法转为html
【Java】SpringBoot模拟流式输出,前端使用流式接收数据并打印
Spring WebFlux 实现 SSE 流式回复:类GPT逐字显示回复效果完整指南
在springboot项目中调用通义千问api多轮对话并实现流式输出
10分钟上手DeepSeek开发:SpringBoot + Vue2快速构建AI对话系统
ai-chat_back - okhttp deepseek
SpringBoot 集成 WebFlux,请求大模型实现 “打字机” 流式响应效果
二次开发一个ChatGPT网站SpringBoot+MongoDB+Vue - B站视频,代码:chatgpt-assistant,文档地址
开源!一个项目集成知识库/图片理解/图片生成/文本流对话 - B站视频,代码:uni-ai,文档地址
全新SpringBoot+SpringAI+Vue3大模型全栈开发 - B站视频,代码:dive-into-spring-ai,文档地址
手把手教你本地部署DeepSeek大模型(零基础也能搞定!)
文章目录
deepseek
效果
登录deepseek官网,打开API开放平台,创建API Key
使用刚刚得到的key,调用deepseek的api
curl https://api.deepseek.com/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <DeepSeek API Key>" \
-d '{
"model": "deepseek-chat",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"stream": false
}'
代码
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-boot-dependencies</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.8.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>demo-sse</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version>
</dependency>
</dependencies>
</project>
DeepSeekController
@RestController
@RequestMapping("/deepseek")
public class DeepSeekController {
@Autowired
private DeepSeekClient deepSeekClient;
@RequestMapping(value = "chatCompletions", produces = "text/event-stream;charset=utf-8")
public Flux<String> chatCompletions(@RequestParam(required = true, value = "content") String content) {
return deepSeekClient.chatCompletions(content);
}
@RequestMapping(value = "chatCompletions2", produces = "text/event-stream;charset=utf-8")
public SseEmitter chatCompletions2(@RequestParam(required = true, value = "content2") String content2) {
return deepSeekClient.chatCompletions2(content2);
}
}
WebConfig
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**")
.maxAge(3600)
.allowCredentials(true)
.allowedOrigins("*")
.allowedMethods("*")
.allowedHeaders("*")
.exposedHeaders("token", "Authorization")
;
}
}
DeepSeekClient
package com.zzhua.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zzhua.pojo.AiChatMessage;
import com.zzhua.pojo.AiChatRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import java.io.IOException;
import java.util.Arrays;
@Slf4j
@Component
public class DeepSeekClient {
private static final ObjectMapper mapper = new ObjectMapper();
public Flux<String> chatCompletions(String content) {
AiChatMessage chatMsg1 = new AiChatMessage("system", "You are a helpful assistant.");
AiChatMessage chatMsg2 = new AiChatMessage("user", content);
AiChatRequest request = new AiChatRequest();
request.setModel("deepseek-chat");
request.setMessages(Arrays.asList(chatMsg1, chatMsg2));
// 流式输出
request.setStream(true);
return WebClient.builder()
.baseUrl("https://api.deepseek.com")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.defaultHeader("Authorization", "Bearer sk-xxx")
.build()
.post()
.uri("/chat/completions")
.body(BodyInserters.fromObject(request))
.retrieve()
.bodyToFlux(String.class)
.flatMap(this::handleResult);
}
private Flux<String> handleResult(String result) {
if ("[DONE]".equals(result)) {
return Flux.empty();
} else {
try {
JsonNode jsonNode = mapper.readTree(result);
String content = jsonNode.get("choices").get(0).get("delta").get("content").asText();
System.out.println(content);
return Flux.just(content);
} catch (Exception e) {
log.error("解析失败: {}", result);
}
}
return Flux.empty();
}
public SseEmitter chatCompletions2(String content2) {
SseEmitter sseEmitter = new SseEmitter();
AiChatMessage chatMsg1 = new AiChatMessage("system", "You are a helpful assistant.");
AiChatMessage chatMsg2 = new AiChatMessage("user", content2);
AiChatRequest request = new AiChatRequest();
request.setModel("deepseek-chat");
request.setMessages(Arrays.asList(chatMsg1, chatMsg2));
// 流式输出
request.setStream(true);
WebClient.builder()
.baseUrl("https://api.deepseek.com")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.defaultHeader("Authorization", "Bearer sk-xxx")
.build()
.post()
.uri("/chat/completions")
.body(BodyInserters.fromObject(request))
.retrieve()
.bodyToFlux(String.class)
.flatMap(this::handleResult)
.doOnComplete(()->{
sseEmitter.complete();
log.warn("结束");
})
.subscribe(data->{
try {
sseEmitter.send(SseEmitter.event().data(data));
} catch (IOException e) {
e.printStackTrace();
}
});
return sseEmitter;
}
public static void main(String[] args) throws IOException {
RestTemplate restTemplate = new RestTemplate();
ObjectMapper mapper = new ObjectMapper();
AiChatMessage chatMsg1 = new AiChatMessage("system", "You are a helpful assistant.");
AiChatMessage chatMsg2 = new AiChatMessage("user", "");
AiChatRequest request = new AiChatRequest();
request.setModel("deepseek-chat");
request.setMessages(Arrays.asList(chatMsg1, chatMsg2));
request.setStream(false);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
headers.add("Authorization", "Bearer sk-xxx");
HttpEntity<AiChatRequest> requestEntity = new HttpEntity<>(request, headers);
String response = restTemplate.exchange("https://api.deepseek.com/chat/completions",
HttpMethod.POST,
requestEntity,
String.class
).getBody();
System.out.println(response);
System.out.println(mapper.readTree(response).get("choices").get(0).get("message").get("content"));
}
}
AiChatRequest
@Data
public class AiChatRequest {
private String model;
private List<AiChatMessage> messages;
private boolean stream;
}
AiChatMessage
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AiChatMessage {
private String role;
private String content;
}
SseApp
@SpringBootApplication
public class SseApp {
public static void main(String[] args) {
SpringApplication.run(SseApp.class, args);
}
}
Sse.vue
<template>
<div style="margin: auto; width: 800px;">
<input type="text" v-model="content">
<button @click="submit">发送</button>
<div style="height: 300px; border: 1px solid #ccc;border-radius: 6px;overflow: auto;">
{{ reply }}
</div>
</div>
</template>
<script>
export default {
name: '',
data() {
return {
content: '',
reply: '',
eventSource: null,
}
},
methods:{
submit() {
if(!this.content) {
alert('没有输入内容')
return
}
if(this.reply) {
this.reply = ''
}
let eventSource = new EventSource('http://127.0.0.1:8080/deepseek/chatCompletions?content=' + this.content)
// let eventSource = new EventSource('http://127.0.0.1:8080/deepseek/chatCompletions2?content2=' + this.content)
this.eventSource = eventSource
eventSource.onopen = ()=>{
console.log('onopen 连接成功');
}
eventSource.onerror = (e)=>{
console.log('onerror 连接断开',e);
this.eventSource.close() // 关闭, 不再自动重连
}
eventSource.onmessage = e=>{
console.log('收到消息: ', e.data);
this.reply += e.data
}
}
},
mounted() {
}
}
</script>
<style>
</style>
多轮对话
package com.zzhua.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zzhua.pojo.AiChatMessage;
import com.zzhua.pojo.AiChatRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicBoolean;
/*
输入 你好 后,deepseek一次输出的示例如下
result=> {"id":"6cfd18b2-06c8-4f72-9ba2-26691f44b13b","object":"chat.completion.chunk","created":1741079904,"model":"deepseek-chat","system_fingerprint":"fp_3a5770e1b4_prod0225","choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]}
result=> {"id":"6cfd18b2-06c8-4f72-9ba2-26691f44b13b","object":"chat.completion.chunk","created":1741079904,"model":"deepseek-chat","system_fingerprint":"fp_3a5770e1b4_prod0225","choices":[{"index":0,"delta":{"content":"你好"},"logprobs":null,"finish_reason":null}]}
result=> {"id":"6cfd18b2-06c8-4f72-9ba2-26691f44b13b","object":"chat.completion.chunk","created":1741079904,"model":"deepseek-chat","system_fingerprint":"fp_3a5770e1b4_prod0225","choices":[{"index":0,"delta":{"content":"!"},"logprobs":null,"finish_reason":null}]}
result=> {"id":"6cfd18b2-06c8-4f72-9ba2-26691f44b13b","object":"chat.completion.chunk","created":1741079904,"model":"deepseek-chat","system_fingerprint":"fp_3a5770e1b4_prod0225","choices":[{"index":0,"delta":{"content":"很高兴"},"logprobs":null,"finish_reason":null}]}
result=> {"id":"6cfd18b2-06c8-4f72-9ba2-26691f44b13b","object":"chat.completion.chunk","created":1741079904,"model":"deepseek-chat","system_fingerprint":"fp_3a5770e1b4_prod0225","choices":[{"index":0,"delta":{"content":"见到"},"logprobs":null,"finish_reason":null}]}
result=> {"id":"6cfd18b2-06c8-4f72-9ba2-26691f44b13b","object":"chat.completion.chunk","created":1741079904,"model":"deepseek-chat","system_fingerprint":"fp_3a5770e1b4_prod0225","choices":[{"index":0,"delta":{"content":"你"},"logprobs":null,"finish_reason":null}]}
result=> {"id":"6cfd18b2-06c8-4f72-9ba2-26691f44b13b","object":"chat.completion.chunk","created":1741079904,"model":"deepseek-chat","system_fingerprint":"fp_3a5770e1b4_prod0225","choices":[{"index":0,"delta":{"content":"。"},"logprobs":null,"finish_reason":null}]}
result=> {"id":"6cfd18b2-06c8-4f72-9ba2-26691f44b13b","object":"chat.completion.chunk","created":1741079904,"model":"deepseek-chat","system_fingerprint":"fp_3a5770e1b4_prod0225","choices":[{"index":0,"delta":{"content":"有什么"},"logprobs":null,"finish_reason":null}]}
result=> {"id":"6cfd18b2-06c8-4f72-9ba2-26691f44b13b","object":"chat.completion.chunk","created":1741079904,"model":"deepseek-chat","system_fingerprint":"fp_3a5770e1b4_prod0225","choices":[{"index":0,"delta":{"content":"我可以"},"logprobs":null,"finish_reason":null}]}
result=> {"id":"6cfd18b2-06c8-4f72-9ba2-26691f44b13b","object":"chat.completion.chunk","created":1741079904,"model":"deepseek-chat","system_fingerprint":"fp_3a5770e1b4_prod0225","choices":[{"index":0,"delta":{"content":"帮忙"},"logprobs":null,"finish_reason":null}]}
result=> {"id":"6cfd18b2-06c8-4f72-9ba2-26691f44b13b","object":"chat.completion.chunk","created":1741079904,"model":"deepseek-chat","system_fingerprint":"fp_3a5770e1b4_prod0225","choices":[{"index":0,"delta":{"content":"的吗"},"logprobs":null,"finish_reason":null}]}
result=> {"id":"6cfd18b2-06c8-4f72-9ba2-26691f44b13b","object":"chat.completion.chunk","created":1741079904,"model":"deepseek-chat","system_fingerprint":"fp_3a5770e1b4_prod0225","choices":[{"index":0,"delta":{"content":"?"},"logprobs":null,"finish_reason":null}]}
result=> {"id":"6cfd18b2-06c8-4f72-9ba2-26691f44b13b","object":"chat.completion.chunk","created":1741079904,"model":"deepseek-chat","system_fingerprint":"fp_3a5770e1b4_prod0225","choices":[{"index":0,"delta":{"content":""},"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":10,"completion_tokens":11,"total_tokens":21,"prompt_tokens_details":{"cached_tokens":0},"prompt_cache_hit_tokens":0,"prompt_cache_miss_tokens":10}}
result=> [DONE]
设置模型为 deepseek-reasoner后, 输入 你好 后,deepseek一次输出的示例如下
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"role":"assistant","content":null,"reasoning_content":""},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"好的"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":","},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"用户"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"用"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"中文"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"问候"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"“"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"你好"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"”,"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"我应该"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"用"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"中文"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"回应"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"。"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"首先"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":","},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"确认"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"是否需要"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"进一步"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"帮助"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"。"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"保持"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"友好"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"和"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"开放"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"的态度"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":","},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"询问"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"他们"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"有什么"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"具体"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"的问题"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"或"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"需要"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"协助"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"的地方"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"。"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"确保"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"回答"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"简洁"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"明了"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":","},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"符合"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"用户"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"的需求"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"。"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"同时"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":","},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"注意"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"使用"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"自然"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"的口"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"语"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"化"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"表达"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":","},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"避免"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"过于"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"正式"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"或"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"生"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"硬"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"。"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"如果有"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"必要"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":","},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"可以"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"分"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"步骤"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"或"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"分"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"点"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"解释"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":","},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"但"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"根据"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"用户"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"的问题"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"复杂"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"程度"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"来决定"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"。"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"现在"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"用户"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"可能"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"只是想"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"打个"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"招呼"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":","},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"或者"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"有"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"具体"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"的问题"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"需要"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"解决"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":","},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"所以"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"需要"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"进一步"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"明确"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"他们的"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"需求"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"。"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"先"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"回应"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"问候"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":","},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"然后"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"邀请"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"他们"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"提出"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"具体"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"的问题"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"或"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"需求"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"。"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"保持"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"积极"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"和"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"乐于"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"助"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"人的"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"语气"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":null,"reasoning_content":"。"},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"你好","reasoning_content":null},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"!","reasoning_content":null},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"很高兴","reasoning_content":null},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"为您","reasoning_content":null},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"提供","reasoning_content":null},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"帮助","reasoning_content":null},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"。","reasoning_content":null},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"您","reasoning_content":null},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"有什么","reasoning_content":null},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"问题","reasoning_content":null},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"或","reasoning_content":null},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"需要","reasoning_content":null},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"协助","reasoning_content":null},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"的地方","reasoning_content":null},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"吗","reasoning_content":null},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"?","reasoning_content":null},"logprobs":null,"finish_reason":null}]}
result=>{"id":"0eedd5b9-3329-4d9e-b5d9-b0c1fd8667b1","object":"chat.completion.chunk","created":1741085504,"model":"deepseek-reasoner","system_fingerprint":"fp_5417b77867_prod0225","choices":[{"index":0,"delta":{"content":"","reasoning_content":null},"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":12,"completion_tokens":142,"total_tokens":154,"prompt_tokens_details":{"cached_tokens":0},"completion_tokens_details":{"reasoning_tokens":124},"prompt_cache_hit_tokens":0,"prompt_cache_miss_tokens":12}}
result=>[DONE]
*/
public class MultiChatDemo {
private static final ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 默认系统消息
List<AiChatMessage> messages = new ArrayList<>();
messages.add(new AiChatMessage("system", "You are a helpful assistant."));
// 消息请求体
AiChatRequest request = new AiChatRequest();
request.setModel("deepseek-reasoner");
request.setMessages(messages);
// 流式输出
request.setStream(true);
WebClient webClient = WebClient.builder()
.baseUrl("https://api.deepseek.com")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.defaultHeader("Authorization", "Bearer sk-xxx")
.build();
while (true) {
System.out.print("请输入: ");
String userInput = sc.nextLine();
if ("exit".equals(userInput)) {
System.out.println("程序结束");
break;
}
if (userInput == null || userInput.length() == 0) {
System.out.println("请输入您想知道的问题,不能为空哦");
continue;
}
// 用户输入消息
AiChatMessage userMessage = new AiChatMessage("user", userInput);
messages.add(userMessage);
// 回复内容
StringBuilder replyContent = new StringBuilder();
StringBuilder reasoningContent = new StringBuilder();
AiChatMessage aiReplayMessage = new AiChatMessage();
AtomicBoolean firstContent = new AtomicBoolean(true);
webClient.post()
.uri("/chat/completions")
// 消息请求体
.body(BodyInserters.fromObject(request))
.retrieve()
.bodyToFlux(String.class)
.flatMap(result -> {
// System.out.println("result=>" + result);
// 流式输出结束标记
if ("[DONE]".equals(result)) {
aiReplayMessage.setContent(replyContent.toString());
messages.add(aiReplayMessage);
System.out.println();
} else {
// 流式输出内容处理
try {
JsonNode jsonNode = mapper.readTree(result);
JsonNode delta = jsonNode.get("choices").get(0).get("delta");
if (delta.has("role")) {
aiReplayMessage.setRole(delta.get("role").asText());
}
if (delta.has("reasoning_content") && !delta.get("reasoning_content").isNull()) {
String content = delta.get("reasoning_content").asText();
if (content != null && content.length() > 0) {
reasoningContent.append(content);
System.out.print(content);
}
}
if (delta.has("content") && !delta.get("content").isNull()) {
// 推理后, 换行
if (firstContent.get()) {
// System.out.println();
firstContent.set(false);
}
String content = delta.get("content").asText();
if (content != null && content.length() > 0) {
replyContent.append(content);
System.out.print(content);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
return Flux.empty();
})
.then()
.block();
}
}
}
模型列表和查询余额&对话(OkHttp3)
package com.zzhua.service;
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zzhua.pojo.AiChatMessage;
import lombok.Data;
import okhttp3.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
或者导入
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.9</version>
</dependency>
*/
public class TestOkHttp {
public static final String SK = "Bearer sk-e37fd25a243e42298aa03ddadf5800b3";
public static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws IOException {
// 查询模型列表
listModels();
// 查询余额
// queryBalance();
// 对话
// 非流式对话, 只输出1条完整的数据的json
// chatCompletions();
// 流式对话
// chatCompletionsUsingStreamStyle();
}
public static void chatCompletionsUsingStreamStyle() throws IOException {
ChatRequest chatRequest = new ChatRequest();
// 设置为流式输出
chatRequest.setStream(true);
AiChatMessage chatMsg1 = new AiChatMessage("system", "You are a helpful assistant.");
AiChatMessage chatMsg2 = new AiChatMessage("user", "精简回答java的三大特性");
chatRequest.setMessages(Arrays.asList(chatMsg1, chatMsg2));
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, mapper.writeValueAsString(chatRequest));
Request request = new Request.Builder()
.url("https://api.deepseek.com/chat/completions")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.addHeader("Authorization", SK)
.build();
Response response = client.newCall(request).execute();
// 这里直接输出会让所有json数据返回后,再一次性输出
// 并且这里输出的节奏是 每一行以data: 开头, 然后是json数据, 然后是\n\n, 最后1个数据是 data: [DONE]\n\n, 最后1行是\n
// System.out.println(response.body().string());
// 假设bytes数组的容量设置过小,这里是不是就得处理拆包/粘包的问题?
/*try (InputStream inputStream = response.body().byteStream()) {
int byteRead = 0;
byte[] bytes = new byte[1024];
while ((byteRead = inputStream.read(bytes)) != -1) {
String content = new String(bytes, 0, byteRead);
System.out.println(content);
}
} catch (Exception e) {
System.err.println(e);
}*/
// 关键点在于利用 SSE 协议的格式规则(基于换行符 \n 和事件分隔符 \n\n),
// 通过 BufferedReader 逐行读取并解析数据流。
InputStream inputStream = response.body().byteStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder currentEvent = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("data:")) {
// 提取数据部分(去掉 "data: ")
String data = line.substring(5).trim();
currentEvent.append(data);
} else if (line.isEmpty()) {
// 遇到空行,表示一个事件结束
String eventData = currentEvent.toString();
currentEvent.setLength(0); // 清空缓存
if ("[DONE]".equals(eventData)) {
System.out.println("Stream completed.");
break; // 结束循环
} else {
// 处理 JSON 数据
System.out.println("Received data: " + eventData);
// 这里可以解析 JSON,例如使用 Jackson/Gson
}
}
}
}
public static void chatCompletions() throws IOException {
ChatRequest chatRequest = new ChatRequest();
AiChatMessage chatMsg1 = new AiChatMessage("system", "You are a helpful assistant.");
AiChatMessage chatMsg2 = new AiChatMessage("user", "精简回答java的三大特性");
chatRequest.setMessages(Arrays.asList(chatMsg1, chatMsg2));
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, mapper.writeValueAsString(chatRequest));
Request request = new Request.Builder()
.url("https://api.deepseek.com/chat/completions")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.addHeader("Authorization", SK)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
System.out.println(response.body().string());
} else {
System.err.println(response.body().string());
}
}
// 查询余额
public static void queryBalance() throws IOException {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
.url("https://api.deepseek.com/user/balance")
.method("GET", null)
.addHeader("Accept", "application/json")
.addHeader("Authorization", SK)
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
}
// 查询模型
public static void listModels() throws IOException {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
.url("https://api.deepseek.com/models")
.method("GET", null)
.addHeader("Accept", "application/json")
.addHeader("Authorization", SK)
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
}
@Data
static class ChatRequest {
private List<AiChatMessage> messages;
private String model = "deepseek-chat";
private double frequency_penalty = 0;
private int max_tokens = 2048;
private double presence_penalty = 0;
private Map<String, Object> response_format = new HashMap<String, Object>() {{
put("type", "text");
}};
private Object stop;
private boolean stream = false;
private Object stream_options;
private double temperature = 1;
private double top_p = 1;
private Object tools;
private String tool_choice = "none";
private boolean logprobs = false;
private Object top_logprobs;
}
}
豆包
开通服务
引入依赖
仍然是在2.1.8的springboot版本中使用
<dependency>
<groupId>com.volcengine</groupId>
<artifactId>volcengine-java-sdk-ark-runtime</artifactId>
<version>0.1.155</version>
</dependency>
单轮&多轮&流式&异步…
public class ArkClientExample {
private static final String API_KEY = "~~~;
private static final String AK = "~~~";
private static final String SK = "~~~";
private static final String MODEL_ID = "doubao-1-5-pro-32k-250115";
public static void main(String[] args) {
// 开通服务后, 测试是否可以调通
/*
curl https://ark.cn-beijing.volces.com/api/v3/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d '{
"model": "doubao-1-5-pro-32k-250115",
"messages": [
{"role": "system","content": "你是人工智能助手."},
{"role": "user","content": "常见的十字花科植物有哪些?"}
],
"stream": true
}'
*/
// test01();
// test02();
// test03();
// test04();
}
// 单轮
public static void test01() {
// 1、Model ID 作为推理接入点 用于模型调用(此方式目前仅支持 API Key 鉴权)
// 2、<Model> 需要替换为模型的 Model ID(或您在平台上创建的推理接入点 Endpoint ID)
// API Key 管理页面获取
ArkService service = ArkService.builder()
.apiKey(API_KEY)
// .ak(AK).sk(SK)
.region("cn-beijing")
.baseUrl("https://ark.cn-beijing.volces.com")
.timeout(Duration.ofSeconds(120))
.connectTimeout(Duration.ofSeconds(20))
.retryTimes(2)
.build();
System.out.println("\n----- standard request -----");
final List<ChatMessage> messages = new ArrayList<>();
final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("精简回答java的三大特性").build();
messages.add(systemMessage);
messages.add(userMessage);
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
// Model ID
.model("doubao-1-5-pro-32k-250115")
// .model("ep-20250310155654-7jwj5")
.messages(messages)
.build();
service.createChatCompletion(chatCompletionRequest).getChoices().forEach(choice -> System.out.println(choice.getMessage().getContent()));
// shutdown service
service.shutdownExecutor();
}
// 多轮会话
public static void test02() {
ArkService service = ArkService.builder().apiKey(API_KEY).build();
System.out.println("\n----- multiple rounds request -----");
final List<ChatMessage> messages = Arrays.asList(
ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build(),
ChatMessage.builder().role(ChatMessageRole.USER).content("花椰菜是什么?").build(),
ChatMessage.builder().role(ChatMessageRole.ASSISTANT).content("花椰菜又称菜花、花菜,是一种常见的蔬菜。").build(),
ChatMessage.builder().role(ChatMessageRole.USER).content("再详细点").build()
);
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
.model("doubao-1-5-pro-32k-250115")
.messages(messages)
.build();
service.createChatCompletion(chatCompletionRequest).getChoices().forEach(choice -> System.out.println(choice.getMessage().getContent()));
// shutdown service
service.shutdownExecutor();
}
// 流式
public static void test03() {
ArkService service = ArkService.builder().apiKey(API_KEY).build();
System.out.println("\n----- streaming request -----");
final List<ChatMessage> streamMessages = new ArrayList<>();
final ChatMessage streamSystemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage streamUserMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些?").build();
streamMessages.add(streamSystemMessage);
streamMessages.add(streamUserMessage);
ChatCompletionRequest streamChatCompletionRequest = ChatCompletionRequest.builder()
.model(MODEL_ID)
.messages(streamMessages)
.build();
service.streamChatCompletion(streamChatCompletionRequest)
.doOnError(Throwable::printStackTrace)
.blockingForEach(
choice -> {
if (choice.getChoices().size() > 0) {
System.out.print(choice.getChoices().get(0).getMessage().getContent());
}
}
);
// shutdown service
service.shutdownExecutor();
}
// 异步
public static void test04() {
ArkService service = ArkService.builder().apiKey(API_KEY).build();
System.out.println("\n----- streaming request -----");
final List<ChatMessage> streamMessages = new ArrayList<>();
final ChatMessage streamSystemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage streamUserMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些?").build();
streamMessages.add(streamSystemMessage);
streamMessages.add(streamUserMessage);
ChatCompletionRequest streamChatCompletionRequest = ChatCompletionRequest.builder()
.model(MODEL_ID)
.messages(streamMessages)
.build();
service.streamChatCompletion(streamChatCompletionRequest)
.doOnError(Throwable::printStackTrace)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.single())
.subscribe(
choice -> {
if (choice.getChoices().size() > 0) {
System.out.print(choice.getChoices().get(0).getMessage().getContent());
}
}
);
// just wait for result
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// shutdown service
service.shutdownExecutor();
}
}
千帆
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-boot-dependencies</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.8.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>demo-qianfan</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version>
</dependency>
<dependency>
<groupId>com.baidubce</groupId>
<artifactId>qianfan</artifactId>
<version>0.1.1</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.9</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
</dependencies>
</project>
QianfanController
package com.zzhua.controller;
import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.core.StreamIterator;
import com.baidubce.qianfan.core.auth.Auth;
import com.baidubce.qianfan.model.chat.ChatResponse;
import com.baidubce.qianfan.model.completion.CompletionResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.concurrent.TimeUnit;
@RequestMapping("/qianfan")
@RestController
public class QianFanController {
/*
Access Key 11
Secret Key 22
Api Key 33
应用id 44
API Key 55
Secret Key 66
V2版本应用id 77
*/
private static final String accessKey = "88";
private static final String secretKey = "99";
// http://localhost:8080/qianfan/ai/generate
@GetMapping("/ai/generate")
public String chatAI(@RequestParam(value = "message", defaultValue = "请给我讲一个笑话") String message) {
Qianfan qianfan = new Qianfan(Auth.TYPE_OAUTH, accessKey, secretKey);
ChatResponse chatResponse = qianfan
.chatCompletion()
.model("ERNIE-4.0-8K")
.addMessage("user", message)
// temperature(0.7) 该参数用于调整生成文本的随机性,值越高,生成的文本越多样化和创新,但也可能更不连贯或者离题,值越低,生成的文本越保守, 0.7 是在创造性和连贯性中间找到一个平衡。
.temperature(0.7)
// topP(0.9) 该参数是介于 0 - 1 之间,表示在生成每个词时,只考虑概率最高的前p%的词汇。
.topP(0.9)
.execute();
System.out.println(chatResponse.getResult());
return chatResponse.getResult();
}
// http://localhost:8080/qianfan/ai/generate
// 对前端来说,这不是流式输出
@GetMapping("/ai/stream")
public void chatStreamAI(@RequestParam(value = "message", defaultValue = "请给我讲一个笑话") String message, HttpServletResponse servletResponse) throws Exception {
// 设置响应的内容类型并指定字符编码
servletResponse.setContentType("text/html;charset=UTF-8");
servletResponse.setCharacterEncoding("UTF-8");
PrintWriter writer = servletResponse.getWriter();
try (StreamIterator<CompletionResponse> response = new Qianfan(Auth.TYPE_OAUTH, accessKey, secretKey)
.completion()
.model("CodeLlama-7b-Instruct")
.prompt(message)
.executeStream()
) {
while (response.hasNext()) {
String result = response.next().getResult();
System.out.println(result);
System.out.println("==================");
writer.write(result);
writer.flush();
TimeUnit.SECONDS.sleep(1);
}
}
}
}
StreamStyleAppTest
package com.zzhua;
import com.sun.net.httpserver.*;
import java.io.*;
import java.net.InetSocketAddress;
import okhttp3.*;
import org.json.JSONObject;
/*
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sample</title>
</head>
<body>
<label for="textInput">Prompt:</label>
<input type="textarea" id="textInput" placeholder="您有什么问题">
<button onclick="run_prompt()">执行prompt</button>
<p><textarea id="answer" rows="10" cols="50" readonly></textarea></p>
<script>
current_text = document.getElementById('answer');
text = "";
char_index = 0
function run_prompt() {
var inputValue = document.getElementById('textInput').value;
document.getElementById('answer').value = "";
// 调用服务端的流式接口, 修改为自己的服务器地址和端口号
fetch('http://127.0.0.1:8000/eb_stream', {
method: 'post',
headers: {'Content-Type': 'text/plain'},
body: JSON.stringify({'prompt': inputValue})
})
.then(response => {
return response.body;
})
.then(body => {
const reader = body.getReader();
const decoder = new TextDecoder();
function read() {
return reader.read().then(({ done, value }) => {
if (done) { // 读取完成
return;
}
data = decoder.decode(value, { stream: true });
text += JSON.parse(data).result;
type(); // 打字机效果输出
return read();
});
}
return read();
})
.catch(error => {
console.error('发生错误:', error);
});
}
function type() {
let enableCursor = true; // 启用光标效果
if (char_index < text.length) {
let txt = document.getElementById('answer').value;
let cursor = enableCursor ? "|" : "";
if (enableCursor && txt.endsWith("|")) {
txt = txt.slice(0, -1);
}
document.getElementById('answer').value = txt + text.charAt(char_index) + cursor;
char_index++;
setTimeout(type, 1000/5); // 打字机速度控制, 每秒5个字
}
}
</script>
</body>
</html>
*/
public class StreamStyleAppTest
{
public static final String API_KEY = "zL8RF3q5kdKqNtRk9uF5Bkrs";
public static final String SECRET_KEY = "~~~";
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/eb_stream", new StreamHandler());
server.setExecutor(null);
server.start();
}
static String getAccessToken() throws IOException {
OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY + "&client_secret=" + SECRET_KEY);
Request request = new Request.Builder()
.url("https://aip.baidubce.com/oauth/2.0/token")
.method("POST", body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
Response response = HTTP_CLIENT.newCall(request).execute();
return new JSONObject(response.body().string()).getString("access_token");
}
static class StreamHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
// 读取body中的prompt入参
InputStreamReader bodyReader = new InputStreamReader(exchange.getRequestBody());
BufferedReader br = new BufferedReader(bodyReader);
StringBuilder strBuilder = new StringBuilder();
String line;
while((line = br.readLine()) != null) {
strBuilder.append(line);
}
br.close();
JSONObject body = new JSONObject(strBuilder.toString());
String prompt = body.getString("prompt");
// 发起流式请求
OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody ebRequestBody = RequestBody.create(mediaType, "{\"messages\":[{\"role\":\"user\",\"content\":\"" + prompt + "\"}],\"stream\":true}");
String source = "&sourceVer=0.0.1&source=app_center&appName=streamDemo";
// 大模型接口URL
String baseUrl = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-lite-8k";
Request request = new Request.Builder()
.url(baseUrl + "?access_token=" + getAccessToken() + source)
.method("POST", ebRequestBody)
.addHeader("Content-Type", "application/json")
.build();
OutputStream outputStream = exchange.getResponseBody();
exchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*"); // 允许跨域
exchange.sendResponseHeaders(200, 0);
// 流式返回
Response response = HTTP_CLIENT.newCall(request).execute();
if (response.isSuccessful()) {
ResponseBody responseBody = response.body();
// 这里的读取方法不合理,应该使用BufferedReader的readLine方法来读取,以换行符作为分隔符,来接收流式数据。
// 这样就不用考虑缓冲区大小该设置多少的问题了
if (responseBody != null) {
InputStream inputStream = responseBody.byteStream();
String rawData = "";
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
String data = new String(buffer, 0, bytesRead);
String[] lines = data.split("\n");
for (String li : lines) {
if (li.startsWith("data: ")) {
if (rawData != "") {
outputStream.write(rawData.substring(6).getBytes());
outputStream.flush();
}
rawData = li;
} else {
rawData = rawData + li;
}
}
}
}
} else {
System.out.println("流式请求异常: " + response);
}
outputStream.close();
exchange.close();
}
}
}
TestQianfan
public class TestQianfan {
public static void main(String[] args) {
String url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions";
String accessToken = "~~~";
HashMap<String, String> msg = new HashMap<>();
msg.put("role", "user");
msg.put("content", "讲个中文的笑话");
ArrayList<HashMap> messages = new ArrayList<>();
messages.add(msg);
HashMap<String, Object> requestBody = new HashMap<>();
requestBody.put("messages", messages);
String response = HttpUtil.post(url + "?access_token=" + accessToken, JSONUtil.toJsonStr(requestBody));
System.out.println(response);
}
}
讯飞
xunfei-spark4j:科大讯飞 星火认知大模型API Java SDK 流式调用、同步调用、FunctionCall、tokens统计、多版本切换;长期更新
准备
1、创建应用
2、点击test应用,选择对应的模型,在星火调试中心调试接口
3、模型调用参数在下面查看
调用
testLite
@Slf4j
public class TestSpark {
private static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) {
test01();
}
// 测试lite
private static void test01() {
AiChatMessage chatMsg1 = new AiChatMessage("system", "You are a helpful assistant.");
AiChatMessage chatMsg2 = new AiChatMessage("user", "java的三大特性");
AiChatRequest request = new AiChatRequest();
request.setModel("lite");
request.setMessages(Arrays.asList(chatMsg1, chatMsg2));
// 流式输出
request.setStream(true);
Flux<String> flux = WebClient.builder()
.baseUrl("https://spark-api-open.xf-yun.com/v1")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.defaultHeader("Authorization", "Bearer MEFxLmxqidSEITjmdJxr:cQOzLvelzuJudpNPyXJz")
.build()
.post()
.uri("/chat/completions")
.body(BodyInserters.fromObject(request))
.retrieve()
.bodyToFlux(String.class)
.flatMap(result -> {
System.err.println(result);
if ("[DONE]".equals(result)) {
return Flux.empty();
} else {
try {
JsonNode jsonNode = mapper.readTree(result);
String content = jsonNode.get("choices").get(0).get("delta").get("content").asText();
System.out.println(content);
return Flux.just(content);
} catch (Exception e) {
log.error("解析失败: {}", result);
}
}
return Flux.empty();
});
flux.then().block();
}
}
ChatGPT
二次开发一个ChatGPT网站SpringBoot+MongoDB+Vue - B站视频,代码:chatgpt-assistant,文档地址
使用这个代码,配上https://api.xty.app的sk,就有下面的效果了
更多推荐
所有评论(0)