Training
Training fits an embedding model to your dataset. Nolabel_column is needed. No validation metrics are computed for embedding models.
import os, time, requests
api_key = os.getenv("WOODWIDE_API_KEY")
base_url = "https://api.woodwide.ai"
headers = {"Authorization": f"Bearer {api_key}"}
# Upload data
with open("products.csv", "rb") as f:
resp = requests.post(
f"{base_url}/datasets",
headers=headers,
files={"file": ("products.csv", f, "text/csv")},
data={"dataset_name": "products"},
)
dataset_id = resp.json()["dataset"]["id"]
# Train an embedding model
resp = requests.post(
f"{base_url}/models/train",
headers=headers,
json={
"model_name": "product_embeddings",
"model_type": "embedding",
"dataset_id": dataset_id,
},
)
model_id = resp.json()["model"]["id"]
# Wait for training
while True:
model = requests.get(
f"{base_url}/models/{model_id}", headers=headers
).json()
if model["status"] == "ready":
break
time.sleep(5)
import time
from pathlib import Path
from woodwide import WoodWide
client = WoodWide()
dataset = client.datasets.create(
file=Path("products.csv"),
dataset_name="products",
override=True,
)
model = client.models.train(
model_type="embedding",
dataset_id=dataset.id,
)
model_id = model.id
while True:
model = client.models.retrieve(model_id)
if model.status in {"ready", "failed"}:
break
time.sleep(5)
if model.status == "failed":
raise RuntimeError("Training failed")
const fs = require("fs");
const FormData = require("form-data");
const apiKey = process.env.WOODWIDE_API_KEY;
const baseUrl = "https://api.woodwide.ai";
const headers = { Authorization: `Bearer ${apiKey}` };
// Upload data
const uploadForm = new FormData();
uploadForm.append("file", fs.createReadStream("products.csv"), "products.csv");
uploadForm.append("dataset_name", "products");
const uploadResp = await fetch(`${baseUrl}/datasets`, {
method: "POST",
headers: { ...headers, ...uploadForm.getHeaders() },
body: uploadForm,
});
const { dataset: { id: datasetId } } = await uploadResp.json();
// Train an embedding model
const trainResp = await fetch(`${baseUrl}/models/train`, {
method: "POST",
headers: { ...headers, "Content-Type": "application/json" },
body: JSON.stringify({
model_name: "product_embeddings",
model_type: "embedding",
dataset_id: datasetId,
}),
});
const { model: { id: modelId } } = await trainResp.json();
// Wait for training
while (true) {
const modelResp = await fetch(`${baseUrl}/models/${modelId}`, { headers });
const model = await modelResp.json();
if (model.status === "ready") break;
await new Promise((r) => setTimeout(r, 5000));
}
export WOODWIDE_API_KEY="your_api_key_here"
export BASE_URL="https://api.woodwide.ai"
# Upload data
DATASET_ID=$(curl -s -X POST "$BASE_URL/datasets" \
-H "Authorization: Bearer $WOODWIDE_API_KEY" \
-F "file=@products.csv" \
-F "dataset_name=products" | jq -r '.dataset.id')
# Train an embedding model
MODEL_ID=$(curl -s -X POST "$BASE_URL/models/train" \
-H "Authorization: Bearer $WOODWIDE_API_KEY" \
-H "Content-Type: application/json" \
-d "{
\"model_name\": \"product_embeddings\",
\"model_type\": \"embedding\",
\"dataset_id\": \"$DATASET_ID\"
}" | jq -r '.model.id')
# Wait for training
while true; do
STATUS=$(curl -s "$BASE_URL/models/$MODEL_ID" \
-H "Authorization: Bearer $WOODWIDE_API_KEY" | jq -r '.status')
if [ "$STATUS" = "ready" ]; then break; fi
if [ "$STATUS" = "failed" ]; then echo "Training failed."; exit 1; fi
sleep 5
done
Inference
Run inference to generate embeddings. You can embed the training data or new data. The model will produce embeddings that are consistent with the representation learned during training.with open("products.csv", "rb") as f:
resp = requests.post(
f"{base_url}/models/{model_id}/infer",
headers=headers,
files={"file": ("products.csv", f, "text/csv")},
data={"output_type": "json"},
)
results = resp.json()["data"]
print(results)
# Uses client and model_id from the training example above.
from pathlib import Path
result = client.models.infer(
model_id,
file=Path("products.csv"),
output_type="json",
)
print(result["data"])
const form = new FormData();
form.append("file", fs.createReadStream("products.csv"), "products.csv");
form.append("output_type", "json");
const resp = await fetch(`${baseUrl}/models/${modelId}/infer`, {
method: "POST",
headers: { ...headers, ...form.getHeaders() },
body: form,
});
const { data: results } = await resp.json();
console.log(results);
curl -s -X POST "$BASE_URL/models/$MODEL_ID/infer" \
-H "Authorization: Bearer $WOODWIDE_API_KEY" \
-F "file=@products.csv" \
-F "output_type=json" | jq '.data'