【添加todo】google gemini 初始化问题

This commit is contained in:
cherishsince 2024-05-24 16:10:23 +08:00
parent 10ca7a93ac
commit 546b522c48

View File

@ -20,9 +20,12 @@ import cn.iocoder.yudao.framework.ai.core.model.xinghuo.api.XingHuoApi;
import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatClient; import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatClient;
import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatOptions; import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatOptions;
import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanApi; import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanApi;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.vertexai.VertexAI; import com.google.cloud.vertexai.VertexAI;
import org.springframework.ai.autoconfigure.ollama.OllamaAutoConfiguration; import org.springframework.ai.autoconfigure.ollama.OllamaAutoConfiguration;
import org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration; import org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration;
import org.springframework.ai.autoconfigure.vertexai.gemini.VertexAiGeminiConnectionProperties;
import org.springframework.ai.autoconfigure.vertexai.palm2.VertexAiPalm2ConnectionProperties;
import org.springframework.ai.chat.StreamingChatClient; import org.springframework.ai.chat.StreamingChatClient;
import org.springframework.ai.chat.prompt.ChatOptions; import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.ollama.OllamaChatClient; import org.springframework.ai.ollama.OllamaChatClient;
@ -35,6 +38,7 @@ import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.ai.vertexai.gemini.VertexAiGeminiChatClient; import org.springframework.ai.vertexai.gemini.VertexAiGeminiChatClient;
import org.springframework.ai.vertexai.gemini.VertexAiGeminiChatOptions; import org.springframework.ai.vertexai.gemini.VertexAiGeminiChatOptions;
import java.io.IOException;
import java.util.List; import java.util.List;
/** /**
@ -169,15 +173,23 @@ public class AiClientFactoryImpl implements AiClientFactory {
return new QianWenChatClient(qianWenApi); return new QianWenChatClient(qianWenApi);
} }
private static VertexAiGeminiChatClient buildGoogleGemir(String key) { private static VertexAiGeminiChatClient buildGoogleGemir(String key) {
List<String> keys = StrUtil.split(key, '|'); List<String> keys = StrUtil.split(key, '|');
Assert.equals(keys.size(), 2, "VertexAiGeminiChatClient 的密钥需要 (projectId|location) 格式"); Assert.equals(keys.size(), 2, "VertexAiGeminiChatClient 的密钥需要 (projectId|location) 格式");
VertexAI vertexApi = new VertexAI(keys.get(0), keys.get(1)); // VertexAiGeminiConnectionProperties connectionProperties = new VertexAiGeminiConnectionProperties();
// connectionProperties.setApiKey("AIzaSyBpe376HTA8uPKJN_OJTh7MEO3v6LMqfXU");
//
// GoogleCredentials credentials = GoogleCredentials.fromStream(connectionProperties.getCredentialsUri().getInputStream());
// todo @芋艿 google gemini 没找到对于初始化 client 方式文档中说是用过 GoogleCredentials 来初始化凭证
// api-key: AIzaSyBpe376HTA8uPKJN_OJTh7MEO3v6LMqfXU
VertexAI vertexApi = new VertexAI(
"skilled-snow-409401",
"us-central1"
);
return new VertexAiGeminiChatClient(vertexApi, return new VertexAiGeminiChatClient(vertexApi,
VertexAiGeminiChatOptions.builder() VertexAiGeminiChatOptions.builder()
.withTemperature(0.4F) .withTemperature(0.4F)
.withModel(VertexAiGeminiChatClient.ChatModel.GEMINI_PRO.getValue())
.build()); .build());
} }
} }