Helper for conceptual system instruction split

This commit is contained in:
Enrico Ros
2024-12-15 12:53:18 -08:00
parent 41a2f1e526
commit 7be72acff3
4 changed files with 24 additions and 13 deletions
+4 -4
View File
@@ -230,13 +230,13 @@ export function Telephone(props: {
// Call Message Generation Prompt
const callSystemInstruction = createDMessageTextContent('system', 'You are having a phone call. Your response style is brief and to the point, and according to your personality, defined below.');
const reMessagesRemapSysToUsr = (reMessages && reMessages?.length > 0)
? reMessages.map(_m => _m.role === 'system' ? { ..._m, role: 'user' as const } : _m) // (MUST: [0] is the system message of the original chat) cast system chat messages to the user role
: null;
const callGenerationInputHistory: DMessage[] = [
// Chat messages, including the system prompt which is casted to a user message
// TODO: when upgrading to dynamic personas, we need to inject the persona message instead - not rely on reMessages, as messages[0] !== 'system'
...((reMessages && reMessages?.length > 0)
? reMessages.map(_m => _m.role === 'system' ? { ..._m, role: 'user' as const } : _m) // (MUST: [0] is the system message of the original chat) cast system chat messages to the user role
: [createDMessageTextContent('user', personaSystemMessage)] // see TO-DO ^
),
...(reMessagesRemapSysToUsr ? reMessagesRemapSysToUsr : [createDMessageTextContent('user', personaSystemMessage)]),
// Call system prompt 2, to indicate the call has started
createDMessageTextContent('user', '**You are now on the phone call related to the chat above**.\nRespect your personality and answer with short, friendly and accurate thoughtful brief lines.'),
// Call history
+14 -3
View File
@@ -23,6 +23,19 @@ export interface PersonaProcessorInterface {
}
export function splitSystemMessageFromHistory(chatHistory: Readonly<DMessage[]>): {
chatSystemInstruction: DMessage | null,
chatHistory: Readonly<DMessage[]>,
} {
const chatSystemInstruction = chatHistory[0].role === 'system' ? chatHistory[0] : null;
return {
chatSystemInstruction,
chatHistory: (chatSystemInstruction ? chatHistory.slice(1) : chatHistory),
// .map(_m => _m.role === 'system' ? { ..._m, role: 'user' as const } : _m) // cast system chat messages to the user role
};
}
/**
* The main "chat" function.
* @returns `true` if the operation was successful, `false` otherwise.
@@ -39,9 +52,7 @@ export async function runPersonaOnConversationHead(
return false;
// split pre dynamic-personas
const chatSystemInstruction = _history[0].role === 'system' ? _history[0] : null;
const chatHistory = (chatSystemInstruction ? _history.slice(1) : _history);
// .map(_m => _m.role === 'system' ? { ..._m, role: 'user' as const } : _m) // cast system chat messages to the user role
let { chatSystemInstruction, chatHistory } = splitSystemMessageFromHistory(_history);
// assistant response placeholder
const isNotifyEnabled = getIsNotificationEnabledForModel(assistantLlmId);
@@ -51,10 +51,10 @@ export async function executeGatherInstruction(_i: GatherInstruction, inputs: Ex
throw new Error('Invalid response role');
const gatherSystemInstruction = createDMessageTextContent('system', _mixChatGeneratePrompt(_i.systemPrompt, inputs.rayMessages.length, prevStepOutput));
const chatMessagesWithoutSystem = inputs.chatMessages.filter(_m => (_m.role === 'user' || _m.role === 'assistant'));
const gatherHistory: DMessage[] = [
// s0-h0-u0: remove the system messages
...inputs.chatMessages
.filter(_m => (_m.role === 'user' || _m.role === 'assistant')),
// s0-h0-u0
...chatMessagesWithoutSystem,
// aN: every proposal is an assistant message
// FIXME: there could be an issue with aix.dispatch fusion of assistant messages, and in the future, this should require a
// re-encoding or structuring of sorts, e.g.: .map(_m => ({ ..._m, metadata: { ..._m.metadata, asAttachment: true } }))
+3 -3
View File
@@ -2,6 +2,8 @@ import type { StateCreator } from 'zustand/vanilla';
import { AixChatGenerateContent_DMessage, aixChatGenerateContent_DMessage_FromConversation } from '~/modules/aix/client/aix.client';
import { splitSystemMessageFromHistory } from '../../../apps/chat/editors/chat-persona';
import type { DLLMId } from '~/common/stores/llms/llms.types';
import { agiUuid } from '~/common/util/idUtils';
import { createDMessageEmpty, DMessage, duplicateDMessageNoVoid, messageWasInterruptedAtStart } from '~/common/stores/chat/chat.message';
@@ -54,9 +56,7 @@ function rayScatterStart(ray: BRay, llmId: DLLMId | null, inputHistory: DMessage
return { ...ray, scatterIssue: `Invalid conversation history (${inputHistory?.length})` };
// split pre dynamic-personas
const scatterSystemInstruction = inputHistory[0].role === 'system' ? inputHistory[0] : null;
const scatterInputHistory = (scatterSystemInstruction ? inputHistory.slice(1) : inputHistory);
// .map(_m => _m.role === 'system' ? { ..._m, role: 'user' as const } : _m) // cast system chat messages to the user role
const { chatSystemInstruction: scatterSystemInstruction, chatHistory: scatterInputHistory } = splitSystemMessageFromHistory(inputHistory);
const abortController = new AbortController();