Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added ._.git
Binary file not shown.
Binary file added ._.github
Binary file not shown.
Binary file added ._.gitignore
Binary file not shown.
Binary file added ._.readthedocs.yaml
Binary file not shown.
Binary file added ._.venv
Binary file not shown.
Binary file added ._AGENTS.md
Binary file not shown.
Binary file added ._Justfile
Binary file not shown.
Binary file added ._LICENSE
Binary file not shown.
Binary file added ._MANIFEST.in
Binary file not shown.
Binary file added ._README.md
Binary file not shown.
Binary file added ._docs
Binary file not shown.
Binary file added ._llm
Binary file not shown.
Binary file added ._mypy.ini
Binary file not shown.
Binary file added ._pyproject.toml
Binary file not shown.
Binary file added ._pytest.ini
Binary file not shown.
Binary file added ._ruff.toml
Binary file not shown.
Binary file added ._tests
Binary file not shown.
Binary file added .github/._FUNDING.yml
Binary file not shown.
Binary file added .github/._dependabot.yml
Binary file not shown.
Binary file added .github/._workflows
Binary file not shown.
Binary file added .github/workflows/._cog.yml
Binary file not shown.
Binary file added .github/workflows/._publish.yml
Binary file not shown.
Binary file added .github/workflows/._stable-docs.yml
Binary file not shown.
Binary file added .github/workflows/._test.yml
Binary file not shown.
Binary file added docs/._.gitignore
Binary file not shown.
Binary file added docs/._Makefile
Binary file not shown.
Binary file added docs/.__templates
Binary file not shown.
Binary file added docs/._aliases.md
Binary file not shown.
Binary file added docs/._changelog.md
Binary file not shown.
Binary file added docs/._conf.py
Binary file not shown.
Binary file added docs/._contributing.md
Binary file not shown.
Binary file added docs/._embeddings
Binary file not shown.
Binary file added docs/._fragments.md
Binary file not shown.
Binary file added docs/._help.md
Binary file not shown.
Binary file added docs/._index.md
Binary file not shown.
Binary file added docs/._logging.md
Binary file not shown.
Binary file added docs/._openai-models.md
Binary file not shown.
Binary file added docs/._other-models.md
Binary file not shown.
Binary file added docs/._plugins
Binary file not shown.
Binary file added docs/._python-api.md
Binary file not shown.
Binary file added docs/._related-tools.md
Binary file not shown.
Binary file added docs/._requirements.txt
Binary file not shown.
Binary file added docs/._schemas.md
Binary file not shown.
Binary file added docs/._setup.md
Binary file not shown.
Binary file added docs/._templates.md
Binary file not shown.
Binary file added docs/._tools.md
Binary file not shown.
Binary file added docs/._usage.md
Binary file not shown.
Binary file added docs/_templates/._base.html
Binary file not shown.
Binary file added docs/embeddings/._cli.md
Binary file not shown.
Binary file added docs/embeddings/._index.md
Binary file not shown.
Binary file added docs/embeddings/._python-api.md
Binary file not shown.
Binary file added docs/embeddings/._storage.md
Binary file not shown.
Binary file added docs/embeddings/._writing-plugins.md
Binary file not shown.
Binary file added docs/plugins/._advanced-model-plugins.md
Binary file not shown.
Binary file added docs/plugins/._directory.md
Binary file not shown.
Binary file added docs/plugins/._index.md
Binary file not shown.
Binary file added docs/plugins/._installing-plugins.md
Binary file not shown.
Binary file added docs/plugins/._llm-markov
Binary file not shown.
Binary file added docs/plugins/._plugin-hooks.md
Binary file not shown.
Binary file added docs/plugins/._plugin-utilities.md
Binary file not shown.
Binary file added docs/plugins/._tutorial-model-plugin.md
Binary file not shown.
Binary file added docs/plugins/llm-markov/._llm_markov.py
Binary file not shown.
Binary file added docs/plugins/llm-markov/._pyproject.toml
Binary file not shown.
Binary file added llm/.___init__.py
Binary file not shown.
Binary file added llm/.___main__.py
Binary file not shown.
Binary file added llm/._cli.py
Binary file not shown.
Binary file added llm/._default_plugins
Binary file not shown.
Binary file added llm/._embeddings.py
Binary file not shown.
Binary file added llm/._embeddings_migrations.py
Binary file not shown.
Binary file added llm/._errors.py
Binary file not shown.
Binary file added llm/._hookspecs.py
Binary file not shown.
Binary file added llm/._migrations.py
Binary file not shown.
Binary file added llm/._models.py
Binary file not shown.
Binary file added llm/._plugins.py
Binary file not shown.
Binary file added llm/._py.typed
Binary file not shown.
Binary file added llm/._templates.py
Binary file not shown.
Binary file added llm/._tools.py
Binary file not shown.
Binary file added llm/._utils.py
Binary file not shown.
Binary file added llm/default_plugins/.___init__.py
Binary file not shown.
Binary file added llm/default_plugins/._default_tools.py
Binary file not shown.
Binary file added llm/default_plugins/._openai_models.py
Binary file not shown.
11 changes: 9 additions & 2 deletions llm/default_plugins/openai_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,12 @@ def set_usage(self, response, usage):
input=input_tokens, output=output_tokens, details=simplify_usage_dict(usage)
)

