Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.
Python agent-evaluationaiai-alignmentai-evaluationai-governanceai-safetyclidiagnostic-tooleu-ai-acthallucination-detectioniso-42001llm-evaluationllm-securitynist-ai-rmfowasp-llmprompt-injectionpythonresponsible-airisk-assessmentrisk-management
fix(huggingface): route inference to the configured endpoint (#172)
Rudy Mizrahi Celekli795b55701f78
diff --git a/ifixai/providers/huggingface.py b/ifixai/providers/huggingface.pyindex 1b0273b..1a433f1 100644--- a/ifixai/providers/huggingface.py+++ b/ifixai/providers/huggingface.py@@ -45,7 +45,7 @@ class HuggingFaceProvider(ChatProvider):endpoint = config.endpoint or "https://api-inference.huggingface.co"client = InferenceClient(- model=config.model or None,+ model=config.endpoint or config.model or None,token=config.api_key or None,timeout=float(config.timeout),)diff --git a/ifixai/tests/test_huggingface_endpoint_native.py b/ifixai/tests/test_huggingface_endpoint_native.pynew file mode 100644index 0000000..da0ef42--- /dev/null+++ b/ifixai/tests/test_huggingface_endpoint_native.py@@ -0,0 +1,73 @@+"""Route native Hugging Face inference to the configured owned endpoint."""++import json+import threading+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer++import pytest++from ifixai.core.types import ChatMessage, ProviderConfig+from ifixai.providers import huggingface++hf = pytest.importorskip("huggingface_hub")+++async def test_endpoint_receives_native_chat_request_with_model_and_key(monkeypatch):+ requests = []++ class Handler(BaseHTTPRequestHandler):+ def do_POST(self):+ requests.append((self.path, self.headers.get("Authorization"), json.loads(self.rfile.read(int(self.headers["Content-Length"])))))+ payload = json.dumps({+ "id": "owned-reply", "object": "chat.completion", "created": 1,+ "model": "owned-model", "choices": [{"index": 0, "finish_reason": "stop", "message": {"role": "assistant", "content": "owned endpoint reply"}}],+ "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},+ }).encode()+ self.send_response(200)+ self.send_header("Content-Type", "application/json")+ self.send_header("Content-Length", str(len(payload)))+ self.end_headers()+ self.wfile.write(payload)++ def log_message(self, *args):+ pass++ server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)+ thread = threading.Thread(target=server.serve_forever, daemon=True)+ thread.start()+ endpoint = f"http://127.0.0.1:{server.server_port}/v1/chat/completions"++ def native_client(**kwargs):+ # Fail before any network access if the adapter ignored the endpoint.+ # Once this passes, the installed SDK performs the actual HTTP call.+ assert kwargs["model"] == endpoint+ return hf.InferenceClient(**kwargs)++ monkeypatch.setattr(huggingface, "InferenceClient", native_client)+ try:+ config = ProviderConfig(provider="huggingface", endpoint=endpoint, model="owned-model", api_key="synthetic-local-key", max_retries=0)+ reply = await huggingface.HuggingFaceProvider().send_message([ChatMessage(content="hello")], config)+ assert reply == "owned endpoint reply"+ assert len(requests) == 1+ path, auth, body = requests[0]+ assert path == "/v1/chat/completions"+ assert auth == "Bearer synthetic-local-key"+ assert body["model"] == "owned-model"+ assert body["messages"] == [{"role": "user", "content": "hello"}]+ finally:+ server.shutdown()+ server.server_close()+ thread.join()+++async def test_default_model_routing_is_retained(monkeypatch):+ class CapturedClient:+ def __init__(self, **kwargs):+ assert kwargs["model"] == "org/model"++ monkeypatch.setattr(huggingface, "InferenceClient", CapturedClient)+ monkeypatch.setattr(huggingface, "_call_chat_completion", lambda *args: "model reply")+ reply = await huggingface.HuggingFaceProvider().send_message([ChatMessage(content="hello")], ProviderConfig(provider="huggingface", model="org/model"))+ assert reply == "model reply"