# 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 using the [`inference-sdk`](https://inference.roboflow.com/inference_helpers/inference_sdk/). Pass [Roboflow's API Key](https://app.roboflow.com/settings/api) via the `API_KEY` env variable.

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

```bash
curl --location 'https://serverless.roboflow.com/clip/compare' \
  --header 'Content-Type: application/json' \
  --data '{
    "api_key": "YOUR_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"
  }'
```

The same call through the SDK. Install it:

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

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

```python
import os
import urllib.request
from inference_sdk import InferenceHTTPClient

image_url = "https://media.roboflow.com/notebooks/examples/dog.jpeg"
image_path = "dog.jpeg"
urllib.request.urlretrieve(image_url, image_path)

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

result = client.clip_compare(
    subject=image_path,
    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]
```

{% 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.roboflow.com/deploy/supported-models/clip.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
