From 07fd622901c58fb5840eded37e6424c406dbfe68 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 14 Oct 2024 23:50:34 -0700 Subject: [PATCH] add OpenRouter - Fixes Add OpenRouter hosting option #20 --- extensions/void/package.json | 16 ++++++++++++++-- extensions/void/src/common/sendLLMMessage.ts | 17 ++++++++++++++++- extensions/void/src/extension.ts | 6 +++++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/extensions/void/package.json b/extensions/void/package.json index 5c80ed9f..ef86a1cb 100644 --- a/extensions/void/package.json +++ b/extensions/void/package.json @@ -22,6 +22,7 @@ "description": "Choose an API provider.", "enum": [ "openAI", + "openRouter", "openAICompatible", "anthropic", "azure", @@ -236,6 +237,16 @@ "smollm:1.7b" ] }, + "void.openRouter.model": { + "type": "string", + "default": "openai/gpt-4o", + "description": "OpenRouter model to use." + }, + "void.openRouter.apiKey": { + "type": "string", + "default": "", + "description": "OpenRouter API key." + }, "void.openAICompatible.endpoint": { "type": "string", "default": "http://127.0.0.1:11434/v1", @@ -279,7 +290,8 @@ "command": "void.toggleThreadSelector", "title": "View past chats", "icon": "$(history)" - }, { + }, + { "command": "void.openSettings", "title": "Void settings", "icon": "$(settings-gear)" @@ -377,4 +389,4 @@ "openai": "^4.57.0", "diff": "^7.0.0" } -} +} \ No newline at end of file diff --git a/extensions/void/src/common/sendLLMMessage.ts b/extensions/void/src/common/sendLLMMessage.ts index c89ef492..f8ea1d97 100644 --- a/extensions/void/src/common/sendLLMMessage.ts +++ b/extensions/void/src/common/sendLLMMessage.ts @@ -31,6 +31,10 @@ export type ApiConfig = { endpoint: string, model: string, apikey: string + }, + openRouter: { + model: string, + apikey: string } whichApi: string } @@ -108,7 +112,7 @@ const sendClaudeMsg: SendLLMMessageFnTypeInternal = ({ messages, onText, onFinal -// OpenAI and OpenAICompatible +// OpenAI, OpenRouter, OpenAICompatible const sendOpenAIMsg: SendLLMMessageFnTypeInternal = ({ messages, onText, onFinalMessage, apiConfig }) => { let didAbort = false @@ -126,6 +130,16 @@ const sendOpenAIMsg: SendLLMMessageFnTypeInternal = ({ messages, onText, onFinal openai = new OpenAI({ apiKey: apiConfig.openAI.apikey, dangerouslyAllowBrowser: true }); options = { model: apiConfig.openAI.model, messages: messages, stream: true, } } + else if (apiConfig.whichApi === 'openRouter') { + openai = new OpenAI({ + baseURL: "https://openrouter.ai/api/v1", apiKey: apiConfig.openRouter.apikey, dangerouslyAllowBrowser: true, + defaultHeaders: { + "HTTP-Referer": 'https://voideditor.com', // Optional, for including your app on openrouter.ai rankings. + "X-Title": 'Void Editor', // Optional. Shows in rankings on openrouter.ai. + }, + }); + options = { model: apiConfig.openRouter.model, messages: messages, stream: true, } + } else if (apiConfig.whichApi === 'openAICompatible') { openai = new OpenAI({ baseURL: apiConfig.openAICompatible.endpoint, apiKey: apiConfig.openAICompatible.apikey, dangerouslyAllowBrowser: true }) options = { model: apiConfig.openAICompatible.model, messages: messages, stream: true, } @@ -286,6 +300,7 @@ export const sendLLMMessage: SendLLMMessageFnTypeExternal = ({ messages, onText, case 'anthropic': return sendClaudeMsg({ messages, onText, onFinalMessage, apiConfig }); case 'openAI': + case 'openRouter': case 'openAICompatible': return sendOpenAIMsg({ messages, onText, onFinalMessage, apiConfig }); case 'greptile': diff --git a/extensions/void/src/extension.ts b/extensions/void/src/extension.ts index 29d82b08..895e2f97 100644 --- a/extensions/void/src/extension.ts +++ b/extensions/void/src/extension.ts @@ -36,8 +36,12 @@ const getApiConfig = () => { }, openAICompatible: { endpoint: vscode.workspace.getConfiguration('void.openAICompatible').get('endpoint') ?? '', - apikey: vscode.workspace.getConfiguration('void.openAICompatible').get('apiKey') ?? '', model: vscode.workspace.getConfiguration('void.openAICompatible').get('model') ?? '', + apikey: vscode.workspace.getConfiguration('void.openAICompatible').get('apiKey') ?? '', + }, + openRouter: { + model: vscode.workspace.getConfiguration('void.openRouter').get('model') ?? '', + apikey: vscode.workspace.getConfiguration('void.openRouter').get('apiKey') ?? '', }, whichApi: vscode.workspace.getConfiguration('void').get('whichApi') ?? '' }