> ## Documentation Index
> Fetch the complete documentation index at: https://docs.morphllm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Embedding API

> Generate code and text embeddings with morph-embedding-v4 via OpenAI-compatible API

<Warning>
  **Planned for deprecation.** The Embedding API will be removed in a future release. For code search, use [WarpGrep](/api-reference/endpoint/warpgrep) instead. WarpGrep is a search agent that handles retrieval, ranking, and file reading in one call, replacing the need to manage embeddings, vector databases, and reranking pipelines yourself.
</Warning>

## Overview

Morph provides an OpenAI-compatible API for generating embeddings from code and text. State of the art on code retrieval tasks with our latest `morph-embedding-v4` model.

## Example Request

<CodeGroup>
  ```typescript embedding.ts theme={null}
  import { OpenAI } from 'openai';

  const client = new OpenAI({
  apiKey: 'YOUR_API_KEY',
  baseURL: 'https://api.morphllm.com/v1'
  });

  async function generateEmbeddings() {
  const response = await client.embeddings.create({
  model: "morph-embedding-v4",
  input: "function calculateSum(a, b) { return a + b; }"
  });

  return response.data[0].embedding;
  }

  ```

  ```python embedding.py theme={null}
  import openai

  client = openai.OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.morphllm.com/v1"
  )

  def generate_embeddings():
    response = client.embeddings.create(
      model="morph-embedding-v4",
      input="function calculateSum(a, b) { return a + b; }"
    )
    return response.data[0].embedding
  ```
</CodeGroup>

## Model Selection

We recommend using `morph-embedding-v4` for the best performance on code retrieval tasks. This model offers:

* **State-of-the-Art Performance**: Achieves SoTA results across all coding benchmarks for accuracy:speed ratio
* **1536 Dimensions**: Optimal dimensionality for rich semantic representation while maintaining efficiency
* **Unmatched Speed**: Fastest inference in the market - no embedding model comes close on accuracy:speed
* **Enhanced Context**: Superior handling of longer code snippets and complex codebases

## Input Format

The request accepts the following parameters:

| Parameter         | Type            | Required | Description                                                                                            |
| ----------------- | --------------- | -------- | ------------------------------------------------------------------------------------------------------ |
| `model`           | string          | Yes      | The model ID to use for embedding generation. Use `morph-embedding-v4` (latest).                       |
| `input`           | string or array | Yes      | The text to generate embeddings for. Can be a string or an array of strings.                           |
| `encoding_format` | string          | No       | The format in which the embeddings are returned. Options are `float` and `base64`. Default is `float`. |

## Batch Processing Example

```python theme={null}
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.morphllm.com/v1"
)

# Example with batch inputs
code_snippets = [
    "function add(a, b) { return a + b; }",
    "class User { constructor(name) { this.name = name; } }",
    "import pandas as pd\ndf = pd.read_csv('data.csv')"
]

response = client.embeddings.create(
    model="morph-embedding-v4",
    input=code_snippets
)

# Access embeddings for each input
for i, embedding_data in enumerate(response.data):
    embedding = embedding_data.embedding
    print(f"Embedding for snippet {i+1}: {len(embedding)} dimensions")
```

## Response Format

```json theme={null}
{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "embedding": [0.0023064255, -0.009327292, ...],
      "index": 0
    }
  ],
  "model": "morph-embedding-v4",
  "usage": {
    "prompt_tokens": 8,
    "total_tokens": 8
  }
}
```

When multiple inputs are provided, the response includes embeddings for each input:

```json theme={null}
{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "embedding": [0.0023064255, -0.009327292, ...],
      "index": 0
    },
    {
      "object": "embedding",
      "embedding": [0.0103662554, -0.007650322, ...],
      "index": 1
    },
    {
      "object": "embedding",
      "embedding": [0.0183664255, -0.002327742, ...],
      "index": 2
    }
  ],
  "model": "morph-embedding-v4",
  "usage": {
    "prompt_tokens": 24,
    "total_tokens": 24
  }
}
```

## Usage with Vector Databases

Embeddings can be stored in vector databases for efficient similarity searching:

```python theme={null}
# Example with Pinecone
import pinecone
from openai import OpenAI

# Initialize clients
openai_client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.morphllm.com/v1"
)
pinecone.init(api_key="your-pinecone-api-key", environment="your-environment")
index = pinecone.Index("code-embeddings")

# Generate embedding for a code snippet
code_snippet = "def calculate_factorial(n):\n    if n == 0:\n        return 1\n    else:\n        return n * calculate_factorial(n-1)"
response = openai_client.embeddings.create(
    model="morph-embedding-v4",
    input=code_snippet
)
embedding = response.data[0].embedding

# Store in Pinecone
index.upsert([
    ("snippet-1", embedding, {"snippet": code_snippet})
])

# Search for similar code
results = index.query(
    vector=embedding,
    top_k=5,
    include_metadata=True
)
```
