Added support for gemini

Tested with AI studio
This commit is contained in:
Barrca 2025-03-30 22:04:30 +02:00
parent 628773c5e4
commit 08ba4da72f
4 changed files with 40 additions and 7 deletions

28
package-lock.json generated
View file

@ -14,6 +14,7 @@
"@codemirror/state": "^6.5.1",
"@codemirror/view": "^6.36.2",
"@langchain/core": "^0.3.16",
"@langchain/google-genai": "^0.2.1",
"@langchain/ollama": "^0.1.1",
"@langchain/openai": "^0.3.11",
"@types/diff-match-patch": "^1.0.36",
@ -26,7 +27,7 @@
"@typescript-eslint/parser": "^8.11.0",
"builtin-modules": "^4.0.0",
"esbuild": "^0.25.0",
"obsidian": "*",
"obsidian": "latest",
"typescript": "^5.6.3"
}
},
@ -677,6 +678,15 @@
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"
}
},
"node_modules/@google/generative-ai": {
"version": "0.24.0",
"resolved": "https://registry.npmjs.org/@google/generative-ai/-/generative-ai-0.24.0.tgz",
"integrity": "sha512-fnEITCGEB7NdX0BhoYZ/cq/7WPZ1QS5IzJJfC3Tg/OwkvBetMiVJciyaan297OvE4B9Jg1xvo0zIazX/9sGu1Q==",
"license": "Apache-2.0",
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/@humanfs/core": {
"version": "0.19.1",
"resolved": "https://registry.npmjs.org/@humanfs/core/-/core-0.19.1.tgz",
@ -765,6 +775,22 @@
"node": ">=18"
}
},
"node_modules/@langchain/google-genai": {
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/@langchain/google-genai/-/google-genai-0.2.1.tgz",
"integrity": "sha512-30utucZZmL605SSIPY3EQeXYZJS9b5Hlrn+gUuW6TS6bvrONFhl/CqpdvmJ6ho8rXJfHDjETGqr8XcolcRGDGQ==",
"license": "MIT",
"dependencies": {
"@google/generative-ai": "^0.24.0",
"zod-to-json-schema": "^3.22.4"
},
"engines": {
"node": ">=18"
},
"peerDependencies": {
"@langchain/core": ">=0.3.17 <0.4.0"
}
},
"node_modules/@langchain/ollama": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/@langchain/ollama/-/ollama-0.1.4.tgz",

View file

@ -27,6 +27,7 @@
"@codemirror/state": "^6.5.1",
"@codemirror/view": "^6.36.2",
"@langchain/core": "^0.3.16",
"@langchain/google-genai": "^0.2.1",
"@langchain/ollama": "^0.1.1",
"@langchain/openai": "^0.3.11",
"@types/diff-match-patch": "^1.0.36",

View file

@ -1,6 +1,7 @@
// api.ts
import { ChatOpenAI } from "@langchain/openai";
import { ChatOllama } from "@langchain/ollama";
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
import { SystemMessage, HumanMessage, AIMessage } from "@langchain/core/messages";
import { InlineAISettings } from "./settings";
import { App, MarkdownView, Notice } from "obsidian";
@ -12,7 +13,7 @@ import { parseCommand } from "./modules/commands/parser";
* Class to manage interactions with different chat APIs.
*/
export class ChatApiManager {
private chatClient: ChatOpenAI | ChatOllama | null;
private chatClient: ChatOpenAI | ChatOllama | ChatGoogleGenerativeAI | null;
private app: App;
private settings: InlineAISettings;
@ -32,7 +33,7 @@ export class ChatApiManager {
* @param settings - Configuration settings for the chat API.
* @returns An instance of ChatOpenAI, ChatOllama, or null if initialization fails.
*/
private initializeChatClient(settings: InlineAISettings): ChatOpenAI | ChatOllama | null {
private initializeChatClient(settings: InlineAISettings): ChatOpenAI | ChatOllama | ChatGoogleGenerativeAI | null {
try {
switch (settings.provider) {
case "openai":
@ -50,7 +51,11 @@ export class ChatApiManager {
return new ChatOllama({
model: settings.model,
});
case "gemini":
return new ChatGoogleGenerativeAI({
model: settings.model,
apiKey: settings.apiKey,
});
case "custom":
if (!settings.apiKey || !settings.customURL) {
new Notice("⚠️ API key and custom base URL are required for custom providers.");

View file

@ -5,7 +5,7 @@ import { SlashCommand } from "./modules/commands/source";
// Interface for the settings
export interface InlineAISettings {
provider: "openai" | "ollama" | "custom";
provider: "openai" | "ollama" | "custom" | "gemini";
model: string;
apiKey?: string;
customURL?: string;
@ -53,10 +53,11 @@ export class InlineAISettingsTab extends PluginSettingTab {
dropdown
.addOption("openai", "OpenAI")
.addOption("ollama", "Ollama")
.addOption("gemini", "Gemini")
.addOption("custom", "Custom/OpenAI-compatible")
.setValue(this.plugin.settings.provider)
.onChange(async (value) => {
this.plugin.settings.provider = value as "openai" | "ollama" | "custom";
this.plugin.settings.provider = value as "openai" | "ollama" | "custom" | "gemini";
await this.saveSettings();
this.display(); // Refresh UI to show/hide API key field
})
@ -76,7 +77,7 @@ export class InlineAISettingsTab extends PluginSettingTab {
});
// API Key setting (conditionally displayed for OpenAI-supported endpoints)
if (this.plugin.settings.provider === "openai" || this.plugin.settings.provider === "custom") {
if (this.plugin.settings.provider === "openai" || this.plugin.settings.provider === "custom" || this.plugin.settings.provider === "gemini") {
new Setting(containerEl)
.setName("API key")
.setDesc("Enter your API key.")