Skip to content

Commit

Permalink
[1/n] Add load_json and load_yaml methods to AIConfigRuntime
Browse files Browse the repository at this point in the history
Allow loading an AIConfigRuntime from a string. There's already a `loadJSON` method in the TS SDK.

This is needed for VSCode extension (top of stack), which gets the state of the document from the editor (and which can contain unsaved changes that aren't present on the filesystem).

Test Plan:

Tested out getting started ipynb with:

load JSON:

```
config = AIConfigRuntime.load('travel.aiconfig.json')
```

load YAML:
```
config.save('travel.aiconfig.yaml', mode="yaml")
yaml_str = ""
with open('travel.aiconfig.yaml') as file:
    yaml_str = file.read()

config = AIConfigRuntime.load_yaml(yaml_str)
```

Made sure config runtime is valid and tried `config.run`
  • Loading branch information
saqadri committed Jan 29, 2024
1 parent 057af07 commit c6c70a6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
42 changes: 34 additions & 8 deletions python/src/aiconfig/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@
ModelParserRegistry.register_model_parser(
DefaultAnyscaleEndpointParser("AnyscaleEndpoint")
)
ModelParserRegistry.register_model_parser(GeminiModelParser("gemini-pro"), ["gemini-pro"])
ModelParserRegistry.register_model_parser(
GeminiModelParser("gemini-pro"), ["gemini-pro"]
)
ModelParserRegistry.register_model_parser(HuggingFaceTextGenerationParser())
for model in gpt_models_extra:
ModelParserRegistry.register_model_parser(DefaultOpenAIParser(model))
ModelParserRegistry.register_model_parser(PaLMChatParser())
ModelParserRegistry.register_model_parser(PaLMTextParser())


class AIConfigRuntime(AIConfig):
# A mapping of model names to their respective parsers

Expand Down Expand Up @@ -114,14 +117,37 @@ def load(cls, config_filepath: str) -> "AIConfigRuntime":
else:
data = file.read()

# load the file as bytes and let pydantic handle the parsing
# validated_data = AIConfig.model_validate_json(file.read())
aiconfigruntime = cls.model_validate_json(data)
update_model_parser_registry_with_config_runtime(aiconfigruntime)

config_runtime = cls.load_json(data)
# set the file path. This is used when saving the config
aiconfigruntime.file_path = config_filepath
return aiconfigruntime
config_runtime.file_path = config_filepath
return config_runtime

@classmethod
def load_json(cls, config_json: str) -> "AIConfigRuntime":
"""
Constructs AIConfigRuntime from provided JSON and returns it.
Args:
config_json (str): The JSON representing the AIConfig.
"""

config_runtime = cls.model_validate_json(config_json)
update_model_parser_registry_with_config_runtime(config_runtime)

return config_runtime

@classmethod
def load_yaml(cls, config_yaml: str) -> "AIConfigRuntime":
"""
Constructs AIConfigRuntime from provided YAML and returns it.
Args:
config_yaml (str): The YAML representing the AIConfig.
"""

yaml_data = yaml.safe_load(config_yaml)
config_json = json.dumps(yaml_data)
return cls.load_json(config_json)

@classmethod
def load_from_workbook(cls, workbook_id: str) -> "AIConfigRuntime":
Expand Down
14 changes: 14 additions & 0 deletions typescript/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,25 @@ export class AIConfigRuntime implements AIConfig {
return config;
}

/**
* Loads an AIConfig from a YAML string.
* @param aiConfigYAML YAML string to load the AIConfig from.
*/
public static loadYAML(aiConfigYAML: string) {
const aiConfigObj = yaml.load(aiConfigYAML);
return this.loadJSON(aiConfigObj);
}

/**
* Loads an AIConfig from a JSON object.
* @param aiConfigObj JSON object to load the AIConfig from.
*/
public static loadJSON(aiConfigObj: any) {
if (typeof aiConfigObj === "string") {
// Parse the string as JSON
aiConfigObj = JSON.parse(aiConfigObj);
}

// TODO: saqadri - validate that the type satisfies AIConfig interface
const aiConfig = new AIConfigRuntime(
aiConfigObj.name,
Expand Down

0 comments on commit c6c70a6

Please sign in to comment.