-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1、对话新增文档模式与联网模式。联网模式实现了百度网页搜索、bing搜索、google搜索。 2、新增支持kimi大模型。 refactor: 1、优化对话页面布局,将部分按钮移至右侧面板。
- Loading branch information
Showing
35 changed files
with
1,442 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/com/hkh/ai/chain/llm/capabilities/generation/KimiAiUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.hkh.ai.chain.llm.capabilities.generation; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Kimi 工具类 | ||
* @author huangkh | ||
*/ | ||
@Component | ||
@Slf4j | ||
public class KimiAiUtil { | ||
|
||
@Value("${chain.llm.kimi.model}") | ||
private String completionModel; | ||
|
||
@Value("${kimi.ai.token}") | ||
private String appKey; | ||
|
||
public String getCompletionModel(){ | ||
return this.completionModel; | ||
} | ||
|
||
public String getAppKey(){ | ||
return this.appKey; | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/hkh/ai/chain/llm/capabilities/generation/KimiApis.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.hkh.ai.chain.llm.capabilities.generation; | ||
|
||
/** | ||
* Kimi AI相关 API | ||
*/ | ||
public interface KimiApis { | ||
|
||
String COMPLETION_TEXT = "https://api.moonshot.cn/v1/chat/completions"; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...in/java/com/hkh/ai/chain/llm/capabilities/generation/text/kimi/BlockCompletionResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.hkh.ai.chain.llm.capabilities.generation.text.kimi; | ||
|
||
import com.alibaba.fastjson2.JSONObject; | ||
import lombok.Data; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
public class BlockCompletionResult { | ||
|
||
private String id; | ||
|
||
private String model; | ||
|
||
private Long created; | ||
|
||
private List<BlockCompletionResultChoice> choices; | ||
|
||
private BlockCompletionResultUsage usage; | ||
|
||
@Data | ||
public class BlockCompletionResultUsage{ | ||
private int prompt_tokens; | ||
private int completion_tokens; | ||
private int total_tokens; | ||
} | ||
|
||
@Data | ||
public class BlockCompletionResultChoice{ | ||
private int index; | ||
private String finish_reason; | ||
|
||
private BlockCompletionResultChoiceMessage message; | ||
} | ||
|
||
@Data | ||
public class BlockCompletionResultChoiceMessage{ | ||
private String role; | ||
private String content; | ||
|
||
private List<BlockCompletionResultChoiceMessageToolCall> tool_calls; | ||
} | ||
|
||
@Data | ||
public class BlockCompletionResultChoiceMessageToolCall{ | ||
private String id; | ||
private String type; | ||
|
||
private BlockCompletionResultChoiceMessageToolCallFunction function; | ||
} | ||
|
||
@Data | ||
public class BlockCompletionResultChoiceMessageToolCallFunction{ | ||
private String name; | ||
private JSONObject arguments; | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...va/com/hkh/ai/chain/llm/capabilities/generation/text/kimi/KimiCompletionBizProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.hkh.ai.chain.llm.capabilities.generation.text.kimi; | ||
|
||
import com.alibaba.fastjson2.JSONObject; | ||
import com.hkh.ai.domain.CustomChatMessage; | ||
import com.hkh.ai.domain.SysUser; | ||
import com.hkh.ai.service.ConversationService; | ||
import com.knuddels.jtokkit.api.Encoding; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** | ||
* 百度千帆业务处理器 | ||
* @author huangkh | ||
*/ | ||
@Builder | ||
@Data | ||
@AllArgsConstructor | ||
@Slf4j | ||
public class KimiCompletionBizProcessor { | ||
|
||
private final ConversationService conversationService; | ||
|
||
private final SysUser sysUser; | ||
|
||
private final CustomChatMessage request; | ||
|
||
private final SseEmitter sseEmitter; | ||
|
||
private final StringBuilder sb; | ||
|
||
private final Encoding enc; | ||
|
||
private final List<Integer> promptTokens; | ||
|
||
public void bizProcess(String item){ | ||
System.out.println("智普流式输出:" +item); | ||
if (!"[DONE]".equals(item)){ | ||
StreamCompletionResult resultObj = JSONObject.parseObject(item, StreamCompletionResult.class); | ||
String content = resultObj.getChoices().get(0).getDelta().getContent(); | ||
try { | ||
if (StringUtils.isNotBlank(resultObj.getChoices().get(0).getFinish_reason())) { | ||
this.getSseEmitter().send("[END]"); | ||
String fullContent = this.getSb().toString(); | ||
List<Integer> completionToken = this.getEnc().encode(fullContent); | ||
System.out.println("total token costs: " + (this.getPromptTokens().size() + completionToken.size())); | ||
this.getConversationService().saveConversation(this.getSysUser().getId(), this.getRequest().getSessionId(), this.getSb().toString(), "A"); | ||
} else { | ||
if (content.contains("\n") || content.contains("\r")) { | ||
content = content.replaceAll("\n", "<br>"); | ||
content = content.replaceAll("\r", "<br>"); | ||
} | ||
if (content.contains(" ")) { | ||
content = content.replaceAll(" ", " "); | ||
} | ||
this.getSb().append(content); | ||
this.getSseEmitter().send(content); | ||
} | ||
} catch (IOException e) { | ||
log.error("KimiCompletionBizProcessor--->>bizProcess异常", e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
.../java/com/hkh/ai/chain/llm/capabilities/generation/text/kimi/KimiCompletionWebClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.hkh.ai.chain.llm.capabilities.generation.text.kimi; | ||
|
||
import com.alibaba.fastjson2.JSONObject; | ||
import com.hkh.ai.chain.llm.capabilities.generation.KimiAiUtil; | ||
import com.hkh.ai.chain.llm.capabilities.generation.KimiApis; | ||
import jakarta.annotation.PostConstruct; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import org.springframework.web.reactive.function.client.WebClientResponseException; | ||
import reactor.core.publisher.Flux; | ||
|
||
/** | ||
* 智普AI web client | ||
* @author huangkh | ||
*/ | ||
@Slf4j | ||
@Component | ||
public class KimiCompletionWebClient { | ||
private WebClient webClient; | ||
|
||
@Autowired | ||
private KimiAiUtil kimiAiUtil; | ||
|
||
@PostConstruct | ||
public void init(){ | ||
log.info("kimi ai api web client init..."); | ||
this.webClient = WebClient.builder() | ||
.defaultHeader("content-type", "application/json") | ||
.build(); | ||
} | ||
|
||
public Flux<String> streamChatCompletion(JSONObject requestBody){ | ||
log.info("streamChatCompletion 参数:{}",requestBody); | ||
String accessToken = kimiAiUtil.getAppKey(); | ||
return webClient.post() | ||
.uri(KimiApis.COMPLETION_TEXT) | ||
.bodyValue(requestBody) | ||
.header("Authorization","Bearer " + accessToken) | ||
.retrieve() | ||
.bodyToFlux(String.class) | ||
.onErrorResume(WebClientResponseException.class, ex -> { | ||
ex.printStackTrace(); | ||
HttpStatusCode statusCode = ex.getStatusCode(); | ||
String res = ex.getResponseBodyAsString(); | ||
log.error("Kimi AI API error: {} {}", statusCode, res); | ||
return Flux.error(new RuntimeException(res)); | ||
}); | ||
|
||
} | ||
|
||
public Flux<JSONObject> createFlux(JSONObject requestBody, KimiCompletionBizProcessor kimiCompletionBizProcessor){ | ||
log.info("createFlux 参数:{}",requestBody); | ||
Flux<JSONObject> flux = Flux.create(emitter -> { | ||
emitter.next(requestBody); | ||
emitter.complete(); | ||
}); | ||
|
||
flux.subscribe( | ||
jsonObject -> { | ||
Flux<String> stringFlux = streamChatCompletion(requestBody); | ||
stringFlux.subscribe(kimiCompletionBizProcessor::bizProcess); | ||
}, | ||
System.err::println, | ||
() -> System.out.println("emitter completed") | ||
); | ||
return flux; | ||
} | ||
|
||
} |
Oops, something went wrong.