Преглед на файлове

Added embedding model support for zhipu ai

tycoding преди 1 година
родител
ревизия
0cf9cec5ef

+ 6 - 0
langchat-auth/src/main/java/cn/tycoding/langchat/auth/service/GlobalExceptionTranslator.java

@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.ResponseStatus;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
 import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
+import org.springframework.web.servlet.resource.NoResourceFoundException;
 
 import java.nio.file.AccessDeniedException;
 
@@ -107,4 +108,9 @@ public class GlobalExceptionTranslator {
         return R.fail(HttpStatus.UNAUTHORIZED);
     }
 
+    @ExceptionHandler({NoResourceFoundException.class})
+    @ResponseStatus(HttpStatus.BAD_REQUEST)
+    public R handleError(NoResourceFoundException e) {
+        return R.fail(HttpStatus.UNAUTHORIZED);
+    }
 }

+ 1 - 0
langchat-core/src/main/java/cn/tycoding/langchat/core/consts/EmbedConst.java

@@ -13,5 +13,6 @@ public interface EmbedConst {
     String CLAZZ_NAME_AZURE_OPENAI = "AzureOpenAiEmbeddingModel";
     String CLAZZ_NAME_QIANFAN = "QianfanEmbeddingModel";
     String CLAZZ_NAME_QIANWEN = "QwenEmbeddingModel";
+    String CLAZZ_NAME_ZHIPU = "ZhipuAiEmbeddingModel";
     String CLAZZ_NAME_OLLAMA = "OllamaEmbeddingModel";
 }

+ 13 - 0
langchat-core/src/main/java/cn/tycoding/langchat/core/provider/ProviderInitialize.java

@@ -19,6 +19,7 @@ import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
 import dev.langchain4j.model.qianfan.QianfanEmbeddingModel;
 import dev.langchain4j.model.qianfan.QianfanStreamingChatModel;
 import dev.langchain4j.model.vertexai.VertexAiGeminiStreamingChatModel;
+import dev.langchain4j.model.zhipu.ZhipuAiEmbeddingModel;
 import dev.langchain4j.model.zhipu.ZhipuAiStreamingChatModel;
 import lombok.AllArgsConstructor;
 import org.springframework.beans.BeansException;
@@ -49,7 +50,9 @@ public class ProviderInitialize implements ApplicationContextAware {
         contextHolder.unregisterBean(EmbedConst.CLAZZ_NAME_OPENAI);
         contextHolder.unregisterBean(EmbedConst.CLAZZ_NAME_AZURE_OPENAI);
         contextHolder.unregisterBean(EmbedConst.CLAZZ_NAME_QIANFAN);
+        contextHolder.unregisterBean(EmbedConst.CLAZZ_NAME_ZHIPU);
         contextHolder.unregisterBean(EmbedConst.CLAZZ_NAME_QIANWEN);
+        contextHolder.unregisterBean(EmbedConst.CLAZZ_NAME_OLLAMA);
 
         List<AigcModel> list = aigcModelService.list();
         list.forEach(model -> {
@@ -234,6 +237,16 @@ public class ProviderInitialize implements ApplicationContextAware {
                     contextHolder.registerBean(EmbedConst.CLAZZ_NAME_QIANWEN, build);
                 }
 
+                if (ProviderEnum.ZHIPU.getModel().equals(model.getModelType())) {
+                    ZhipuAiEmbeddingModel build = ZhipuAiEmbeddingModel
+                            .builder()
+                            .apiKey(model.getApiKey())
+                            .model(model.getModel())
+                            .baseUrl(model.getBaseUrl())
+                            .build();
+                    contextHolder.registerBean(EmbedConst.CLAZZ_NAME_ZHIPU, build);
+                }
+
                 if (ProviderEnum.OLLAMA.getModel().equals(model.getModelType())) {
                     OllamaEmbeddingModel build = OllamaEmbeddingModel
                             .builder()