Skip to content

Commit c9f2a8e

Browse files
committed
perf: rebuild ai classify prompt which will have more better performance
1 parent eb3de2c commit c9f2a8e

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

src/server/model/prompt/survey.ts

+50-11
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,62 @@ export function buildSurveyClassifyPrompt(
9292
}[],
9393
suggestionCategory: string[],
9494
language: string = 'en'
95-
): string {
96-
return `
97-
You are a content data analysis and classification expert. You need to make a simple classification based on the information collected from users in multiple languages ​​around the world, and return the classified json directly to me.
95+
) {
96+
return {
97+
prompt: `
98+
## Role
99+
100+
You are a content analysis expert. You will receive a list of content from various countries in JSON format. Your job is to analyze these items and provide a classification for each record.
101+
102+
## Skills
103+
104+
- **Multilingual Proficiency**: Able to understand content in different languages.
105+
- **Semantic Understanding**: Capable of interpreting the meaning behind the text.
106+
- **Content Summarization & Organization**: Skilled in extracting key points and structuring them.
107+
- **Structured Output Production**: Able to present findings in a clear, organized format.
108+
109+
## Action
110+
111+
1. Read the user's input, which is a JSON array of objects. Each object contains:
112+
- \`id\`: a unique identifier
113+
- \`content\`: text in one or more languages
114+
2. Determine the best-fit category for each \`content\` from the list below.
115+
3. Return a JSON object where each key is the \`id\` and the value is the chosen category.
98116
99-
The data is as follows:
100-
${data.map((obj) => `- ${JSON.stringify(obj)}`).join('\n')}
117+
## Example
101118
102-
The classification results of the example are as follows:
103-
{"id1": "some category which summary", "id2": "another category which summary"}
119+
### Input Example
120+
\`\`\`
121+
- {"id": "id1", "content": "This option dont appear i cant use it"}
122+
- {"id": "id2", "content": "Toda vez que tento fazer uma persona acaba que o aplicativo saí, impossibilitado de fazer uma persona"}
123+
\`\`\`
124+
125+
### Output Example
126+
127+
\`\`\`json
128+
{
129+
"id1": "Application malfunction",
130+
"id2": "Application crash"
131+
}
132+
\`\`\`
133+
134+
## Reference Categories
104135
105-
The existing categories are as follows. Please refer to the existing categories as much as possible:
106136
${JSON.stringify(suggestionCategory)}
107137
108-
No explanation is required.
109138
110-
${language !== 'en' ? `And response result should use \`${language}\` as response language.` : ''}
111-
`.trim();
139+
## Constraints
140+
141+
1. **Output Format**: All responses must be returned in JSON format.
142+
2. **Do not create new categories**: If you introduce a new category, ensure it is strictly necessary to capture the main intent of the content. If content seems to fall between two categories, pick the one that is the closest match rather than creating a new category.
143+
3. **Key-Value Structure**: The output must be a JSON object where each \`id\` serves as the key and its corresponding classification is the value.
144+
4. **Consistency in Data Count**: The number of entries in your output must match the number of entries in the input.
145+
5. **Use Existing Categories**: Only create new categories if absolutely necessary.
146+
6. **Return user perfer language**: response result should use **${language}** as response language.
147+
148+
`.trim(),
149+
question: data.map((obj) => `- ${JSON.stringify(obj)}`).join('\n'),
150+
};
112151
}
113152

114153
export function buildSurveyTranslationPrompt(

src/server/mq/worker.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ async function runSurveyAIClassifyWorker(msg: string) {
198198

199199
let inc = 0;
200200
for (const group of groups) {
201-
const prompt = buildSurveyClassifyPrompt(
201+
const { prompt, question } = buildSurveyClassifyPrompt(
202202
group,
203203
currentSuggestionCategory,
204204
languageStrategy === 'user' ? language : 'en'
@@ -208,14 +208,9 @@ async function runSurveyAIClassifyWorker(msg: string) {
208208
`Process run survey AI classify, group batch ${++inc} group size: ${group.length}`
209209
);
210210

211-
const res = await requestOpenAI(
212-
workspaceId,
213-
prompt,
214-
'Please help me generate data.',
215-
{
216-
response_format: { type: 'json_object' },
217-
}
218-
);
211+
const res = await requestOpenAI(workspaceId, prompt, question, {
212+
response_format: { type: 'json_object' },
213+
});
219214

220215
const json = JSON.parse(res);
221216

0 commit comments

Comments
 (0)