Skip to content

Commit bc5299e

Browse files
committed
enhance question output
1 parent 50e9491 commit bc5299e

File tree

3 files changed

+36
-42
lines changed

3 files changed

+36
-42
lines changed

components/question-area.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
"use client";
22

33
import { Badge } from "@/components/ui/badge";
4-
import { Button } from "@/components/ui/button";
54
import { Card, CardContent } from "@/components/ui/card";
65
import type { Question } from "@/lib/types";
76
import { useTranslation } from "@/lib/i18n";
8-
import { cleanupTestCase } from "@/lib/utils";
97
import ReactMarkdown from "react-markdown";
108
import remarkGfm from "remark-gfm";
119
import rehypeRaw from "rehype-raw";
@@ -31,14 +29,6 @@ export default function QuestionArea({
3129
const topic =
3230
question.translations[language]?.topic || question.translations.en.topic;
3331

34-
// 移除不必要的 \n 到 <br> 转换,让 Markdown 处理换行
35-
// const correctedDescription = description.replace(/\n/g, "<br>");
36-
37-
// 处理转义引号问题
38-
const correctedDescription = description
39-
.replace(/\\"/g, '"') // 将 \" 转换为 "
40-
.replace(/\\'/g, "'"); // 将 \' 转换为 '
41-
4232
return (
4333
<Card className="mb-6 shadow-sm border border-gray-200 dark:border-gray-800 bg-white dark:bg-[#2c2c2e] rounded-xl">
4434
<CardContent className="p-6">
@@ -75,7 +65,7 @@ export default function QuestionArea({
7565
remarkPlugins={[remarkGfm]}
7666
rehypePlugins={[rehypeRaw]}
7767
>
78-
{correctedDescription}
68+
{description}
7969
</ReactMarkdown>
8070

8171
{question.type === "Coding" && question.testCases && (
@@ -91,11 +81,11 @@ export default function QuestionArea({
9181
>
9282
<div>
9383
<strong>{t("question.input")}:</strong>{" "}
94-
{cleanupTestCase(testCase.input)}
84+
{testCase.input}
9585
</div>
9686
<div>
9787
<strong>{t("question.output")}:</strong>{" "}
98-
{cleanupTestCase(testCase.output)}
88+
{testCase.output}
9989
</div>
10090
</div>
10191
))}

lib/api.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,17 @@ export async function generateQuestion(type: string, category: string, difficult
260260
The response MUST be directly parseable as JSON without any cleanup needed.
261261
Ensure all special characters in strings are properly escaped according to JSON standards.
262262
263+
When creating coding questions with test cases, format the input as a plain string rather than a JSON object.
264+
For example, use 'a = "helloworld", b = "world"' rather than {a: "helloworld", b: "world"}.
265+
266+
IMPORTANT: If a test case output is a string, you MUST enclose it in additional double quotes and escape them properly in JSON.
267+
For example:
268+
- For string output "hello": { "input": "some input", "output": "\"hello\"" }
269+
- For empty string: { "input": "some input", "output": "\"\"" }
270+
- For numbers: { "input": "some input", "output": "42" } (no extra quotes for numbers)
271+
272+
DO NOT leave string outputs without proper double quotes. Only add the extra quotes for string outputs, not for numbers or other types.
273+
263274
Return your response in JSON format exactly matching the structure provided, with no additional text.`;
264275

265276
const prompt = `
@@ -287,19 +298,37 @@ export async function generateQuestion(type: string, category: string, difficult
287298
}
288299
}${type === 'Coding' ? `,
289300
"testCases": [
290-
{ "input": "example input 1", "output": "expected output 1" },
291-
{ "input": "example input 2", "output": "expected output 2" }
301+
{ "input": "a = \"helloworld\", b = \"world\"", "output": "\"world\"" },
302+
{"input": "a = \"programming\", b = \"prog\"", "output": "\"prog\"" },
303+
{ "input": "x = 5, y = 10", "output": "50" },
304+
{ "input": "str = \"\"", "output": "\"\"" },
305+
{ "input": "", "output": "\"\"" }
292306
]` : ''}
293307
}
294308
295309
Make sure the question is appropriate for the difficulty level and incorporates concepts from all the specified categories.
296310
If multiple categories are provided, create a question that combines elements from these categories.
311+
312+
IMPORTANT: For test cases where the output is a string, ALWAYS enclose the output in additional double quotes:
313+
- For string output: "output": "\"hello\""
314+
- For empty string: "output": "\"\""
315+
- For number output: "output": "42" (no extra quotes for numbers)
316+
317+
Pay careful attention to the data type of the expected output and format it accordingly.
297318
`
298319

299320
try {
300321
const result = await callOpenAI(prompt, systemPrompt, (_) => {}, "question")
301-
302-
return JSON.parse(jsonrepair(result))
322+
323+
// Preprocess the JSON to handle incorrectly formatted empty strings
324+
const preprocessedResult = result
325+
// Replace four consecutive double quotes """" with the properly escaped empty string "\"\""
326+
.replace(/""""/g, "\"\\\"\\\"\"")
327+
// Replace two consecutive double quotes "" with the properly escaped empty string "\"\""
328+
// But avoid replacing already properly escaped empty strings "\"\""
329+
.replace(/(?<!\\)""/g, "\"\\\"\\\"\"");
330+
331+
return JSON.parse(jsonrepair(preprocessedResult))
303332
} catch (error) {
304333
console.error("Error generating question:", error)
305334
throw new Error("Failed to generate question. Please check your API settings and try again.")

lib/utils.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,6 @@ export function encodeCodeBlocks(text: string): string {
4343
return processAnswerWithRegexImproved(text)
4444
}
4545

46-
// Function to clean up test case displays by removing extra quotes
47-
export function cleanupTestCase(value: any): string {
48-
if (typeof value !== 'string') {
49-
return JSON.stringify(value);
50-
}
51-
52-
// 先尝试直接处理转义引号
53-
let cleanedValue = value.replace(/\\"/g, '"').replace(/\\'/g, "'");
54-
55-
// 如果字符串以引号开始和结束,可能是 JSON 字符串
56-
if ((cleanedValue.startsWith('"') && cleanedValue.endsWith('"')) ||
57-
(cleanedValue.startsWith("'") && cleanedValue.endsWith("'"))) {
58-
try {
59-
// Try to parse the JSON string to get the actual value
60-
const parsed = JSON.parse(cleanedValue);
61-
return typeof parsed === 'string' ? parsed : JSON.stringify(parsed);
62-
} catch (e) {
63-
// If parsing fails, return the cleaned value
64-
return cleanedValue;
65-
}
66-
}
67-
68-
return cleanedValue;
69-
}
70-
7146
// Function to format code blocks for display
7247
export function formatCodeBlock(codeText: string): { code: string; language: string } {
7348
// Default values

0 commit comments

Comments
 (0)