When trying to use an OpenAI-Compatible API, I would get this error:
(note: I added logging during debugging, but that was removed afterwards)
2024-11-14 01:10:56.468 [info] aider-chat: INFO:main:Updating model with new settings: ChatSetting(provider='openai_compatible', api_key='key', model='openai/keyless-gpt-4o-mini', base_url='http://localhost:1337/v1')
2024-11-14 01:10:56.468 [info] aider-chat: INFO:main:No changes to settings; model update not required.
2024-11-14 01:10:56.468 [info] aider-chat: INFO:werkzeug:127.0.0.1 - - [14/Nov/2024 01:10:56] "POST /api/chat/setting HTTP/1.1" 200 -
2024-11-14 01:11:00.240 [info] From Webview: sendChatMessage: hello testing!
2024-11-14 01:11:00.255 [info] aider-chat: INFO:werkzeug:127.0.0.1 - - [14/Nov/2024 01:11:00] "OPTIONS /api/chat HTTP/1.1" 200 -
2024-11-14 01:11:00.282 [info] aider-chat: INFO:werkzeug:127.0.0.1 - - [14/Nov/2024 01:11:00] "POST /api/chat HTTP/1.1" 200 -
2024-11-14 01:11:00.284 [error] From Webview: server error: Unexpected error: litellm.AuthenticationError: AuthenticationError: OpenAIException - The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
Traceback (most recent call last):
File "/Users/alex/.pyenv/versions/3.11.8/lib/python3.11/site-packages/litellm/llms/OpenAI/openai.py", line 907, in completion
raise e
File "/Users/alex/.pyenv/versions/3.11.8/lib/python3.11/site-packages/litellm/llms/OpenAI/openai.py", line 784, in completion
return self.streaming(
^^^^^^^^^^^^^^^
File "/Users/alex/.pyenv/versions/3.11.8/lib/python3.11/site-packages/litellm/llms/OpenAI/openai.py", line 1025, in streaming
openai_client: OpenAI = self._get_openai_client( # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/alex/.pyenv/versions/3.11.8/lib/python3.11/site-packages/litellm/llms/OpenAI/openai.py", line 617, in _get_openai_client
_new_client = OpenAI(
^^^^^^^
File "/Users/alex/.pyenv/versions/3.11.8/lib/python3.11/site-packages/openai/_client.py", line 105, in __init__
raise OpenAIError(
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
This is because the current implementation is not correctly differentiating between two configuration types:
1.) Configurations where only an API key is required (string type).
2.) Configurations where both a base URL and an API key are needed (dict type).
Solution:
My PR modifies the update_model function to explicitly check the configuration type in provider_env_map (either string or dict). Depending on the type, it sets the necessary environment variables as follows:
For string configurations, it directly assigns the api_key to the designated environment variable (this is already done in the previous implementation).
For dict configurations, it iterates through each key-value pair in the dict, setting all required variables such as base_url and api_key to support providers that need multiple environment variables (this is not handled in the previous implementation, it is only set as os.environ[key] = value)
Testing:
Tested by running an OpenAI-Compatible API setup and verifying that the environment variables are properly set, with successful responses from API calls without authentication errors.
Problem:
When trying to use an OpenAI-Compatible API, I would get this error:
(note: I added logging during debugging, but that was removed afterwards)
This is because the current implementation is not correctly differentiating between two configuration types:
1.) Configurations where only an API key is required (string type). 2.) Configurations where both a base URL and an API key are needed (dict type).
Solution:
My PR modifies the
update_model
function to explicitly check the configuration type inprovider_env_map
(either string or dict). Depending on the type, it sets the necessary environment variables as follows:For
string
configurations, it directly assigns theapi_key
to the designated environment variable (this is already done in the previous implementation). Fordict
configurations, it iterates through each key-value pair in the dict, setting all required variables such asbase_url
andapi_key
to support providers that need multiple environment variables (this is not handled in the previous implementation, it is only set asos.environ[key] = value
)Testing:
Tested by running an OpenAI-Compatible API setup and verifying that the environment variables are properly set, with successful responses from API calls without authentication errors.