1
- import { App , Notice , PluginSettingTab , Setting } from "obsidian" ;
1
+ import { App , Notice , PluginSettingTab , requestUrl , Setting } from "obsidian" ;
2
2
import { DEFAULT_SETTINGS } from "defaultSettings" ;
3
3
import LocalGPT from "./main" ;
4
4
import { LocalGPTAction } from "./interfaces" ;
@@ -7,6 +7,7 @@ export class LocalGPTSettingTab extends PluginSettingTab {
7
7
plugin : LocalGPT ;
8
8
editEnabled : boolean = false ;
9
9
isNew : boolean = false ;
10
+ modelsOptions : any = { } ;
10
11
11
12
constructor ( app : App , plugin : LocalGPT ) {
12
13
super ( app , plugin ) ;
@@ -18,31 +19,79 @@ export class LocalGPTSettingTab extends PluginSettingTab {
18
19
19
20
containerEl . empty ( ) ;
20
21
21
- new Setting ( containerEl )
22
- . setName ( "Ollama URL" )
23
- . setDesc ( "Default is http://localhost:11434" )
24
- . addText ( ( text ) =>
25
- text
26
- . setPlaceholder ( "http://localhost:11434" )
27
- . setValue ( this . plugin . settings . ollamaUrl )
22
+ const aiProvider = new Setting ( containerEl )
23
+ . setName ( 'AI provider' )
24
+ . setDesc ( '' )
25
+ . addDropdown ( dropdown =>
26
+ dropdown . addOptions ( {
27
+ 'ollama' : 'Ollama' ,
28
+ } )
29
+ . setValue ( String ( this . plugin . settings . selectedProvider ) )
28
30
. onChange ( async ( value ) => {
29
- this . plugin . settings . ollamaUrl = value ;
31
+ this . plugin . settings . selectedProvider = value ;
30
32
await this . plugin . saveSettings ( ) ;
33
+ this . display ( )
31
34
} )
32
- ) ;
35
+ )
33
36
34
- new Setting ( containerEl )
35
- . setName ( "Default model" )
36
- . setDesc ( "Name of the default Ollama model to use for prompts" )
37
- . addText ( ( text ) =>
38
- text
39
- . setPlaceholder ( "llama2" )
40
- . setValue ( this . plugin . settings . defaultModel )
41
- . onChange ( async ( value ) => {
42
- this . plugin . settings . defaultModel = value ;
43
- await this . plugin . saveSettings ( ) ;
37
+ aiProvider . descEl . innerHTML = `If you would like to use other providers, please let me know <a href="https://github.com/pfrankov/obsidian-local-gpt/discussions/1">in the discussions</a>`
38
+
39
+ if ( this . plugin . settings . selectedProvider === 'ollama' ) {
40
+ new Setting ( containerEl )
41
+ . setName ( "Ollama URL" )
42
+ . setDesc ( "Default is http://localhost:11434" )
43
+ . addText ( ( text ) =>
44
+ text
45
+ . setPlaceholder ( "http://localhost:11434" )
46
+ . setValue ( this . plugin . settings . providers . ollama . ollamaUrl )
47
+ . onChange ( async ( value ) => {
48
+ this . plugin . settings . providers . ollama . ollamaUrl = value ;
49
+ await this . plugin . saveSettings ( ) ;
50
+ } )
51
+ ) ;
52
+
53
+ const ollamaDefaultModel = new Setting ( containerEl )
54
+ . setName ( "Default model" )
55
+ . setDesc ( "Name of the default Ollama model to use for prompts" )
56
+ if ( this . plugin . settings . providers . ollama . ollamaUrl ) {
57
+ requestUrl ( `${ this . plugin . settings . providers . ollama . ollamaUrl } /api/tags` )
58
+ . then ( ( { json} ) => {
59
+ if ( ! json . models || json . models . length === 0 ) {
60
+ return Promise . reject ( ) ;
61
+ }
62
+ this . modelsOptions = json . models . reduce ( ( acc : any , el :any ) => {
63
+ const name = el . name . replace ( ":latest" , "" ) ;
64
+ acc [ name ] = name ;
65
+ return acc ;
66
+ } , { } )
67
+
68
+ ollamaDefaultModel
69
+ . addDropdown ( dropdown =>
70
+ dropdown . addOptions ( this . modelsOptions )
71
+ . setValue ( String ( this . plugin . settings . providers . ollama . defaultModel ) )
72
+ . onChange ( async ( value ) => {
73
+ this . plugin . settings . providers . ollama . defaultModel = value ;
74
+ await this . plugin . saveSettings ( ) ;
75
+ } )
76
+
77
+ )
78
+ . addButton ( ( button ) =>
79
+ button . setIcon ( 'refresh-cw' ) . onClick ( async ( ) => {
80
+ this . display ( )
81
+ } )
82
+ )
44
83
} )
45
- ) ;
84
+ . catch ( ( ) => {
85
+ ollamaDefaultModel . descEl . innerHTML = `Get the models from <a href="https://ollama.ai/library">Ollama library</a> or check that Ollama URL is correct.`
86
+ ollamaDefaultModel . addButton ( ( button ) =>
87
+ button . setIcon ( 'refresh-cw' ) . onClick ( async ( ) => {
88
+ this . display ( )
89
+ } )
90
+ )
91
+ } )
92
+ }
93
+ }
94
+
46
95
47
96
const editingAction : LocalGPTAction = {
48
97
name : "" ,
@@ -97,15 +146,21 @@ export class LocalGPTSettingTab extends PluginSettingTab {
97
146
} ) ;
98
147
} ) ;
99
148
100
- new Setting ( containerEl )
101
- . setName ( "Model" )
102
- . setDesc ( 'Optional' )
103
- . addText ( ( text ) => {
104
- text . setPlaceholder ( this . plugin . settings . defaultModel ) ;
105
- text . onChange ( async ( value ) => {
106
- editingAction . model = value ;
107
- } ) ;
108
- } ) ;
149
+ if ( this . plugin . settings . selectedProvider === 'ollama' ) {
150
+ new Setting ( containerEl )
151
+ . setName ( "Model" )
152
+ . setDesc ( 'Optional' )
153
+ . addDropdown ( dropdown =>
154
+ dropdown
155
+ . addOption ( '' , 'Default model' )
156
+ . addOptions ( this . modelsOptions )
157
+ . onChange ( async ( value ) => {
158
+ this . plugin . settings . providers . ollama . defaultModel = value ;
159
+ await this . plugin . saveSettings ( ) ;
160
+ } )
161
+
162
+ )
163
+ }
109
164
110
165
new Setting ( containerEl )
111
166
. setName ( "Replace selected text" )
0 commit comments