POST
/
v1
/
embeddings
cURL
curl --request POST \
  --url https://api.morphllm.com/v1/embeddings \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '{
  "model": "morph-embedding-v3",
  "input": "function calculateSum(a, b) { return a + b; }",
  "encoding_format": "float"
}'
{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "embedding": [
        0.0023064255,
        -0.009327292,
        0.01183665
      ],
      "index": 0
    }
  ],
  "model": "morph-embedding-v3",
  "usage": {
    "prompt_tokens": 8,
    "total_tokens": 8
  }
}

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-v3 model.

Example Request

import { OpenAI } from 'openai';

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

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

return response.data[0].embedding;
}

Model Selection

We recommend using morph-embedding-v3 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
  • 1024 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
For backward compatibility, morph-embedding-v3 remains available.

Input Format

The request accepts the following parameters:
ParameterTypeRequiredDescription
modelstringYesThe model ID to use for embedding generation. Use morph-embedding-v3 (latest) or morph-embedding-v3.
inputstring or arrayYesThe text to generate embeddings for. Can be a string or an array of strings.
encoding_formatstringNoThe format in which the embeddings are returned. Options are float and base64. Default is float.

Batch Processing Example

from openai import OpenAI

client = OpenAI(
    api_key="your-morph-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-v3",
    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

{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "embedding": [0.0023064255, -0.009327292, ...],
      "index": 0
    }
  ],
  "model": "morph-embedding-v3",
  "usage": {
    "prompt_tokens": 8,
    "total_tokens": 8
  }
}
When multiple inputs are provided, the response includes embeddings for each input:
{
  "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-v3",
  "usage": {
    "prompt_tokens": 24,
    "total_tokens": 24
  }
}

Usage with Vector Databases

Embeddings can be stored in vector databases for efficient similarity searching:
# Example with Pinecone
import pinecone
from openai import OpenAI

# Initialize clients
openai_client = OpenAI(
    api_key="your-morph-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-v3",
    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
)

Authorizations

Authorization
string
header
required

Bearer authentication header of the form Bearer <token>, where <token> is your auth token.

Body

application/json

Embedding generation request

The body is of type object.

Response

200
application/json

Embedding response

The response is of type object.