From 75ff5d828b04a024decd1b5841ad9ac4762e467d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dae=E2=9D=A4=EF=B8=8F?= <74119677+daeisbae@users.noreply.github.com> Date: Sun, 5 Jan 2025 22:19:14 -0800 Subject: [PATCH 1/4] Ollama Support (#149) --- src/llm/provider/ollama.ts | 26 ++++++++++++++++++++++++++ src/service/llm-factory.ts | 11 +++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/llm/provider/ollama.ts diff --git a/src/llm/provider/ollama.ts b/src/llm/provider/ollama.ts new file mode 100644 index 0000000..c234245 --- /dev/null +++ b/src/llm/provider/ollama.ts @@ -0,0 +1,26 @@ +import { ChatOllama } from '@langchain/ollama'; +import LLMConfig from '../llm-config'; +import { HistoryItem, LLMProvider } from '../llm-provider'; + +export class OllamaProvider extends LLMProvider { + private llm: ChatOllama; + + constructor( + modelName: string, + llmconfig: LLMConfig) { + super('', modelName, llmconfig, ''); + this.llm = new ChatOllama({ + model: modelName, + temperature: llmconfig.temperature, + format: 'json', + topP: llmconfig.topP, + topK: llmconfig.topK, + }); + } + + async run(userPrompt: string, history: HistoryItem[]): Promise { + const response = await this.llm.invoke(userPrompt); + console.log(response.content.toString()); + return response.content.toString(); + } +} \ No newline at end of file diff --git a/src/service/llm-factory.ts b/src/service/llm-factory.ts index e04bd81..adeb323 100644 --- a/src/service/llm-factory.ts +++ b/src/service/llm-factory.ts @@ -3,6 +3,7 @@ import DeepSeekProvider from '@/llm/provider/deepseek' import GoogleProvider from '@/llm/provider/google' import LLMConfig from '@/llm/llm-config' import dotenv from 'dotenv' +import { OllamaProvider } from '@/llm/provider/ollama' dotenv.config() /** @@ -19,11 +20,21 @@ export default class LLMFactory { const apiKey = process.env.LLM_APIKEY const modelName = process.env.LLM_MODELNAME + if (!provider) { + throw new Error('LLM Provider is not specified. Please set LLM_PROVIDER in the environment\nExample: LLM_PROVIDER=google, LLM_PROVIDER=deepseek, LLM_PROVIDER=ollama') + } + + if(!modelName) { + throw new Error('LLM Model name is not specified. Example: LLM_MODELNAME=llama3.3 for llama3.3') + } + switch (provider) { case 'google': return new GoogleProvider(apiKey!, modelName!, llmConfig) case 'deepseek': return new DeepSeekProvider(apiKey!, modelName!, llmConfig) + case 'ollama': + return new OllamaProvider(modelName!, llmConfig) default: throw new Error(`Unsupported LLM provider: ${provider}`) } From 27472443fd46d48bdb244d5a7ed9506c2bce09ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dae=E2=9D=A4=EF=B8=8F?= <74119677+daeisbae@users.noreply.github.com> Date: Sun, 5 Jan 2025 23:29:04 -0800 Subject: [PATCH 2/4] Forcing JSON output degrades the quality (#149) --- src/llm/provider/ollama.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/llm/provider/ollama.ts b/src/llm/provider/ollama.ts index c234245..1d42335 100644 --- a/src/llm/provider/ollama.ts +++ b/src/llm/provider/ollama.ts @@ -12,7 +12,7 @@ export class OllamaProvider extends LLMProvider { this.llm = new ChatOllama({ model: modelName, temperature: llmconfig.temperature, - format: 'json', + // format: 'json', topP: llmconfig.topP, topK: llmconfig.topK, }); From d5b0ffb94b7d75e97a5cd9938c98f9ea90e80008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dae=E2=9D=A4=EF=B8=8F?= <74119677+daeisbae@users.noreply.github.com> Date: Sun, 5 Jan 2025 23:31:10 -0800 Subject: [PATCH 3/4] Forcing JSON output in ollama degrades the quality of the response (#149) --- src/llm/provider/ollama.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/llm/provider/ollama.ts b/src/llm/provider/ollama.ts index 1d42335..c80b084 100644 --- a/src/llm/provider/ollama.ts +++ b/src/llm/provider/ollama.ts @@ -12,7 +12,7 @@ export class OllamaProvider extends LLMProvider { this.llm = new ChatOllama({ model: modelName, temperature: llmconfig.temperature, - // format: 'json', + // format: 'json', // Forcing JSON output degraded the quality of the responses topP: llmconfig.topP, topK: llmconfig.topK, }); From 0f0f9163d974c3217dd84b7b6fdd9cbf213e189a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dae=E2=9D=A4=EF=B8=8F?= <74119677+daeisbae@users.noreply.github.com> Date: Sun, 5 Jan 2025 23:40:26 -0800 Subject: [PATCH 4/4] Ollama Configuration Guide (#149) --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index de70377..965f882 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,22 @@ 4. Build the server (`npm run build`) 5. Run (`npm start`) +#### Ollama Configuration Guide + +- It's recommended if you can run bigger LLM than 14b parameter. +- You do not need to provide the API KEY +- Set LLM_PROVIDER to Ollama (It is going to connect to default ollama endpoint) +- Set LLM_MODELNAME to the model name you can see from Ollama using the command `ollama ls` +- It is recommended to set TOKEN_PROCESSING_CHARACTER_LIMIT between 10000-20000 (Approx 300-600 lines of code) if you are using low param LLM (ex. 8b, 14b) + +**Example:** + +``` +LLM_PROVIDER=ollama +LLM_APIKEY= +LLM_MODELNAME=qwen2.5:14b +``` + ### Additional Information > [!CAUTION]