OpenAi最簡(jiǎn)潔的Java流式返回接入方式,沒有第三方依賴,只需要使用Spring Boot即可!輕松構(gòu)建你的帶有聊天記憶、畫圖功能的chatgpt!
GitHub - NiuXiangQian/chatgpt-stream: OpenAi最簡(jiǎn)潔的Java流式返回接入方式,沒有第三方依賴,只需要使用Spring Boot即可!輕松構(gòu)建你的帶有聊天記憶、畫圖功能的chatgpt!
預(yù)覽
???????
模型:GPT-3.5-turbo
記憶功能
GPT-3.5-turbo本身不帶有記憶功能需要每次把上下文傳遞過去
int currentToken = (int) (content.length() / TOKEN_CONVERSION_RATE);
List<Message> history = userSessionUtil.getHistory(sessionId, MessageType.TEXT, (int) ((MAX_TOKEN / TOKEN_CONVERSION_RATE) - currentToken));
log.info("history:{}", history);
String historyDialogue = history.stream().map(e -> String.format(e.getUserType().getCode(), e.getMessage())).collect(Collectors.joining());
String prompt = StringUtils.hasLength(historyDialogue) ? String.format("%sQ:%s\n\n", historyDialogue, content) : content;
流式返回
基于WebFlux+SSE實(shí)現(xiàn)
接口需要返回 text/event-stream類型
@GetMapping(value = "/completions/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
返回響應(yīng)式數(shù)據(jù)
log.info("prompt:{}", prompt);
return Flux.create(emitter -> {
OpenAISubscriber subscriber = new OpenAISubscriber(emitter, sessionId, this, userMessage);
Flux<String> openAiResponse =
openAiWebClient.getChatResponse(sessionId, prompt, null, null, null);
openAiResponse.subscribe(subscriber);
emitter.onDispose(subscriber);
});
完整代碼:GitHub - NiuXiangQian/chatgpt-stream: OpenAi最簡(jiǎn)潔的Java流式返回接入方式,沒有第三方依賴,只需要使用Spring Boot即可!輕松構(gòu)建你的帶有聊天記憶功能的chatgpt!文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-527979.html
幫忙star噢 您的支持就是我的動(dòng)力文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-527979.html
到了這里,關(guān)于OpenAi最簡(jiǎn)潔的Java流式返回接入方式,沒有第三方依賴,只需要使用Spring Boot即可!輕松構(gòu)建你的帶有聊天記憶、畫圖功能的chatgpt!的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!