-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: load conversation history into a
LlamaChatSession
(#51)
- Loading branch information
Showing
10 changed files
with
177 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/chatWrappers/generateContextTextFromConversationHistory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import {ChatPromptWrapper} from "../ChatPromptWrapper.js"; | ||
import {defaultChatSystemPrompt} from "../config.js"; | ||
import {ConversationInteraction} from "../types.js"; | ||
|
||
|
||
/** | ||
* Generate context text to load into a model context from a conversation history. | ||
* @param {ChatPromptWrapper} chatPromptWrapper | ||
* @param {ConversationInteraction[]} conversationHistory | ||
* @param {object} [options] | ||
* @param {string} [options.systemPrompt] | ||
* @param {number} [options.currentPromptIndex] | ||
* @param {string | null} [options.lastStopString] | ||
* @param {string | null} [options.lastStopStringSuffix] | ||
* @returns {{text: string, stopString: (string | null), stopStringSuffix: (string | null)}} | ||
*/ | ||
export function generateContextTextFromConversationHistory( | ||
chatPromptWrapper: ChatPromptWrapper, | ||
conversationHistory: readonly ConversationInteraction[], | ||
{ | ||
systemPrompt = defaultChatSystemPrompt, currentPromptIndex = 0, lastStopString = null, lastStopStringSuffix = null | ||
}: { | ||
systemPrompt?: string, currentPromptIndex?: number, lastStopString?: string | null, lastStopStringSuffix?: string | null | ||
} = {} | ||
): { | ||
text: string; | ||
stopString: string | null; | ||
stopStringSuffix: string | null; | ||
} { | ||
let res = ""; | ||
|
||
for (let i = 0; i < conversationHistory.length; i++) { | ||
const interaction = conversationHistory[i]; | ||
const wrappedPrompt = chatPromptWrapper.wrapPrompt(interaction.prompt, { | ||
systemPrompt, | ||
promptIndex: currentPromptIndex, | ||
lastStopString, | ||
lastStopStringSuffix | ||
}); | ||
const stopStrings = chatPromptWrapper.getStopStrings(); | ||
const defaultStopString = chatPromptWrapper.getDefaultStopString(); | ||
const stopStringsToCheckInResponse = new Set([...stopStrings, defaultStopString]); | ||
|
||
currentPromptIndex++; | ||
lastStopString = null; | ||
lastStopStringSuffix = null; | ||
|
||
res += wrappedPrompt; | ||
|
||
for (const stopString of stopStringsToCheckInResponse) { | ||
if (interaction.response.includes(stopString)) { | ||
console.error( | ||
`Stop string "${stopString}" was found in model response of conversation interaction index ${i}`, | ||
{interaction, stopString} | ||
); | ||
throw new Error("A stop string cannot be in a conversation history interaction model response"); | ||
} | ||
} | ||
|
||
res += interaction.response; | ||
res += defaultStopString; | ||
lastStopString = defaultStopString; | ||
lastStopStringSuffix = ""; | ||
} | ||
|
||
return { | ||
text: res, | ||
stopString: lastStopString, | ||
stopStringSuffix: lastStopStringSuffix | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
export type Token = number; | ||
|
||
export type ConversationInteraction = { | ||
prompt: string, | ||
response: string | ||
}; |