Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1/n] Add load_json and load_yaml methods to AIConfigRuntime #1057

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions python/src/aiconfig/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,17 @@
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(ClaudeBedrockModelParser())
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 @@ -116,14 +119,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
Loading