翻篇收录说明
翻篇网页快照

Embeddings API - Generate Vector Embeddings from Text and Images

https://openrouter.ai/docs/api_reference/embeddings

最近抓取:2026-12-04 13:27:43(北京时间)

最近内容变化:2026-12-04 13:27:43(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Generate vector embeddings from text and images

正文摘录

Embeddings are numerical representations of text that capture semantic meaning. They convert text into vectors (arrays of numbers) that can be used for various machine learning tasks. OpenRouter provides a unified API to access embedding models from multiple providers. What are embeddings? Embeddings transform text into high-dimensional vectors where semantically similar texts are positioned closer together in vector space. For example, “cat” and “kitten” would have similar embeddings, while “cat” and “airplane” would be far apart. These vector representations enable machines to understand relationships between pieces of text, making them essential for many AI applications. Common use cases Embeddings are used in a wide variety of applications: RAG (Retrieval-Augmented Generation): Build RAG systems that retrieve relevant context from a knowledge base before generating answers. Embeddings help find the most relevant documents to include in the LLM’s context. Semantic Search: Convert documents and queries into embeddings, then find the most relevant documents by comparing vector similarity. This provides more accurate results than traditional keyword matching because it understands meaning rather than just matching words. Recommendation Systems: Generate embeddings for items (products, articles, movies) and user preferences to recommend similar items. By comparing embedding vectors, you can find items that are semantically related even if they don’t share obvious keywords. Clustering and Classification: Group similar documents together or classify text into categories by analyzing embedding patterns. Documents with similar embeddings likely belong to the same topic or category. Duplicate Detection: Identify duplicate or near-duplicate content by comparing embedding similarity. This works even when text is paraphrased or reworded. Anomaly Detection: Detect unusual or outlier content by identifying embeddings that are far from typical patterns in your dataset. How to use embeddings Basic request To generate embeddings, send a POST request to /embeddings with your text input and chosen model: TypeScript SDKPythonTypeScript (fetch)ShellGoimport { OpenRouter } from '@openrouter/sdk'; const openRouter = new OpenRouter({ apiKey: '<OPENROUTER_API_KEY>', }); const response = await openRouter.embeddings.generate({ requestBody: { model: 'openai/text-embedding-3-small', input: 'The quick brown fox jumps over the lazy dog', }, }); console.log(response.data[0].embedding); Output[ -0.0208282470703125, -0.01690673828125, -0.0045318603515625, -0.05084228515625, -0.02593994140625, 0.0295867919921875, 0.029998779296875, 0.0290679931640625, 0.010589599609375, -0.024383544921875, -0.0093536376953125, 0.00662994384765625, -0.0260467529296875, 0.0347900390625, -0.0165557861328125, 0.00022661685943603516, -0.00965118408203125, -0.055572509765625, -0.0379638671875, -0.024017333984375, -0.0213775634765625, 0.0030517578125, 0.01099395751953125, -0.00490570068359375, -0.0007491111755371094, -0.00244140625, 0.0113983154296875, -0.031524658203125, 0.010772705078125, -0.0010700225830078125, 0.0298919677734375, -0.031097412109375, -0.01044464111328125, -0.061370849609375, -0.0149993896484375, -0.03729248046875, -0.0293731689453125, -0.0111541748046875, -0.0218353271484375, -0.004467010498046875, 0.0037364959716796875, -0.06390380859375, 0.03448486328125, -0.050872802734375, 0.06536865234375, 0.035797119140625, 0.029998779296875, -0.028961181640625, 0.00821685791015625, 0.043914794921875, -0.0123138427734375, 0.03131103515625, -0.0292510986328125, 0.01812744140625, -0.002674102783203125, 0.0016508102416992188, 0.0313720703125, 0.022430419921875, -0.0292816162109375, -0.089599609375, 0.036529541015625, -0.004489898681640625, 0.0089263916015625, -0.01013946533203125, -0.0112152099609375, -0.01397705078125, -0.0030994415283203125, -0.02301025390625, 0.019378662109375, -0.02777099609375, 0.02362060546875, 0.0185699462890625, -0.031585693359375, 0.03509521484375, 0.0489501953125, 0.02294921875, 0.00844573974609375, 0.01560211181640625, 0.0163726806640625, 0.046661376953125, 0.00504302978515625, 0.0225830078125, -0.008209228515625, -0.06842041015625, -0.00664520263671875, 0.048553466796875, -0.049591064453125, 0.0291900634765625, 0.0301055908203125, -0.03814697265625, 0.06549072265625, 0.038330078125, -0.0211029052734375, -0.044464111328125, 0.05279541015625, -0.00145721435546875, 0.01337432861328125, 0.019317626953125, 0.043701171875, 0.0234222412109375, ... 1436 more items ] Batch processing You can generate embeddings for multiple texts in a single request by passing an array of strings: TypeScript SDKPythonTypeScript (fetch)ShellGoimport { OpenRouter } from '@openrouter/sdk'; const openRouter = new OpenRouter({ apiKey: '<OPENROUTER_API_KEY>', }); const response = await openRouter.embeddings.generate({ requestBody: { model: 'openai/text-embedding-3-small', input: [ 'Machine learning is a subset of artificial intelligence', 'Deep learning uses neural networks with multiple layers', 'Natural language processing enables computers to understand text' ], }, }); // Process each embedding response.data.forEach((item, index) => { console.log(`Embedding ${index}: ${item.embedding.length} dimensions`); }); OutputEmbedding 0: 1536 dimensions Embedding 1: 1536 dimensions Embedding 2: 1536 dimensions Image input Some embedding models support image inputs, enabling multimodal embeddings that capture visual content alongside text. This is useful for image search, visual similarity, and cross-modal retrieval tasks. To send an image, wrap your input in the multimodal format with a content array containing image_url objects. You can also combine text and images in a single input block. PythonTypeScript (fetch)ShellGoimport requests response = requests.post( "https://openrouter.ai/api/v1/embeddings", headers={ "Authorization": f"Bearer <OPENROUTER_API_KEY>", "Content-Type": "application/json", }, json={ "model": "google/gemini-embedding-2-preview", "input": [ { "content": [ {"type": "image_url", "image_url": {"url": "https://storage.googleapis.com/cloud-samples-data/generative-ai/image/scones.jpg"}} ] } ], "encoding_format": "float", } ) data = response.json() if not data.get("data"): raise RuntimeError(f"No embeddings returned: {data}") embedding = data["data"][0]["embedding"] print(f"Embedding dimension: {len(embedding)}") print(embedding[:5]) See all 28 lines OutputEmbedding dimension: 3072 [ -0.016795406, -0.015404839, -0.00029555237, 0.011511302, -0.0063522956 ] You can also combine text and images in a single input to generate a joint embedding: PythonTypeScript (fetch)ShellGoimport requests response = requests.post( "https://openrouter.ai/api/v1/embeddings", headers={ "Authorization": f"Bearer <OPENROUTER_API_KEY>", "Content-Type": "application/json", }, json={ "model": "google/gemini-embedding-2-preview", "input": [ { "content": [ {"type": "text", "text": "A scenic boardwalk through a green meadow"}, {"type": "image_url", "image_url": {"url": "https://storage.googleapis.com/cloud-samples-data/generative-ai/image/scones.jpg"}} ] } ], "encoding_format": "float", } ) data = response.json() embedding = data["data"][0]["embedding"] print(f"Embedding dimension: {len(embedding)}") See all 25 lines OutputEmbedding dimension: 3072 Contextual embeddings Some embedding models embed every chunk of a document with the rest of that document as context, so each chunk’s vector carries the meaning of the document it came from. This improves retrieval when chunks are short or ambiguous on their own, such as a paragraph that refers to “it” or “the company” without naming the subject. To use contextual mode, send input as an array of documents, where each document is an ordered array of chunk strings, and set input_type to "document". You get one embedding back per chunk, in document order. PythonTypeScript (fetch)ShellGoimport requests response = requests.post( "https://openrouter.ai/api/v1/embeddings", headers={ "Authorization": "Bearer <OPENROUTER_API_KEY>", "Content-Type": "application/json", }, json={ "model": "perplexity/pplx-embed-v2-context-9b", "input_type": "document", "input": [ ["Paris is the capital of France.", "It sits on the Seine.", "Its population is about two million."], ["Berlin is the capital of Germany."] ] } ) data = response.json() for item in data["data"]: print(item["document_index"], item["index"], len(item["embedding"])) Output0 0 2048 0 1 2048 0 2 2048 1 3 2048 Response shape The response is the same flat data array as every other embeddings request, with one extra field. index counts across all chunks in the request, and document_index is the position of the chunk’s document in your input: { "object": "list", "model": "perplexity/pplx-embed-v2-context-9b", "data": [ { "object": "embedding", "index": 0, "document_index": 0, "embedding": [...] }, { "object": "embedding", "index": 1, "document_index": 0, "embedding": [...] }, { "object": "embedding", "index": 2, "document_index": 0, "embedding": [...] }, { "object": "embedding", "index": 3, "document_index": 1, "embedding": [...] } ], "usage": { "prompt_tokens": 33, "total_tokens": 33 } } Providers that offer contextual embeddings natively, such as Perplexity, return a nested response: one list per document, each with its own data array and a chunk index that restarts at 0 in every document. OpenRouter flattens that into the single data array that existing embeddings clients expect. If you are porting code from a provider’s SDK, read document_index instead of nesting, and treat index as global rather than per document. Chunks of the same document are contiguous and in the order you sent them, so grouping is a one-liner: TypeScriptGocURLby_document = {} for item in data["data"]: by_document.setdefault(item["document_index"], []).append(item["embedding"]) Querying a contextual index Embed search queries as a plain string or string array with input_type: "query". Queries have no document context, so th