Skip to content

Commit 1777462

Browse files
committed
feat(providers): add gemini
1 parent ecad065 commit 1777462

File tree

8 files changed

+151
-1
lines changed

8 files changed

+151
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@langchain/anthropic": "^0.2.3",
3232
"@langchain/community": "^0.2.16",
3333
"@langchain/openai": "^0.0.25",
34+
"@langchain/google-genai": "^0.0.23",
3435
"@xenova/transformers": "^2.17.1",
3536
"axios": "^1.6.8",
3637
"better-sqlite3": "^11.0.0",

sample.config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ KEEP_ALIVE = "5m" # How long to keep Ollama models loaded into memory. (Instead
77
OPENAI = "" # OpenAI API key - sk-1234567890abcdef1234567890abcdef
88
GROQ = "" # Groq API key - gsk_1234567890abcdef1234567890abcdef
99
ANTHROPIC = "" # Anthropic API key - sk-ant-1234567890abcdef1234567890abcdef
10+
GEMINI = "" # Gemini API key - sk-1234567890abcdef1234567890abcdef
1011

1112
[API_ENDPOINTS]
1213
SEARXNG = "http://localhost:32768" # SearxNG API URL

src/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface Config {
1414
OPENAI: string;
1515
GROQ: string;
1616
ANTHROPIC: string;
17+
GEMINI: string;
1718
};
1819
API_ENDPOINTS: {
1920
SEARXNG: string;
@@ -43,6 +44,8 @@ export const getGroqApiKey = () => loadConfig().API_KEYS.GROQ;
4344

4445
export const getAnthropicApiKey = () => loadConfig().API_KEYS.ANTHROPIC;
4546

47+
export const getGeminiApiKey = () => loadConfig().API_KEYS.GEMINI;
48+
4649
export const getSearxngApiEndpoint = () =>
4750
process.env.SEARXNG_API_URL || loadConfig().API_ENDPOINTS.SEARXNG;
4851

src/lib/providers/gemini.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import {
2+
ChatGoogleGenerativeAI,
3+
GoogleGenerativeAIEmbeddings,
4+
} from '@langchain/google-genai';
5+
import { getGeminiApiKey } from '../../config';
6+
import logger from '../../utils/logger';
7+
8+
export const loadGeminiChatModels = async () => {
9+
const geminiApiKey = getGeminiApiKey();
10+
11+
if (!geminiApiKey) return {};
12+
13+
try {
14+
const chatModels = {
15+
'gemini-1.5-flash': {
16+
displayName: 'Gemini 1.5 Flash',
17+
model: new ChatGoogleGenerativeAI({
18+
modelName: 'gemini-1.5-flash',
19+
temperature: 0.7,
20+
apiKey: geminiApiKey,
21+
}),
22+
},
23+
'gemini-1.5-flash-8b': {
24+
displayName: 'Gemini 1.5 Flash 8B',
25+
model: new ChatGoogleGenerativeAI({
26+
modelName: 'gemini-1.5-flash-8b',
27+
temperature: 0.7,
28+
apiKey: geminiApiKey,
29+
}),
30+
},
31+
'gemini-1.5-pro': {
32+
displayName: 'Gemini 1.5 Pro',
33+
model: new ChatGoogleGenerativeAI({
34+
modelName: 'gemini-1.5-pro',
35+
temperature: 0.7,
36+
apiKey: geminiApiKey,
37+
}),
38+
},
39+
};
40+
41+
return chatModels;
42+
} catch (err) {
43+
logger.error(`Error loading Gemini models: ${err}`);
44+
return {};
45+
}
46+
};
47+
48+
export const loadGeminiEmbeddingsModels = async () => {
49+
const geminiApiKey = getGeminiApiKey();
50+
51+
if (!geminiApiKey) return {};
52+
53+
try {
54+
const embeddingModels = {
55+
'text-embedding-004': {
56+
displayName: 'Text Embedding',
57+
model: new GoogleGenerativeAIEmbeddings({
58+
apiKey: geminiApiKey,
59+
modelName: 'text-embedding-004',
60+
}),
61+
},
62+
};
63+
64+
return embeddingModels;
65+
} catch (err) {
66+
logger.error(`Error loading Gemini embeddings model: ${err}`);
67+
return {};
68+
}
69+
};

src/lib/providers/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ import { loadOllamaChatModels, loadOllamaEmbeddingsModels } from './ollama';
33
import { loadOpenAIChatModels, loadOpenAIEmbeddingsModels } from './openai';
44
import { loadAnthropicChatModels } from './anthropic';
55
import { loadTransformersEmbeddingsModels } from './transformers';
6+
import { loadGeminiChatModels, loadGeminiEmbeddingsModels } from './gemini';
67

78
const chatModelProviders = {
89
openai: loadOpenAIChatModels,
910
groq: loadGroqChatModels,
1011
ollama: loadOllamaChatModels,
1112
anthropic: loadAnthropicChatModels,
13+
gemini: loadGeminiChatModels,
1214
};
1315

1416
const embeddingModelProviders = {
1517
openai: loadOpenAIEmbeddingsModels,
1618
local: loadTransformersEmbeddingsModels,
1719
ollama: loadOllamaEmbeddingsModels,
20+
gemini: loadGeminiEmbeddingsModels,
1821
};
1922

2023
export const getAvailableChatModelProviders = async () => {

src/routes/config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getGroqApiKey,
88
getOllamaApiEndpoint,
99
getAnthropicApiKey,
10+
getGeminiApiKey,
1011
getOpenaiApiKey,
1112
updateConfig,
1213
} from '../config';
@@ -52,7 +53,8 @@ router.get('/', async (_, res) => {
5253
config['ollamaApiUrl'] = getOllamaApiEndpoint();
5354
config['anthropicApiKey'] = getAnthropicApiKey();
5455
config['groqApiKey'] = getGroqApiKey();
55-
56+
config['geminiApiKey'] = getGeminiApiKey();
57+
5658
res.status(200).json(config);
5759
} catch (err: any) {
5860
res.status(500).json({ message: 'An error has occurred.' });
@@ -68,6 +70,7 @@ router.post('/', async (req, res) => {
6870
OPENAI: config.openaiApiKey,
6971
GROQ: config.groqApiKey,
7072
ANTHROPIC: config.anthropicApiKey,
73+
GEMINI: config.geminiApiKey,
7174
},
7275
API_ENDPOINTS: {
7376
OLLAMA: config.ollamaApiUrl,

ui/components/SettingsDialog.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ interface SettingsType {
6363
openaiApiKey: string;
6464
groqApiKey: string;
6565
anthropicApiKey: string;
66+
geminiApiKey: string;
6667
ollamaApiUrl: string;
6768
}
6869

@@ -476,6 +477,22 @@ const SettingsDialog = ({
476477
}
477478
/>
478479
</div>
480+
<div className="flex flex-col space-y-1">
481+
<p className="text-black/70 dark:text-white/70 text-sm">
482+
Gemini API Key
483+
</p>
484+
<Input
485+
type="text"
486+
placeholder="Gemini API key"
487+
defaultValue={config.geminiApiKey}
488+
onChange={(e) =>
489+
setConfig({
490+
...config,
491+
geminiApiKey: e.target.value,
492+
})
493+
}
494+
/>
495+
</div>
479496
</div>
480497
)}
481498
{isLoading && (

yarn.lock

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@
293293
resolved "https://registry.yarnpkg.com/@esbuild/win32-x64/-/win32-x64-0.19.12.tgz#c57c8afbb4054a3ab8317591a0b7320360b444ae"
294294
integrity sha512-T1QyPSDCyMXaO3pzBkF96E8xMkiRYbUEZADd29SyPGabqxMViNoii+NcK7eWJAEoU6RZyEm5lVSIjTmcdoB9HA==
295295

296+
"@google/generative-ai@^0.7.0":
297+
version "0.7.1"
298+
resolved "https://registry.yarnpkg.com/@google/generative-ai/-/generative-ai-0.7.1.tgz#eb187c75080c0706245699dbc06816c830d8c6a7"
299+
integrity sha512-WTjMLLYL/xfA5BW6xAycRPiAX7FNHKAxrid/ayqC1QMam0KAK0NbMeS9Lubw80gVg5xFMLE+H7pw4wdNzTOlxw==
300+
296301
"@huggingface/jinja@^0.2.2":
297302
version "0.2.2"
298303
resolved "https://registry.yarnpkg.com/@huggingface/jinja/-/jinja-0.2.2.tgz#faeb205a9d6995089bef52655ddd8245d3190627"
@@ -380,6 +385,23 @@
380385
zod "^3.22.4"
381386
zod-to-json-schema "^3.22.3"
382387

388+
"@langchain/core@>=0.2.16 <0.3.0":
389+
version "0.2.36"
390+
resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.2.36.tgz#75754c33aa5b9310dcf117047374a1ae011005a4"
391+
integrity sha512-qHLvScqERDeH7y2cLuJaSAlMwg3f/3Oc9nayRSXRU2UuaK/SOhI42cxiPLj1FnuHJSmN0rBQFkrLx02gI4mcVg==
392+
dependencies:
393+
ansi-styles "^5.0.0"
394+
camelcase "6"
395+
decamelize "1.2.0"
396+
js-tiktoken "^1.0.12"
397+
langsmith "^0.1.56-rc.1"
398+
mustache "^4.2.0"
399+
p-queue "^6.6.2"
400+
p-retry "4"
401+
uuid "^10.0.0"
402+
zod "^3.22.4"
403+
zod-to-json-schema "^3.22.3"
404+
383405
"@langchain/core@>=0.2.9 <0.3.0":
384406
version "0.2.15"
385407
resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.2.15.tgz#1bb99ac4fffe935c7ba37edcaa91abfba3c82219"
@@ -415,6 +437,15 @@
415437
zod "^3.22.4"
416438
zod-to-json-schema "^3.22.3"
417439

440+
"@langchain/google-genai@^0.0.23":
441+
version "0.0.23"
442+
resolved "https://registry.yarnpkg.com/@langchain/google-genai/-/google-genai-0.0.23.tgz#e73af501bc1df4c7642b531759b82dc3eb7ae459"
443+
integrity sha512-MTSCJEoKsfU1inz0PWvAjITdNFM4s41uvBCwLpcgx3jWJIEisczFD82x86ahYqJlb2fD6tohYSaCH/4tKAdkXA==
444+
dependencies:
445+
"@google/generative-ai" "^0.7.0"
446+
"@langchain/core" ">=0.2.16 <0.3.0"
447+
zod-to-json-schema "^3.22.4"
448+
418449
"@langchain/openai@^0.0.25", "@langchain/openai@~0.0.19":
419450
version "0.0.25"
420451
resolved "https://registry.yarnpkg.com/@langchain/openai/-/openai-0.0.25.tgz#8332abea1e3acb9b1169f90636e518c0ee90622e"
@@ -712,6 +743,11 @@
712743
resolved "https://registry.yarnpkg.com/@types/triple-beam/-/triple-beam-1.3.5.tgz#74fef9ffbaa198eb8b588be029f38b00299caa2c"
713744
integrity sha512-6WaYesThRMCl19iryMYP7/x2OVgCtbIVflDGFpWnb9irXI3UjYE4AzmYuiUKY1AJstGijoY+MgUszMgRxIYTYw==
714745

746+
"@types/uuid@^10.0.0":
747+
version "10.0.0"
748+
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-10.0.0.tgz#e9c07fe50da0f53dc24970cca94d619ff03f6f6d"
749+
integrity sha512-7gqG38EyHgyP1S+7+xomFtL+ZNHcKv6DwNaCZmJmo1vgMugyF3TCnXVg4t1uk89mLNwnLtnY3TpOpCOyp1/xHQ==
750+
715751
"@types/uuid@^9.0.1":
716752
version "9.0.8"
717753
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-9.0.8.tgz#7545ba4fc3c003d6c756f651f3bf163d8f0f29ba"
@@ -1900,6 +1936,18 @@ langchainhub@~0.0.8:
19001936
resolved "https://registry.yarnpkg.com/langchainhub/-/langchainhub-0.0.8.tgz#fd4b96dc795e22e36c1a20bad31b61b0c33d3110"
19011937
integrity sha512-Woyb8YDHgqqTOZvWIbm2CaFDGfZ4NTSyXV687AG4vXEfoNo7cGQp7nhl7wL3ehenKWmNEmcxCLgOZzW8jE6lOQ==
19021938

1939+
langsmith@^0.1.56-rc.1:
1940+
version "0.1.68"
1941+
resolved "https://registry.yarnpkg.com/langsmith/-/langsmith-0.1.68.tgz#848332e822fe5e6734a07f1c36b6530cc1798afb"
1942+
integrity sha512-otmiysWtVAqzMx3CJ4PrtUBhWRG5Co8Z4o7hSZENPjlit9/j3/vm3TSvbaxpDYakZxtMjhkcJTqrdYFipISEiQ==
1943+
dependencies:
1944+
"@types/uuid" "^10.0.0"
1945+
commander "^10.0.1"
1946+
p-queue "^6.6.2"
1947+
p-retry "4"
1948+
semver "^7.6.3"
1949+
uuid "^10.0.0"
1950+
19031951
langsmith@~0.1.1, langsmith@~0.1.7:
19041952
version "0.1.14"
19051953
resolved "https://registry.yarnpkg.com/langsmith/-/langsmith-0.1.14.tgz#2b889dbcfb49547614df276a4a5a063092a1585d"
@@ -2568,6 +2616,11 @@ semver@^7.3.5, semver@^7.5.3, semver@^7.5.4:
25682616
dependencies:
25692617
lru-cache "^6.0.0"
25702618

2619+
semver@^7.6.3:
2620+
version "7.6.3"
2621+
resolved "https://registry.yarnpkg.com/semver/-/semver-7.6.3.tgz#980f7b5550bc175fb4dc09403085627f9eb33143"
2622+
integrity sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A==
2623+
25712624
25722625
version "0.18.0"
25732626
resolved "https://registry.yarnpkg.com/send/-/send-0.18.0.tgz#670167cc654b05f5aa4a767f9113bb371bc706be"

0 commit comments

Comments
 (0)