fix(gemini): prepend empty user placeholder and normalize role alternation
This commit is contained in:
parent
a27c781dd9
commit
19ea81e449
2 changed files with 87 additions and 5 deletions
|
|
@ -123,24 +123,60 @@ export class RevolverEngine {
|
|||
return sorted;
|
||||
}
|
||||
|
||||
private convertToGeminiContent(messages: any[]): {
|
||||
public convertToGeminiContent(messages: any[]): {
|
||||
systemInstruction?: string;
|
||||
geminiMessages: any[];
|
||||
} {
|
||||
let systemInstruction = '';
|
||||
const geminiMessages: any[] = [];
|
||||
const rawMessages: { role: 'user' | 'model'; parts: { text: string }[] }[] = [];
|
||||
|
||||
for (const msg of messages) {
|
||||
if (msg.role === 'system') {
|
||||
systemInstruction += (systemInstruction ? '\n\n' : '') + (typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content));
|
||||
const text = typeof msg.content === 'string' ? msg.content : (msg.content != null ? JSON.stringify(msg.content) : '');
|
||||
systemInstruction += (systemInstruction ? '\n\n' : '') + text;
|
||||
} else {
|
||||
geminiMessages.push({
|
||||
const text = typeof msg.content === 'string' ? msg.content : (msg.content != null ? JSON.stringify(msg.content) : '');
|
||||
rawMessages.push({
|
||||
role: msg.role === 'assistant' ? 'model' : 'user',
|
||||
parts: [{ text: typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content) }]
|
||||
parts: [{ text }]
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Step 1: Prepend empty user placeholder if the first non-system message is from 'model' (Option 1)
|
||||
if (rawMessages.length > 0 && rawMessages[0].role === 'model') {
|
||||
rawMessages.unshift({
|
||||
role: 'user',
|
||||
parts: [{ text: '' }]
|
||||
});
|
||||
}
|
||||
|
||||
// Step 2: Ensure normalization by merging consecutive messages with the same role (Option 3)
|
||||
const geminiMessages: { role: 'user' | 'model'; parts: { text: string }[] }[] = [];
|
||||
for (const msg of rawMessages) {
|
||||
if (geminiMessages.length === 0) {
|
||||
geminiMessages.push({ role: msg.role, parts: [{ text: msg.parts[0]?.text ?? '' }] });
|
||||
} else {
|
||||
const prevMsg = geminiMessages[geminiMessages.length - 1];
|
||||
if (prevMsg.role === msg.role) {
|
||||
const prevText = prevMsg.parts[0]?.text ?? '';
|
||||
const currText = msg.parts[0]?.text ?? '';
|
||||
const mergedText = !prevText ? currText : (!currText ? prevText : `${prevText}\n\n${currText}`);
|
||||
prevMsg.parts = [{ text: mergedText }];
|
||||
} else {
|
||||
geminiMessages.push({ role: msg.role, parts: [{ text: msg.parts[0]?.text ?? '' }] });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the conversation ends with a user message for Gemini chat completion
|
||||
if (geminiMessages.length > 0 && geminiMessages[geminiMessages.length - 1].role === 'model') {
|
||||
geminiMessages.push({
|
||||
role: 'user',
|
||||
parts: [{ text: '' }]
|
||||
});
|
||||
}
|
||||
|
||||
return { systemInstruction, geminiMessages };
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -102,6 +102,52 @@ async function runTests() {
|
|||
assert.strictEqual(maskedShort, '****');
|
||||
console.log(' ✓ Key masking security verified.');
|
||||
|
||||
// TEST 5: Gemini Message Normalization & Empty Placeholder Prepending
|
||||
console.log('\n5. Testing Gemini message normalization & role alternation...');
|
||||
|
||||
// 5a. Assistant first with system instruction
|
||||
const conv1 = engine.convertToGeminiContent([
|
||||
{ role: 'system', content: 'You are a helpful assistant.' },
|
||||
{ role: 'assistant', content: 'Hello! How can I help you today?' },
|
||||
{ role: 'user', content: 'What is 2+2?' }
|
||||
]);
|
||||
assert.strictEqual(conv1.systemInstruction, 'You are a helpful assistant.');
|
||||
assert.strictEqual(conv1.geminiMessages.length, 3);
|
||||
assert.strictEqual(conv1.geminiMessages[0].role, 'user', 'First message must be normalized to user role');
|
||||
assert.strictEqual(conv1.geminiMessages[0].parts[0].text, '', 'Leading placeholder user message should be empty');
|
||||
assert.strictEqual(conv1.geminiMessages[1].role, 'model');
|
||||
assert.strictEqual(conv1.geminiMessages[1].parts[0].text, 'Hello! How can I help you today?');
|
||||
assert.strictEqual(conv1.geminiMessages[2].role, 'user');
|
||||
assert.strictEqual(conv1.geminiMessages[2].parts[0].text, 'What is 2+2?');
|
||||
|
||||
// 5b. Consecutive assistant messages merged, consecutive user messages merged
|
||||
const conv2 = engine.convertToGeminiContent([
|
||||
{ role: 'assistant', content: 'Part 1' },
|
||||
{ role: 'assistant', content: 'Part 2' },
|
||||
{ role: 'user', content: 'Question 1' },
|
||||
{ role: 'user', content: 'Question 2' }
|
||||
]);
|
||||
assert.strictEqual(conv2.geminiMessages.length, 3);
|
||||
assert.strictEqual(conv2.geminiMessages[0].role, 'user');
|
||||
assert.strictEqual(conv2.geminiMessages[0].parts[0].text, '');
|
||||
assert.strictEqual(conv2.geminiMessages[1].role, 'model');
|
||||
assert.strictEqual(conv2.geminiMessages[1].parts[0].text, 'Part 1\n\nPart 2');
|
||||
assert.strictEqual(conv2.geminiMessages[2].role, 'user');
|
||||
assert.strictEqual(conv2.geminiMessages[2].parts[0].text, 'Question 1\n\nQuestion 2');
|
||||
|
||||
// 5c. Conversation ending with an assistant turn ensures trailing user turn
|
||||
const conv3 = engine.convertToGeminiContent([
|
||||
{ role: 'user', content: 'Tell a story' },
|
||||
{ role: 'assistant', content: 'Once upon a time' }
|
||||
]);
|
||||
assert.strictEqual(conv3.geminiMessages.length, 3);
|
||||
assert.strictEqual(conv3.geminiMessages[0].role, 'user');
|
||||
assert.strictEqual(conv3.geminiMessages[1].role, 'model');
|
||||
assert.strictEqual(conv3.geminiMessages[2].role, 'user');
|
||||
assert.strictEqual(conv3.geminiMessages[2].parts[0].text, '');
|
||||
|
||||
console.log(' ✓ Empty placeholder prepending and turn alternation normalization verified.');
|
||||
|
||||
// Clean up test file
|
||||
if (fs.existsSync(testConfigPath)) {
|
||||
fs.unlinkSync(testConfigPath);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue