mirror of
https://github.com/theroyallab/tabbyAPI.git
synced 2026-05-01 03:31:24 +00:00
Config: Alter YAML generation script for formatting adherence
Properly add comments and newlines where they need to go. Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
@@ -333,12 +333,8 @@ class DraftModelConfig(BaseConfigModel):
|
||||
class LoraInstanceModel(BaseConfigModel):
|
||||
"""Model representing an instance of a Lora."""
|
||||
|
||||
name: Optional[str] = Field(None, description=("Name of the LoRA model."))
|
||||
scaling: float = Field(
|
||||
1.0,
|
||||
description=("Scaling factor for the LoRA model (default: 1.0)."),
|
||||
ge=0,
|
||||
)
|
||||
name: Optional[str] = None
|
||||
scaling: float = Field(1.0, ge=0)
|
||||
|
||||
|
||||
class LoraConfig(BaseConfigModel):
|
||||
|
||||
@@ -2,15 +2,15 @@ import pathlib
|
||||
from inspect import getdoc
|
||||
from os import getenv
|
||||
from textwrap import dedent
|
||||
from typing import Any, Optional
|
||||
from typing import Optional
|
||||
|
||||
from loguru import logger
|
||||
from pydantic import BaseModel
|
||||
from pydantic_core import PydanticUndefined
|
||||
from ruamel.yaml import YAML
|
||||
from ruamel.yaml.comments import CommentedMap, CommentedSeq
|
||||
from ruamel.yaml.scalarstring import PreservedScalarString
|
||||
|
||||
from common.config_models import TabbyConfigModel
|
||||
from common.config_models import BaseConfigModel, TabbyConfigModel
|
||||
from common.utils import merge_dicts, unwrap
|
||||
|
||||
yaml = YAML()
|
||||
@@ -174,22 +174,10 @@ config: TabbyConfig = TabbyConfig()
|
||||
def generate_config_file(
|
||||
model: BaseModel = None,
|
||||
filename: str = "config_sample.yml",
|
||||
indentation: int = 2,
|
||||
) -> None:
|
||||
"""Creates a config.yml file from Pydantic models."""
|
||||
|
||||
schema = unwrap(model, TabbyConfigModel())
|
||||
preamble = get_preamble()
|
||||
|
||||
yaml_content = pydantic_model_to_yaml(schema)
|
||||
|
||||
with open(filename, "w") as f:
|
||||
f.write(preamble)
|
||||
yaml.dump(yaml_content, f)
|
||||
|
||||
|
||||
def get_preamble() -> str:
|
||||
"""Returns the cleaned up preamble for the config file."""
|
||||
preamble = """
|
||||
# Sample YAML file for configuration.
|
||||
# Comment and uncomment values as needed.
|
||||
@@ -199,43 +187,80 @@ def get_preamble() -> str:
|
||||
# Unless specified in the comments, DO NOT put these options in quotes!
|
||||
# You can use https://www.yamllint.com/ if you want to check your YAML formatting.\n
|
||||
"""
|
||||
return dedent(preamble).lstrip()
|
||||
|
||||
yaml_content = pydantic_model_to_yaml(schema)
|
||||
|
||||
with open(filename, "w") as f:
|
||||
f.write(dedent(preamble).lstrip())
|
||||
yaml.dump(yaml_content, f)
|
||||
|
||||
|
||||
# Function to convert pydantic model to dict with field descriptions as comments
|
||||
def pydantic_model_to_yaml(model: BaseModel) -> CommentedMap:
|
||||
def pydantic_model_to_yaml(model: BaseModel, indentation: int = 0) -> CommentedMap:
|
||||
"""
|
||||
Recursively converts a Pydantic model into a CommentedMap,
|
||||
with descriptions as comments in YAML.
|
||||
"""
|
||||
|
||||
# Create a CommentedMap to hold the output data
|
||||
yaml_data = CommentedMap()
|
||||
|
||||
# Loop through all fields in the model
|
||||
iteration = 1
|
||||
for field_name, field_info in model.model_fields.items():
|
||||
# Get the inner pydantic model
|
||||
value = getattr(model, field_name)
|
||||
|
||||
if isinstance(value, BaseConfigModel):
|
||||
# If the field is another Pydantic model
|
||||
if isinstance(value, BaseModel):
|
||||
yaml_data[field_name] = pydantic_model_to_yaml(value)
|
||||
# If the field is a list of Pydantic models
|
||||
elif (
|
||||
isinstance(value, list)
|
||||
and len(value) > 0
|
||||
and isinstance(value[0], BaseModel)
|
||||
):
|
||||
yaml_list = CommentedSeq()
|
||||
for item in value:
|
||||
yaml_list.append(pydantic_model_to_yaml(item))
|
||||
yaml_data[field_name] = yaml_list
|
||||
# Otherwise, just assign the value
|
||||
else:
|
||||
yaml_data[field_name] = value
|
||||
|
||||
# Add field description as a comment if available
|
||||
if field_info.description:
|
||||
if not value._metadata.include_in_config:
|
||||
continue
|
||||
|
||||
yaml_data[field_name] = pydantic_model_to_yaml(
|
||||
value, indentation=indentation + 2
|
||||
)
|
||||
comment = getdoc(value)
|
||||
elif isinstance(value, list) and len(value) > 0:
|
||||
# If the field is a list
|
||||
|
||||
yaml_list = CommentedSeq()
|
||||
if isinstance(value[0], BaseModel):
|
||||
# If the field is a list of Pydantic models
|
||||
# Do not add comments for these items
|
||||
|
||||
for item in value:
|
||||
yaml_list.append(
|
||||
pydantic_model_to_yaml(item, indentation=indentation + 2)
|
||||
)
|
||||
else:
|
||||
# If the field is a normal list, prefer the YAML flow style
|
||||
|
||||
yaml_list.fa.set_flow_style()
|
||||
yaml_list += [
|
||||
PreservedScalarString(element)
|
||||
if isinstance(element, str)
|
||||
else element
|
||||
for element in value
|
||||
]
|
||||
|
||||
yaml_data[field_name] = yaml_list
|
||||
comment = field_info.description
|
||||
else:
|
||||
# Otherwise, just assign the value
|
||||
|
||||
yaml_data[field_name] = value
|
||||
comment = field_info.description
|
||||
|
||||
if comment:
|
||||
# Add a newline to every comment but the first one
|
||||
if iteration != 1:
|
||||
comment = f"\n{comment}"
|
||||
|
||||
yaml_data.yaml_set_comment_before_after_key(
|
||||
field_name, before=field_info.description
|
||||
field_name, before=comment, indent=indentation
|
||||
)
|
||||
|
||||
# Increment the iteration counter
|
||||
iteration += 1
|
||||
|
||||
return yaml_data
|
||||
|
||||
@@ -18,27 +18,27 @@ network:
|
||||
# Disable HTTP token authentication with requests.
|
||||
# WARNING: This will make your instance vulnerable!
|
||||
# Turn on this option if you are ONLY connecting from localhost.
|
||||
disable_auth: False
|
||||
disable_auth: false
|
||||
|
||||
# Send tracebacks over the API (default: False).
|
||||
# NOTE: Only enable this for debug purposes.
|
||||
send_tracebacks: False
|
||||
send_tracebacks: false
|
||||
|
||||
# Select API servers to enable (default: ["OAI"]).
|
||||
# Possible values: OAI, Kobold.
|
||||
api_servers: ['OAI']
|
||||
api_servers: ["OAI"]
|
||||
|
||||
# Options for logging
|
||||
logging:
|
||||
# Enable prompt logging (default: False).
|
||||
log_prompt: False
|
||||
log_prompt: false
|
||||
|
||||
# Enable generation parameter logging (default: False).
|
||||
log_generation_params: False
|
||||
log_generation_params: false
|
||||
|
||||
# Enable request logging (default: False).
|
||||
# NOTE: Only use this for debugging!
|
||||
log_requests: False
|
||||
log_requests: false
|
||||
|
||||
# Options for model overrides and loading
|
||||
# Please read the comments to understand how arguments are handled
|
||||
@@ -49,11 +49,11 @@ model:
|
||||
model_dir: models
|
||||
|
||||
# Allow direct loading of models from a completion or chat completion request (default: False).
|
||||
inline_model_loading: False
|
||||
inline_model_loading: false
|
||||
|
||||
# Sends dummy model names when the models endpoint is queried.
|
||||
# Enable this if the client is looking for specific OAI models.
|
||||
use_dummy_models: False
|
||||
use_dummy_models: false
|
||||
|
||||
# An initial model to load.
|
||||
# Make sure the model is located in the model directory!
|
||||
@@ -77,11 +77,11 @@ model:
|
||||
# Load model with tensor parallelism.
|
||||
# Falls back to autosplit if GPU split isn't provided.
|
||||
# This ignores the gpu_split_auto value.
|
||||
tensor_parallel: False
|
||||
tensor_parallel: false
|
||||
|
||||
# Automatically allocate resources to GPUs (default: True).
|
||||
# Not parsed for single GPU users.
|
||||
gpu_split_auto: True
|
||||
gpu_split_auto: true
|
||||
|
||||
# Reserve VRAM used for autosplit loading (default: 96 MB on GPU 0).
|
||||
# Represented as an array of MB per GPU.
|
||||
@@ -135,7 +135,7 @@ model:
|
||||
num_experts_per_token:
|
||||
|
||||
# Enables fasttensors to possibly increase model loading speeds (default: False).
|
||||
fasttensors: False
|
||||
fasttensors: false
|
||||
|
||||
# Options for draft models (speculative decoding)
|
||||
# This will use more VRAM!
|
||||
@@ -198,19 +198,19 @@ sampling:
|
||||
developer:
|
||||
# Skip Exllamav2 version check (default: False).
|
||||
# WARNING: It's highly recommended to update your dependencies rather than enabling this flag.
|
||||
unsafe_launch: False
|
||||
unsafe_launch: false
|
||||
|
||||
# Disable API request streaming (default: False).
|
||||
disable_request_streaming: False
|
||||
disable_request_streaming: false
|
||||
|
||||
# Enable the torch CUDA malloc backend (default: False).
|
||||
cuda_malloc_backend: False
|
||||
cuda_malloc_backend: false
|
||||
|
||||
# Run asyncio using Uvloop or Winloop which can improve performance.
|
||||
# NOTE: It's recommended to enable this, but if something breaks turn this off.
|
||||
uvloop: False
|
||||
uvloop: false
|
||||
|
||||
# Set process to use a higher priority.
|
||||
# For realtime process priority, run as administrator or sudo.
|
||||
# Otherwise, the priority will be set to high.
|
||||
realtime_process_priority: False
|
||||
realtime_process_priority: false
|
||||
|
||||
Reference in New Issue
Block a user