2024-10-03 18:21:27 -07:00
|
|
|
import os
|
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
import yaml
|
|
|
|
|
from jsonschema import validate
|
|
|
|
|
|
2024-10-09 11:25:07 -07:00
|
|
|
ENVOY_CONFIG_TEMPLATE_FILE = os.getenv(
|
|
|
|
|
"ENVOY_CONFIG_TEMPLATE_FILE", "envoy.template.yaml"
|
|
|
|
|
)
|
2024-11-15 10:44:01 -08:00
|
|
|
ARCH_CONFIG_FILE = os.getenv("ARCH_CONFIG_FILE", "/app/arch_config.yaml")
|
2024-10-09 11:25:07 -07:00
|
|
|
ENVOY_CONFIG_FILE_RENDERED = os.getenv(
|
|
|
|
|
"ENVOY_CONFIG_FILE_RENDERED", "/etc/envoy/envoy.yaml"
|
|
|
|
|
)
|
|
|
|
|
ARCH_CONFIG_SCHEMA_FILE = os.getenv(
|
|
|
|
|
"ARCH_CONFIG_SCHEMA_FILE", "arch_config_schema.yaml"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_secret_key_to_llm_providers(config_yaml):
|
2024-10-03 18:21:27 -07:00
|
|
|
llm_providers = []
|
|
|
|
|
for llm_provider in config_yaml.get("llm_providers", []):
|
2024-10-09 11:25:07 -07:00
|
|
|
access_key_env_var = llm_provider.get("access_key", False)
|
2024-10-04 11:10:59 -07:00
|
|
|
access_key_value = os.getenv(access_key_env_var, False)
|
|
|
|
|
if access_key_env_var and access_key_value:
|
2024-10-09 11:25:07 -07:00
|
|
|
llm_provider["access_key"] = access_key_value
|
2024-10-03 18:21:27 -07:00
|
|
|
llm_providers.append(llm_provider)
|
|
|
|
|
config_yaml["llm_providers"] = llm_providers
|
|
|
|
|
return config_yaml
|
|
|
|
|
|
2024-10-09 11:25:07 -07:00
|
|
|
|
2024-10-03 18:21:27 -07:00
|
|
|
def validate_and_render_schema():
|
2024-10-09 11:25:07 -07:00
|
|
|
env = Environment(loader=FileSystemLoader("./"))
|
|
|
|
|
template = env.get_template("envoy.template.yaml")
|
2024-10-03 18:21:27 -07:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
validate_prompt_config(ARCH_CONFIG_FILE, ARCH_CONFIG_SCHEMA_FILE)
|
|
|
|
|
except Exception as e:
|
2024-10-30 17:54:51 -07:00
|
|
|
print(str(e))
|
2024-10-09 11:25:07 -07:00
|
|
|
exit(1) # validate_prompt_config failed. Exit
|
2024-10-03 18:21:27 -07:00
|
|
|
|
2024-10-09 11:25:07 -07:00
|
|
|
with open(ARCH_CONFIG_FILE, "r") as file:
|
2024-10-03 18:21:27 -07:00
|
|
|
arch_config = file.read()
|
|
|
|
|
|
2024-10-09 11:25:07 -07:00
|
|
|
with open(ARCH_CONFIG_SCHEMA_FILE, "r") as file:
|
2024-10-03 18:21:27 -07:00
|
|
|
arch_config_schema = file.read()
|
|
|
|
|
|
|
|
|
|
config_yaml = yaml.safe_load(arch_config)
|
|
|
|
|
config_schema_yaml = yaml.safe_load(arch_config_schema)
|
|
|
|
|
inferred_clusters = {}
|
|
|
|
|
|
2024-10-28 20:05:06 -04:00
|
|
|
if "prompt_targets" in config_yaml:
|
|
|
|
|
for prompt_target in config_yaml["prompt_targets"]:
|
|
|
|
|
name = prompt_target.get("endpoint", {}).get("name", "")
|
|
|
|
|
if name not in inferred_clusters:
|
|
|
|
|
inferred_clusters[name] = {
|
|
|
|
|
"name": name,
|
|
|
|
|
"port": 80, # default port
|
|
|
|
|
}
|
2024-10-03 18:21:27 -07:00
|
|
|
|
|
|
|
|
endpoints = config_yaml.get("endpoints", {})
|
|
|
|
|
|
|
|
|
|
# override the inferred clusters with the ones defined in the config
|
|
|
|
|
for name, endpoint_details in endpoints.items():
|
|
|
|
|
if name in inferred_clusters:
|
|
|
|
|
print("updating cluster", endpoint_details)
|
|
|
|
|
inferred_clusters[name].update(endpoint_details)
|
2024-10-09 11:25:07 -07:00
|
|
|
endpoint = inferred_clusters[name]["endpoint"]
|
|
|
|
|
if len(endpoint.split(":")) > 1:
|
|
|
|
|
inferred_clusters[name]["endpoint"] = endpoint.split(":")[0]
|
|
|
|
|
inferred_clusters[name]["port"] = int(endpoint.split(":")[1])
|
2024-10-03 18:21:27 -07:00
|
|
|
else:
|
|
|
|
|
inferred_clusters[name] = endpoint_details
|
|
|
|
|
|
|
|
|
|
print("updated clusters", inferred_clusters)
|
|
|
|
|
|
|
|
|
|
arch_llm_providers = config_yaml["llm_providers"]
|
2024-10-08 16:24:08 -07:00
|
|
|
arch_tracing = config_yaml.get("tracing", {})
|
2024-10-03 18:21:27 -07:00
|
|
|
arch_config_string = yaml.dump(config_yaml)
|
2024-10-09 15:47:32 -07:00
|
|
|
config_yaml["mode"] = "llm"
|
|
|
|
|
arch_llm_config_string = yaml.dump(config_yaml)
|
2024-10-03 18:21:27 -07:00
|
|
|
|
|
|
|
|
data = {
|
2024-10-09 11:25:07 -07:00
|
|
|
"arch_config": arch_config_string,
|
2024-10-09 15:47:32 -07:00
|
|
|
"arch_llm_config": arch_llm_config_string,
|
2024-10-09 11:25:07 -07:00
|
|
|
"arch_clusters": inferred_clusters,
|
|
|
|
|
"arch_llm_providers": arch_llm_providers,
|
|
|
|
|
"arch_tracing": arch_tracing,
|
2024-10-03 18:21:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rendered = template.render(data)
|
|
|
|
|
print(ENVOY_CONFIG_FILE_RENDERED)
|
2024-10-09 11:25:07 -07:00
|
|
|
with open(ENVOY_CONFIG_FILE_RENDERED, "w") as file:
|
2024-10-03 18:21:27 -07:00
|
|
|
file.write(rendered)
|
|
|
|
|
|
2024-10-09 11:25:07 -07:00
|
|
|
|
2024-10-03 18:21:27 -07:00
|
|
|
def validate_prompt_config(arch_config_file, arch_config_schema_file):
|
2024-10-09 11:25:07 -07:00
|
|
|
with open(arch_config_file, "r") as file:
|
2024-10-03 18:21:27 -07:00
|
|
|
arch_config = file.read()
|
|
|
|
|
|
2024-10-09 11:25:07 -07:00
|
|
|
with open(arch_config_schema_file, "r") as file:
|
2024-10-03 18:21:27 -07:00
|
|
|
arch_config_schema = file.read()
|
|
|
|
|
|
|
|
|
|
config_yaml = yaml.safe_load(arch_config)
|
|
|
|
|
config_schema_yaml = yaml.safe_load(arch_config_schema)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
validate(config_yaml, config_schema_yaml)
|
|
|
|
|
except Exception as e:
|
2024-10-09 11:25:07 -07:00
|
|
|
print(
|
2024-11-26 13:13:02 -08:00
|
|
|
f"Error validating arch_config file: {arch_config_file}, schema file: {arch_config_schema_file}, error: {e.message}"
|
2024-10-09 11:25:07 -07:00
|
|
|
)
|
2024-10-03 18:21:27 -07:00
|
|
|
raise e
|
|
|
|
|
|
2024-10-09 11:25:07 -07:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-10-03 18:21:27 -07:00
|
|
|
validate_and_render_schema()
|