@@ -5,14 +5,15 @@ import {CommandModule} from "yargs";
5
5
import chalk from "chalk" ;
6
6
import fs from "fs-extra" ;
7
7
import withOra from "../../utils/withOra.js" ;
8
- import { defaultChatSystemPrompt } from "../../config.js" ;
8
+ import { chatCommandHistoryFilePath , defaultChatSystemPrompt } from "../../config.js" ;
9
9
import { LlamaChatPromptWrapper } from "../../chatWrappers/LlamaChatPromptWrapper.js" ;
10
10
import { GeneralChatPromptWrapper } from "../../chatWrappers/GeneralChatPromptWrapper.js" ;
11
11
import { ChatMLChatPromptWrapper } from "../../chatWrappers/ChatMLChatPromptWrapper.js" ;
12
12
import { getChatWrapperByBos } from "../../chatWrappers/createChatWrapperByBos.js" ;
13
13
import { ChatPromptWrapper } from "../../ChatPromptWrapper.js" ;
14
14
import { FalconChatPromptWrapper } from "../../chatWrappers/FalconChatPromptWrapper.js" ;
15
15
import { getIsInDocumentationMode } from "../../state.js" ;
16
+ import { ReplHistory } from "../../utils/ReplHistory.js" ;
16
17
import type { LlamaGrammar } from "../../llamaEvaluator/LlamaGrammar.js" ;
17
18
18
19
const modelWrappers = [ "auto" , "general" , "llamaChat" , "chatML" , "falconChat" ] as const ;
@@ -36,7 +37,8 @@ type ChatCommand = {
36
37
penalizeRepeatingNewLine : boolean ,
37
38
repeatFrequencyPenalty ?: number ,
38
39
repeatPresencePenalty ?: number ,
39
- maxTokens : number
40
+ maxTokens : number ,
41
+ noHistory : boolean
40
42
} ;
41
43
42
44
export const ChatCommand : CommandModule < object , ChatCommand > = {
@@ -176,19 +178,26 @@ export const ChatCommand: CommandModule<object, ChatCommand> = {
176
178
default : 0 ,
177
179
description : "Maximum number of tokens to generate in responses. Set to `0` to disable. Set to `-1` to set to the context size" ,
178
180
group : "Optional:"
181
+ } )
182
+ . option ( "noHistory" , {
183
+ alias : "nh" ,
184
+ type : "boolean" ,
185
+ default : false ,
186
+ description : "Don't load or save chat history" ,
187
+ group : "Optional:"
179
188
} ) ;
180
189
} ,
181
190
async handler ( {
182
191
model, systemInfo, systemPrompt, prompt, wrapper, contextSize,
183
192
grammar, jsonSchemaGrammarFile, threads, temperature, topK, topP,
184
193
gpuLayers, repeatPenalty, lastTokensRepeatPenalty, penalizeRepeatingNewLine,
185
- repeatFrequencyPenalty, repeatPresencePenalty, maxTokens
194
+ repeatFrequencyPenalty, repeatPresencePenalty, maxTokens, noHistory
186
195
} ) {
187
196
try {
188
197
await RunChat ( {
189
198
model, systemInfo, systemPrompt, prompt, wrapper, contextSize, grammar, jsonSchemaGrammarFile, threads, temperature, topK,
190
199
topP, gpuLayers, lastTokensRepeatPenalty, repeatPenalty, penalizeRepeatingNewLine, repeatFrequencyPenalty,
191
- repeatPresencePenalty, maxTokens
200
+ repeatPresencePenalty, maxTokens, noHistory
192
201
} ) ;
193
202
} catch ( err ) {
194
203
console . error ( err ) ;
@@ -201,7 +210,7 @@ export const ChatCommand: CommandModule<object, ChatCommand> = {
201
210
async function RunChat ( {
202
211
model : modelArg , systemInfo, systemPrompt, prompt, wrapper, contextSize, grammar : grammarArg ,
203
212
jsonSchemaGrammarFile : jsonSchemaGrammarFilePath , threads, temperature, topK, topP, gpuLayers, lastTokensRepeatPenalty, repeatPenalty,
204
- penalizeRepeatingNewLine, repeatFrequencyPenalty, repeatPresencePenalty, maxTokens
213
+ penalizeRepeatingNewLine, repeatFrequencyPenalty, repeatPresencePenalty, maxTokens, noHistory
205
214
} : ChatCommand ) {
206
215
const { LlamaChatSession} = await import ( "../../llamaEvaluator/LlamaChatSession.js" ) ;
207
216
const { LlamaModel} = await import ( "../../llamaEvaluator/LlamaModel.js" ) ;
@@ -273,21 +282,32 @@ async function RunChat({
273
282
// this is for ora to not interfere with readline
274
283
await new Promise ( resolve => setTimeout ( resolve , 1 ) ) ;
275
284
276
- const rl = readline . createInterface ( {
277
- input : process . stdin ,
278
- output : process . stdout
279
- } ) ;
285
+ const replHistory = await ReplHistory . load ( chatCommandHistoryFilePath , ! noHistory ) ;
286
+
287
+ async function getPrompt ( ) {
288
+ const rl = readline . createInterface ( {
289
+ input : process . stdin ,
290
+ output : process . stdout ,
291
+ history : replHistory . history . slice ( )
292
+ } ) ;
293
+
294
+ const res : string = await new Promise ( ( accept ) => rl . question ( chalk . yellow ( "> " ) , accept ) ) ;
295
+ rl . close ( ) ;
296
+
297
+ return res ;
298
+ }
280
299
281
300
// eslint-disable-next-line no-constant-condition
282
301
while ( true ) {
283
- const input : string = initialPrompt != null
302
+ const input = initialPrompt != null
284
303
? initialPrompt
285
- : await new Promise ( ( accept ) => rl . question ( chalk . yellow ( "> " ) , accept ) ) ;
304
+ : await getPrompt ( ) ;
286
305
287
306
if ( initialPrompt != null ) {
288
307
console . log ( chalk . green ( "> " ) + initialPrompt ) ;
289
308
initialPrompt = null ;
290
- }
309
+ } else
310
+ await replHistory . add ( input ) ;
291
311
292
312
if ( input === ".exit" )
293
313
break ;
0 commit comments