Skip to content

Commit

Permalink
Merge pull request #20 from TarsLab/ollama
Browse files Browse the repository at this point in the history
Add Ollama Vendor
  • Loading branch information
ae86jack committed Sep 9, 2024
2 parents 0ed139d + 24ec318 commit d841849
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Tars 是一个 Obsidian 插件,基于标签建议进行文本生成,支持 C

- [Claude](https://claude.ai)
- [OpenAI](https://platform.openai.com/api-keys)
- [Ollama](https://www.ollama.com)
- [Kimi](https://www.moonshot.cn)
- [Doubao 豆包](https://www.volcengine.com/product/doubao)
- [Qianfan 百度千帆](https://qianfan.cloud.baidu.com)
Expand Down
1 change: 1 addition & 0 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Tars is an Obsidian plugin that supports text generation based on tag suggestion

- [Claude](https://claude.ai)
- [OpenAI](https://platform.openai.com/api-keys)
- [Ollama](https://www.ollama.com)
- [Kimi](https://www.moonshot.cn)
- [Doubao](https://www.volcengine.com/product/doubao)
- [Qianfan](https://qianfan.cloud.baidu.com)
Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"id": "tars",
"name": "Tars",
"version": "0.5.0",
"version": "0.6.0",
"minAppVersion": "1.5.8",
"description": "Text generation based on tag suggestions, using Claude, OpenAI, Kimi, Doubao, Qwen, Zhipu, DeepSeek, QianFan & more.",
"description": "Text generation based on tag suggestions, using Claude, OpenAI, Ollama, Kimi, Doubao, Qwen, Zhipu, DeepSeek, QianFan & more.",
"author": "Tarslab",
"authorUrl": "https://github.com/tarslab",
"isDesktopOnly": true
Expand Down
20 changes: 18 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "obsidian-tars",
"version": "0.5.0",
"description": "Use Kimi and other Chinese LLMs for text generation based on tag suggestions.",
"version": "0.6.0",
"description": "Text generation based on tag suggestions, using Claude, OpenAI, Ollama, Kimi, Doubao, Qwen, Zhipu, DeepSeek, QianFan & more.",
"main": "main.js",
"scripts": {
"dev": "node esbuild.config.mjs",
Expand All @@ -21,6 +21,7 @@
"jose": "^5.2.4",
"node-fetch": "^3.3.2",
"obsidian": "latest",
"ollama": "^0.5.8",
"openai": "^4.41.0",
"tslib": "^2.4.0",
"typescript": "^5.5.2"
Expand Down
28 changes: 28 additions & 0 deletions src/providers/ollama.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Ollama } from 'ollama'
import { BaseOptions, Message, SendRequest, Vendor } from '.'

const sendRequestFunc = (settings: BaseOptions): SendRequest =>
async function* (messages: Message[]) {
const { parameters, ...optionsExcludingParams } = settings
const options = { ...optionsExcludingParams, ...parameters } // 这样的设计,让parameters 可以覆盖掉前面的设置 optionsExcludingParams
const { baseURL, model, ...remains } = options

const ollama = new Ollama({ host: baseURL })
const response = await ollama.chat({ model, messages, stream: true, ...remains })
for await (const part of response) {
yield part.message.content
}
}

export const ollamaVendor: Vendor = {
name: 'Ollama',
defaultOptions: {
apiKey: '',
baseURL: 'http://127.0.0.1:11434',
model: 'llama3.1',
parameters: {}
},
sendRequestFunc,
models: [],
websiteToObtainKey: 'https://ollama.com'
}
26 changes: 21 additions & 5 deletions src/settingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,16 @@ export class TarsSettingTab extends PluginSettingTab {
details.createEl('summary', { text: vendor.name, cls: 'tars-setting-h4' })

this.addTagSection(details, settings, index, vendor.name)
this.addAPIkeySection(
details,
settings.options,
vendor.websiteToObtainKey ? t('Obtain key from ') + vendor.websiteToObtainKey : ''
)
if (settings.vendor === 'Ollama') {
// Ollama 没有apiKey可配置,主要是配置 baseURL
this.addBaseURLSection(details, settings.options as BaseOptions, 'e.g. http://127.0.0.1:11434')
} else {
this.addAPIkeySection(
details,
settings.options,
vendor.websiteToObtainKey ? t('Obtain key from ') + vendor.websiteToObtainKey : ''
)
}

if ('apiSecret' in settings.options)
this.addAPISecretOptional(details, settings.options as BaseOptions & Pick<Optional, 'apiSecret'>)
Expand Down Expand Up @@ -173,6 +178,17 @@ export class TarsSettingTab extends PluginSettingTab {
})
)

addBaseURLSection = (details: HTMLDetailsElement, options: BaseOptions, desc: string = '') =>
new Setting(details)
.setName('baseURL')
.setDesc(desc)
.addText((text) =>
text.setValue(options.baseURL).onChange(async (value) => {
options.baseURL = value
await this.plugin.saveSettings()
})
)

addAPIkeySection = (details: HTMLDetailsElement, options: BaseOptions, desc: string = '') =>
new Setting(details)
.setName('API key')
Expand Down
2 changes: 2 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { qwenVendor } from './providers/qwen'
import { zhipuVendor } from './providers/zhipu'
import { claudeVendor } from './providers/claude'
import { azureVendor } from './providers/azure'
import { ollamaVendor } from './providers/ollama'

export interface PluginSettings {
providers: ProviderSettings[]
Expand All @@ -34,5 +35,6 @@ export const availableVendors: Vendor[] = [
zhipuVendor,
deepSeekVendor,
claudeVendor,
ollamaVendor,
azureVendor
]
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"0.3.1": "1.5.8",
"0.4.0": "1.5.8",
"0.4.1": "1.5.8",
"0.5.0": "1.5.8"
"0.5.0": "1.5.8",
"0.6.0": "1.5.8"
}

0 comments on commit d841849

Please sign in to comment.