diff options
| -rw-r--r-- | README.md | 7 | ||||
| -rw-r--r-- | autoload/vim_ai_config.vim | 3 | ||||
| -rw-r--r-- | doc/vim-ai.txt | 3 | ||||
| -rw-r--r-- | py/utils.py | 15 |
4 files changed, 24 insertions, 4 deletions
@@ -207,6 +207,7 @@ Below are listed all available configuration options, along with their default v " - options: openai config (see https://platform.openai.com/docs/api-reference/completions) " - options.request_timeout: request timeout in seconds " - options.base_url: openai endpoint url +" - options.enable_auth: enable authorization using openai key " - options.selection_boundary: seleciton prompt wrapper (eliminates empty responses, see #20) " - ui.paste_mode: use paste mode (see more info in the Notes below) let g:vim_ai_complete = { @@ -217,6 +218,7 @@ let g:vim_ai_complete = { \ "temperature": 0.1, \ "request_timeout": 20, \ "base_url": "https://api.openai.com", +\ "enable_auth": 1, \ "selection_boundary": "#####", \ }, \ "ui": { @@ -229,6 +231,7 @@ let g:vim_ai_complete = { " - options: openai config (see https://platform.openai.com/docs/api-reference/completions) " - options.request_timeout: request timeout in seconds " - options.base_url: openai endpoint url +" - options.enable_auth: enable authorization using openai key " - options.selection_boundary: seleciton prompt wrapper (eliminates empty responses, see #20) " - ui.paste_mode: use paste mode (see more info in the Notes below) let g:vim_ai_edit = { @@ -239,6 +242,7 @@ let g:vim_ai_edit = { \ "temperature": 0.1, \ "request_timeout": 20, \ "base_url": "https://api.openai.com", +\ "enable_auth": 1, \ "selection_boundary": "#####", \ }, \ "ui": { @@ -259,6 +263,7 @@ END " - options.initial_prompt: prompt prepended to every chat request (list of lines or string) " - options.request_timeout: request timeout in seconds " - options.base_url: openai endpoint url +" - options.enable_auth: enable authorization using openai key " - options.selection_boundary: seleciton prompt wrapper (eliminates empty responses, see #20) " - ui.populate_options: put [chat-options] to the chat header " - ui.open_chat_command: preset (preset_below, preset_tab, preset_right) or a custom command @@ -271,6 +276,7 @@ let g:vim_ai_chat = { \ "temperature": 1, \ "request_timeout": 20, \ "base_url": "https://api.openai.com", +\ "enable_auth": 1, \ "selection_boundary": "", \ "initial_prompt": s:initial_chat_prompt, \ }, @@ -320,7 +326,6 @@ let chat_engine_config = { \ "max_tokens": 1000, \ "temperature": 0.1, \ "request_timeout": 20, -\ "base_url": "https://api.openai.com", \ "selection_boundary": "", \ "initial_prompt": initial_prompt, \ }, diff --git a/autoload/vim_ai_config.vim b/autoload/vim_ai_config.vim index 5095528..4aadc5a 100644 --- a/autoload/vim_ai_config.vim +++ b/autoload/vim_ai_config.vim @@ -6,6 +6,7 @@ let g:vim_ai_complete_default = { \ "temperature": 0.1, \ "request_timeout": 20, \ "base_url": "https://api.openai.com", +\ "enable_auth": 1, \ "selection_boundary": "#####", \ }, \ "ui": { @@ -20,6 +21,7 @@ let g:vim_ai_edit_default = { \ "temperature": 0.1, \ "request_timeout": 20, \ "base_url": "https://api.openai.com", +\ "enable_auth": 1, \ "selection_boundary": "#####", \ }, \ "ui": { @@ -40,6 +42,7 @@ let g:vim_ai_chat_default = { \ "temperature": 1, \ "request_timeout": 20, \ "base_url": "https://api.openai.com", +\ "enable_auth": 1, \ "selection_boundary": "", \ "initial_prompt": s:initial_chat_prompt, \ }, diff --git a/doc/vim-ai.txt b/doc/vim-ai.txt index be9df1e..fa044f9 100644 --- a/doc/vim-ai.txt +++ b/doc/vim-ai.txt @@ -29,6 +29,7 @@ Options: > \ "temperature": 0.1, \ "request_timeout": 20, \ "base_url": "https://api.openai.com", + \ "enable_auth": 1, \ "selection_boundary": "#####", \ }, \ "ui": { @@ -54,6 +55,7 @@ Options: > \ "temperature": 0.1, \ "request_timeout": 20, \ "base_url": "https://api.openai.com", + \ "enable_auth": 1, \ "selection_boundary": "#####", \ }, \ "ui": { @@ -86,6 +88,7 @@ Options: > \ "temperature": 1, \ "request_timeout": 20, \ "base_url": "https://api.openai.com", + \ "enable_auth": 1, \ "selection_boundary": "#####", \ "initial_prompt": s:initial_chat_prompt, \ }, diff --git a/py/utils.py b/py/utils.py index 3b34517..045bbc9 100644 --- a/py/utils.py +++ b/py/utils.py @@ -14,6 +14,9 @@ import traceback is_debugging = vim.eval("g:vim_ai_debug") == "1" debug_log_file = vim.eval("g:vim_ai_debug_log_file") +class KnownError(Exception): + pass + def load_api_key(): config_file_path = os.path.join(os.path.expanduser("~"), ".config/openai.token") api_key = os.getenv("OPENAI_API_KEY") @@ -23,7 +26,7 @@ def load_api_key(): except Exception: pass if not api_key: - raise Exception("Missing OpenAI API key") + raise KnownError("Missing OpenAI API key") return api_key.strip() def normalize_config(config): @@ -45,6 +48,7 @@ def make_openai_options(options): def make_http_options(options): return { 'request_timeout': float(options['request_timeout']), + 'enable_auth': bool(int(options['enable_auth'])), } def render_text_chunks(chunks): @@ -119,13 +123,16 @@ def printDebug(text, *args): OPENAI_RESP_DATA_PREFIX = 'data: ' OPENAI_RESP_DONE = '[DONE]' -OPENAI_API_KEY = load_api_key() def openai_request(url, data, options): + enable_auth=options['enable_auth'] headers = { "Content-Type": "application/json", - "Authorization": f"Bearer {OPENAI_API_KEY}" } + if enable_auth: + OPENAI_API_KEY = load_api_key() + headers['Authorization'] = f"Bearer {OPENAI_API_KEY}" + request_timeout=options['request_timeout'] req = urllib.request.Request( url, @@ -168,6 +175,8 @@ def handle_completion_error(error): elif status_code == 429: msg += ' (Hint: verify that your billing plan is "Pay as you go")' print_info_message(msg) + elif isinstance(error, KnownError): + print_info_message(str(error)) else: raise error |