@@ -4,7 +4,8 @@ import {ChatPromptWrapper} from "../ChatPromptWrapper.js";
44import { AbortError } from "../AbortError.js" ;
55import { GeneralChatPromptWrapper } from "../chatWrappers/GeneralChatPromptWrapper.js" ;
66import { getChatWrapperByBos } from "../chatWrappers/createChatWrapperByBos.js" ;
7- import { Token } from "../types.js" ;
7+ import { ConversationInteraction , Token } from "../types.js" ;
8+ import { generateContextTextFromConversationHistory } from "../chatWrappers/generateContextTextFromConversationHistory.js" ;
89import { LlamaModel } from "./LlamaModel.js" ;
910import { LlamaContext } from "./LlamaContext.js" ;
1011
@@ -15,7 +16,10 @@ export type LlamaChatSessionOptions = {
1516 context : LlamaContext ,
1617 printLLamaSystemInfo ?: boolean ,
1718 promptWrapper ?: ChatPromptWrapper | "auto" ,
18- systemPrompt ?: string
19+ systemPrompt ?: string ,
20+
21+ /** Conversation history to load into the context to continue an existing conversation */
22+ conversationHistory ?: readonly ConversationInteraction [ ]
1923} ;
2024
2125export class LlamaChatSession {
@@ -26,17 +30,22 @@ export class LlamaChatSession {
2630 private _initialized : boolean = false ;
2731 private _lastStopString : string | null = null ;
2832 private _lastStopStringSuffix : string | null = null ;
33+ private _conversationHistoryToLoad : readonly ConversationInteraction [ ] | null = null ;
2934 private readonly _ctx : LlamaContext ;
3035
3136 public constructor ( {
3237 context,
3338 printLLamaSystemInfo = false ,
3439 promptWrapper = new GeneralChatPromptWrapper ( ) ,
35- systemPrompt = defaultChatSystemPrompt
40+ systemPrompt = defaultChatSystemPrompt ,
41+ conversationHistory
3642 } : LlamaChatSessionOptions ) {
3743 this . _ctx = context ;
3844 this . _printLLamaSystemInfo = printLLamaSystemInfo ;
3945 this . _systemPrompt = systemPrompt ;
46+ this . _conversationHistoryToLoad = ( conversationHistory != null && conversationHistory . length > 0 )
47+ ? conversationHistory
48+ : null ;
4049
4150 if ( promptWrapper === "auto" ) {
4251 const chatWrapper = getChatWrapperByBos ( context . getBosString ( ) ) ;
@@ -76,7 +85,32 @@ export class LlamaChatSession {
7685 await this . init ( ) ;
7786
7887 return await withLock ( this , "prompt" , async ( ) => {
79- const promptText = this . _promptWrapper . wrapPrompt ( prompt , {
88+ let promptText = "" ;
89+
90+ if ( this . _promptIndex == 0 && this . _conversationHistoryToLoad != null ) {
91+ const { text, stopString, stopStringSuffix} =
92+ generateContextTextFromConversationHistory ( this . _promptWrapper , this . _conversationHistoryToLoad , {
93+ systemPrompt : this . _systemPrompt ,
94+ currentPromptIndex : this . _promptIndex ,
95+ lastStopString : this . _lastStopString ,
96+ lastStopStringSuffix : this . _promptIndex == 0
97+ ? (
98+ this . _ctx . prependBos
99+ ? this . _ctx . getBosString ( )
100+ : null
101+ )
102+ : this . _lastStopStringSuffix
103+ } ) ;
104+
105+ promptText += text ;
106+ this . _lastStopString = stopString ;
107+ this . _lastStopStringSuffix = stopStringSuffix ;
108+ this . _promptIndex += this . _conversationHistoryToLoad . length ;
109+
110+ this . _conversationHistoryToLoad = null ;
111+ }
112+
113+ promptText += this . _promptWrapper . wrapPrompt ( prompt , {
80114 systemPrompt : this . _systemPrompt ,
81115 promptIndex : this . _promptIndex ,
82116 lastStopString : this . _lastStopString ,
0 commit comments