1- import strutils, std/ strformat, std / httpclient, os, json, asyncdispatch
1+ import strutils, std/ [ strformat, httpclient] , os, json, asyncdispatch
22import libchatllm
33import packages/ docutils/ highlite, terminal
44
7171 highlighter = object
7272 line_acc: string
7373 lang: SourceLanguage
74+ thought_acc: string
75+
76+ proc receive_thought_chunk (ht: var highlighter, chunk: string ) =
77+ if ht.thought_acc == " " :
78+ stdout.setForegroundColor (fgMagenta)
79+
80+ ht.thought_acc &= chunk
81+ stdout.write (chunk)
82+
83+ proc thought_end (ht: var highlighter) =
84+ stdout.setForegroundColor (fgDefault)
85+
86+ proc reset (ht: var highlighter) =
87+ ht.line_acc = " "
88+ ht.thought_acc = " "
89+ ht.lang = langNone
7490
7591proc receive_chunk (ht: var highlighter, chunk: string ) =
7692 ht.line_acc &= chunk
@@ -127,23 +143,40 @@ proc receive_chunk(ht: var highlighter, chunk: string) =
127143 lang_chunk (ht)
128144
129145proc chatllm_print (user_data: pointer , print_type: cint , utf8_str: cstring ) {.cdecl .} =
146+ var ht = cast [ptr highlighter](user_data)
147+ var s: string = $ utf8_str
130148 case cast [PrintType ](print_type)
131149 of PRINT_CHAT_CHUNK :
132- var ht = cast [ptr highlighter](user_data)
133- var s: string = $ utf8_str
134150 var n = 0
135151 for l in s.splitLines ():
136152 if n > 0 : receive_chunk (ht[], " " )
137153 n += 1
138154 if l != " " :
139155 receive_chunk (ht[], l)
156+ of PRINT_THOUGHT_CHUNK :
157+ receive_thought_chunk (ht[], s)
158+ of PRINT_EVT_THOUGHT_COMPLETED :
159+ thought_end (ht[])
160+ of RINTLN_MODEL_INFO :
161+ discard
140162 else :
141- if utf8_str != nil : echo utf8_str
163+ echo s
142164 stdout.flushFile ()
143165
144166proc chatllm_end (user_data: pointer ) {.cdecl .} =
145167 echo " "
146168
169+ proc user_input (multi_input: bool ): string =
170+ if multi_input:
171+ var r: seq [string ] = @ []
172+ while true :
173+ var line = readLine (stdin)
174+ if line == " \\ ." : break
175+ r.add line
176+ return r.join (" \n " )
177+ else :
178+ return readLine (stdin)
179+
147180const candidates = [" -m" , " --model" , " --embedding_model" , " --reranker_model" ]
148181let storage_dir = joinPath ([parentDir (paramStr (0 )), " ../quantized" ])
149182
@@ -154,6 +187,7 @@ let chat = chatllm_create()
154187var prompt: string = " hello"
155188var interactive: bool = false
156189var reversed_role = false
190+ var use_multiple_lines = false
157191
158192for i in 1 .. paramCount ():
159193 if paramStr (i) in [" -i" , " --interactive" ]:
@@ -162,6 +196,9 @@ for i in 1 .. paramCount():
162196 if paramStr (i) in [" --reversed_role" ]:
163197 reversed_role = true
164198
199+ if paramStr (i) in [" --multi" ]:
200+ use_multiple_lines = true
201+
165202 if (i > 1 ) and (paramStr (i - 1 ) in [" -p" , " --prompt" ]):
166203 prompt = paramStr (i)
167204
@@ -181,17 +218,20 @@ if interactive:
181218 let user_tag = " You > "
182219 let ai_tag = " A.I. > "
183220
221+ enableTrueColors ()
222+
184223 if reversed_role:
185224 stdout.write (ai_tag)
186225 stdout.writeLine (prompt)
187226 chatllm_history_append (chat, ord (RoleType .ROLE_USER ), prompt.cstring )
188227
189228 while true :
190229 stdout.write (user_tag)
191- let input = stdin. readLine ( )
230+ let input = user_input (use_multiple_lines )
192231 if input.isEmptyOrWhitespace (): continue
193232
194233 stdout.write (ai_tag)
234+ ht.reset ()
195235 let r = chatllm_user_input (chat, input.cstring )
196236 if r != 0 :
197237 echo " >>> chatllm_user_input error: " , r
0 commit comments