diff options
| author | Martin Bielik <mx.bielik@gmail.com> | 2023-10-21 19:01:48 +0200 |
|---|---|---|
| committer | Martin Bielik <mx.bielik@gmail.com> | 2023-10-21 19:01:48 +0200 |
| commit | b26a83cd2c8de151b3ed1e00133231c7eb9fc44c (patch) | |
| tree | db60fee61af4f22e598f0c1c6ead5be2bb229202 | |
| parent | 61870b4bb62412772c6fb9f3c9d05b1a0586a036 (diff) | |
| parent | 8f8083ba0eed23150020b698e74d9302f7212c5d (diff) | |
| download | vim-ai-b26a83cd2c8de151b3ed1e00133231c7eb9fc44c.tar.gz | |
Merge remote-tracking branch 'origin/main' into base-url-config
Diffstat (limited to '')
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | autoload/vim_ai_config.vim | 4 | ||||
| -rw-r--r-- | doc/vim-ai.txt | 4 | ||||
| -rw-r--r-- | py/utils.py | 24 |
4 files changed, 29 insertions, 11 deletions
@@ -34,6 +34,10 @@ echo "YOUR_OPENAI_API_KEY" > ~/.config/openai.token # alternatively set it as an environment variable export OPENAI_API_KEY="YOUR_OPENAI_API_KEY" + +# or configure it with your organization id +echo "YOUR_OPENAI_API_KEY,YOUR_OPENAI_ORG_ID" > ~/.config/openai.token +export OPENAI_API_KEY="YOUR_OPENAI_API_KEY,YOUR_OPENAI_ORG_ID" ``` ### Using `vim-plug` @@ -212,7 +216,7 @@ Below are listed all available configuration options, along with their default v let g:vim_ai_complete = { \ "engine": "complete", \ "options": { -\ "model": "text-davinci-003", +\ "model": "gpt-3.5-turbo-instruct", \ "endpoint_url": "https://api.openai.com/v1/completions", \ "max_tokens": 1000, \ "temperature": 0.1, @@ -235,7 +239,7 @@ let g:vim_ai_complete = { let g:vim_ai_edit = { \ "engine": "complete", \ "options": { -\ "model": "text-davinci-003", +\ "model": "gpt-3.5-turbo-instruct", \ "endpoint_url": "https://api.openai.com/v1/completions", \ "max_tokens": 1000, \ "temperature": 0.1, diff --git a/autoload/vim_ai_config.vim b/autoload/vim_ai_config.vim index 54645ed..6119e4f 100644 --- a/autoload/vim_ai_config.vim +++ b/autoload/vim_ai_config.vim @@ -1,7 +1,7 @@ let g:vim_ai_complete_default = { \ "engine": "complete", \ "options": { -\ "model": "text-davinci-003", +\ "model": "gpt-3.5-turbo-instruct", \ "endpoint_url": "https://api.openai.com/v1/completions", \ "max_tokens": 1000, \ "temperature": 0.1, @@ -16,7 +16,7 @@ let g:vim_ai_complete_default = { let g:vim_ai_edit_default = { \ "engine": "complete", \ "options": { -\ "model": "text-davinci-003", +\ "model": "gpt-3.5-turbo-instruct", \ "endpoint_url": "https://api.openai.com/v1/completions", \ "max_tokens": 1000, \ "temperature": 0.1, diff --git a/doc/vim-ai.txt b/doc/vim-ai.txt index 85ca5ff..f5f2cde 100644 --- a/doc/vim-ai.txt +++ b/doc/vim-ai.txt @@ -24,7 +24,7 @@ Options: > let g:vim_ai_complete = { \ "engine": "complete", \ "options": { - \ "model": "text-davinci-003", + \ "model": "gpt-3.5-turbo-instruct", \ "endpoint_url": "https://api.openai.com/v1/completions", \ "max_tokens": 1000, \ "temperature": 0.1, @@ -50,7 +50,7 @@ Options: > let g:vim_ai_edit = { \ "engine": "complete", \ "options": { - \ "model": "text-davinci-003", + \ "model": "gpt-3.5-turbo-instruct", \ "endpoint_url": "https://api.openai.com/v1/completions", \ "max_tokens": 1000, \ "temperature": 0.1, diff --git a/py/utils.py b/py/utils.py index 045bbc9..e5203bd 100644 --- a/py/utils.py +++ b/py/utils.py @@ -19,15 +19,26 @@ class KnownError(Exception): def load_api_key(): config_file_path = os.path.join(os.path.expanduser("~"), ".config/openai.token") - api_key = os.getenv("OPENAI_API_KEY") + api_key_param_value = os.getenv("OPENAI_API_KEY") try: with open(config_file_path, 'r') as file: - api_key = file.read() + api_key_param_value = file.read() except Exception: pass - if not api_key: + + if not api_key_param_value: raise KnownError("Missing OpenAI API key") - return api_key.strip() + + # The text is in format of "<api key>,<org id>" and the + # <org id> part is optional + elements = api_key_param_value.strip().split(",") + api_key = elements[0].strip() + org_id = None + + if len(elements) > 1: + org_id = elements[1].strip() + + return (api_key, org_id) def normalize_config(config): normalized = { **config } @@ -130,9 +141,12 @@ def openai_request(url, data, options): "Content-Type": "application/json", } if enable_auth: - OPENAI_API_KEY = load_api_key() + (OPENAI_API_KEY, OPENAI_ORG_ID) = load_api_key() headers['Authorization'] = f"Bearer {OPENAI_API_KEY}" + if OPENAI_ORG_ID is not None: + headers["OpenAI-Organization"] = f"{OPENAI_ORG_ID}" + request_timeout=options['request_timeout'] req = urllib.request.Request( url, |