1414colorama .init ()
1515dbg = False
1616
17+
1718def debug (message ):
1819 if dbg :
19- print (f"{ colorama .Fore .CYAN } { colorama .Style .BRIGHT } [DEBUG]{ colorama .Style .RESET_ALL } { message } " )
20+ print (
21+ f"{ colorama .Fore .CYAN } { colorama .Style .BRIGHT } [DEBUG]{ colorama .Style .RESET_ALL } { message } "
22+ )
23+
2024
2125def error (message ):
22- print (f"{ colorama .Fore .RED } { colorama .Style .BRIGHT } [ERROR]{ colorama .Style .RESET_ALL } { message } " )
26+ print (
27+ f"{ colorama .Fore .RED } { colorama .Style .BRIGHT } [ERROR]{ colorama .Style .RESET_ALL } { message } "
28+ )
29+
2330
2431def warning (message ):
25- print (f"{ colorama .Fore .YELLOW } { colorama .Style .BRIGHT } [WARNING]{ colorama .Style .RESET_ALL } { message } " )
32+ print (
33+ f"{ colorama .Fore .YELLOW } { colorama .Style .BRIGHT } [WARNING]{ colorama .Style .RESET_ALL } { message } "
34+ )
35+
2636
2737# Set up logging
2838logging .basicConfig (level = logging .WARNING )
2939
40+
3041class Lexer :
3142 def __init__ (self , input_text ):
3243 self .tokens = []
3344 self .tokenize (input_text )
3445
3546 def tokenize (self , text ):
3647 token_specification = [
37- ("ATTRIBUTE" , r"#\[(.*?)\]" ), # Attributes e.g., #[stream(true)]
38- ("NUMBER" , r"\d+(\.\d*)?" ), # Integer or decimal number
39- ("STRING" , r"\".*?\"" ), # String literals
40- ("ID" , r"[A-Za-z_][A-Za-z0-9_]*" ), # Identifiers
41- ("LPAREN" , r"\(" ), # Left parenthesis
42- ("RPAREN" , r"\)" ), # Right parenthesis
43- ("COMMA" , r"," ), # Comma
44- ("NEWLINE" , r"\n" ), # Line endings
45- ("SKIP" , r"[ \t]+" ), # Skip over spaces and tabs
46- ("MISMATCH" , r"." ), # Any other character
48+ ("ATTRIBUTE" , r"#\[(.*?)\]" ), # Attributes e.g., #[stream(true)]
49+ ("NUMBER" , r"\d+(\.\d*)?" ), # Integer or decimal number
50+ ("STRING" , r"\".*?\"" ), # String literals
51+ ("ID" , r"[A-Za-z_][A-Za-z0-9_]*" ), # Identifiers
52+ ("LPAREN" , r"\(" ), # Left parenthesis
53+ ("RPAREN" , r"\)" ), # Right parenthesis
54+ ("COMMA" , r"," ), # Comma
55+ ("NEWLINE" , r"\n" ), # Line endings
56+ ("SKIP" , r"[ \t]+" ), # Skip over spaces and tabs
57+ ("MISMATCH" , r"." ), # Any other character
4758 ]
4859 tok_regex = "|" .join ("(?P<%s>%s)" % pair for pair in token_specification )
4960 for mo in re .finditer (tok_regex , text ):
@@ -62,6 +73,7 @@ def tokenize(self, text):
6273 error (f"Invalid character: { value } " )
6374 sys .exit (1 )
6475
76+
6577class Parser :
6678 def __init__ (self , tokens ):
6779 self .tokens = tokens
@@ -73,7 +85,9 @@ def parse(self):
7385 while self .current < len (self .tokens ):
7486 token = self .tokens [self .current ]
7587 if token [0 ] == "ATTRIBUTE" :
76- current_attributes = self .parse_attribute (token [1 ][2 :- 1 ].strip ()) # Remove #[ and ]
88+ current_attributes = self .parse_attribute (
89+ token [1 ][2 :- 1 ].strip ()
90+ ) # Remove #[ and ]
7791 self .current += 1 # Skip ATTRIBUTE
7892 elif token [0 ] == "ID" :
7993 ast .append (self .statement (current_attributes ))
@@ -103,7 +117,9 @@ def statement(self, attributes):
103117
104118 def arguments (self ):
105119 args = []
106- while self .current < len (self .tokens ) and self .tokens [self .current ][0 ] != "RPAREN" :
120+ while (
121+ self .current < len (self .tokens ) and self .tokens [self .current ][0 ] != "RPAREN"
122+ ):
107123 token = self .tokens [self .current ]
108124 if token [0 ] in {"STRING" , "NUMBER" }:
109125 args .append (token [1 ])
@@ -116,7 +132,7 @@ def arguments(self):
116132 return args
117133
118134 def parse_attribute (self , attr_str ):
119- match = re .match (r' (\w+)\((.+)\)' , attr_str )
135+ match = re .match (r" (\w+)\((.+)\)" , attr_str )
120136 if match :
121137 attr_name = match .group (1 ).lower ()
122138 attr_value = match .group (2 ).strip ('"' ).strip ("'" )
@@ -129,6 +145,7 @@ def parse_attribute(self, attr_str):
129145 error (f"Invalid attribute: { attr_str } " )
130146 sys .exit (1 )
131147
148+
132149class Interpreter :
133150 def __init__ (self , ast , llama_instance ):
134151 self .ast = ast
@@ -146,16 +163,21 @@ def execute(self):
146163 elif command == "system" :
147164 self .llama .system_command (args [0 ], attributes )
148165 elif command == "save" :
149- self .llama .create_model (args [0 ], {
150- "model" : self .llama .model ,
151- "temperature" : args [1 ],
152- "system_message" : self .llama .system [0 ]["content" ],
153- }, attributes )
166+ self .llama .create_model (
167+ args [0 ],
168+ {
169+ "model" : self .llama .model ,
170+ "temperature" : args [1 ],
171+ "system_message" : self .llama .system [0 ]["content" ],
172+ },
173+ attributes ,
174+ )
154175 elif command == "chat" :
155176 self .llama .chat (attributes )
156177 else :
157178 raise ValueError (f"Unknown command: { command } " )
158179
180+
159181class Llama :
160182 def __init__ (self ):
161183 self .model = ""
@@ -187,7 +209,9 @@ def chat(self, attributes):
187209 )
188210 debug ("Chat successful." )
189211 if stream :
190- warning ("Streaming is a work in progress. Please wait for the final response." )
212+ warning (
213+ "Streaming is a work in progress. Please wait for the final response."
214+ )
191215 for message in response :
192216 print (message ["message" ]["content" ], end = "" )
193217 print ()
@@ -253,6 +277,7 @@ def read(self, filename):
253277 logging .error ("File %s not found." , filename )
254278 error (f"File { filename } not found." )
255279
280+
256281def run ():
257282 parser = argparse .ArgumentParser (description = "Run llama script." )
258283 parser .add_argument ("file_name" , type = str , help = "The name of the file to run" )
@@ -287,5 +312,6 @@ def run():
287312 except KeyboardInterrupt :
288313 pass
289314
315+
290316if __name__ == "__main__" :
291- run ()
317+ run ()
0 commit comments