Skip to content

Commit 36a541f

Browse files
sounmindclaude
andcommitted
feat: add approach analysis function to OpenAI utils
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Co-Authored-By: soobing <https://github.com/soobing>
1 parent 10fbc14 commit 36a541f

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

utils/openai.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,81 @@ ${fileContent}
173173
description: typeof parsed.description === "string" ? parsed.description : "",
174174
};
175175
}
176+
177+
/**
178+
* 솔루션 코드가 의도된 알고리즘 접근법과 일치하는지 분석
179+
*
180+
* @param {string} fileContent - 분석할 소스 코드 내용
181+
* @param {string} problemName - 문제 이름 (폴더명)
182+
* @param {{difficulty: string, categories: string[], intended_approach: string}} problemInfo - 문제 메타 정보
183+
* @param {string} apiKey - OpenAI API 키
184+
* @returns {Promise<{matches: boolean, explanation: string}>}
185+
*/
186+
export async function generateApproachAnalysis(fileContent, problemName, problemInfo, apiKey) {
187+
const systemPrompt = `You are an algorithm analysis expert. Determine if code matches the intended approach.
188+
189+
Respond with a JSON object in this exact format:
190+
{
191+
"matches": true or false,
192+
"explanation": "한국어로 1문장, 80자 이내"
193+
}
194+
195+
Rules:
196+
- matches=true if the core data structure or algorithm matches the intended approach (does not need to be identical)
197+
- matches=false if brute force was used when an optimized approach was intended
198+
- Keep explanation to 1 sentence in Korean, 80 characters or fewer`;
199+
200+
const truncatedContent = fileContent.slice(0, 15000);
201+
202+
const userPrompt = `# 문제 이름
203+
${problemName}
204+
205+
# 문제 정보
206+
- 난이도: ${problemInfo.difficulty}
207+
- 카테고리: ${(problemInfo.categories || []).join(", ")}
208+
- 의도된 접근법: ${problemInfo.intended_approach}
209+
210+
# 소스 코드
211+
\`\`\`
212+
${truncatedContent}
213+
\`\`\`
214+
215+
위 코드가 의도된 접근법과 일치하는지 분석해주세요.`;
216+
217+
const response = await fetch("https://api.openai.com/v1/chat/completions", {
218+
method: "POST",
219+
headers: {
220+
Authorization: `Bearer ${apiKey}`,
221+
"Content-Type": "application/json",
222+
},
223+
body: JSON.stringify({
224+
model: "gpt-4.1-nano",
225+
messages: [
226+
{ role: "system", content: systemPrompt },
227+
{ role: "user", content: userPrompt },
228+
],
229+
response_format: { type: "json_object" },
230+
max_tokens: 200,
231+
temperature: 0.2,
232+
}),
233+
});
234+
235+
if (!response.ok) {
236+
const error = await response.text();
237+
throw new Error(`OpenAI API error: ${error}`);
238+
}
239+
240+
const data = await response.json();
241+
const content = data.choices[0]?.message?.content;
242+
243+
if (!content) {
244+
throw new Error("Empty response from OpenAI");
245+
}
246+
247+
const parsed = JSON.parse(content);
248+
249+
return {
250+
matches: parsed.matches === true,
251+
explanation: typeof parsed.explanation === "string" ? parsed.explanation : "",
252+
};
253+
}

0 commit comments

Comments
 (0)