From db19d1dd6eef9ad2dbe292786d258d2f61a80aaf Mon Sep 17 00:00:00 2001 From: tumf Date: Wed, 1 May 2024 15:57:54 +0700 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20(config.ts,=20engine/ollama.?= =?UTF-8?q?ts,=20utils/engine.ts):=20improve=20Ollama=20AI=20configuration?= =?UTF-8?q?=20and=20usage=20=E2=9C=A8=20(config.ts):=20add=20support=20for?= =?UTF-8?q?=20multiple=20Ollama=20models=20and=20allow=20users=20to=20spec?= =?UTF-8?q?ify=20the=20model=20in=20their=20config=20=E2=9C=85=20(engine/o?= =?UTF-8?q?llama.ts,=20utils/engine.ts):=20refactor=20code=20to=20use=20th?= =?UTF-8?q?e=20specified=20Ollama=20model=20instead=20of=20hardcoding=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/config.ts | 10 +++------- src/engine/ollama.ts | 7 ++++++- src/utils/engine.ts | 9 +++++++-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/commands/config.ts b/src/commands/config.ts index 9bc2e3e9..5236c780 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -57,7 +57,7 @@ export const configValidators = { //need api key unless running locally with ollama validateConfig( 'API_KEY', - value || config.OCO_AI_PROVIDER == 'ollama', + value || config.OCO_AI_PROVIDER.startsWith('ollama'), 'You need to provide an API key' ); validateConfig( @@ -187,12 +187,8 @@ export const configValidators = { [CONFIG_KEYS.OCO_AI_PROVIDER](value: any) { validateConfig( CONFIG_KEYS.OCO_AI_PROVIDER, - [ - '', - 'openai', - 'ollama' - ].includes(value), - `${value} is not supported yet, use 'ollama' or 'openai' (default)` + ['', 'openai','ollama'].includes(value) || value.startsWith('ollama/'), + `${value} is not supported yet, use 'ollama/{model}', 'ollama' or 'openai' (default)` ); return value; }, diff --git a/src/engine/ollama.ts b/src/engine/ollama.ts index f431ee1b..2bf7c001 100644 --- a/src/engine/ollama.ts +++ b/src/engine/ollama.ts @@ -3,10 +3,15 @@ import { ChatCompletionRequestMessage } from 'openai'; import { AiEngine } from './Engine'; export class OllamaAi implements AiEngine { + private model = "mistral"; // as default model of Ollama + + setModel(model: string) { + this.model = model; + } async generateCommitMessage( messages: Array ): Promise { - const model = 'mistral'; // todo: allow other models + const model = this.model; //console.log(messages); //process.exit() diff --git a/src/utils/engine.ts b/src/utils/engine.ts index 74d780b1..b2bc4eaf 100644 --- a/src/utils/engine.ts +++ b/src/utils/engine.ts @@ -5,9 +5,14 @@ import { ollamaAi } from '../engine/ollama'; export function getEngine(): AiEngine { const config = getConfig(); - if (config?.OCO_AI_PROVIDER == 'ollama') { + const provider = config?.OCO_AI_PROVIDER; + if (provider?.startsWith('ollama')) { + const model = provider.split('/')[1]; + if (model) { + ollamaAi.setModel(model); + } return ollamaAi; } - //open ai gpt by default + // open ai gpt by default return api; } From b7efffe225bb348e4938a37046c112e322b2c8c5 Mon Sep 17 00:00:00 2001 From: tumf Date: Thu, 2 May 2024 14:03:41 +0700 Subject: [PATCH 2/2] add build results --- out/cli.cjs | 23 ++++++++++++++--------- out/github-action.cjs | 23 ++++++++++++++--------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/out/cli.cjs b/out/cli.cjs index 52581c38..6938b591 100755 --- a/out/cli.cjs +++ b/out/cli.cjs @@ -18662,7 +18662,7 @@ var configValidators = { ["OCO_OPENAI_API_KEY" /* OCO_OPENAI_API_KEY */](value, config8 = {}) { validateConfig( "API_KEY", - value || config8.OCO_AI_PROVIDER == "ollama", + value || config8.OCO_AI_PROVIDER.startsWith("ollama"), "You need to provide an API key" ); validateConfig( @@ -18775,12 +18775,8 @@ var configValidators = { ["OCO_AI_PROVIDER" /* OCO_AI_PROVIDER */](value) { validateConfig( "OCO_AI_PROVIDER" /* OCO_AI_PROVIDER */, - [ - "", - "openai", - "ollama" - ].includes(value), - `${value} is not supported yet, use 'ollama' or 'openai' (default)` + ["", "openai", "ollama"].includes(value) || value.startsWith("ollama/"), + `${value} is not supported yet, use 'ollama/{model}', 'ollama' or 'openai' (default)` ); return value; }, @@ -22004,8 +22000,12 @@ var api = new OpenAi(); // src/engine/ollama.ts var OllamaAi = class { + model = "mistral"; + setModel(model) { + this.model = model; + } async generateCommitMessage(messages) { - const model = "mistral"; + const model = this.model; const url3 = "http://localhost:11434/api/chat"; const p4 = { model, @@ -22032,7 +22032,12 @@ var ollamaAi = new OllamaAi(); // src/utils/engine.ts function getEngine() { const config8 = getConfig(); - if (config8?.OCO_AI_PROVIDER == "ollama") { + const provider = config8?.OCO_AI_PROVIDER; + if (provider?.startsWith("ollama")) { + const model = provider.split("/")[1]; + if (model) { + ollamaAi.setModel(model); + } return ollamaAi; } return api; diff --git a/out/github-action.cjs b/out/github-action.cjs index 967d82a3..eb732060 100644 --- a/out/github-action.cjs +++ b/out/github-action.cjs @@ -24157,7 +24157,7 @@ var configValidators = { ["OCO_OPENAI_API_KEY" /* OCO_OPENAI_API_KEY */](value, config7 = {}) { validateConfig( "API_KEY", - value || config7.OCO_AI_PROVIDER == "ollama", + value || config7.OCO_AI_PROVIDER.startsWith("ollama"), "You need to provide an API key" ); validateConfig( @@ -24270,12 +24270,8 @@ var configValidators = { ["OCO_AI_PROVIDER" /* OCO_AI_PROVIDER */](value) { validateConfig( "OCO_AI_PROVIDER" /* OCO_AI_PROVIDER */, - [ - "", - "openai", - "ollama" - ].includes(value), - `${value} is not supported yet, use 'ollama' or 'openai' (default)` + ["", "openai", "ollama"].includes(value) || value.startsWith("ollama/"), + `${value} is not supported yet, use 'ollama/{model}', 'ollama' or 'openai' (default)` ); return value; }, @@ -27499,8 +27495,12 @@ var api = new OpenAi(); // src/engine/ollama.ts var OllamaAi = class { + model = "mistral"; + setModel(model) { + this.model = model; + } async generateCommitMessage(messages) { - const model = "mistral"; + const model = this.model; const url2 = "http://localhost:11434/api/chat"; const p2 = { model, @@ -27527,7 +27527,12 @@ var ollamaAi = new OllamaAi(); // src/utils/engine.ts function getEngine() { const config7 = getConfig(); - if (config7?.OCO_AI_PROVIDER == "ollama") { + const provider = config7?.OCO_AI_PROVIDER; + if (provider?.startsWith("ollama")) { + const model = provider.split("/")[1]; + if (model) { + ollamaAi.setModel(model); + } return ollamaAi; } return api;