Pass a string to input to embed a single piece of text:
Copy
import { Tracia } from 'tracia';const tracia = new Tracia({ apiKey: process.env.TRACIA_API_KEY });const result = await tracia.runEmbedding({ model: 'text-embedding-3-small', input: 'What is the meaning of life?',});console.log(result.embeddings[0].values.length); // 1536console.log(result.embeddings[0].index); // 0console.log(result.usage.totalTokens); // 8
Not all models support the dimensions parameter. Currently, OpenAI’s text-embedding-3-small and text-embedding-3-large, and Google’s text-embedding-004 support it.
Sessions automatically chain embedding spans with other spans under the same trace:
Copy
const session = tracia.createSession();// First: generate an embeddingconst embeddingResult = await session.runEmbedding({ model: 'text-embedding-3-small', input: 'What is quantum computing?',});// Second: use the embedding context in a completionconst completionResult = await session.runLocal({ model: 'gpt-4o', messages: [ { role: 'user', content: 'Explain the concept I just embedded.' }, ],});// Both spans are linked under the same trace in the dashboardconsole.log(session.getTraceId());
The session manages traceId and parentSpanId automatically, so all spans appear in sequence in the Tracia dashboard.