add export openAPI to config

This commit is contained in:
TerminalMan 2024-09-15 00:17:36 +01:00
parent 533e7c9119
commit 0903f852db
5 changed files with 67 additions and 15 deletions

27
common/actions.py Normal file
View file

@ -0,0 +1,27 @@
import json
from loguru import logger
from common.tabby_config import config
from endpoints.server import export_openapi
from common.config_models import generate_config_file
def branch_to_actions() -> bool:
if config.actions.export_openapi:
openapi_json = export_openapi()
with open(config.actions.openapi_export_path, "w") as f:
f.write(json.dumps(openapi_json))
logger.info(
"Successfully wrote OpenAPI spec to "
+ f"{config.actions.openapi_export_path}"
)
elif config.actions.export_config:
generate_config_file(config.actions.config_export_path)
else:
# did not branch
return False
# branched and ran an action
return True

View file

@ -8,14 +8,28 @@ from pydantic import BaseModel
from common.config_models import TabbyConfigModel
def is_list_type(type_hint):
if hasattr(type_hint, "__origin__") and type_hint.__origin__ is list:
return True
if hasattr(type_hint, "__args__"):
# Recursively check for lists inside type arguments
return any(is_list_type(arg) for arg in type_hint.__args__)
return False
def add_field_to_group(group, field_name, field_type, field) -> None:
"""
Adds a Pydantic field to an argparse argument group.
"""
help_text = field.description if field.description else "No description available"
kwargs = {
"help": field.description if field.description else "No description available",
}
group.add_argument(f"--{field_name}", help=help_text)
if is_list_type(field_type):
kwargs["nargs"] = "+"
group.add_argument(f"--{field_name}", **kwargs)
def init_argparser() -> argparse.ArgumentParser:

View file

@ -1,5 +1,6 @@
from pydantic import BaseModel, ConfigDict, Field
from typing import List, Literal, Optional, Union
from pathlib import Path
CACHE_SIZES = Literal["FP16", "Q8", "Q6", "Q4"]
@ -11,6 +12,22 @@ class ConfigOverrideConfig(BaseModel):
)
class UtilityActions(BaseModel):
export_config: Optional[str] = Field(
None, description="generate a template config file"
)
config_export_path: Optional[Path] = Field(
"config_sample.yml", description="path to export configuration file to"
)
export_openapi: Optional[bool] = Field(
False, description="export openapi schema files"
)
openapi_export_path: Optional[Path] = Field(
"openapi.json", description="path to export openapi schema to"
)
class NetworkConfig(BaseModel):
host: Optional[str] = Field("127.0.0.1", description=("The IP to host on"))
port: Optional[int] = Field(5000, description=("The port to host on"))
@ -308,6 +325,7 @@ class TabbyConfigModel(BaseModel):
embeddings: EmbeddingsConfig = Field(
default_factory=EmbeddingsConfig.model_construct
)
actions: UtilityActions = Field(default_factory=UtilityActions.model_construct)
model_config = ConfigDict(validate_assignment=True, protected_namespaces=())