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(-) (limited to 'py') 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 --- py/chat.py | 3 +-- py/complete.py | 6 ++---- 2 files changed, 3 insertions(+), 6 deletions(-) (limited to 'py') 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 --- py/utils.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'py') 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 --- py/chat.py | 2 +- py/complete.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'py') 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 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(-) (limited to 'py') 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