mirror of
https://github.com/langgenius/dify-plugin-sdks.git
synced 2026-08-26 18:56:41 -04:00
fix(openai_compatible): omit rerank authorization without an API key (#206)
Co-authored-by: WH-2099 <wh2099@pm.me>
This commit is contained in:
@@ -66,10 +66,9 @@ class OAICompatRerankModel(RerankModel):
|
||||
raise CredentialsValidateFailedError(msg)
|
||||
|
||||
url = server_url
|
||||
headers = {
|
||||
"Authorization": f"Bearer {credentials.get('api_key')}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if api_key := credentials.get("api_key"):
|
||||
headers["Authorization"] = f"Bearer {api_key}"
|
||||
|
||||
# Open question: truncate docs before llama.cpp-compatible requests?
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from dify_plugin.interfaces.model.openai_compatible.rerank import (
|
||||
OAICompatRerankModel,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("credentials", "expected_headers"),
|
||||
[
|
||||
(
|
||||
{"endpoint_url": "https://example.com/v1"},
|
||||
{"Content-Type": "application/json"},
|
||||
),
|
||||
(
|
||||
{"endpoint_url": "https://example.com/v1", "api_key": None},
|
||||
{"Content-Type": "application/json"},
|
||||
),
|
||||
(
|
||||
{"endpoint_url": "https://example.com/v1", "api_key": ""},
|
||||
{"Content-Type": "application/json"},
|
||||
),
|
||||
(
|
||||
{"endpoint_url": "https://example.com/v1", "api_key": str(123)},
|
||||
{
|
||||
"Authorization": "Bearer 123",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_invoke_only_sends_authorization_with_api_key(
|
||||
credentials: dict, expected_headers: dict[str, str]
|
||||
) -> None:
|
||||
response = MagicMock()
|
||||
response.json.return_value = {"results": [{"index": 0, "relevance_score": 1.0}]}
|
||||
|
||||
with patch(
|
||||
"dify_plugin.interfaces.model.openai_compatible.rerank.post",
|
||||
return_value=response,
|
||||
) as post:
|
||||
OAICompatRerankModel([]).invoke(
|
||||
model="model",
|
||||
credentials=credentials,
|
||||
query="query",
|
||||
docs=["document"],
|
||||
)
|
||||
|
||||
assert post.call_args.kwargs["headers"] == expected_headers
|
||||
Reference in New Issue
Block a user