Skip to content

Commit cc51849

Browse files
committed
support vertex ai with api key
1 parent 559f418 commit cc51849

File tree

6 files changed

+95
-10
lines changed

6 files changed

+95
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1+
import { checkAuth } from '@/app/(backend)/middleware/auth';
12
import { AgentRuntime, ModelProvider } from '@/libs/agent-runtime';
23
import { LobeVertexAI } from '@/libs/agent-runtime/vertexai';
34
import { safeParseJSON } from '@/utils/safeParseJSON';
45

56
import { POST as UniverseRoute } from '../[provider]/route';
67

7-
export const POST = async (req: Request) =>
8+
// due to the Chinese region does not support accessing Google
9+
// we need to use proxy to access it
10+
// refs: https://github.com/google/generative-ai-js/issues/29#issuecomment-1866246513
11+
// if (process.env.HTTP_PROXY_URL) {
12+
// const { setGlobalDispatcher, ProxyAgent } = require('undici');
13+
//
14+
// setGlobalDispatcher(new ProxyAgent({ uri: process.env.HTTP_PROXY_URL }));
15+
// }
16+
17+
export const POST = checkAuth(async (req: Request, { jwtPayload }) =>
818
UniverseRoute(req, {
919
createRuntime: () => {
1020
const credentialsContent = process.env.VERTEXAI_CREDENTIALS ?? undefined;
1121

12-
const googleAuthOptions = credentialsContent ? safeParseJSON(credentialsContent) : {};
22+
const googleAuthOptions =
23+
jwtPayload.apiKey ?? (credentialsContent ? safeParseJSON(credentialsContent) : {});
1324

1425
const instance = LobeVertexAI.initFromVertexAI({
1526
googleAuthOptions: googleAuthOptions,
@@ -20,4 +31,5 @@ export const POST = async (req: Request) =>
2031
return new AgentRuntime(instance);
2132
},
2233
params: Promise.resolve({ provider: ModelProvider.VertexAI }),
23-
});
34+
}),
35+
);

src/app/[variants]/(main)/settings/provider/(detail)/huggingface/page.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ const useProviderCard = (): ProviderItem => {
4343
) : (
4444
<FormPassword
4545
autoComplete={'new-password'}
46-
placeholder={t(`huggingface.accessToken.placeholder`)}
46+
placeholder={t('huggingface.accessToken.placeholder')}
4747
/>
4848
),
4949
desc: (
5050
<Markdown className={styles.markdown} fontSize={12} variant={'chat'}>
51-
{t(`huggingface.accessToken.desc`)}
51+
{t('huggingface.accessToken.desc')}
5252
</Markdown>
5353
),
54-
label: t(`huggingface.accessToken.title`),
54+
label: t('huggingface.accessToken.title'),
5555
name: [KeyVaultsConfigKey, LLMProviderApiTokenKey],
5656
},
5757
],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use client';
2+
3+
import { Markdown } from '@lobehub/ui';
4+
import { createStyles } from 'antd-style';
5+
import { useTranslation } from 'react-i18next';
6+
7+
import { FormPassword } from '@/components/FormInput';
8+
import { VertexAIProviderCard } from '@/config/modelProviders';
9+
import { aiProviderSelectors, useAiInfraStore } from '@/store/aiInfra';
10+
import { GlobalLLMProviderKey } from '@/types/user/settings';
11+
12+
import { KeyVaultsConfigKey, LLMProviderApiTokenKey } from '../../const';
13+
import { SkeletonInput } from '../../features/ProviderConfig';
14+
import { ProviderItem } from '../../type';
15+
import ProviderDetail from '../[id]';
16+
17+
const useStyles = createStyles(({ css, token }) => ({
18+
markdown: css`
19+
p {
20+
color: ${token.colorTextDescription} !important;
21+
}
22+
`,
23+
tip: css`
24+
font-size: 12px;
25+
color: ${token.colorTextDescription};
26+
`,
27+
}));
28+
29+
const providerKey: GlobalLLMProviderKey = 'vertexai';
30+
31+
// Same as OpenAIProvider, but replace API Key with HuggingFace Access Token
32+
const useProviderCard = (): ProviderItem => {
33+
const { t } = useTranslation('modelProvider');
34+
const { styles } = useStyles();
35+
const isLoading = useAiInfraStore(aiProviderSelectors.isAiProviderConfigLoading(providerKey));
36+
37+
return {
38+
...VertexAIProviderCard,
39+
apiKeyItems: [
40+
{
41+
children: isLoading ? (
42+
<SkeletonInput />
43+
) : (
44+
<FormPassword
45+
autoComplete={'new-password'}
46+
placeholder={t('vertexai.apiKey.placeholder')}
47+
/>
48+
),
49+
desc: (
50+
<Markdown className={styles.markdown} fontSize={12} variant={'chat'}>
51+
{t('vertexai.apiKey.desc')}
52+
</Markdown>
53+
),
54+
label: t('vertexai.apiKey.title'),
55+
name: [KeyVaultsConfigKey, LLMProviderApiTokenKey],
56+
},
57+
],
58+
};
59+
};
60+
61+
const Page = () => {
62+
const card = useProviderCard();
63+
64+
return <ProviderDetail {...card} />;
65+
};
66+
67+
export default Page;

src/config/modelProviders/vertexai.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ const VertexAI: ModelProviderCard = {
77
description:
88
'Google 的 Gemini 系列是其最先进、通用的 AI模型,由 Google DeepMind 打造,专为多模态设计,支持文本、代码、图像、音频和视频的无缝理解与处理。适用于从数据中心到移动设备的多种环境,极大提升了AI模型的效率与应用广泛性。',
99
id: 'vertexai',
10-
modelsUrl: 'https://cloud.google.com/vertex-ai/generative-ai/docs/learn/model-versioning',
10+
modelsUrl: 'https://console.cloud.google.com/vertex-ai/model-garden',
1111
name: 'VertexAI',
1212
settings: {
13-
showApiKey: false,
14-
// showChecker: false,
13+
disableBrowserRequest: true,
1514
smoothing: {
1615
speed: 2,
1716
text: true,

src/locales/default/modelProvider.ts

+7
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,13 @@ export default {
325325
tooltip: '更新服务商基础配置',
326326
updateSuccess: '更新成功',
327327
},
328+
vertexai: {
329+
apiKey: {
330+
desc: '填入你的 Vertex Ai Keys',
331+
placeholder: `{ "type": "service_account", "project_id": "xxx", "private_key_id": ... }`,
332+
title: 'Vertex AI Keys',
333+
},
334+
},
328335
zeroone: {
329336
title: '01.AI 零一万物',
330337
},

src/types/user/settings/keyVaults.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ export interface UserKeyVaults {
6868
tencentcloud?: OpenAICompatibleKeyVault;
6969
togetherai?: OpenAICompatibleKeyVault;
7070
upstage?: OpenAICompatibleKeyVault;
71+
vertexai?: OpenAICompatibleKeyVault;
7172
vllm?: OpenAICompatibleKeyVault;
7273
volcengine?: OpenAICompatibleKeyVault;
7374
wenxin?: OpenAICompatibleKeyVault;
74-
vertexai?: undefined;
7575
xai?: OpenAICompatibleKeyVault;
7676
zeroone?: OpenAICompatibleKeyVault;
7777
zhipu?: OpenAICompatibleKeyVault;

0 commit comments

Comments
 (0)