Skip to content

Commit

Permalink
feat: 调整日志输出& 模型选择策略
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyueyi committed Jun 28, 2023
1 parent bf045e8 commit 26aca19
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
import com.github.paicoding.forum.service.chatai.service.impl.chatgpt.ChatGptIntegration;
import com.github.paicoding.forum.service.chatai.service.impl.xunfei.XunFeiIntegration;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
Expand All @@ -24,13 +23,11 @@
* @author YiHui
* @date 2023/6/9
*/
@Slf4j
@Service
public class ChatFacade {
private final Map<AISourceEnum, ChatService> chatServiceMap;

@Autowired
private ChatGptIntegration chatGptIntegration;

public ChatFacade(List<ChatService> chatServiceList) {
chatServiceMap = Maps.newHashMapWithExpectedSize(chatServiceList.size());
for (ChatService chatService : chatServiceList) {
Expand Down Expand Up @@ -104,21 +101,20 @@ public ChatRecordsVo history(AISourceEnum source) {
* @return
*/
public AISourceEnum getRecommendAiSource() {
AISourceEnum source;
try {
ChatGptIntegration.ChatGptConfig config = SpringUtil.getBean(ChatGptIntegration.ChatGptConfig.class);
if (!CollectionUtils.isEmpty(config.getConf().get(config.getMain()).getKeys())) {
if (chatGptIntegration.creditInfo(AISourceEnum.CHAT_GPT_3_5).getTotalAvailable().compareTo(BigDecimal.ZERO) > 0) {
return AISourceEnum.CHAT_GPT_3_5;
}
}

if (StringUtils.isNotBlank(SpringUtil.getBean(XunFeiIntegration.XunFeiConfig.class).getApiKey())) {
return AISourceEnum.XUN_FEI_AI;
source = AISourceEnum.CHAT_GPT_3_5;
} else if (StringUtils.isNotBlank(SpringUtil.getBean(XunFeiIntegration.XunFeiConfig.class).getApiKey())) {
source = AISourceEnum.XUN_FEI_AI;
} else {
source = AISourceEnum.PAI_AI;
}

return AISourceEnum.PAI_AI;
} catch (Exception e) {
return AISourceEnum.PAI_AI;
source = AISourceEnum.PAI_AI;
}
log.info("当前选中的AI模型:{}", source);
return source;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public void onMsg(String message) {
if (StringUtils.isNotBlank(message)) {
item.appendAnswer(message);
consumer.accept(AiChatStatEnum.MID, chatRes);
log.info("ChatGpt返回内容: {}", lastMessage);
if (log.isDebugEnabled()) {
log.debug("ChatGpt返回内容: {}", lastMessage);
}
}
}

Expand All @@ -56,7 +58,7 @@ public void onError(Throwable throwable, String response) {

// 注册回答结束的回调钩子
listener.setOnComplate((s) -> {
item.appendAnswer("\n--------回答完成---------\n")
item.appendAnswer("\n")
.setAnswerType(ChatAnswerTypeEnum.STREAM_END);
consumer.accept(AiChatStatEnum.END, chatRes);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private ChatGPT simpleGPT(Long routingKey, AISourceEnum model) {
GptConf conf = config.getConf().getOrDefault(model, config.getConf().get(config.getMain()));
Proxy proxy = conf.isProxy() ? ProxyCenter.loadProxy(String.valueOf(routingKey)) : Proxy.NO_PROXY;

return ChatGPT.builder().apiKey(conf.fetchKey()).proxy(proxy).apiHost(conf.getApiHost()) //反向代理地址
return ChatGPT.builder().apiKeyList(conf.getKeys()).proxy(proxy).apiHost(conf.getApiHost()) //反向代理地址
.timeout(conf.getTimeOut()).build().init();
}

Expand Down

0 comments on commit 26aca19

Please sign in to comment.