1111import json
1212import logging
1313import sys
14- from typing import Any , Dict
14+ from typing import Any , Dict , List
1515
1616from .engine import CielEngine
17+ from .hf_backends import AuxLLMBackend , PrimaryLLMBackend
1718
1819
1920def setup_logging (level : str ) -> None :
@@ -31,6 +32,9 @@ def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
3132 parser = argparse .ArgumentParser (description = "CIEL/Ω unified engine" )
3233 parser .add_argument ("--mode" , choices = ["repl" , "once" ], default = "repl" )
3334 parser .add_argument ("--text" , type = str , default = "" , help = "Input text for once mode." )
35+ parser .add_argument ("--primary-model" , type = str , default = "mistral-7b-instruct" )
36+ parser .add_argument ("--aux-model" , type = str , default = "phi-3-mini-3.8b" )
37+ parser .add_argument ("--enable-llm" , action = "store_true" , help = "Attach HF language backends." )
3438 parser .add_argument (
3539 "--log-level" ,
3640 type = str ,
@@ -56,29 +60,47 @@ def default(o: Any) -> Any:
5660 return json .dumps (obj , default = default , ensure_ascii = False , indent = 2 )
5761
5862
59- def run_repl (engine : CielEngine ) -> None :
63+ def run_repl (engine : CielEngine , enable_llm : bool ) -> None :
6064 """Process lines from stdin, printing JSON results per line."""
6165
66+ dialogue : List [Dict [str , str ]] = []
6267 print ("CIEL Engine REPL. Ctrl+D to exit." , file = sys .stderr )
6368 for line in sys .stdin :
6469 line = line .rstrip ("\n " )
6570 if not line .strip ():
6671 continue
67- result = engine . step ( line )
72+ result = _process_text ( engine , line , enable_llm , dialogue )
6873 print (_dump (result ))
6974 print ()
7075
7176
72- def run_once (engine : CielEngine , text : str ) -> None :
77+ def run_once (engine : CielEngine , text : str , enable_llm : bool ) -> None :
7378 """Run the engine once with the provided text."""
7479
7580 if not text .strip ():
7681 print ("No --text provided." , file = sys .stderr )
7782 sys .exit (1 )
78- result = engine . step ( text )
83+ result = _process_text ( engine , text , enable_llm , [] )
7984 print (_dump (result ))
8085
8186
87+ def _process_text (
88+ engine : CielEngine ,
89+ text : str ,
90+ enable_llm : bool ,
91+ dialogue : List [Dict [str , str ]],
92+ ) -> Dict [str , Any ]:
93+ if not enable_llm :
94+ return engine .step (text )
95+
96+ dialogue .append ({"role" : "user" , "content" : text })
97+ response = engine .interact (text , dialogue )
98+ reply = response .get ("reply" )
99+ if reply is not None :
100+ dialogue .append ({"role" : "assistant" , "content" : str (reply )})
101+ return response
102+
103+
82104def main (argv : list [str ] | None = None ) -> None :
83105 """Entry point used by ``python -m ciel``."""
84106
@@ -89,12 +111,17 @@ def main(argv: list[str] | None = None) -> None:
89111 log .info ("Starting CielEngine in mode=%s" , args .mode )
90112
91113 engine = CielEngine ()
114+ if args .enable_llm :
115+ primary = PrimaryLLMBackend (model_name = args .primary_model )
116+ aux = AuxLLMBackend (model_name = args .aux_model )
117+ engine .language_backend = primary
118+ engine .aux_backend = aux
92119 engine .boot ()
93120 try :
94121 if args .mode == "repl" :
95- run_repl (engine )
122+ run_repl (engine , args . enable_llm )
96123 else :
97- run_once (engine , args .text )
124+ run_once (engine , args .text , args . enable_llm )
98125 finally :
99126 engine .shutdown ()
100127
0 commit comments