> For the complete documentation index, see [llms.txt](https://docs.roboflow.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.roboflow.com/deploy/supported-models/clip.md).

# CLIP

We support OpenAI's [CLIP](https://github.com/openai/CLIP) model for generating image and text embeddings, and for zero-shot similarity comparison between them, via our [Serverless Hosted API](/deploy/serverless-hosted-api-v2.md). We expose three endpoints:

* [`/clip/embed_image`](#post-clip-embed_image), returns an embedding vector for an image
* [`/clip/embed_text`](#post-clip-embed_text), returns an embedding vector for a string or list of strings
* [`/clip/compare`](#post-clip-compare), returns similarity scores between a subject and a list of prompts

Embeddings can be cached and reused for tasks like classification, retrieval, clustering, and semantic search. For broader usage details, see the [Inference documentation](https://inference.roboflow.com/).

## Code sample

Below is a code sample that compares an image against a list of text labels. Call the HTTP endpoint directly with `curl`, or use the [`inference-sdk`](https://inference.roboflow.com/inference_helpers/inference_sdk/) wrapper.

{% tabs %}
{% tab title="HTTP (curl)" icon="webhook" %}
{% stepper %}
{% step %}

### Get your API Key

Create a Roboflow account, find your key on the [Roboflow API settings page](https://app.roboflow.com/settings/api) and make it available to your shell:

```bash
export ROBOFLOW_API_KEY="your-key-here"
```

{% endstep %}

{% step %}

### Run the model

Call the `/clip/compare` endpoint with `curl`:

```bash
curl --location 'https://serverless.roboflow.com/clip/compare' \
  --header 'Content-Type: application/json' \
  --data '{
    "api_key": "'"$ROBOFLOW_API_KEY"'",
    "subject": {"type": "url", "value": "https://media.roboflow.com/notebooks/examples/dog.jpeg"},
    "subject_type": "image",
    "prompt": ["a photo of a dog", "a photo of a cat", "a photo of a car"],
    "prompt_type": "text"
  }'
```

{% endstep %}
{% endstepper %}
{% endtab %}

{% tab title="SDK (Python)" icon="python" %}
{% stepper %}
{% step %}

### Get your API Key

Create a Roboflow account, find your key on the [Roboflow API settings page](https://app.roboflow.com/settings/api) and make it available to your shell:

```bash
export ROBOFLOW_API_KEY="your-key-here"
```

{% endstep %}

{% step %}

### Install the dependencies

This package calls the model:

```bash
pip install inference-sdk
```

{% endstep %}

{% step %}

### Run the model

Run the comparison (image from [here](https://media.roboflow.com/notebooks/examples/dog.jpeg)):

```python
import os
import cv2
import numpy as np
import requests
from inference_sdk import InferenceHTTPClient

content = requests.get("https://media.roboflow.com/notebooks/examples/dog.jpeg").content
image = cv2.imdecode(np.frombuffer(content, np.uint8), cv2.IMREAD_COLOR)

client = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key=os.environ["ROBOFLOW_API_KEY"],
)

result = client.clip_compare(
    subject=image,
    prompt=[
        "a photo of a dog",
        "a photo of a cat",
        "a photo of a car",
    ],
    subject_type="image",
    prompt_type="text",
)

# similarity is a list of cosine similarity scores, one per prompt
print(result["similarity"])
```

The code above prints inference results to the terminal:

```
[0.2726989686489105, 0.19865083694458008, 0.20997387170791626]
```

{% endstep %}
{% endstepper %}
{% endtab %}
{% endtabs %}

## Inference speed

Latency measured with [Roboflow Inference](https://inference.roboflow.com/) on 1x NVIDIA L4, batch size 1, mean after warmup.

<table data-search="false"><thead><tr><th>Model</th><th>Latency (ms)</th></tr></thead><tbody><tr><td><code>clip</code></td><td>3.9</td></tr></tbody></table>

Measured with `embed_image` on the `ViT-B-16` checkpoint (image embedding only).

{% hint style="info" %}
Set `api_url` to match your deployment target:

* `https://serverless.roboflow.com` for the Serverless Hosted API.
* `http://localhost:9001` for a local [Inference](https://inference.roboflow.com/) server.
* Your [Dedicated Deployment](/deploy/dedicated-deployments.md) URL for a private endpoint.
  {% endhint %}
