Skip to content

Commit b56bfd6

Browse files
committed
feat: Improve chart title generation to explicitly mention tied terms and refine AI tie-breaker instructions.
1 parent 48be7cb commit b56bfd6

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

app/Http/Controllers/Api/ChatV2Controller.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,31 @@ public function search(Request $request): Response
5454
}
5555
}
5656

57+
$tiedTerms = [];
5758
foreach ($topGroup['global_stats'] as $stat) {
59+
$isMax = $stat['resource_count'] === $maxCount;
60+
if ($isMax) {
61+
$tiedTerms[] = $stat['term'];
62+
}
5863
$chartDataRows[] = [
5964
'name' => $stat['term'],
6065
'value' => $stat['resource_count'],
61-
'isMax' => $stat['resource_count'] === $maxCount
66+
'isMax' => $isMax
6267
];
6368
}
6469

65-
$dominantTerm = $topGroup['global_stats'][0]['term'];
70+
if (count($tiedTerms) > 1) {
71+
// Limit to first 3 to avoid super long title
72+
$displayTies = array_slice($tiedTerms, 0, 3);
73+
$titleAppend = 'تعادل بين: ' . implode(' و ', $displayTies) . (count($tiedTerms) > 3 ? '...' : '') . ' (انظر التقرير للترجيح)';
74+
} else {
75+
$dominantTerm = $tiedTerms[0] ?? $topGroup['global_stats'][0]['term'];
76+
$titleAppend = 'الأغلبية لـ: ' . $dominantTerm;
77+
}
6678

6779
$initialChartData = [
6880
'type' => 'bar',
69-
'title' => 'توزيع انتشار التراجم (' . ($topGroup['display_term_en'] ?? $q) . ') - الأغلبية لـ: ' . $dominantTerm,
81+
'title' => 'توزيع انتشار التراجم (' . ($topGroup['display_term_en'] ?? $q) . ') - ' . $titleAppend,
7082
'data' => array_slice($chartDataRows, 0, 10)
7183
];
7284
}
@@ -139,7 +151,7 @@ public function chat(Request $request)
139151
- Use `list_resources` for structural queries.
140152
7. 0 RESULTS FALLBACK: If `search_terms` returns an empty array `[]` or no database results, YOU MUST inform the user that there are NO results found in the database. Then, YOU MUST provide a professional definition, explanation, and translation of the term based ONLY on your own internal knowledge. Clearly state that this information is from your own knowledge and not the database.
141153
8. COMPOSITION SEARCH FALLBACK: If `search_terms` returns `is_composition_fallback: true` (a multi-word phrase was not found as a whole, but its individual words were), YOU MUST inform the user of this. Then synthesis the combined meaning of the phrase based on the individual word results provided to you.
142-
9. TIE-BREAKER LOGIC: If two or more Arabic translations have the EXACT same `resource_count` and `total_count` in the database results, YOU MUST use your own internal knowledge of Arabic Computer Science terminology to decide and declare which one is linguistically and technically the "most appropriate/best" translation. Explain your reasoning briefly.
154+
9. TIE-BREAKER LOGIC (CRITICAL): If you observe from the JSON data that two or more Arabic translations have the EXACT same `resource_count` and `total_count` (a tie), you CANNOT just randomly pick one. YOU MUST explicitly mention that there is a tie between them in the database, and then use your own internal knowledge as an Arabic Computer Science Linguist to decide and declare which one is logically and linguistically the "most appropriate/best" translation. You MUST fully explain the reasoning for your choice in the report.
143155
';
144156

145157
if ($detailedMode) {

0 commit comments

Comments
 (0)