1- import { defaultChatSystemPrompt } from "./config.js" ;
2- import { withLock } from "./utils/withLock.js" ;
1+ import { defaultChatSystemPrompt } from "../config.js" ;
2+ import { withLock } from "../utils/withLock.js" ;
3+ import { ChatPromptWrapper } from "../ChatPromptWrapper.js" ;
4+ import { AbortError } from "../AbortError.js" ;
5+ import { GeneralChatPromptWrapper } from "../chatWrappers/GeneralChatPromptWrapper.js" ;
36import { LlamaModel } from "./LlamaModel.js" ;
4- import { ChatPromptWrapper } from "./ChatPromptWrapper.js" ;
5- import { AbortError } from "./AbortError.js" ;
6- import { GeneralChatPromptWrapper } from "./chatWrappers/GeneralChatPromptWrapper.js" ;
7+ import { LlamaContext } from "./LlamaContext.js" ;
78
89const UNKNOWN_UNICODE_CHAR = "�" ;
910
1011export class LlamaChatSession {
11- private readonly _model : LlamaModel ;
1212 private readonly _systemPrompt : string ;
1313 private readonly _printLLamaSystemInfo : boolean ;
1414 private readonly _promptWrapper : ChatPromptWrapper ;
1515 private _promptIndex : number = 0 ;
1616 private _initialized : boolean = false ;
17+ private readonly _ctx : LlamaContext ;
1718
1819 public constructor ( {
19- model ,
20+ context ,
2021 printLLamaSystemInfo = false ,
2122 promptWrapper = new GeneralChatPromptWrapper ( ) ,
2223 systemPrompt = defaultChatSystemPrompt
2324 } : {
24- model : LlamaModel ,
25+ context : LlamaContext ,
2526 printLLamaSystemInfo ?: boolean ,
2627 promptWrapper ?: ChatPromptWrapper ,
2728 systemPrompt ?: string ,
2829 } ) {
29- this . _model = model ;
30+ this . _ctx = context ;
3031 this . _printLLamaSystemInfo = printLLamaSystemInfo ;
3132 this . _promptWrapper = promptWrapper ;
3233
@@ -37,8 +38,8 @@ export class LlamaChatSession {
3738 return this . _initialized ;
3839 }
3940
40- public get model ( ) {
41- return this . _model ;
41+ public get context ( ) {
42+ return this . _ctx ;
4243 }
4344
4445 public async init ( ) {
@@ -47,7 +48,7 @@ export class LlamaChatSession {
4748 return ;
4849
4950 if ( this . _printLLamaSystemInfo )
50- console . log ( "Llama system info" , this . _model . systemInfo ) ;
51+ console . log ( "Llama system info" , LlamaModel . systemInfo ) ;
5152
5253 this . _initialized = true ;
5354 } ) ;
@@ -61,20 +62,20 @@ export class LlamaChatSession {
6162 const promptText = this . _promptWrapper . wrapPrompt ( prompt , { systemPrompt : this . _systemPrompt , promptIndex : this . _promptIndex } ) ;
6263 this . _promptIndex ++ ;
6364
64- return await this . _evalTokens ( this . _model . encode ( promptText ) , onToken , { signal} ) ;
65+ return await this . _evalTokens ( this . _ctx . encode ( promptText ) , onToken , { signal} ) ;
6566 } ) ;
6667 }
6768
6869 private async _evalTokens ( tokens : Uint32Array , onToken ?: ( tokens : number [ ] ) => void , { signal} : { signal ?: AbortSignal } = { } ) {
69- const decodeTokens = ( tokens : number [ ] ) => this . _model . decode ( Uint32Array . from ( tokens ) ) ;
70+ const decodeTokens = ( tokens : number [ ] ) => this . _ctx . decode ( Uint32Array . from ( tokens ) ) ;
7071
7172 const stopStrings = this . _promptWrapper . getStopStrings ( ) ;
7273 const stopStringIndexes = Array ( stopStrings . length ) . fill ( 0 ) ;
7374 const skippedChunksQueue : number [ ] = [ ] ;
7475 const res : number [ ] = [ ] ;
7576
7677
77- for await ( const chunk of this . _model . evaluate ( tokens ) ) {
78+ for await ( const chunk of this . _ctx . evaluate ( tokens ) ) {
7879 if ( signal ?. aborted )
7980 throw new AbortError ( ) ;
8081
0 commit comments