Skip to content

Commit 94ce294

Browse files
committed
fix: fix sonnet mode will output invalid json problem
1 parent 850b642 commit 94ce294

5 files changed

Lines changed: 26 additions & 28 deletions

File tree

src/server/model/openai.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ export function getOpenAIClient() {
3434
}
3535
}
3636

37+
export type RequestOpenAIOptions = Omit<
38+
ChatCompletionCreateParamsBase,
39+
'model' | 'messages' | 'stream'
40+
>;
41+
3742
export async function requestOpenAI(
3843
workspaceId: string,
3944
prompt: string,
4045
question: string,
41-
options: Omit<
42-
ChatCompletionCreateParamsBase,
43-
'model' | 'messages' | 'stream'
44-
> = {},
46+
options: RequestOpenAIOptions = {},
4547
context?: Record<string, string>
4648
): Promise<string> {
4749
if (!env.openai.enable) {

src/server/model/prompt/survey.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('buildSurveyClassifyPrompt', () => {
2828
const { prompt, question } = buildSurveyClassifyPrompt(
2929
[
3030
{ id: 'fooo', content: 'Hello, World!' },
31-
{ id: 'bar', content: 'Hello, Tianji!' },
31+
{ id: 'baar', content: 'Hello, Tianji!' },
3232
],
3333
[]
3434
);
@@ -60,13 +60,14 @@ describe('buildSurveyTranslationPrompt', () => {
6060
test.runIf(Boolean(process.env.TEST_SURVEY_PROMPT))(
6161
'test prompt effect',
6262
{
63-
timeout: 30_000,
63+
timeout: 60_000,
6464
},
6565
async () => {
66-
const { prompt } = buildSurveyTranslationPrompt(
66+
const { prompt, question } = buildSurveyTranslationPrompt(
6767
[
6868
{ id: 'fooo', content: 'Hello, World!' },
69-
{ id: 'bar', content: 'Hello, Tianji!' },
69+
{ id: 'baar', content: 'Hello, Tianji!' },
70+
{ id: 'baaz', content: 'Some bad case with "wrap with qutoe"' },
7071
],
7172
'fr-FR'
7273
);
@@ -80,7 +81,7 @@ describe('buildSurveyTranslationPrompt', () => {
8081
},
8182
{
8283
role: 'user',
83-
content: 'Please help me generate data.',
84+
content: question,
8485
},
8586
],
8687
response_format: {

src/server/model/prompt/survey.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,18 @@ export function buildSurveyTranslationPrompt(
160160
const prompt = `
161161
You are a translator expert.
162162
163-
Please help me translate those file to '${language}', direct give me json format response, don't be verbose:
164-
165-
${JSON.stringify(
166-
data.reduce((prev, curr) => {
167-
return {
168-
...prev,
169-
[curr.id]: curr.content,
170-
};
171-
}, {})
172-
)}
163+
Please help me translate those file to '${language}', direct give me json format response, strictly follow the following format and only output the following content, no other information. The NSFW format is not suitable for reading. If you need to wrap a line, you must use \n to indicate it. Do not split complete words when wrapping a line, if you need type " in a string, you must use \\"
173164
`.trim();
174165

175166
return {
176167
prompt,
168+
question: JSON.stringify(
169+
data.reduce((prev, curr) => {
170+
return {
171+
...prev,
172+
[curr.id]: curr.content,
173+
};
174+
}, {})
175+
),
177176
};
178177
}

src/server/model/task.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export async function runTask(
3636
});
3737
} catch (err) {
3838
const meta = { error: String(err), usageTime: Date.now() - start };
39+
console.error('run task error:', meta);
3940
await prisma.workspaceTask.update({
4041
where: {
4142
id: task.id,

src/server/mq/worker.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ async function runSurveyAITranslationWorker(msg: string) {
370370

371371
let resultJson: Record<string, string> = {};
372372
for (const group of groups) {
373-
const { prompt } = buildSurveyTranslationPrompt(
373+
const { prompt, question } = buildSurveyTranslationPrompt(
374374
group,
375375
languageStrategy === 'user' ? language : 'en'
376376
);
@@ -379,14 +379,9 @@ async function runSurveyAITranslationWorker(msg: string) {
379379
`Process run survey AI translation, group batch ${++inc}/${groups.length}, size: ${group.length}`
380380
);
381381

382-
const res = await requestOpenAI(
383-
workspaceId,
384-
prompt,
385-
'Please help me generate data.',
386-
{
387-
response_format: { type: 'json_object' },
388-
}
389-
);
382+
const res = await requestOpenAI(workspaceId, prompt, question, {
383+
response_format: { type: 'json_object' },
384+
});
390385

391386
const json = ensureJSONOutput(res);
392387

0 commit comments

Comments
 (0)