6
6
7
7
import inspect
8
8
import json
9
- import logging
10
9
from enum import Enum
11
10
from typing import Annotated , Any , Literal , Union , get_args , get_origin
12
11
13
12
from pydantic import BaseModel
14
13
from pydantic .fields import FieldInfo
15
14
from pydantic_core import PydanticUndefinedType
16
15
17
- log = logging .getLogger (__name__ )
16
+ from llama_stack .log import get_logger
17
+
18
+ logger = get_logger (name = __name__ , category = "core" )
18
19
19
20
20
21
def is_list_of_primitives (field_type ):
@@ -107,7 +108,7 @@ def prompt_for_discriminated_union(
107
108
108
109
if discriminator_value in type_map :
109
110
chosen_type = type_map [discriminator_value ]
110
- log .info (f"\n Configuring { chosen_type .__name__ } :" )
111
+ logger .info (f"\n Configuring { chosen_type .__name__ } :" )
111
112
112
113
if existing_value and (getattr (existing_value , discriminator ) != discriminator_value ):
113
114
existing_value = None
@@ -117,7 +118,7 @@ def prompt_for_discriminated_union(
117
118
setattr (sub_config , discriminator , discriminator_value )
118
119
return sub_config
119
120
else :
120
- log .error (f"Invalid { discriminator } . Please try again." )
121
+ logger .error (f"Invalid { discriminator } . Please try again." )
121
122
122
123
123
124
# This is somewhat elaborate, but does not purport to be comprehensive in any way.
@@ -166,7 +167,7 @@ def prompt_for_config(config_type: type[BaseModel], existing_config: BaseModel |
166
167
config_data [field_name ] = validated_value
167
168
break
168
169
except KeyError :
169
- log .error (f"Invalid choice. Please choose from: { ', ' .join (e .name for e in field_type )} " )
170
+ logger .error (f"Invalid choice. Please choose from: { ', ' .join (e .name for e in field_type )} " )
170
171
continue
171
172
172
173
if is_discriminated_union (field ):
@@ -179,7 +180,7 @@ def prompt_for_config(config_type: type[BaseModel], existing_config: BaseModel |
179
180
config_data [field_name ] = None
180
181
continue
181
182
nested_type = get_non_none_type (field_type )
182
- log .info (f"Entering sub-configuration for { field_name } :" )
183
+ logger .info (f"Entering sub-configuration for { field_name } :" )
183
184
config_data [field_name ] = prompt_for_config (nested_type , existing_value )
184
185
elif is_optional (field_type ) and is_discriminated_union (get_non_none_type (field_type )):
185
186
prompt = f"Do you want to configure { field_name } ? (y/n): "
@@ -193,7 +194,7 @@ def prompt_for_config(config_type: type[BaseModel], existing_config: BaseModel |
193
194
existing_value ,
194
195
)
195
196
elif can_recurse (field_type ):
196
- log .info (f"\n Entering sub-configuration for { field_name } :" )
197
+ logger .info (f"\n Entering sub-configuration for { field_name } :" )
197
198
config_data [field_name ] = prompt_for_config (
198
199
field_type ,
199
200
existing_value ,
@@ -220,7 +221,7 @@ def prompt_for_config(config_type: type[BaseModel], existing_config: BaseModel |
220
221
config_data [field_name ] = None
221
222
break
222
223
else :
223
- log .error ("This field is required. Please provide a value." )
224
+ logger .error ("This field is required. Please provide a value." )
224
225
continue
225
226
else :
226
227
try :
@@ -242,10 +243,10 @@ def prompt_for_config(config_type: type[BaseModel], existing_config: BaseModel |
242
243
value = [element_type (item ) for item in value ]
243
244
244
245
except json .JSONDecodeError :
245
- log .error ('Invalid JSON. Please enter a valid JSON-encoded list e.g., ["foo","bar"]' )
246
+ logger .error ('Invalid JSON. Please enter a valid JSON-encoded list e.g., ["foo","bar"]' )
246
247
continue
247
248
except ValueError as e :
248
- log .error (f"{ str (e )} " )
249
+ logger .error (f"{ str (e )} " )
249
250
continue
250
251
251
252
elif get_origin (field_type ) is dict :
@@ -255,7 +256,7 @@ def prompt_for_config(config_type: type[BaseModel], existing_config: BaseModel |
255
256
raise ValueError ("Input must be a JSON-encoded dictionary" )
256
257
257
258
except json .JSONDecodeError :
258
- log .error ("Invalid JSON. Please enter a valid JSON-encoded dict." )
259
+ logger .error ("Invalid JSON. Please enter a valid JSON-encoded dict." )
259
260
continue
260
261
261
262
# Convert the input to the correct type
@@ -268,7 +269,9 @@ def prompt_for_config(config_type: type[BaseModel], existing_config: BaseModel |
268
269
value = field_type (user_input )
269
270
270
271
except ValueError :
271
- log .error (f"Invalid input. Expected type: { getattr (field_type , '__name__' , str (field_type ))} " )
272
+ logger .error (
273
+ f"Invalid input. Expected type: { getattr (field_type , '__name__' , str (field_type ))} "
274
+ )
272
275
continue
273
276
274
277
try :
@@ -277,6 +280,6 @@ def prompt_for_config(config_type: type[BaseModel], existing_config: BaseModel |
277
280
config_data [field_name ] = validated_value
278
281
break
279
282
except ValueError as e :
280
- log .error (f"Validation error: { str (e )} " )
283
+ logger .error (f"Validation error: { str (e )} " )
281
284
282
285
return config_type (** config_data )
0 commit comments