-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
edit.ts
303 lines (245 loc) · 9.16 KB
/
edit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { ChatMessage, PromptTemplate } from "../../index.js";
const simplifiedEditPrompt = `Consider the following code:
\`\`\`{{{language}}}
{{{codeToEdit}}}
\`\`\`
Edit the code to perfectly satisfy the following user request:
{{{userInput}}}
Output nothing except for the code. No code block, no English explanation, no start/end tags.`;
const simplestEditPrompt = `Here is the code before editing:
\`\`\`{{{language}}}
{{{codeToEdit}}}
\`\`\`
Here is the edit requested:
"{{{userInput}}}"
Here is the code after editing:`;
const gptEditPrompt: PromptTemplate = (_, otherData) => {
if (otherData?.codeToEdit?.trim().length === 0) {
return `\
\`\`\`${otherData.language}
${otherData.prefix}[BLANK]${otherData.codeToEdit}${otherData.suffix}
\`\`\`
Above is the file of code that the user is currently editing in. Their cursor is located at the "[BLANK]". They have requested that you fill in the "[BLANK]" with code that satisfies the following request:
"${otherData.userInput}"
Please generate this code. Your output will be only the code that should replace the "[BLANK]", without repeating any of the prefix or suffix, without any natural language explanation, and without messing up indentation. Here is the code that will replace the "[BLANK]":`;
}
const paragraphs = [
"The user has requested a section of code in a file to be rewritten.",
];
if (otherData.prefix?.trim().length > 0) {
paragraphs.push(`This is the prefix of the file:
\`\`\`${otherData.language}
${otherData.prefix}
\`\`\``);
}
if (otherData.suffix?.trim().length > 0) {
paragraphs.push(`This is the suffix of the file:
\`\`\`${otherData.language}
${otherData.suffix}
\`\`\``);
}
paragraphs.push(`This is the code to rewrite:
\`\`\`${otherData.language}
${otherData.codeToEdit}
\`\`\`
The user's request is: "${otherData.userInput}"
Here is the rewritten code:`);
return paragraphs.join("\n\n");
};
const codellamaInfillEditPrompt = "{{filePrefix}}<FILL>{{fileSuffix}}";
const START_TAG = "<START EDITING HERE>";
const osModelsEditPrompt: PromptTemplate = (history, otherData) => {
// "No sufix" means either there is no suffix OR
// it's a clean break at end of function or something
// (what we're trying to avoid is just the language model trying to complete the closing brackets of a function or something)
const firstCharOfFirstLine = otherData.suffix?.split("\n")[0]?.[0]?.trim();
const isSuffix =
otherData.suffix?.trim() !== "" &&
// First character of first line is whitespace
// Otherwise we assume it's a clean break
!firstCharOfFirstLine;
const suffixTag = isSuffix ? "<STOP EDITING HERE>" : "";
const suffixExplanation = isSuffix
? ' When you get to "<STOP EDITING HERE>", end your response.'
: "";
// If neither prefilling nor /v1/completions are supported, we have to use a chat prompt without putting words in the model's mouth
if (
otherData.supportsCompletions !== "true" &&
otherData.supportsPrefill !== "true"
) {
return gptEditPrompt(history, otherData);
}
// Use a different prompt when there's neither prefix nor suffix
if (otherData.prefix?.trim() === "" && otherData.suffix?.trim() === "") {
return [
{
role: "user",
content: `\`\`\`${otherData.language}
${otherData.codeToEdit}
${suffixTag}
\`\`\`
Please rewrite the entire code block above in order to satisfy the following request: "${otherData.userInput}". You should rewrite the entire code block without leaving placeholders, even if the code is the same as before.${suffixExplanation}`,
},
{
role: "assistant",
content: `Sure! Here's the entire rewritten code block:
\`\`\`${otherData.language}
`,
},
];
}
return [
{
role: "user",
content: `\`\`\`${otherData.language}
${otherData.prefix}${START_TAG}
${otherData.codeToEdit}
${suffixTag}
\`\`\`
Please rewrite the entire code block above, editing the portion below "${START_TAG}" in order to satisfy the following request: "${otherData.userInput}". You should rewrite the entire code block without leaving placeholders, even if the code is the same as before.${suffixExplanation}
`,
},
{
role: "assistant",
content: `Sure! Here's the entire code block, including the rewritten portion:
\`\`\`${otherData.language}
${otherData.prefix}${START_TAG}
`,
},
];
};
const mistralEditPrompt = `[INST] You are a helpful code assistant. Your task is to rewrite the following code with these instructions: "{{{userInput}}}"
\`\`\`{{{language}}}
{{{codeToEdit}}}
\`\`\`
Just rewrite the code without explanations: [/INST]
\`\`\`{{{language}}}`;
const alpacaEditPrompt = `Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction: Rewrite the code to satisfy this request: "{{{userInput}}}"
### Input:
\`\`\`{{{language}}}
{{{codeToEdit}}}
\`\`\`
### Response:
Sure! Here's the code you requested:
\`\`\`{{{language}}}
`;
const phindEditPrompt = `### System Prompt
You are an expert programmer and write code on the first attempt without any errors or fillers.
### User Message:
Rewrite the code to satisfy this request: "{{{userInput}}}"
\`\`\`{{{language}}}
{{{codeToEdit}}}
\`\`\`
### Assistant:
Sure! Here's the code you requested:
\`\`\`{{{language}}}
`;
const deepseekEditPrompt = `### System Prompt
You are an AI programming assistant, utilizing the DeepSeek Coder model, developed by DeepSeek Company, and you only answer questions related to computer science. For politically sensitive questions, security and privacy issues, and other non-computer science questions, you will refuse to answer.
### Instruction:
Rewrite the code to satisfy this request: "{{{userInput}}}"
\`\`\`{{{language}}}
{{{codeToEdit}}}
\`\`\`<|EOT|>
### Response:
Sure! Here's the code you requested:
\`\`\`{{{language}}}
`;
const zephyrEditPrompt = `<|system|>
You are an expert programmer and write code on the first attempt without any errors or fillers.</s>
<|user|>
Rewrite the code to satisfy this request: "{{{userInput}}}"
\`\`\`{{{language}}}
{{{codeToEdit}}}
\`\`\`</s>
<|assistant|>
Sure! Here's the code you requested:
\`\`\`{{{language}}}
`;
const openchatEditPrompt = `GPT4 Correct User: You are an expert programmer and personal assistant. You are asked to rewrite the following code in order to {{{userInput}}}.
\`\`\`{{{language}}}
{{{codeToEdit}}}
\`\`\`
Please only respond with code and put it inside of a markdown code block. Do not give any explanation, but your code should perfectly satisfy the user request.<|end_of_turn|>GPT4 Correct Assistant: Sure thing! Here is the rewritten code that you requested:
\`\`\`{{{language}}}
`;
const xWinCoderEditPrompt = `<system>: You are an AI coding assistant that helps people with programming. Write a response that appropriately completes the user's request.
<user>: Please rewrite the following code with these instructions: "{{{userInput}}}"
\`\`\`{{{language}}}
{{{codeToEdit}}}
\`\`\`
Just rewrite the code without explanations:
<AI>:
\`\`\`{{{language}}}`;
const neuralChatEditPrompt = `### System:
You are an expert programmer and write code on the first attempt without any errors or fillers.
### User:
Rewrite the code to satisfy this request: "{{{userInput}}}"
\`\`\`{{{language}}}
{{{codeToEdit}}}
\`\`\`
### Assistant:
Sure! Here's the code you requested:
\`\`\`{{{language}}}
`;
const codeLlama70bEditPrompt = `<s>Source: system\n\n You are an expert programmer and write code on the first attempt without any errors or fillers. <step> Source: user\n\n Rewrite the code to satisfy this request: "{{{userInput}}}"
\`\`\`{{{language}}}
{{{codeToEdit}}}
\`\`\` <step> Source: assistant\nDestination: user\n\n `;
const claudeEditPrompt: PromptTemplate = (
history: ChatMessage[],
otherData: Record<string, string>,
) => [
{
role: "user",
content: `\
\`\`\`${otherData.language}
${otherData.codeToEdit}
\`\`\`
You are an expert programmer. You will rewrite the above code to do the following:
${otherData.userInput}
Output only a code block with the rewritten code:
`,
},
{
role: "assistant",
content: `Sure! Here is the rewritten code:
\`\`\`${otherData.language}`,
},
];
const llama3EditPrompt: PromptTemplate = `<|begin_of_text|><|start_header_id|>user<|end_header_id|>
\`\`\`{{{language}}}
{{{codeToEdit}}}
\`\`\`
Rewrite the above code to satisfy this request: "{{{userInput}}}"<|eot_id|><|start_header_id|>assistant<|end_header_id|>
Sure! Here's the code you requested:
\`\`\`{{{language}}}`;
const gemmaEditPrompt = `<start_of_turn>user
You are an expert programmer and write code on the first attempt without any errors or fillers. Rewrite the code to satisfy this request: "{{{userInput}}}"
\`\`\`{{{language}}}
{{{codeToEdit}}}
\`\`\`<end_of_turn>
<start_of_turn>model
Sure! Here's the code you requested:
\`\`\`{{{language}}}
`;
export {
alpacaEditPrompt,
claudeEditPrompt,
codeLlama70bEditPrompt,
codellamaInfillEditPrompt,
deepseekEditPrompt,
gemmaEditPrompt,
gptEditPrompt,
llama3EditPrompt,
mistralEditPrompt,
neuralChatEditPrompt,
openchatEditPrompt,
osModelsEditPrompt,
phindEditPrompt,
simplestEditPrompt,
simplifiedEditPrompt,
xWinCoderEditPrompt,
zephyrEditPrompt,
};