Skip to content

Commit 6a125d5

Browse files
committed
feat: add partial to save to database because translation task have more bad case to parse json format
1 parent 917306a commit 6a125d5

3 files changed

Lines changed: 299 additions & 27 deletions

File tree

src/server/model/openai.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
// @ts-ignore
1313
} from 'openai/resources/chat/completions.mjs';
1414
import { createAuditLog } from './auditLog.js';
15+
import { logger } from '../utils/logger.js';
1516

1617
export const modelName = env.openai.modelName ?? 'gpt-4o';
1718

@@ -129,6 +130,7 @@ export function ensureJSONOutput(content: string): Record<string, any> | null {
129130
try {
130131
return JSON.parse(jsonBlockMatch[1].trim());
131132
} catch (error) {
133+
logger.error('[ensureJSONOutput-2]:', error);
132134
// JSON block parsing failed, continue
133135
}
134136
}
@@ -144,6 +146,7 @@ export function ensureJSONOutput(content: string): Record<string, any> | null {
144146
try {
145147
return JSON.parse(codeContent);
146148
} catch (error) {
149+
logger.error('[ensureJSONOutput-3]:', error);
147150
// Code block JSON parsing failed, continue
148151
}
149152
}
@@ -157,6 +160,7 @@ export function ensureJSONOutput(content: string): Record<string, any> | null {
157160
try {
158161
return JSON.parse(jsonLikeMatch[1]);
159162
} catch (error) {
163+
logger.error('[ensureJSONOutput-4]:', error);
160164
// JSON-like content parsing failed
161165
}
162166
}

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

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,263 @@ describe('buildSurveyTranslationPrompt', () => {
9393
expect(json).not.toBeNull();
9494
}
9595
);
96+
97+
test.runIf(Boolean(process.env.TEST_SURVEY_PROMPT)).skip(
98+
'test prompt effect with real world bad case',
99+
{
100+
timeout: 300_000,
101+
},
102+
async () => {
103+
const { prompt, question } = buildSurveyTranslationPrompt(
104+
[
105+
{
106+
id: 'cmbd5yc8izwio7b2r56z1phvf',
107+
content:
108+
'Tidak ada tanda petik untuk memisahkan antara percakapan dan penjelasan jadi susah dimengerti dan tidak nyaman saat membacanya, tolong segera diperbaiki🙏🏻',
109+
},
110+
{
111+
id: 'cmbd71fam0xts7b2ruo0rnyfx',
112+
content:
113+
'Bueno cada vez que me salgo de esta App me sale que intente de nuevo como si no tuviera señal',
114+
},
115+
{
116+
id: 'cmbd9v3js3f667b2r6w9vh6tz',
117+
content: 'bug tulisannya abu semua dan tidak ada tanda kutip " nya',
118+
},
119+
{
120+
id: 'cmbda4ia83n0g7b2rzfocjgqp',
121+
content:
122+
'Inició sesión en la aplicación y me dice error de token, entró a la página web, y me permite entrar. Solucinen eso por favor',
123+
},
124+
{
125+
id: 'cmbdb1hcg4gte7b2rbf6lcoko',
126+
content:
127+
'Eu fiz o plano mensal plus, e tive acesso a novos tipos de chat. Mais em qualquer personagem que vou conversar além do modo Vanilla, são respondidos por mim. Eu não consigo usar outros modelos sem que a Ia escreva por mim.',
128+
},
129+
{
130+
id: 'cmbdbopf6507c7b2r37dhavvn',
131+
content:
132+
'aplikasi nya suka keluar sendiri, padahal lagi asik main.. Perbaiki dong developer.. Saya jadi gak nyaman.',
133+
},
134+
{
135+
id: 'cmbcyjjz9suv57b2rzopn7k64',
136+
content: 'Aunque tenga buen internet, no me deja acceder a nada',
137+
},
138+
{
139+
id: 'cmbczjo0ktpgr7b2r5yxhgxvu',
140+
content:
141+
'Her girip çıktığımda giriş mesajı sıfırlanıyor. Eski sürüm gelmesini istiyorum',
142+
},
143+
{
144+
id: 'cmbd17kgav3vq7b2r6e6lpyz4',
145+
content: 'Aplikasi keluar sendiri \nPerbaiki hari ini',
146+
},
147+
{
148+
id: 'cmbd44egqy4kq7b2rjqh5svh2',
149+
content: 'It doesn’t want to work ',
150+
},
151+
{
152+
id: 'cmbd4gxm4yh587b2rp1mc0wqq',
153+
content: 'Me saca de la aplicación y los anuncios se traban \n',
154+
},
155+
{
156+
id: 'cmbd7n4tm1g7y7b2r3qkmurve',
157+
content:
158+
'Aplikasi keluar sendiri terus menerus \nPerbaiki agar tidak keluar sendiri \n',
159+
},
160+
{
161+
id: 'cmbde4nyz6vqm7b2rsfysc770',
162+
content:
163+
'Sering keluar dari aplikasi dan sering iklan mendadak, perubahan pada bahasa',
164+
},
165+
{
166+
id: 'cmbdg66x88c8m7b2rzzgkob7y',
167+
content:
168+
'ketika Emochi saya sudah dipakai lebih dari 4 jam, itu selalu nge bug dan keluar aplikasi Emochi sendiri, saya sudah menghapus canche dan uninstall lalu tetap sama selalu keluar sendiri, tolong bantuannya',
169+
},
170+
{
171+
id: 'cmbd1jgxovf2m7b2rhaw4k4ad',
172+
content: 'No me deja entrar con mi cuenta de google',
173+
},
174+
{
175+
id: 'cmbd90uhz2ovc7b2rtg2kmngc',
176+
content:
177+
'Hello Emochi team, \nI\'m getting a "Failed to load!" error when opening a conversation in the app. I have tried clearing cache, switching networks, logging out/in, and updating the app, but the issue still persists. \nI’m using [Android], and my account UID/ID is [qlXVR78Gz14OO0-6lt0Kh].\n\nThank you!',
178+
},
179+
{
180+
id: 'cmbdankh244v07b2rkwkdrj77',
181+
content:
182+
'En la aplicación, no me deja iniciar sesión, y en la página web si. Necesito ayuda',
183+
},
184+
{
185+
id: 'cmbd2ta2awsxs7b2rvwmkq5o5',
186+
content:
187+
'Me pone mensajes con partes en español e ingles siendo que mi configuración está en español ',
188+
},
189+
{
190+
id: 'cmbd0xbgzuuiw7b2rmv510dnd',
191+
content:
192+
"App is constantly timing out, especially after ads. An ad will come up, and I'll press the x and the screen will start flickering, ultimately closing the app all together and sometimes not letting me back in. Or it'll time out right as an ad comes up. Recently, I'll send a message and it'll glitch out. It's gotten progressively worse and has been happening to my other friends who have it too. Pleaseee fix",
193+
},
194+
{
195+
id: 'cmbd2f90dwe5y7b2rxmf7kz4l',
196+
content: 'No me deja chatear, está rarisimo y como que se traba.',
197+
},
198+
{
199+
id: 'cmbd3l78vxkda7b2rftfdxor4',
200+
content:
201+
'Estou tentando editar a minha introdução e corpo da conversa, já editei o texto, atualizei milhares de vezes e não atualiza. Já sai do app, voltei..e nada muda.',
202+
},
203+
{
204+
id: 'cmbd83sfz1vba7b2r3l6pm1ye',
205+
content:
206+
'Não carrega as mensagens, não tá funcionando direito, sempre tá saindo de lógica horrível pra mevher, não pega nada de vez enquanto, ',
207+
},
208+
{
209+
id: 'cmbd2hk5bwgp07b2rotzvlvgc',
210+
content:
211+
'Por qué cuando pongo mis preferencias del personaje me sale lo contrario a lo que pedí ',
212+
},
213+
{
214+
id: 'cmbd1ygmuvv0e7b2r69gjjg8b',
215+
content:
216+
'As falas estão vindo em inglês,eu só quero em português \n',
217+
},
218+
{
219+
id: 'cmbdh5f5a8zha7b2riscdhgeq',
220+
content:
221+
'Добрый день. Вел рассказ, и резко ответы персонажа в данном диалоге стал на иностранном языке. Помогите пожалуйста исправить эту ошибку',
222+
},
223+
{
224+
id: 'cmbd3c5aexbom7b2rv6l8zv8j',
225+
content: 'I am unable to select any of my chats',
226+
},
227+
{
228+
id: 'cmbd7bh43168q7b2rdji944xq',
229+
content:
230+
'Cuando quiero mandar algún mensaje al personaje me sale que no se editó o algo parecido, incluso cuando apenas terminó de crear un personaje y quiero mandar un mensaje, no puedo debido a esto y la verdad es molesto. Ya que ni siquiera puedo hablar con el personaje',
231+
},
232+
{
233+
id: 'cmbd89vru20qm7b2r56h5xu23',
234+
content:
235+
'Hola! Lamento la molestia pero al momento de editar un mensaje este no suele mandarse o no me permite picar aquel botón, trate de cerrar y reiniciar mi teléfono creyendo que era un fallo mio pero aun así no puedo y es incomodo. Aparte de que el puntero en el momento de editar aquel mensaje no puede moverse y tienes que borrar todo para poder editarlo aunque como decía antes, no te deja. Se los agradecería mucho si lo revisaran por que es molesto. ',
236+
},
237+
{
238+
id: 'cmbdfamou7qny7b2rrz4t7k4u',
239+
content: 'Hikaye davem ettiremiyorum en başı yazıyo ',
240+
},
241+
{
242+
id: 'cmbdgp1jh8ols7b2r31vnjbwi',
243+
content: "I can't select/open characters from my character list.",
244+
},
245+
{
246+
id: 'cmbd4s58ayspq7b2ry7yeoy7c',
247+
content: 'Por que ahora parece que tiene filtro?',
248+
},
249+
{
250+
id: 'cmbczp7tgttyx7b2r8st3eabp',
251+
content:
252+
'O que seria não encontrar o bot? Eu estava conversando com ele normalmente e de repente apareceu isso.\n Eu já tentei até reiniciar meu celular, mas não resolveu nada\nIsso significa que o criador deletou o bot, ou algo assim?',
253+
},
254+
{
255+
id: 'cmbd1zoqmvwfa7b2r8cjafn9b',
256+
content: '机器人消失了',
257+
},
258+
{
259+
id: 'cmbd86nqz1xu47b2r1kau4kxp',
260+
content:
261+
'Saya harap dari model Macha sampai peach dapat memerankan persona dengan pengguna dengan baik ',
262+
},
263+
{
264+
id: 'cmbd7jmot1d8k7b2rgco8ay6m',
265+
content:
266+
'Nao sei se é um bug. Mas é bem irritante sair por 1 segundo do app e ter que esperar ele fazer load de novo.',
267+
},
268+
{
269+
id: 'cmbd2yr1zwyca7b2rumfd2tyr',
270+
content:
271+
'1. kao nulis selalu dempet ga di spasi\n2. selalu pelupa masa aku udah di jelasin aku jadi cowok tapi malah jadi cewek\n3. selalu salah nama\n4. ngetiknya ga jelas',
272+
},
273+
{
274+
id: 'cmbd8zggm2nnk7b2rlwjd3jqc',
275+
content:
276+
'Chào Emochi team, \nTôi đang gặp lỗi "Failed to load!" khi mở cuộc hội thoại trong ứng dụng. Đã thử kiểm tra mạng, xoá cache, đăng xuất - đăng nhập lại và cập nhật ứng dụng nhưng lỗi vẫn xảy ra. Mong team kiểm tra giúp. \nTôi dùng [Android], UID/ID tài khoản là [qlXVR78Gz14OO0-6lt0Kh].\n\nCảm ��n team!',
277+
},
278+
{
279+
id: 'cmbd42wjby3067b2r8c3ifn7d',
280+
content:
281+
'Những danh sách nhân vật của tôi đã biến mất ngoại trừ người tôi nói chuyện gần nhất',
282+
},
283+
{
284+
id: 'cmbdaj9gt40pc7b2rqvhy6g3e',
285+
content:
286+
'toda vez que eu abro o emochi seja pelo aplicativo ou pelo site e logo na minha conta todos os dados da minha conta são excluídos e eu perco tudo, por favor salvem as contas',
287+
},
288+
{
289+
id: 'cmbdawngx4cnk7b2rpb5t6fns',
290+
content:
291+
"When {{user}} has dialogue, AI does not know how to repeat {{user}}'s dialogue when generating text. Please fix it.",
292+
},
293+
{
294+
id: 'cmbdflhb47yd87b2rnk7ozw5g',
295+
content:
296+
'Tidak ada tanda petik untuk memisahkan antara percakapan dan penjelasan jadi tidak nyaman saat membacanya, tolong diperbaiki ya!',
297+
},
298+
{
299+
id: 'cmbdgjy3g8l7s7b2rvsvr8sj1',
300+
content:
301+
"Usually to differentiate narrative sentences and dialogues there are quotation marks between the dialogue sentences, but why aren't there any? It's very uncomfortable, please fix it \nfor example : \n*I turned to him and smiled* \"hello how are you?\"\nBut instead there is a bug that is annoying and uncomfortable, the bug is like this:\n*I turned to him and smiled hello how are you*\nnow that's confusing, because it makes us not know whether it's a dialogue or not!\nSO PLEASE FIX IT IMMEDIATELY !!!!!!!!!!!!",
302+
},
303+
{
304+
id: 'cmbdgqh718pic7b2r02hop8po',
305+
content:
306+
'I would like to report a critical bug that interferes with the usability of this application. Dialogue and narration should be separated by quotation marks (“…”). Example of correct writing:\n\nI turned to him and smiled “Hello, how are you?”\n\nHowever, what happens in the application is that the dialogue is written without quotation marks, like this:\n\nI turned to him and smiled hello, how are you?\n\nWithout quotation marks, sentences become confusing because it is not clear which is narration and which is dialogue. This is very distracting, especially in writing stories that involve a lot of interaction between characters.\n\nPlease fix this bug soon. Thank you.!',
307+
},
308+
{
309+
id: 'cmbd72kqn0yt27b2rs407n20g',
310+
content:
311+
'Por error puse el modo "Vainilla Short" y cuando lo intenté cambiar a "Vainilla" me aparece que ese es el modelo seleccionado, pero las respuestas siguen siendo cortas :( ',
312+
},
313+
{
314+
id: 'cmbdbj9gz4vlq7b2rluj7dw3n',
315+
content: "The search doesn't work. Nothing appears, please fix",
316+
},
317+
{
318+
id: 'cmbd4spsnytas7b2ra3i654dt',
319+
content: 'Resposta crutas',
320+
},
321+
{
322+
id: 'cmbddez1y6ca47b2rlseo22fd',
323+
content:
324+
'Cada vez que trato de entrar a ese bot la pantalla se pone en negro aproximadamente 30-40 segundos y después me saca de la aplicación, aparte que por afuera (lo que subrayé de rosa) se ve ese mensaje que aparecía como la introducción del bot, y ahí no fue donde me quedé ya que yo llevo más de 4 meses con ese, y cada vez que trato de meterme a otros bots si me deja sin ningún problema, excepto a este, pero este es el que yo quiero, por favor arréglenlo, lleva así 2 días 🙏🏻\n',
325+
},
326+
],
327+
'fr-FR'
328+
);
329+
330+
const res = await getOpenAIClient().chat.completions.create({
331+
model: modelName,
332+
messages: [
333+
{
334+
role: 'system',
335+
content: prompt,
336+
},
337+
{
338+
role: 'user',
339+
content: question,
340+
},
341+
],
342+
response_format: {
343+
type: 'json_object',
344+
},
345+
});
346+
347+
const raw = res.choices[0].message.content ?? '';
348+
const json = ensureJSONOutput(raw);
349+
if (json === null) {
350+
console.log(raw);
351+
}
352+
expect(json).not.toBeNull();
353+
}
354+
);
96355
});

