Embeddings

You can get privacy-conserving text and image embeddings from available models using a call to the /embeddings REST API endpoint or any of the official SDKs (Python, Go, Rust, JS, or cURL).

Generate Embeddings

To generate embeddings, you can use the following code examples. Depending on your preference or requirements, select the appropriate method for your application. This functionality accepts text and image inputs, and supports batching multiple inputs. The Go REST API only supports images that are input as a base64 encoded string, whereas the python client supports image files, image urls, and images encoded in a base64 string.

1import os
2import json
3
4from predictionguard import PredictionGuard
5
6# Set your Prediction Guard token as an environmental variable.
7os.environ["PREDICTIONGUARD_API_KEY"] = "<api key>"
8
9client = PredictionGuard()
10
11response = client.embeddings.create(
12 model="bridgetower-large-itm-mlm-itc",
13 input=[
14 {
15 "text": "Tell me a joke.",
16 "image": "https://farm4.staticflickr.com/3300/3497460990_11dfb95dd1_z.jpg"
17 }
18 ]
19)
20
21print(json.dumps(
22 response,
23 sort_keys=True,
24 indent=4,
25 separators=(',', ': ')
26))

The output will look something like this.

1{
2 "data": [
3 {
4 "embedding": [
5 -0.010075676254928112,
6 -0.024130256846547127,
7 -0.017985647544264793,
8 ...
9 ],
10 "index": 0,
11 "object": "embedding"
12 }
13 ],
14 "id": "emb-c8159360-feaf-4fc8-8275-1cab8b790f8d",
15 "object": "list",
16 "created": 1727795282,
17 "model": "bridgetower-large-itm-mlm-itc",
18}

This approach presents a straightforward way for readers to choose and apply the code example that best suits their needs for generating text completions using either Python, Go, Rust, JS, or cURL.