Skip to content

Commit e66c4aa

Browse files
committed
added auth_type option, closes #153
1 parent 903e88b commit e66c4aa

File tree

5 files changed

+30
-22
lines changed

5 files changed

+30
-22
lines changed

README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ END
326326
" - options: openai config (see https://platform.openai.com/docs/api-reference/completions)
327327
" - options.initial_prompt: prompt prepended to every chat request (list of lines or string)
328328
" - options.request_timeout: request timeout in seconds
329-
" - options.enable_auth: enable authorization using openai key
329+
" - options.auth_type: API authentication method (bearer, api-key, none)
330330
" - options.token_file_path: override global token configuration
331331
" - options.selection_boundary: selection prompt wrapper (eliminates empty responses, see #20)
332332
" - ui.paste_mode: use paste mode (see more info in the Notes below)
@@ -341,7 +341,7 @@ let g:vim_ai_complete = {
341341
\ "temperature": 0.1,
342342
\ "request_timeout": 20,
343343
\ "stream": 1,
344-
\ "enable_auth": 1,
344+
\ "auth_type": "bearer",
345345
\ "token_file_path": "",
346346
\ "selection_boundary": "#####",
347347
\ "initial_prompt": s:initial_complete_prompt,
@@ -357,7 +357,7 @@ let g:vim_ai_complete = {
357357
" - options: openai config (see https://platform.openai.com/docs/api-reference/completions)
358358
" - options.initial_prompt: prompt prepended to every chat request (list of lines or string)
359359
" - options.request_timeout: request timeout in seconds
360-
" - options.enable_auth: enable authorization using openai key
360+
" - options.auth_type: API authentication method (bearer, api-key, none)
361361
" - options.token_file_path: override global token configuration
362362
" - options.selection_boundary: selection prompt wrapper (eliminates empty responses, see #20)
363363
" - ui.paste_mode: use paste mode (see more info in the Notes below)
@@ -372,7 +372,7 @@ let g:vim_ai_edit = {
372372
\ "temperature": 0.1,
373373
\ "request_timeout": 20,
374374
\ "stream": 1,
375-
\ "enable_auth": 1,
375+
\ "auth_type": "bearer",
376376
\ "token_file_path": "",
377377
\ "selection_boundary": "#####",
378378
\ "initial_prompt": s:initial_complete_prompt,
@@ -396,7 +396,7 @@ END
396396
" - options: openai config (see https://platform.openai.com/docs/api-reference/chat)
397397
" - options.initial_prompt: prompt prepended to every chat request (list of lines or string)
398398
" - options.request_timeout: request timeout in seconds
399-
" - options.enable_auth: enable authorization using openai key
399+
" - options.auth_type: API authentication method (bearer, api-key, none)
400400
" - options.token_file_path: override global token configuration
401401
" - options.selection_boundary: selection prompt wrapper (eliminates empty responses, see #20)
402402
" - ui.open_chat_command: preset (preset_below, preset_tab, preset_right) or a custom command
@@ -415,7 +415,7 @@ let g:vim_ai_chat = {
415415
\ "temperature": 1,
416416
\ "request_timeout": 20,
417417
\ "stream": 1,
418-
\ "enable_auth": 1,
418+
\ "auth_type": "bearer",
419419
\ "token_file_path": "",
420420
\ "selection_boundary": "",
421421
\ "initial_prompt": s:initial_chat_prompt,
@@ -434,7 +434,7 @@ let g:vim_ai_chat = {
434434
" - prompt: optional prepended prompt
435435
" - options: openai config (https://platform.openai.com/docs/api-reference/images/create)
436436
" - options.request_timeout: request timeout in seconds
437-
" - options.enable_auth: enable authorization using openai key
437+
" - options.auth_type: API authentication method (bearer, api-key, none)
438438
" - options.token_file_path: override global token configuration
439439
" - options.download_dir: path to image download directory, `cwd` if not defined
440440
let g:vim_ai_image = {
@@ -447,7 +447,7 @@ let g:vim_ai_image = {
447447
\ "size": "1024x1024",
448448
\ "style": "vivid",
449449
\ "request_timeout": 40,
450-
\ "enable_auth": 1,
450+
\ "auth_type": "bearer",
451451
\ "token_file_path": "",
452452
\ },
453453
\ "ui": {
@@ -486,7 +486,7 @@ See some cool projects listed in [Custom APIs](https://github.com/madox2/vim-ai/
486486
let g:vim_ai_chat = {
487487
\ "options": {
488488
\ "endpoint_url": "http://localhost:8000/v1/chat/completions",
489-
\ "enable_auth": 0,
489+
\ "auth_type": "none",
490490
\ },
491491
\}
492492
```

autoload/vim_ai_config.vim

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ let g:vim_ai_openai_complete = {
7373
\ "temperature": 0.1,
7474
\ "request_timeout": 20,
7575
\ "stream": 1,
76-
\ "enable_auth": 1,
76+
\ "auth_type": "bearer",
7777
\ "token_file_path": "",
7878
\ "selection_boundary": "#####",
7979
\ "initial_prompt": s:initial_complete_prompt,
@@ -87,7 +87,7 @@ let g:vim_ai_openai_chat = {
8787
\ "temperature": 1,
8888
\ "request_timeout": 20,
8989
\ "stream": 1,
90-
\ "enable_auth": 1,
90+
\ "auth_type": "bearer",
9191
\ "token_file_path": "",
9292
\ "selection_boundary": "",
9393
\ "initial_prompt": s:initial_chat_prompt,
@@ -99,7 +99,7 @@ let g:vim_ai_openai_image = {
9999
\ "size": "1024x1024",
100100
\ "style": "vivid",
101101
\ "request_timeout": 40,
102-
\ "enable_auth": 1,
102+
\ "auth_type": "bearer",
103103
\ "token_file_path": "",
104104
\}
105105

doc/vim-ai.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Options: >
4040
\ "temperature": 0.1,
4141
\ "request_timeout": 20,
4242
\ "stream": 1,
43-
\ "enable_auth": 1,
43+
\ "auth_type": "bearer",
4444
\ "token_file_path": "",
4545
\ "selection_boundary": "#####",
4646
\ "initial_prompt": s:initial_complete_prompt,
@@ -80,7 +80,7 @@ Options: >
8080
\ "temperature": 0.1,
8181
\ "request_timeout": 20,
8282
\ "stream": 1,
83-
\ "enable_auth": 1,
83+
\ "auth_type": "bearer",
8484
\ "token_file_path": "",
8585
\ "selection_boundary": "#####",
8686
\ "initial_prompt": s:initial_complete_prompt,
@@ -118,7 +118,7 @@ Options: >
118118
\ "temperature": 1,
119119
\ "request_timeout": 20,
120120
\ "stream": 1,
121-
\ "enable_auth": 1,
121+
\ "auth_type": "bearer",
122122
\ "token_file_path": "",
123123
\ "selection_boundary": "",
124124
\ "initial_prompt": s:initial_chat_prompt,
@@ -170,7 +170,7 @@ Options: >
170170
\ "size": "1024x1024",
171171
\ "style": "vivid",
172172
\ "request_timeout": 40,
173-
\ "enable_auth": 1,
173+
\ "auth_type": "bearer",
174174
\ "token_file_path": "",
175175
\ },
176176
\ "ui": {

py/providers/openai.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def request(self, messages: Sequence[AIMessage]) -> Iterator[AIResponseChunk]:
2828
openai_options = self._make_openai_options(options)
2929
http_options = {
3030
'request_timeout': options['request_timeout'],
31-
'enable_auth': options['enable_auth'],
31+
'auth_type': options['auth_type'],
3232
'token_file_path': options['token_file_path'],
3333
}
3434

@@ -83,9 +83,12 @@ def _load_api_key(self):
8383
return (api_key, org_id)
8484

8585
def _parse_raw_options(self, raw_options: Mapping[str, Any]):
86+
if raw_options.get('enable_auth', 1) == "0":
87+
# raise error for users who don't use default value of this obsolete option
88+
raise self.utils.make_known_error("`enable_auth = 0` option is no longer supported. use `auth_type = none` instead")
89+
8690
options = {**raw_options}
8791
options['request_timeout'] = float(options['request_timeout'])
88-
options['enable_auth'] = bool(int(options['enable_auth']))
8992
if self.command_type != 'image':
9093
options['max_tokens'] = int(options['max_tokens'])
9194
options['max_completion_tokens'] = int(options['max_completion_tokens'])
@@ -111,7 +114,7 @@ def request_image(self, prompt: str) -> list[AIImageResponseChunk]:
111114
options = self.options
112115
http_options = {
113116
'request_timeout': options['request_timeout'],
114-
'enable_auth': options['enable_auth'],
117+
'auth_type': options['auth_type'],
115118
'token_file_path': options['token_file_path'],
116119
}
117120
openai_options = {
@@ -133,18 +136,23 @@ def _openai_request(self, url, data, options):
133136
RESP_DATA_PREFIX = 'data: '
134137
RESP_DONE = '[DONE]'
135138

136-
enable_auth=options['enable_auth']
139+
auth_type = options['auth_type']
137140
headers = {
138141
"Content-Type": "application/json",
139142
"User-Agent": "VimAI",
140143
}
141-
if enable_auth:
144+
145+
if auth_type == 'bearer':
142146
(OPENAI_API_KEY, OPENAI_ORG_ID) = self._load_api_key()
143147
headers['Authorization'] = f"Bearer {OPENAI_API_KEY}"
144148

145149
if OPENAI_ORG_ID is not None:
146150
headers["OpenAI-Organization"] = f"{OPENAI_ORG_ID}"
147151

152+
if auth_type == 'api-key':
153+
(OPENAI_API_KEY, _) = self._load_api_key()
154+
headers['api-key'] = f"{OPENAI_API_KEY}"
155+
148156
request_timeout=options['request_timeout']
149157
req = urllib.request.Request(
150158
url,

tests/deprecated_role_syntax_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"temperature": "1",
1010
"request_timeout": "20",
1111
"stream": "1",
12-
"enable_auth": "1",
12+
"auth_type": "bearer",
1313
"token_file_path": "",
1414
"selection_boundary": "",
1515
"initial_prompt": "You are a general assistant.",

0 commit comments

Comments
 (0)