src/server/mq/worker.ts

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ async function runSurveyAITranslationWorker(msg: string) {
352352
}
353353

354354
logger.info(
355-
'Process run survey AI classify, filtered data count:',
355+
'Process run survey AI translation, filtered data count:',
356356
data.length
357357
);
358358

@@ -364,11 +364,12 @@ async function runSurveyAITranslationWorker(msg: string) {
364364
100 // use 100 as group size default
365365
);
366366

367-
logger.info('Process run survey AI classify, groups', groups.length);
367+
logger.info('Process run survey AI translation, groups', groups.length);
368368

369369
let inc = 0;
370370

371371
let resultJson: Record<string, string> = {};
372+
let effectCount = 0;
372373
for (const group of groups) {
373374
const { prompt, question } = buildSurveyTranslationPrompt(
374375
group,
@@ -386,6 +387,14 @@ async function runSurveyAITranslationWorker(msg: string) {
386387
const json = ensureJSONOutput(res);
387388

388389
if (json === null) {
390+
logger.error(
391+
'Process run survey AI translation, failed to parse JSON',
392+
{
393+
prompt,
394+
question,
395+
res,
396+
}
397+
);
389398
throw new Error('Failed to parse JSON');
390399
}
391400

@@ -398,37 +407,37 @@ async function runSurveyAITranslationWorker(msg: string) {
398407
values(json).length
399408
);
400409

410+
// Write to the database in advance to ensure that some of the data that has already been successfully processed can be retained in case other batches encounter problems, reducing the token usage for the next time
411+
await pMap(
412+
Object.keys(json),
413+
async (id) => {
414+
const prev = data.find((d) => d.id === id);
415+
416+
if (prev && json[id]) {
417+
await prisma.surveyResult.update({
418+
where: {
419+
id,
420+
},
421+
data: {
422+
aiTranslation: json[id],
423+
},
424+
});
425+
426+
effectCount++;
427+
}
428+
},
429+
{
430+
concurrency: 5,
431+
}
432+
);
433+
401434
resultJson = {
402435
...resultJson,
403436
...json,
404437
};
405438
}
406439

407-
logger.info('Process run survey AI classify, AI completed.');
408-
409-
let effectCount = 0;
410-
await pMap(
411-
Object.keys(resultJson),
412-
async (id) => {
413-
const prev = data.find((d) => d.id === id);
414-
415-
if (prev && resultJson[id]) {
416-
await prisma.surveyResult.update({
417-
where: {
418-
id,
419-
},
420-
data: {
421-
aiTranslation: resultJson[id],
422-
},
423-
});
424-
425-
effectCount++;
426-
}
427-
},
428-
{
429-
concurrency: 5,
430-
}
431-
);
440+
logger.info('Process run survey AI translation, AI completed.');
432441

433442
const result = {
434443
workspaceId,

0 commit comments

Comments
 (0)