Skip to main content

Prerequisites

1

Prepare Your Inputs

Decide what code snippets or text you want to embed. You can embed multiple strings in a single request.
const inputs = [
  "def add(a, b): return a + b",
  "class User: pass",
  // ... more code or text
];
2

Call the Embeddings API

Send your inputs to the embeddings endpoint to get vector representations.
const url = "https://embeddings.endpoint.relace.run/v1/code/embed";
const apiKey = "[YOUR_API_KEY]";

const headers = {
  "Authorization": `Bearer ${apiKey}`,
  "Content-Type": "application/json"
};

const data = {
  model: "relace-embed-v1",
  input: inputs,
  // Optional: output_dtype: "float"  // or "binary"
};

const response = await fetch(url, {
  method: "POST",
  headers: headers,
  body: JSON.stringify(data)
});

const embeddings = await response.json();
3

Parse Embedding Results

The API returns an array of embeddings, one for each input string, and usage info.
{
  "results": [
    { "index": 0, "embedding": [0.123, 0.456, 0.789] },
    { "index": 1, "embedding": [0.234, 0.567, 0.89] }
  ],
  "usage": {
    "total_tokens": 8
  }
}
For more details and options, see the API reference.
4

Store and Search with a Vector Database

Once you have your embeddings, you can store them in a vector database such as Turbopuffer or Pinecone. These databases are designed for efficient similarity search over large collections of vectors.

Example: Storing Embeddings

Most vector databases let you upsert (insert or update) vectors with an ID and metadata:
import { Pinecone } from '@pinecone-database/pinecone';

// Initialize Pinecone (example)
const pinecone = new Pinecone({ apiKey: "[PINECONE_API_KEY]" });
const index = pinecone.index("my-embeddings-index");

// Prepare vectors for upsert
const vectors = embeddings.results.map(r => ({
  id: `code-${r.index}`,
  values: r.embedding,
  metadata: { source: inputs[r.index] }
}));

// Upsert to Pinecone
await index.upsert(vectors);
Turbopuffer and other vector databases have similar APIs for inserting vectors.

Example: Semantic Search with a User Query

To search, embed the user query using the same API, then query the vector database for the most similar vectors (using cosine similarity or the database’s default metric):
// Embed the user query
const query = "How do I add two numbers?";
const queryData = {
  model: "relace-embed-v1",
  input: [query]
};

const queryResponse = await fetch(url, {
  method: "POST",
  headers: headers,
  body: JSON.stringify(queryData)
});

const queryResult = await queryResponse.json();
const queryEmbedding = queryResult.results[0].embedding;

// Query Pinecone for similar code
const results = await index.query({
  vector: queryEmbedding,
  topK: 5,
  includeMetadata: true
});

for (const match of results.matches) {
  console.log(`Score: ${match.score}, Source: ${match.metadata.source}`);
}
This will return the most relevant code snippets or documents from your database, ranked by similarity to the user query.For more details, see the Turbopuffer docs or Pinecone docs.