DeepSeek API-KEY 获取
- 登录DeekSeek 官网,进入API 开放平台
2. 创建API-KEY
- 复制API-KEY进行保存,后期API调用使用
项目中集成DeepSeek
这里只展示部分核心代码,具体请查看源码orange-ai-deepseek-biz-starter
@Slf4j
@AllArgsConstructor
public class DeepSeekChatModelServiceImpl implements ChatModelService {
private final DeepSeekStorageProperties deepSeekStorageProperties;
@Override
public PlatformEnum getPlatform() {
return PlatformEnum.DEEP_SEEK;
}
@Override
public Flux<Result<ConversationReplyVO>> conversationStream(ConversationParam param) {
return conversationStream(param, List.of());
}
@Override
public Flux<Result<ConversationReplyVO>> conversationStream(ConversationParam param, List<ChatSessionRecordVO> contextMessageList) {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.get("application/json");
ChatCompletionsParam reqBody = generateChatRequestBody(param, contextMessageList);
Request request = new Request.Builder()
.url(deepSeekStorageProperties.getChatUrl())
.addHeader(SecurityConstant.AUTHORIZATION, "Bearer " + deepSeekStorageProperties.getToken())
.post(RequestBody.create(JSONUtil.toJsonStr(reqBody), mediaType))
.build();
return Flux.create(emitter -> {
EventSourceListener listener = new EventSourceListener() {
@Override
public void onOpen(@NotNull EventSource eventSource, @NotNull Response response) {
System.out.println("SSE connection opened");
}
@Override
public void onClosed(@NotNull EventSource eventSource) {
emitter.complete();
System.out.println("SSE connection closed");
}
@Override
public void onEvent(@NotNull EventSource eventSource, String id, String type, @NotNull String data) {
if (log.isDebugEnabled()) {
log.debug("data:{}", data);
}
if (StrUtil.isBlank(data) || DeepSeekContent.DONE.equalsIgnoreCase(data)) {
emitter.complete();
return;
}
ChatCompletionsResponse response = JSONUtil.toBean(data, ChatCompletionsResponse.class);
StringBuilder content = new StringBuilder();
for (ChatCompletionsResponse.ChoiceItem choice : response.getChoices()) {
content.append(choice.getDelta().getContent());
}
emitter.next(ResultWrapper.ok(ConversationReplyVO.builder().content(content.toString()).build()));
}
@Override
public void onFailure(@NotNull EventSource eventSource, Throwable t, Response response) {
System.err.println("Error occurred: " + t);
}
};
EventSource.Factory factory = EventSources.createFactory(client);
factory.newEventSource(request, listener);
});
}
private ChatCompletionsParam generateChatRequestBody(ConversationParam param, List<ChatSessionRecordVO> contextMessageList) {
List<MessageItem> messages = new ArrayList<>();
if (CollUtil.isNotEmpty(contextMessageList)) {
for (ChatSessionRecordVO record : contextMessageList) {
if (MessageTypeEnum.USER.equals(record.getMessageType())) {
messages.add(MessageItem.builder().role(MessageType.USER.getValue()).content(record.getContent()).build());
continue;
}
if (MessageTypeEnum.ASSISTANT.equals(record.getMessageType())) {
messages.add(MessageItem.builder().role(MessageType.ASSISTANT.getValue()).content(record.getContent()).build());
}
}
}
messages.add(MessageItem.builder().role(MessageType.USER.getValue()).content(param.getPrompt()).build());
return ChatCompletionsParam.builder()
.model(param.getModelCode())
.messages(messages)
.stream(Boolean.TRUE)
.build();
}
}
体验DeepSeek
- 在线体验: http://tiny.hengzq.cn
Orange开源项目推荐
- Orange 官网: http://hengzq.cn
- 在线体验: http://tiny.hengzq.cn
- 项目文档: http://hengzq.cn/orange-monomer/
- 单体架构-后端源码下载【GitHub】: https://github.com/hengzq/orange-monomer
- 单体架构-后端源码下载【Gitee】: https://gitee.com/hengzq/orange-monomer
- 微服务版本-后端源码下载【GitHub】: https://github.com/hengzq/orange-cloud
- 微服务版本-后端源码下载【Gitee】: https://gitee.com/hengzq/orange-cloud
- 前端源码下载【GitHub】: https://github.com/hengzq/orange-cloud
- 前端源码下载【Gitee】: https://gitee.com/hengzq/orange-cloud