Fast Apply
Apply changes from big models into your files. Find your API key.
curl --request POST \
--url https://api.morphllm.com/v1/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "morph-v3-large",
"messages": [
{
"role": "user",
"content": "<code>def calculate_total(items):\n total = 0\n for item in items:\n total += item.price\n return total</code>\n<update>def calculate_total(items):\n total = 0\n for item in items:\n total += item.price\n return total * 1.1 # Add 10% tax</update>"
}
]
}'
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\ndef calculate_total(items):\n total = 0\n for item in items:\n total += item.price\n return total * 1.1 # Add 10% tax\n"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 25,
"completion_tokens": 32,
"total_tokens": 57
}
}
API Endpoint
Morph uses a standard OpenAI-compatible API.
https://api.morphllm.com/v1/chat/completions
Model | Speed | Accuracy |
---|---|---|
morph-v3-fast | 4500+ tok/sec | 96% |
morph-v3-large | 2500+ tok/sec | 98% |
auto | 2500-4500 tok/sec | ~98% |
auto
will intelligently route your request to the right model based on the complexity of your edit.
Example Request
import { OpenAI } from 'openai';
const client = new OpenAI({
apiKey: 'your-api-key',
baseURL: 'https://api.morphllm.com/v1'
});
async function applyCodeUpdate(originalCode: string, updateSnippet: string) {
const response = await client.chat.completions.create({
model: "morph-v3-large",
messages: [
{
role: "user",
content: `<code>${originalCode}</code>\n<update>${updateSnippet}</update>`
}
],
stream: true
});
return response.choices[0].message.content;
}
Tool Definition
Tool Description:
Use this tool to propose an edit to an existing file.
This will be read by a less intelligent model, which will quickly apply the edit. You should make it clear what the edit is, while also minimizing the unchanged code you write.
When writing the edit, you should specify each edit in sequence, with the special comment // ... existing code ... to represent unchanged code in between edited lines.
For example:
// ... existing code ...
FIRST_EDIT
// ... existing code ...
SECOND_EDIT
// ... existing code ...
THIRD_EDIT
// ... existing code ...
You should still bias towards repeating as few lines of the original file as possible to convey the change.
But, each edit should contain sufficient context of unchanged lines around the code you're editing to resolve ambiguity.
DO NOT omit spans of pre-existing code (or comments) without using the // ... existing code ... comment to indicate its absence. If you omit the existing code comment, the model may inadvertently delete these lines.
If you plan on deleting a section, you must provide context before and after to delete it. If the initial code is ```code \n Block 1 \n Block 2 \n Block 3 \n code```, and you want to remove Block 2, you would output ```// ... existing code ... \n Block 1 \n Block 3 \n // ... existing code ...```.
Make sure it is clear what the edit should be, and where it should be applied.
ALWAYS make all edits to a file in a single edit_file instead of multiple edit_file calls to the same file. The apply model can handle many distinct edits at once.
Parameters:
target_file
(string, required): The target file to modifycode_edit
(string, required): Specify ONLY the precise lines of code that you wish to edit. Use// ... existing code ...
for unchanged sections.
Tool Description:
Use this tool to propose an edit to an existing file.
This will be read by a less intelligent model, which will quickly apply the edit. You should make it clear what the edit is, while also minimizing the unchanged code you write.
When writing the edit, you should specify each edit in sequence, with the special comment // ... existing code ... to represent unchanged code in between edited lines.
For example:
// ... existing code ...
FIRST_EDIT
// ... existing code ...
SECOND_EDIT
// ... existing code ...
THIRD_EDIT
// ... existing code ...
You should still bias towards repeating as few lines of the original file as possible to convey the change.
But, each edit should contain sufficient context of unchanged lines around the code you're editing to resolve ambiguity.
DO NOT omit spans of pre-existing code (or comments) without using the // ... existing code ... comment to indicate its absence. If you omit the existing code comment, the model may inadvertently delete these lines.
If you plan on deleting a section, you must provide context before and after to delete it. If the initial code is ```code \n Block 1 \n Block 2 \n Block 3 \n code```, and you want to remove Block 2, you would output ```// ... existing code ... \n Block 1 \n Block 3 \n // ... existing code ...```.
Make sure it is clear what the edit should be, and where it should be applied.
ALWAYS make all edits to a file in a single edit_file instead of multiple edit_file calls to the same file. The apply model can handle many distinct edits at once.
Parameters:
target_file
(string, required): The target file to modifycode_edit
(string, required): Specify ONLY the precise lines of code that you wish to edit. Use// ... existing code ...
for unchanged sections.
Tool Definition:
{
"name": "edit_file",
"description": "Use this tool to propose an edit to an existing file.\n\nThis will be read by a less intelligent model, which will quickly apply the edit. You should make it clear what the edit is, while also minimizing the unchanged code you write.\nWhen writing the edit, you should specify each edit in sequence, with the special comment // ... existing code ... to represent unchanged code in between edited lines.\n\nFor example:\n\n// ... existing code ...\nFIRST_EDIT\n// ... existing code ...\nSECOND_EDIT\n// ... existing code ...\nTHIRD_EDIT\n// ... existing code ...\n\nYou should still bias towards repeating as few lines of the original file as possible to convey the change.\nBut, each edit should contain sufficient context of unchanged lines around the code you're editing to resolve ambiguity.\nDO NOT omit spans of pre-existing code (or comments) without using the // ... existing code ... comment to indicate its absence. If you omit the existing code comment, the model may inadvertently delete these lines.\nIf you plan on deleting a section, you must provide context before and after to delete it. If the initial code is ```code \\n Block 1 \\n Block 2 \\n Block 3 \\n code```, and you want to remove Block 2, you would output ```// ... existing code ... \\n Block 1 \\n Block 3 \\n // ... existing code ...```.\nMake sure it is clear what the edit should be, and where it should be applied.\nALWAYS make all edits to a file in a single edit_file instead of multiple edit_file calls to the same file. The apply model can handle many distinct edits at once.",
"parameters": {
"properties": {
"target_file": {
"type": "string",
"description": "The target file to modify."
},
"code_edit": {
"type": "string",
"description": "Specify ONLY the precise lines of code that you wish to edit. Use // ... existing code ... for unchanged sections."
}
},
"required": ["target_file", "code_edit"]
}
}
Example Good Edit Pattern:
// ... existing code ...
function calculateTotal(items) {
// ... existing code ...
for item in items:
total += item.price;
return total * 1.1; // Add 10% tax
}
// ... existing code ...
const total, setTotal = useState(0);
useEffect(() => {
setTotal(calculateTotal(items));
}, [items]);
// ... existing code ...
return <div>Total: {total}</div>;
Input Format
The content
field in your message should contain two parts:
- Original code wrapped in
<code>...</code>
tags - Update snippet wrapped in
<update>...</update>
tags
The update snippet should use truncation markers like // ... existing code ...
to indicate unchanged parts of the code. Your system prompt should include instructions on how to handle these markers.
Read more about how to handle prompting here.
Edit File Tool Prompt
Use this tool to propose an edit to an existing file.
This will be read by a less intelligent model, which will quickly apply the edit. You should make it clear what the edit is, while also minimizing the unchanged code you write.
When writing the edit, you should specify each edit in sequence, with the special comment // ... existing code ... to represent unchanged code in between edited lines.
For example:
// ... existing code ...
FIRST_EDIT
// ... existing code ...
SECOND_EDIT
// ... existing code ...
THIRD_EDIT
// ... existing code ...
This will be read by a less intelligent model, which will quickly apply the edit. You should make it clear what the edit is, while also minimizing the unchanged code you write.
When writing the edit, you should specify each edit in sequence, with the special comment // ... existing code ... to represent unchanged code in between edited lines.
You should bias towards repeating as few lines of the original file as possible to convey the change.
NEVER show unmodified code in the edit, unless sufficient context of unchanged lines around the code you're editing is needed to resolve ambiguity.
If you plan on deleting a section, you must provide surrounding context to indicate the deletion.
DO NOT omit spans of pre-existing code without using the // ... existing code ... comment to indicate its absence.`,
You should specify the following arguments before the others: [target_file]
Example with Python
import openai
import os
USER_PROMPT = """<code>{original_code}</code>
<update>{update_snippet}</update>"""
client = openai.OpenAI(
api_key=os.getenv("MORPH_API_KEY"),
base_url="https://api.morphllm.com/v1"
)
def execute_query(original_code: str, update_snippet: str) -> str:
response = client.chat.completions.create(
model="morph-v3-large",
messages=[
{
"role": "user",
"content": USER_PROMPT.format(
original_code=original_code,
update_snippet=update_snippet
)
}
]
)
return response.choices[0].message.content
Response Format
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "\ndef calculate_total(items):\n total = 0\n for item in items:\n total += item.price\n return total * 1.1 # Add 10% tax\n"
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 25,
"completion_tokens": 32,
"total_tokens": 57
}
}
Authorizations
Bearer authentication header of the form Bearer <token>
, where <token>
is your auth token.
Body
Code update request
The body is of type object
.
Response
Chat completion response
The response is of type object
.
curl --request POST \
--url https://api.morphllm.com/v1/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "morph-v3-large",
"messages": [
{
"role": "user",
"content": "<code>def calculate_total(items):\n total = 0\n for item in items:\n total += item.price\n return total</code>\n<update>def calculate_total(items):\n total = 0\n for item in items:\n total += item.price\n return total * 1.1 # Add 10% tax</update>"
}
]
}'
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\ndef calculate_total(items):\n total = 0\n for item in items:\n total += item.price\n return total * 1.1 # Add 10% tax\n"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 25,
"completion_tokens": 32,
"total_tokens": 57
}
}