kikocis/llama-stack public
Changesets · Branches · Tags · Compare
| Changeset | 147493617926e3b901b684f1364c68662b603339830ff6d2c91d274bb08b0b9b |
|---|---|
| Author | Ashwin Bharambe <ashwin.bharambe@gmail.com> · 2024-09-14 12:22 -07:00 |
| Committer | Ashwin Bharambe <ashwin.bharambe@gmail.com> · 2024-09-14 14:12 -07:00 |
| Parents | fdc098c86bcc |
1 file changed
modified llama_toolchain/common/prompt_for_config.py +81 −49
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | from typing import Any, get_args, get_origin, List, Literal, Optional, Type, Union |
| 12 | 12 | |
| 13 | 13 | from pydantic import BaseModel |
| 14 | +from pydantic.fields import FieldInfo | |
| 14 | 15 | from pydantic_core import PydanticUndefinedType |
| 15 | 16 | |
| 16 | 17 | from typing_extensions import Annotated |
| @@ -26,6 +27,12 @@ | ||
| 26 | 27 | return False |
| 27 | 28 | |
| 28 | 29 | |
| 30 | +def can_recurse(typ): | |
| 31 | + return ( | |
| 32 | + inspect.isclass(typ) and issubclass(typ, BaseModel) and len(typ.__fields__) > 0 | |
| 33 | + ) | |
| 34 | + | |
| 35 | + | |
| 29 | 36 | def get_literal_values(field): |
| 30 | 37 | """Extract literal values from a field if it's a Literal type.""" |
| 31 | 38 | if get_origin(field.annotation) is Literal: |
| @@ -52,6 +59,60 @@ | ||
| 52 | 59 | return value |
| 53 | 60 | |
| 54 | 61 | |
| 62 | +def is_discriminated_union(typ) -> bool: | |
| 63 | + if isinstance(typ, FieldInfo): | |
| 64 | + return typ.discriminator | |
| 65 | + else: | |
| 66 | + if not (get_origin(typ) is Annotated): | |
| 67 | + return False | |
| 68 | + args = get_args(typ) | |
| 69 | + return len(args) >= 2 and args[1].discriminator | |
| 70 | + | |
| 71 | + | |
| 72 | +def prompt_for_discriminated_union( | |
| 73 | + field_name, | |
| 74 | + typ, | |
| 75 | + existing_value, | |
| 76 | +): | |
| 77 | + if isinstance(typ, FieldInfo): | |
| 78 | + inner_type = typ.annotation | |
| 79 | + discriminator = typ.discriminator | |
| 80 | + else: | |
| 81 | + args = get_args(typ) | |
| 82 | + inner_type = args[0] | |
| 83 | + discriminator = args[1].discriminator | |
| 84 | + | |
| 85 | + union_types = get_args(inner_type) | |
| 86 | + # Find the discriminator field in each union type | |
| 87 | + type_map = {} | |
| 88 | + for t in union_types: | |
| 89 | + disc_field = t.__fields__[discriminator] | |
| 90 | + literal_values = get_literal_values(disc_field) | |
| 91 | + if literal_values: | |
| 92 | + for value in literal_values: | |
| 93 | + type_map[value] = t | |
| 94 | + | |
| 95 | + while True: | |
| 96 | + discriminator_value = input( | |
| 97 | + f"Enter `{discriminator}` for {field_name} (options: {', '.join(type_map.keys())}): " | |
| 98 | + ) | |
| 99 | + if discriminator_value in type_map: | |
| 100 | + chosen_type = type_map[discriminator_value] | |
| 101 | + print(f"\nConfiguring {chosen_type.__name__}:") | |
| 102 | + | |
| 103 | + if existing_value and ( | |
| 104 | + getattr(existing_value, discriminator) != discriminator_value | |
| 105 | + ): | |
| 106 | + existing_value = None | |
| 107 | + | |
| 108 | + sub_config = prompt_for_config(chosen_type, existing_value) | |
| 109 | + # Set the discriminator field in the sub-config | |
| 110 | + setattr(sub_config, discriminator, discriminator_value) | |
| 111 | + return sub_config | |
| 112 | + else: | |
| 113 | + print(f"Invalid {discriminator}. Please try again.") | |
| 114 | + | |
| 115 | + | |
| 55 | 116 | # This is somewhat elaborate, but does not purport to be comprehensive in any way. |
| 56 | 117 | # We should add handling for the most common cases to tide us over. |
| 57 | 118 | # |
| @@ -73,7 +134,6 @@ | ||
| 73 | 134 | |
| 74 | 135 | for field_name, field in config_type.__fields__.items(): |
| 75 | 136 | field_type = field.annotation |
| 76 | - | |
| 77 | 137 | existing_value = ( |
| 78 | 138 | getattr(existing_config, field_name) if existing_config else None |
| 79 | 139 | ) |
| @@ -107,50 +167,13 @@ | ||
| 107 | 167 | ) |
| 108 | 168 | continue |
| 109 | 169 | |
| 110 | - # Check if the field is a discriminated union | |
| 111 | - if get_origin(field_type) is Annotated: | |
| 112 | - inner_type = get_args(field_type)[0] | |
| 113 | - if get_origin(inner_type) is Union: | |
| 114 | - discriminator = field.field_info.discriminator | |
| 115 | - if discriminator: | |
| 116 | - union_types = get_args(inner_type) | |
| 117 | - # Find the discriminator field in each union type | |
| 118 | - type_map = {} | |
| 119 | - for t in union_types: | |
| 120 | - disc_field = t.__fields__[discriminator] | |
| 121 | - literal_values = get_literal_values(disc_field) | |
| 122 | - if literal_values: | |
| 123 | - for value in literal_values: | |
| 124 | - type_map[value] = t | |
| 125 | - | |
| 126 | - while True: | |
| 127 | - discriminator_value = input( | |
| 128 | - f"Enter the {discriminator} (options: {', '.join(type_map.keys())}): " | |
| 129 | - ) | |
| 130 | - if discriminator_value in type_map: | |
| 131 | - chosen_type = type_map[discriminator_value] | |
| 132 | - print(f"\nConfiguring {chosen_type.__name__}:") | |
| 133 | - | |
| 134 | - if existing_value and ( | |
| 135 | - getattr(existing_value, discriminator) | |
| 136 | - != discriminator_value | |
| 137 | - ): | |
| 138 | - existing_value = None | |
| 139 | - | |
| 140 | - sub_config = prompt_for_config(chosen_type, existing_value) | |
| 141 | - config_data[field_name] = sub_config | |
| 142 | - # Set the discriminator field in the sub-config | |
| 143 | - setattr(sub_config, discriminator, discriminator_value) | |
| 144 | - break | |
| 145 | - else: | |
| 146 | - print(f"Invalid {discriminator}. Please try again.") | |
| 147 | - continue | |
| 148 | - | |
| 149 | - if ( | |
| 150 | - is_optional(field_type) | |
| 151 | - and inspect.isclass(get_non_none_type(field_type)) | |
| 152 | - and issubclass(get_non_none_type(field_type), BaseModel) | |
| 153 | - ): | |
| 170 | + if is_discriminated_union(field): | |
| 171 | + config_data[field_name] = prompt_for_discriminated_union( | |
| 172 | + field_name, field, existing_value | |
| 173 | + ) | |
| 174 | + continue | |
| 175 | + | |
| 176 | + if is_optional(field_type) and can_recurse(get_non_none_type(field_type)): | |
| 154 | 177 | prompt = f"Do you want to configure {field_name}? (y/n): " |
| 155 | 178 | if input(prompt).lower() == "n": |
| 156 | 179 | config_data[field_name] = None |
| @@ -158,11 +181,20 @@ | ||
| 158 | 181 | nested_type = get_non_none_type(field_type) |
| 159 | 182 | print(f"Entering sub-configuration for {field_name}:") |
| 160 | 183 | config_data[field_name] = prompt_for_config(nested_type, existing_value) |
| 161 | - elif ( | |
| 162 | - inspect.isclass(field_type) | |
| 163 | - and issubclass(field_type, BaseModel) | |
| 164 | - and len(field_type.__fields__) > 0 | |
| 184 | + elif is_optional(field_type) and is_discriminated_union( | |
| 185 | + get_non_none_type(field_type) | |
| 165 | 186 | ): |
| 187 | + prompt = f"Do you want to configure {field_name}? (y/n): " | |
| 188 | + if input(prompt).lower() == "n": | |
| 189 | + config_data[field_name] = None | |
| 190 | + continue | |
| 191 | + nested_type = get_non_none_type(field_type) | |
| 192 | + config_data[field_name] = prompt_for_discriminated_union( | |
| 193 | + field_name, | |
| 194 | + nested_type, | |
| 195 | + existing_value, | |
| 196 | + ) | |
| 197 | + elif can_recurse(field_type): | |
| 166 | 198 | print(f"\nEntering sub-configuration for {field_name}:") |
| 167 | 199 | config_data[field_name] = prompt_for_config( |
| 168 | 200 | field_type, |