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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -622,3 +622,12 @@ The DataFlow paper has been accepted for publication in the **Proceedings of the
on Xiaohongshu: <strong>26133106768</strong>
</sub>
</div>

### Alternative Perspective endpoints

The Perspective adapter accepts optional `api_endpoint` (the full API base URL,
including `/v1alpha1/`) and `discovery_service_url` (a compatible discovery JSON
URL). Omit both to retain Google defaults. To remove the Google dependency,
configure both URLs. Only use trusted endpoints: requests include your API key
and text. A different model requires fresh threshold calibration; its scores
must not be compared as though they were Google Perspective scores.
7 changes: 5 additions & 2 deletions dataflow/serving/google_api_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

class PerspectiveAPIServing(LLMServingABC):
"""Service adapter for Google Perspective API."""
def __init__(self, max_workers: int = 10):
def __init__(
self, max_workers: int = 10, api_endpoint=None, discovery_service_url=None
):
self.api_key = os.environ.get("GOOGLE_API_KEY")
if self.api_key is None:
raise ValueError("Lack of Google API_KEY")
Expand All @@ -15,7 +17,8 @@ def __init__(self, max_workers: int = 10):
"commentanalyzer",
"v1alpha1",
developerKey=self.api_key,
discoveryServiceUrl="https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1",
discoveryServiceUrl=discovery_service_url or "https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1",
client_options={"api_endpoint": api_endpoint} if api_endpoint else None,
static_discovery=False,
)

Expand Down
14 changes: 14 additions & 0 deletions test/cpu_only/test_perspective_serving_instantiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,17 @@ def test_perspective_serving_is_concrete_against_real_llm_serving_contract(monke
def test_builtin_filter_constructs_perspective_serving_directly():
source = FILTER.read_text(encoding="utf-8")
assert "PerspectiveAPIServing(max_workers=10)" in source


@pytest.mark.cpu
def test_perspective_custom_discovery_and_endpoint(monkeypatch):
namespace = _load_serving(monkeypatch)
calls = []
monkeypatch.setattr(namespace["discovery"], "build", lambda *a, **kw: calls.append(kw) or object())
cls = namespace["PerspectiveAPIServing"]
cls()
assert calls[-1]["client_options"] is None
assert calls[-1]["discoveryServiceUrl"] == "https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1"
cls(api_endpoint="https://moderation.example/v1alpha1/", discovery_service_url="https://moderation.example/discovery.json")
assert calls[-1]["client_options"] == {"api_endpoint": "https://moderation.example/v1alpha1/"}
assert calls[-1]["discoveryServiceUrl"] == "https://moderation.example/discovery.json"