def _headers_include_auth(self):
"""Check if custom headers include an authorization header (case-insensitive)."""
if not self.headers:
return False
return any(key.lower() == "authorization" for key in self.headers)

def get_client(self, key, *, async_=False):
kwargs = {}
if self.api_base:
Expand All @@ -692,9 +698,10 @@ def get_client(self, key, *, async_=False):
kwargs["engine"] = self.api_engine
if self.needs_key:
kwargs["api_key"] = self.get_key(key)
else:
elif not self._headers_include_auth():
# OpenAI-compatible models don't need a key, but the
# openai client library requires one
# openai client library requires one - unless custom
# headers already provide authorization
kwargs["api_key"] = "DUMMY_KEY"
if self.headers:
kwargs["default_headers"] = self.headers
Expand Down
Binary file added tests/._cassettes
Binary file not shown.
Binary file added tests/._conftest.py
Binary file not shown.
Binary file added tests/._test-llm-load-plugins.sh
Binary file not shown.
Binary file added tests/._test_aliases.py
Binary file not shown.
Binary file added tests/._test_async.py
Binary file not shown.
Binary file added tests/._test_attachments.py
Binary file not shown.
Binary file added tests/._test_chat.py
Binary file not shown.
Binary file added tests/._test_chat_templates.py
Binary file not shown.
Binary file added tests/._test_cli_openai_models.py
Binary file not shown.
Binary file added tests/._test_cli_options.py
Binary file not shown.
Binary file added tests/._test_embed.py
Binary file not shown.
Binary file added tests/._test_embed_cli.py
Binary file not shown.
Binary file added tests/._test_encode_decode.py
Binary file not shown.
Binary file added tests/._test_fragments_cli.py
Binary file not shown.
Binary file added tests/._test_keys.py
Binary file not shown.
Binary file added tests/._test_llm.py
Binary file not shown.
Binary file added tests/._test_llm_logs.py
Binary file not shown.
Binary file added tests/._test_migrate.py
Binary file not shown.
Binary file added tests/._test_plugins.py
Binary file not shown.
Binary file added tests/._test_templates.py
Binary file not shown.
Binary file added tests/._test_tools.py
Binary file not shown.
Binary file added tests/._test_tools_streaming.py
Binary file not shown.
Binary file added tests/._test_utils.py
Binary file not shown.
Binary file added tests/cassettes/._test_tools
Binary file not shown.
Binary file added tests/cassettes/._test_tools_streaming
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
38 changes: 38 additions & 0 deletions tests/test_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,44 @@ def test_openai_localai_configuration(mocked_localai, user_path):
}


EXTRA_MODELS_YAML_WITH_AUTH = """
- model_id: custom-auth-model
model_name: my-model
api_base: "http://custom-auth.localhost"
headers:
authorization: "Basic dXNlcjpwYXNzd29yZA=="
"""


def test_openai_custom_auth_headers(httpx_mock, user_path):
"""Test that custom authorization headers are used instead of DUMMY_KEY."""
httpx_mock.add_response(
method="POST",
url="http://custom-auth.localhost/chat/completions",
json={
"model": "my-model",
"usage": {"prompt_tokens": 1, "completion_tokens": 2, "total_tokens": 3},
"choices": [{"message": {"content": "Authenticated response"}}],
},
headers={"Content-Type": "application/json"},
)
# Write the configuration file with custom auth header
config_path = user_path / "extra-openai-models.yaml"
config_path.write_text(EXTRA_MODELS_YAML_WITH_AUTH, "utf-8")
# Run the prompt
runner = CliRunner()
result = runner.invoke(
cli, ["--no-stream", "--model", "custom-auth-model", "test prompt"]
)
assert result.exit_code == 0
assert result.output == "Authenticated response\n"
# Check that the custom authorization header was sent, not DUMMY_KEY
last_request = httpx_mock.get_requests()[-1]
auth_header = last_request.headers.get("authorization")
assert auth_header == "Basic dXNlcjpwYXNzd29yZA=="
assert "Bearer DUMMY_KEY" not in str(last_request.headers)


@pytest.mark.parametrize(
"args,exit_code",
(
Expand Down