Skip to content

Commit b1c6a7a

Browse files
improved the starcoder code suggest prompt
1 parent 7d2d555 commit b1c6a7a

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

Codespace_Service/src/routes/aiRoutes.js

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ router.post("/suggest", async (req, res) => {
122122
typeof rawPrefix !== "string" ||
123123
rawPrefix.trim().length < 3
124124
) {
125+
console.log("Prefix too short, returning empty suggestion");
125126
return res.json({ suggestion: "" });
126127
}
127128

@@ -139,9 +140,27 @@ router.post("/suggest", async (req, res) => {
139140
process.env.NVIDIA_STARCODER_MODEL || "gemini"
140141
).toLowerCase();
141142

142-
const prompt = `You are a code autocomplete engine. Given a code prefix, return ONLY the next likely code continuation without repeating the prefix or explanations or prompts. Maintain indentation.\n\nLanguage: ${
143-
language || "auto"
144-
}\n\n<code_prefix>\n${prefix}\n</code_prefix>\n\n<continuation>`;
143+
const lastLine = prefix.split(/\r?\n/).pop() || "";
144+
145+
let prompt = prefix;
146+
147+
if (provider !== "gemini" && lastLine.trim().length < 60) {
148+
prompt = prefix;
149+
} else if (provider === "gemini") {
150+
// Gemini-specific prompt with clear instructions
151+
prompt = `Complete this code snippet with a short, focused continuation. Only provide the next logical part of the code, without additional explanations, comments, or console.log statements:
152+
153+
${prefix}`;
154+
}
155+
156+
const nvConfig =
157+
provider !== "gemini"
158+
? {
159+
// Starcoder specific settings
160+
stop: ["\n\n", "*/", "*/\n"], // Stop at double newlines or comment ends
161+
suffix: "", // No suffix needed
162+
}
163+
: {};
145164

146165
const maxNew = Math.max(1, Math.min(256, Number(max_tokens) || 32));
147166
const temp = Math.max(0, Math.min(1, Number(temperature) || 0.2));
@@ -154,7 +173,7 @@ router.post("/suggest", async (req, res) => {
154173
overallTimeoutMs
155174
);
156175

157-
if (provider === "nvidia" || provider === "starcoder") {
176+
if (provider !== "gemini") {
158177
const nvKey = process.env.NVIDIA_API_KEY;
159178
const baseUrl =
160179
process.env.NVIDIA_STARCODER_URL ||
@@ -166,9 +185,9 @@ router.post("/suggest", async (req, res) => {
166185
return res.status(500).json({ error: "NVIDIA API key not configured" });
167186
}
168187

169-
// Use OpenAI-compatible Completions endpoint on NVIDIA Integrate API
170188
try {
171189
const controller = overallController; // reuse overall timeout
190+
172191
const nvRes = await fetch(`${baseUrl.replace(/\/$/, "")}/completions`, {
173192
method: "POST",
174193
headers: {
@@ -181,6 +200,8 @@ router.post("/suggest", async (req, res) => {
181200
max_tokens: maxNew,
182201
temperature: temp,
183202
stream: false,
203+
// Add the stop sequences from nvConfig
204+
stop: nvConfig.stop,
184205
}),
185206
signal: controller.signal,
186207
});
@@ -201,13 +222,20 @@ router.post("/suggest", async (req, res) => {
201222
(data && Array.isArray(data.choices) && data.choices[0]?.text) || "";
202223

203224
let suggestion = String(generated || "").trim();
204-
// If suggestion starts with original prefix (hopefully wiill not happen), drop it
205-
if (suggestion.startsWith(prefix)) {
206-
suggestion = suggestion.slice(prefix.length);
225+
226+
const firstSemicolon = suggestion.indexOf(";");
227+
const firstNewline = suggestion.indexOf("\n");
228+
if (firstSemicolon > 0) {
229+
suggestion = suggestion.substring(0, firstSemicolon + 1);
230+
} else if (firstNewline > 0) {
231+
suggestion = suggestion.substring(0, firstNewline);
207232
}
208-
// Try to strip any echo of our continuation tg
209-
suggestion = suggestion.replace(/<\/continuation>.*/s, "").trim();
210233

234+
// Remove any comments
235+
suggestion = suggestion.replace(/\/\/.*$/gm, "");
236+
suggestion = suggestion.replace(/\/\*[\s\S]*?\*\//g, "");
237+
238+
console.log("Processed suggestion:", suggestion);
211239
return res.json({ suggestion });
212240
} catch (e) {
213241
clearTimeout(overallTimer);
@@ -239,7 +267,12 @@ router.post("/suggest", async (req, res) => {
239267
]);
240268

241269
clearTimeout(overallTimer);
242-
const suggestion = (text || "").trim();
270+
271+
// Enhanced post-processing
272+
let suggestion = (text || "").trim();
273+
suggestion = suggestion.replace(/```[\w]*\n?|```/g, "");
274+
suggestion = suggestion.replace(/^\s*=\s*/, "");
275+
243276
return res.json({ suggestion });
244277
} catch (e) {
245278
clearTimeout(overallTimer);

0 commit comments

Comments
 (0)