AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
[SECRT-2806] fix(blocks): run community Replicate models without a version hash (#15128)
Bentlyb7bce9716bdb
diff --git a/autogpt_platform/backend/backend/blocks/replicate/_helper.py b/autogpt_platform/backend/backend/blocks/replicate/_helper.pyindex 25fcb4fdcf..1b3c87be25 100644--- a/autogpt_platform/backend/backend/blocks/replicate/_helper.py+++ b/autogpt_platform/backend/backend/blocks/replicate/_helper.py@@ -1,12 +1,60 @@import logging+from replicate.client import Client as ReplicateClient+from replicate.exceptions import ReplicateErrorfrom replicate.helpers import FileOutput+from replicate.prediction import Predictionlogger = logging.getLogger(__name__)ReplicateOutputs = FileOutput | list[FileOutput] | list[str] | str | list[dict]+class ReplicateModelNotRunnableError(ValueError):+ """The model has no version we can run: it doesn't exist or has none."""+++async def create_unpinned_prediction(+ client: ReplicateClient, model_name: str, model_inputs: dict+) -> Prediction:+ """Start a prediction for a model given without a version hash.++ Replicate's model-level endpoint (``/v1/models/{owner}/{name}/predictions``)+ only serves official models and answers 404 for community models, so on+ a 404 we run the model's latest published version instead.+ """+ try:+ return await client.predictions.async_create(+ model=model_name, input=model_inputs+ )+ except ReplicateError as e:+ if e.status != 404:+ raise++ version = await _resolve_latest_version(client, model_name)+ return await client.predictions.async_create(version=version, input=model_inputs)+++async def _resolve_latest_version(client: ReplicateClient, model_name: str) -> str:+ try:+ model = await client.models.async_get(model_name)+ except ReplicateError as e:+ if e.status != 404:+ raise+ raise ReplicateModelNotRunnableError(+ f"Replicate model '{model_name}' was not found. Check that the name "+ "is 'owner/model-name' and that your API key can access it."+ ) from e++ if model.latest_version is None:+ raise ReplicateModelNotRunnableError(+ f"Replicate model '{model_name}' has no published version to run. "+ "Set 'version' to a version hash from the model's Versions tab on "+ "Replicate."+ )+ return model.latest_version.id++def extract_result(output: ReplicateOutputs) -> str:result = ("Unable to process result. Please contact us with the models and inputs used"diff --git a/autogpt_platform/backend/backend/blocks/replicate/replicate_block.py b/autogpt_platform/backend/backend/blocks/replicate/replicate_block.pyindex 8d900e6cea..91e131c1f4 100644--- a/autogpt_platform/backend/backend/blocks/replicate/replicate_block.py+++ b/autogpt_platform/backend/backend/blocks/replicate/replicate_block.py@@ -18,7 +18,11 @@ from backend.blocks.replicate._auth import (TEST_CREDENTIALS_INPUT,ReplicateCredentialsInput,)-from backend.blocks.replicate._helper import extract_result+from backend.blocks.replicate._helper import (+ ReplicateModelNotRunnableError,+ create_unpinned_prediction,+ extract_result,+)from backend.data.execution import ExecutionContextfrom backend.data.model import (APIKeyCredentials,@@ -96,7 +100,12 @@ class ReplicateModelBlock(Block):)version: Optional[str] = SchemaField(default=None,- description="Specific version hash of the model (optional)",+ description=(+ "Version hash of the model to run. Leave empty to run the "+ "latest version: official models run through Replicate's "+ "model endpoint, and community models run their latest "+ "published version."+ ),placeholder="db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf",advanced=True,)@@ -168,6 +177,10 @@ class ReplicateModelBlock(Block):yield "result", resultyield "status", "succeeded"yield "model_name", input_data.model_name+ except ReplicateModelNotRunnableError as e:+ raise BlockInputError(+ message=str(e), block_name=self.name, block_id=self.id+ ) from eexcept Exception as e:error_msg = str(e)logger.error(f"Error running Replicate model: {error_msg}")@@ -242,16 +255,17 @@ class ReplicateModelBlock(Block):api_key_str = api_key.get_secret_value()client = ReplicateClient(api_token=api_key_str)- # Replicate SDK: version-pinned refs use `version=`; unpinned use- # `model=`. Matches the `owner/name[:version]` contract above.+ # Replicate SDK: version-pinned refs use `version=`; unpinned refs+ # try `model=` and fall back to the latest version (see _helper).+ # Matches the `owner/name[:version]` contract above.if ":" in model_ref:model_name, version = model_ref.split(":", 1)prediction = await client.predictions.async_create(version=version, input=model_inputs)else:- prediction = await client.predictions.async_create(- model=model_ref, input=model_inputs+ prediction = await create_unpinned_prediction(+ client, model_ref, model_inputs)await prediction.async_wait()diff --git a/autogpt_platform/backend/backend/blocks/replicate/replicate_block_version_test.py b/autogpt_platform/backend/backend/blocks/replicate/replicate_block_version_test.pynew file mode 100644index 0000000000..7393008a09--- /dev/null+++ b/autogpt_platform/backend/backend/blocks/replicate/replicate_block_version_test.py@@ -0,0 +1,232 @@+"""Tests for running ReplicateModelBlock without a version hash.++Replicate's model-level endpoint (``POST /v1/models/{owner}/{name}/predictions``)+only serves official models; community models answer 404 there. The HTTP+layer is mocked with an ``httpx.MockTransport`` so the real Replicate SDK+builds the requests:++1. Community model, no version → resolves ``latest_version`` and runs it+2. Official model, no version → runs through the model endpoint, no lookup+3. Unknown model → clear input error naming the model+4. Model with no published version → clear input error naming the model+5. A non-404 error from the model endpoint is not masked by the fallback+"""++import json++import httpx+import pytest+from replicate.client import Client as RealReplicateClient++from backend.blocks.replicate._auth import TEST_CREDENTIALS, TEST_CREDENTIALS_INPUT+from backend.blocks.replicate.replicate_block import ReplicateModelBlock+from backend.data.execution import ExecutionContext+from backend.util.exceptions import BlockExecutionError, BlockInputError++COMMUNITY_MODEL = "cuuupid/glm-4v-9b"+OFFICIAL_MODEL = "black-forest-labs/flux-schnell"+LATEST_VERSION = "69196a53d2a33ec4b1e4e3e8ee32d2e1a3a3f8e6e5d4c3b2a1f0e9d8c7b6a5f4"++NOT_FOUND = {"detail": "The requested resource could not be found.", "status": 404}+++def _model_json(name: str, latest_version: str | None) -> dict:+ owner, model = name.split("/")+ return {+ "url": f"https://replicate.com/{name}",+ "owner": owner,+ "name": model,+ "description": None,+ "visibility": "public",+ "github_url": None,+ "paper_url": None,+ "license_url": None,+ "run_count": 1,+ "cover_image_url": None,+ "default_example": None,+ "latest_version": (+ {+ "id": latest_version,+ "created_at": "2024-06-12T10:00:00.000000Z",+ "cog_version": "0.9.9",+ "openapi_schema": {},+ }+ if latest_version+ else None+ ),+ }+++def _prediction_json(model: str, version: str) -> dict:+ return {+ "id": "pred-1",+ "model": model,+ "version": version,+ "status": "succeeded",+ "input": {},+ "output": "a cat on a sofa",+ "logs": "",+ "error": None,+ "metrics": {"predict_time": 1.5},+ "urls": {},+ }+++class FakeReplicate:+ """Answers the Replicate endpoints the block calls and records requests."""++ def __init__(self, models: dict[str, dict | None], official: set[str]):+ self.models = models+ self.official = official+ self.requests: list[tuple[str, str, dict]] = []++ def handler(self, request: httpx.Request) -> httpx.Response:+ body = json.loads(request.content) if request.content else {}+ path = request.url.path+ self.requests.append((request.method, path, body))++ if request.method == "POST" and path.endswith("/predictions"):+ if path == "/v1/predictions":+ return httpx.Response(201, json=_prediction_json("", body["version"]))+ name = path.removeprefix("/v1/models/").removesuffix("/predictions")+ if name in self.official:+ return httpx.Response(201, json=_prediction_json(name, ""))+ return httpx.Response(404, json=NOT_FOUND)++ if request.method == "GET" and path.startswith("/v1/models/"):+ name = path.removeprefix("/v1/models/")+ model = self.models.get(name)+ if model is None:+ return httpx.Response(404, json=NOT_FOUND)+ return httpx.Response(200, json=model)++ return httpx.Response(500, json={"detail": f"unexpected {path}"})+++def _patch_client(monkeypatch, fake: FakeReplicate):+ def build(**kwargs):+ return RealReplicateClient(+ transport=httpx.MockTransport(fake.handler), **kwargs+ )++ monkeypatch.setattr(+ "backend.blocks.replicate.replicate_block.ReplicateClient", build+ )+++async def _run(model_name: str, version: str | None = None) -> dict:+ block = ReplicateModelBlock()+ input_data = ReplicateModelBlock.Input(+ **{+ "credentials": TEST_CREDENTIALS_INPUT,+ "model_name": model_name,+ "model_inputs": {"prompt": "describe"},+ "version": version,+ }+ )+ outputs = {}+ async for name, value in block.run(+ input_data,+ credentials=TEST_CREDENTIALS,+ execution_context=ExecutionContext(user_id="user-1", graph_exec_id="exec-1"),+ ):+ outputs[name] = value+ return outputs+++async def test_community_model_without_version_runs_latest_version(monkeypatch):+ fake = FakeReplicate(+ models={COMMUNITY_MODEL: _model_json(COMMUNITY_MODEL, LATEST_VERSION)},+ official=set(),+ )+ _patch_client(monkeypatch, fake)++ outputs = await _run(COMMUNITY_MODEL)++ assert outputs == {+ "result": "a cat on a sofa",+ "status": "succeeded",+ "model_name": COMMUNITY_MODEL,+ }+ assert ("GET", f"/v1/models/{COMMUNITY_MODEL}", {}) in fake.requests+ method, path, body = fake.requests[-1]+ assert (method, path) == ("POST", "/v1/predictions")+ assert body["version"] == LATEST_VERSION+ assert body["input"] == {"prompt": "describe"}+++async def test_official_model_without_version_uses_model_endpoint(monkeypatch):+ fake = FakeReplicate(+ models={OFFICIAL_MODEL: _model_json(OFFICIAL_MODEL, None)},+ official={OFFICIAL_MODEL},+ )+ _patch_client(monkeypatch, fake)++ outputs = await _run(OFFICIAL_MODEL)++ assert outputs["result"] == "a cat on a sofa"+ assert [(m, p) for m, p, _ in fake.requests] == [+ ("POST", f"/v1/models/{OFFICIAL_MODEL}/predictions")+ ]+++async def test_pinned_version_skips_model_endpoint(monkeypatch):+ fake = FakeReplicate(models={}, official=set())+ _patch_client(monkeypatch, fake)++ await _run(COMMUNITY_MODEL, version=LATEST_VERSION)++ assert [(m, p) for m, p, _ in fake.requests] == [("POST", "/v1/predictions")]+ assert fake.requests[0][2]["version"] == LATEST_VERSION+++async def test_unknown_model_gives_clear_input_error(monkeypatch):+ fake = FakeReplicate(models={}, official=set())+ _patch_client(monkeypatch, fake)++ with pytest.raises(BlockInputError) as exc_info:+ await _run("nobody/does-not-exist")++ message = str(exc_info.value)+ assert "'nobody/does-not-exist'" in message+ assert "not found" in message+++async def test_model_without_versions_gives_clear_input_error(monkeypatch):+ fake = FakeReplicate(+ models={COMMUNITY_MODEL: _model_json(COMMUNITY_MODEL, None)},+ official=set(),+ )+ _patch_client(monkeypatch, fake)++ with pytest.raises(BlockInputError) as exc_info:+ await _run(COMMUNITY_MODEL)++ message = str(exc_info.value)+ assert f"'{COMMUNITY_MODEL}'" in message+ assert "no published version" in message+ assert not any(p == "/v1/predictions" for _, p, _ in fake.requests)+++async def test_non_404_error_is_not_retried_as_community_model(monkeypatch):+ class Unauthorized(FakeReplicate):+ def handler(self, request: httpx.Request) -> httpx.Response:+ self.requests.append((request.method, request.url.path, {}))+ return httpx.Response(401, json={"detail": "Invalid token", "status": 401})++ fake = Unauthorized(models={}, official=set())+ _patch_client(monkeypatch, fake)++ with pytest.raises(BlockExecutionError) as exc_info:+ await _run(COMMUNITY_MODEL)++ assert "Invalid token" in str(exc_info.value)+ assert [(m, p) for m, p, _ in fake.requests] == [+ ("POST", f"/v1/models/{COMMUNITY_MODEL}/predictions")+ ]diff --git a/docs/integrations/block-integrations/replicate/replicate_block.md b/docs/integrations/block-integrations/replicate/replicate_block.mdindex d096ae4125..7889239ef5 100644--- a/docs/integrations/block-integrations/replicate/replicate_block.md+++ b/docs/integrations/block-integrations/replicate/replicate_block.md@@ -23,7 +23,7 @@ The block waits for completion and returns the model output along with status in| model_inputs | Dictionary of inputs to pass to the model. Values may be strings, integers, floats, or booleans — Replicate model schemas commonly require booleans (e.g. ``generate_audio``, ``safety_checker``) and floats (e.g. ``temperature``, ``guidance_scale``). | Dict[str, str \| int \| float \| bool] | No || files | Files (image, audio, video, etc.) to send to the model. Each file is uploaded to Replicate and passed to the model under the field named by ``file_input_field``. Pass file references (URLs, uploaded files) here instead of inlining base64 in ``model_inputs``. | List[str (file)] | No || file_input_field | Name of the model input field that receives the uploaded ``files``. Defaults to ``image``, the most common name. Check the model's schema on Replicate for the exact name (common alternatives: ``input_image``, ``img``, ``image_input``, ``audio``, ``video``, ``mask``). A single file is sent as one value; multiple files are sent as a list. | str | No |-| version | Specific version hash of the model (optional) | str | No |+| version | Version hash of the model to run. Leave empty to run the latest version: official models run through Replicate's model endpoint, and community models run their latest published version. | str | No |### Outputs