From 356ead8aa66e939d78bf38b4e5515545bbdf5a91 Mon Sep 17 00:00:00 2001 From: juodumas Date: Mon, 18 Sep 2023 13:59:51 +0300 Subject: Add support for base_url option to use local models For example, you can start llama-cpp-python like this (it emulates the openai api): ```sh CMAKE_ARGS="-DLLAMA_CUBLAS=on" pip install 'llama-cpp-python[server]' wget https://huggingface.co/TheBloke/CodeLlama-13B-Instruct-GGUF/resolve/main/codellama-13b-instruct.Q5_K_M.gguf python3 -m llama_cpp.server --n_gpu_layers 100 --model codellama-13b-instruct.Q5_K_M.gguf ``` Then set the API url in your `.vimrc`: ```vim let g:vim_ai_chat = { \ "engine": "chat", \ "options": { \ "base_url": "http://127.0.0.1:8000", \ }, \ } ``` And chat with the locally hosted AI using `:AIChat`. The change in utils.py was needed because llama-cpp-python adds a new line to the final response: `[DONE]^M`. --- py/chat.py | 5 ++++- py/complete.py | 9 +++++++-- py/utils.py | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/py/chat.py b/py/chat.py index 93d31bf..7bca31d 100644 --- a/py/chat.py +++ b/py/chat.py @@ -1,3 +1,4 @@ +from urllib.parse import urljoin # import utils plugin_root = vim.eval("s:plugin_root") vim.command(f"py3file {plugin_root}/py/utils.py") @@ -69,7 +70,9 @@ try: **openai_options } printDebug("[chat] request: {}", request) - response = openai_request('https://api.openai.com/v1/chat/completions', request, http_options) + base_url = options.get('base_url', 'https://api.openai.com') + url = urljoin(base_url, 'v1/chat/completions') + response = openai_request(url, request, http_options) def map_chunk(resp): printDebug("[chat] response: {}", resp) return resp['choices'][0]['delta'].get('content', '') diff --git a/py/complete.py b/py/complete.py index c8d45fe..a5c4711 100644 --- a/py/complete.py +++ b/py/complete.py @@ -1,3 +1,4 @@ +from urllib.parse import urljoin # import utils plugin_root = vim.eval("s:plugin_root") vim.command(f"py3file {plugin_root}/py/utils.py") @@ -17,7 +18,9 @@ def complete_engine(prompt): **openai_options } printDebug("[engine-complete] request: {}", request) - response = openai_request('https://api.openai.com/v1/completions', request, http_options) + base_url = config_options.get('base_url', 'https://api.openai.com') + url = urljoin(base_url, 'v1/completions') + response = openai_request(url, request, http_options) def map_chunk(resp): printDebug("[engine-complete] response: {}", resp) return resp['choices'][0].get('text', '') @@ -35,7 +38,9 @@ def chat_engine(prompt): **openai_options } printDebug("[engine-chat] request: {}", request) - response = openai_request('https://api.openai.com/v1/chat/completions', request, http_options) + base_url = config_options.get('base_url', 'https://api.openai.com') + url = urljoin(base_url, 'v1/chat/completions') + response = openai_request(url, request, http_options) def map_chunk(resp): printDebug("[engine-chat] response: {}", resp) return resp['choices'][0]['delta'].get('content', '') diff --git a/py/utils.py b/py/utils.py index 76ae1e4..3b34517 100644 --- a/py/utils.py +++ b/py/utils.py @@ -138,7 +138,7 @@ def openai_request(url, data, options): line = line_bytes.decode("utf-8", errors="replace") if line.startswith(OPENAI_RESP_DATA_PREFIX): line_data = line[len(OPENAI_RESP_DATA_PREFIX):-1] - if line_data == OPENAI_RESP_DONE: + if line_data.strip() == OPENAI_RESP_DONE: pass else: openai_obj = json.loads(line_data) -- cgit v1.2.3 From bd0e7668f6709b8fc9cac79e42ccecafde949aff Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Sat, 21 Oct 2023 12:30:41 +0200 Subject: base_url extracted to config, docu --- README.md | 7 +++++++ autoload/vim_ai_config.vim | 3 +++ doc/vim-ai.txt | 3 +++ py/chat.py | 3 +-- py/complete.py | 6 ++---- 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1ff7973..76996b1 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,7 @@ Below are listed all available configuration options, along with their default v " - engine: complete | chat - see how to configure chat engine in the section below " - 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.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 = { @@ -215,6 +216,7 @@ let g:vim_ai_complete = { \ "max_tokens": 1000, \ "temperature": 0.1, \ "request_timeout": 20, +\ "base_url": "https://api.openai.com", \ "selection_boundary": "#####", \ }, \ "ui": { @@ -226,6 +228,7 @@ let g:vim_ai_complete = { " - engine: complete | chat - see how to configure chat engine in the section below " - 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.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 = { @@ -235,6 +238,7 @@ let g:vim_ai_edit = { \ "max_tokens": 1000, \ "temperature": 0.1, \ "request_timeout": 20, +\ "base_url": "https://api.openai.com", \ "selection_boundary": "#####", \ }, \ "ui": { @@ -254,6 +258,7 @@ END " - options: openai config (see https://platform.openai.com/docs/api-reference/chat) " - 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.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 @@ -265,6 +270,7 @@ let g:vim_ai_chat = { \ "max_tokens": 1000, \ "temperature": 1, \ "request_timeout": 20, +\ "base_url": "https://api.openai.com", \ "selection_boundary": "", \ "initial_prompt": s:initial_chat_prompt, \ }, @@ -314,6 +320,7 @@ 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 c501bbd..5095528 100644 --- a/autoload/vim_ai_config.vim +++ b/autoload/vim_ai_config.vim @@ -5,6 +5,7 @@ let g:vim_ai_complete_default = { \ "max_tokens": 1000, \ "temperature": 0.1, \ "request_timeout": 20, +\ "base_url": "https://api.openai.com", \ "selection_boundary": "#####", \ }, \ "ui": { @@ -18,6 +19,7 @@ let g:vim_ai_edit_default = { \ "max_tokens": 1000, \ "temperature": 0.1, \ "request_timeout": 20, +\ "base_url": "https://api.openai.com", \ "selection_boundary": "#####", \ }, \ "ui": { @@ -37,6 +39,7 @@ let g:vim_ai_chat_default = { \ "max_tokens": 1000, \ "temperature": 1, \ "request_timeout": 20, +\ "base_url": "https://api.openai.com", \ "selection_boundary": "", \ "initial_prompt": s:initial_chat_prompt, \ }, diff --git a/doc/vim-ai.txt b/doc/vim-ai.txt index 931d018..be9df1e 100644 --- a/doc/vim-ai.txt +++ b/doc/vim-ai.txt @@ -28,6 +28,7 @@ Options: > \ "max_tokens": 1000, \ "temperature": 0.1, \ "request_timeout": 20, + \ "base_url": "https://api.openai.com", \ "selection_boundary": "#####", \ }, \ "ui": { @@ -52,6 +53,7 @@ Options: > \ "max_tokens": 1000, \ "temperature": 0.1, \ "request_timeout": 20, + \ "base_url": "https://api.openai.com", \ "selection_boundary": "#####", \ }, \ "ui": { @@ -83,6 +85,7 @@ Options: > \ "max_tokens": 1000, \ "temperature": 1, \ "request_timeout": 20, + \ "base_url": "https://api.openai.com", \ "selection_boundary": "#####", \ "initial_prompt": s:initial_chat_prompt, \ }, diff --git a/py/chat.py b/py/chat.py index 7bca31d..8374efb 100644 --- a/py/chat.py +++ b/py/chat.py @@ -70,8 +70,7 @@ try: **openai_options } printDebug("[chat] request: {}", request) - base_url = options.get('base_url', 'https://api.openai.com') - url = urljoin(base_url, 'v1/chat/completions') + url = urljoin(config_options['base_url'], 'v1/chat/completions') response = openai_request(url, request, http_options) def map_chunk(resp): printDebug("[chat] response: {}", resp) diff --git a/py/complete.py b/py/complete.py index a5c4711..668591d 100644 --- a/py/complete.py +++ b/py/complete.py @@ -18,8 +18,7 @@ def complete_engine(prompt): **openai_options } printDebug("[engine-complete] request: {}", request) - base_url = config_options.get('base_url', 'https://api.openai.com') - url = urljoin(base_url, 'v1/completions') + url = urljoin(config_options['base_url'], 'v1/completions') response = openai_request(url, request, http_options) def map_chunk(resp): printDebug("[engine-complete] response: {}", resp) @@ -38,8 +37,7 @@ def chat_engine(prompt): **openai_options } printDebug("[engine-chat] request: {}", request) - base_url = config_options.get('base_url', 'https://api.openai.com') - url = urljoin(base_url, 'v1/chat/completions') + url = urljoin(config_options['base_url'], 'v1/chat/completions') response = openai_request(url, request, http_options) def map_chunk(resp): printDebug("[engine-chat] response: {}", resp) -- cgit v1.2.3 From dca2bcf256df92196febf9bae77206bb6e51dac1 Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Sat, 21 Oct 2023 18:18:10 +0200 Subject: option to disable authorization --- README.md | 7 ++++++- autoload/vim_ai_config.vim | 3 +++ doc/vim-ai.txt | 3 +++ py/utils.py | 15 ++++++++++++--- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 76996b1..6439b49 100644 --- a/README.md +++ b/README.md @@ -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 -- cgit v1.2.3 From d9e1e193b6d8a8d2eb4eb2deb64d774ab5d5079b Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Sat, 21 Oct 2023 18:29:55 +0200 Subject: endpoint_url config --- README.md | 10 ++++------ autoload/vim_ai_config.vim | 6 +++--- doc/vim-ai.txt | 6 +++--- py/chat.py | 2 +- py/complete.py | 4 ++-- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 6439b49..d9d7a29 100644 --- a/README.md +++ b/README.md @@ -206,7 +206,6 @@ Below are listed all available configuration options, along with their default v " - engine: complete | chat - see how to configure chat engine in the section below " - 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) @@ -214,10 +213,10 @@ let g:vim_ai_complete = { \ "engine": "complete", \ "options": { \ "model": "text-davinci-003", +\ "endpoint_url": "https://api.openai.com/v1/completions", \ "max_tokens": 1000, \ "temperature": 0.1, \ "request_timeout": 20, -\ "base_url": "https://api.openai.com", \ "enable_auth": 1, \ "selection_boundary": "#####", \ }, @@ -230,7 +229,6 @@ let g:vim_ai_complete = { " - engine: complete | chat - see how to configure chat engine in the section below " - 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) @@ -238,10 +236,10 @@ let g:vim_ai_edit = { \ "engine": "complete", \ "options": { \ "model": "text-davinci-003", +\ "endpoint_url": "https://api.openai.com/v1/completions", \ "max_tokens": 1000, \ "temperature": 0.1, \ "request_timeout": 20, -\ "base_url": "https://api.openai.com", \ "enable_auth": 1, \ "selection_boundary": "#####", \ }, @@ -262,7 +260,6 @@ END " - options: openai config (see https://platform.openai.com/docs/api-reference/chat) " - 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 @@ -272,10 +269,10 @@ END let g:vim_ai_chat = { \ "options": { \ "model": "gpt-3.5-turbo", +\ "endpoint_url": "https://api.openai.com/v1/chat/completions", \ "max_tokens": 1000, \ "temperature": 1, \ "request_timeout": 20, -\ "base_url": "https://api.openai.com", \ "enable_auth": 1, \ "selection_boundary": "", \ "initial_prompt": s:initial_chat_prompt, @@ -323,6 +320,7 @@ let chat_engine_config = { \ "engine": "chat", \ "options": { \ "model": "gpt-3.5-turbo", +\ "endpoint_url": "https://api.openai.com/v1/chat/completions", \ "max_tokens": 1000, \ "temperature": 0.1, \ "request_timeout": 20, diff --git a/autoload/vim_ai_config.vim b/autoload/vim_ai_config.vim index 4aadc5a..54645ed 100644 --- a/autoload/vim_ai_config.vim +++ b/autoload/vim_ai_config.vim @@ -2,10 +2,10 @@ let g:vim_ai_complete_default = { \ "engine": "complete", \ "options": { \ "model": "text-davinci-003", +\ "endpoint_url": "https://api.openai.com/v1/completions", \ "max_tokens": 1000, \ "temperature": 0.1, \ "request_timeout": 20, -\ "base_url": "https://api.openai.com", \ "enable_auth": 1, \ "selection_boundary": "#####", \ }, @@ -17,10 +17,10 @@ let g:vim_ai_edit_default = { \ "engine": "complete", \ "options": { \ "model": "text-davinci-003", +\ "endpoint_url": "https://api.openai.com/v1/completions", \ "max_tokens": 1000, \ "temperature": 0.1, \ "request_timeout": 20, -\ "base_url": "https://api.openai.com", \ "enable_auth": 1, \ "selection_boundary": "#####", \ }, @@ -38,10 +38,10 @@ END let g:vim_ai_chat_default = { \ "options": { \ "model": "gpt-3.5-turbo", +\ "endpoint_url": "https://api.openai.com/v1/chat/completions", \ "max_tokens": 1000, \ "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 fa044f9..85ca5ff 100644 --- a/doc/vim-ai.txt +++ b/doc/vim-ai.txt @@ -25,10 +25,10 @@ Options: > \ "engine": "complete", \ "options": { \ "model": "text-davinci-003", + \ "endpoint_url": "https://api.openai.com/v1/completions", \ "max_tokens": 1000, \ "temperature": 0.1, \ "request_timeout": 20, - \ "base_url": "https://api.openai.com", \ "enable_auth": 1, \ "selection_boundary": "#####", \ }, @@ -51,10 +51,10 @@ Options: > \ "engine": "complete", \ "options": { \ "model": "text-davinci-003", + \ "endpoint_url": "https://api.openai.com/v1/completions", \ "max_tokens": 1000, \ "temperature": 0.1, \ "request_timeout": 20, - \ "base_url": "https://api.openai.com", \ "enable_auth": 1, \ "selection_boundary": "#####", \ }, @@ -85,9 +85,9 @@ Options: > \ "options": { \ "model": "gpt-3.5-turbo", \ "max_tokens": 1000, + \ "endpoint_url": "https://api.openai.com/v1/chat/completions", \ "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/chat.py b/py/chat.py index 8374efb..71783ca 100644 --- a/py/chat.py +++ b/py/chat.py @@ -70,7 +70,7 @@ try: **openai_options } printDebug("[chat] request: {}", request) - url = urljoin(config_options['base_url'], 'v1/chat/completions') + url = config_options['endpoint_url'] response = openai_request(url, request, http_options) def map_chunk(resp): printDebug("[chat] response: {}", resp) diff --git a/py/complete.py b/py/complete.py index 668591d..4b07271 100644 --- a/py/complete.py +++ b/py/complete.py @@ -18,7 +18,7 @@ def complete_engine(prompt): **openai_options } printDebug("[engine-complete] request: {}", request) - url = urljoin(config_options['base_url'], 'v1/completions') + url = config_options['endpoint_url'] response = openai_request(url, request, http_options) def map_chunk(resp): printDebug("[engine-complete] response: {}", resp) @@ -37,7 +37,7 @@ def chat_engine(prompt): **openai_options } printDebug("[engine-chat] request: {}", request) - url = urljoin(config_options['base_url'], 'v1/chat/completions') + url = config_options['endpoint_url'] response = openai_request(url, request, http_options) def map_chunk(resp): printDebug("[engine-chat] response: {}", resp) -- cgit v1.2.3 From 61870b4bb62412772c6fb9f3c9d05b1a0586a036 Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Sat, 21 Oct 2023 18:55:10 +0200 Subject: docu on custom apis --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index d9d7a29..408e2b8 100644 --- a/README.md +++ b/README.md @@ -298,6 +298,20 @@ let g:vim_ai_chat = { " hits token limit, which respond with `OpenAI: HTTPError 400` ``` +### Using custom API + +It is possible to configure the plugin to use different OpenAI-compatible endpoints. +See some cool projects listed in [Custom APIs](https://github.com/madox2/vim-ai/wiki/Custom-APIs) section on the [Community Wiki](https://github.com/madox2/vim-ai/wiki). + +```vim +let g:vim_ai_chat = { +\ "options": { +\ "endpoint_url": "http://localhost:8000/v1/chat/completions", +\ "enable_auth": 0, +\ }, +\} +``` + ### Using chat engine for completion and edits It is possible to configure chat models, such as `gpt-3.5-turbo`, to be used in `:AI` and `:AIEdit` commands. -- cgit v1.2.3 From 55c4e2ec836e48552b52fb4b7878f7b50f67b53b Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Sat, 21 Oct 2023 19:02:54 +0200 Subject: removed unused import --- py/chat.py | 1 - py/complete.py | 1 - 2 files changed, 2 deletions(-) diff --git a/py/chat.py b/py/chat.py index 71783ca..6d88015 100644 --- a/py/chat.py +++ b/py/chat.py @@ -1,4 +1,3 @@ -from urllib.parse import urljoin # import utils plugin_root = vim.eval("s:plugin_root") vim.command(f"py3file {plugin_root}/py/utils.py") diff --git a/py/complete.py b/py/complete.py index 4b07271..8386c09 100644 --- a/py/complete.py +++ b/py/complete.py @@ -1,4 +1,3 @@ -from urllib.parse import urljoin # import utils plugin_root = vim.eval("s:plugin_root") vim.command(f"py3file {plugin_root}/py/utils.py") -- cgit v1.2.3