fix: settings now can be set from cli
This commit is contained in:
parent
cfbd22d2b0
commit
7c86f38fb3
2 changed files with 34 additions and 12 deletions
|
|
@ -2,6 +2,7 @@ import logging
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
import platform
|
import platform
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from langflow.settings import settings
|
||||||
|
|
||||||
import typer
|
import typer
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
|
@ -17,9 +18,20 @@ def get_number_of_workers(workers=None):
|
||||||
return workers
|
return workers
|
||||||
|
|
||||||
|
|
||||||
|
def update_settings(config: str):
|
||||||
|
"""Update the settings from a config file."""
|
||||||
|
if config:
|
||||||
|
settings.update_from_yaml(config)
|
||||||
|
|
||||||
|
|
||||||
def serve(
|
def serve(
|
||||||
host: str = "127.0.0.1", workers: int = 1, timeout: int = 60, port: int = 7860
|
host: str = "127.0.0.1",
|
||||||
|
workers: int = 1,
|
||||||
|
timeout: int = 60,
|
||||||
|
port: int = 7860,
|
||||||
|
config: str = "config.yaml",
|
||||||
):
|
):
|
||||||
|
update_settings(config)
|
||||||
app = create_app()
|
app = create_app()
|
||||||
# get the directory of the current file
|
# get the directory of the current file
|
||||||
path = Path(__file__).parent
|
path = Path(__file__).parent
|
||||||
|
|
|
||||||
|
|
@ -2,28 +2,39 @@ import os
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
from pydantic import BaseSettings, Field, root_validator
|
from pydantic import BaseSettings, root_validator
|
||||||
|
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
chains: List[str] = Field(default=[])
|
chains: List[str] = []
|
||||||
agents: List[str] = Field(default=[])
|
agents: List[str] = []
|
||||||
prompts: List[str] = Field(default=[])
|
prompts: List[str] = []
|
||||||
llms: List[str] = Field(default=[])
|
llms: List[str] = []
|
||||||
tools: List[str] = Field(default=[])
|
tools: List[str] = []
|
||||||
memories: List[str] = Field(default=[])
|
memories: List[str] = []
|
||||||
dev: bool = Field(default=False)
|
dev: bool = False
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
validate_assignment = True
|
validate_assignment = True
|
||||||
|
extra = "ignore"
|
||||||
|
|
||||||
@root_validator
|
@root_validator(allow_reuse=True)
|
||||||
def validate_lists(cls, values):
|
def validate_lists(cls, values):
|
||||||
for key, value in values.items():
|
for key, value in values.items():
|
||||||
if key != "dev" and not value:
|
if key != "dev" and not value:
|
||||||
values[key] = []
|
values[key] = []
|
||||||
return values
|
return values
|
||||||
|
|
||||||
|
def update_from_yaml(self, file_path: str):
|
||||||
|
new_settings = load_settings_from_yaml(file_path)
|
||||||
|
self.chains = new_settings.chains or []
|
||||||
|
self.agents = new_settings.agents or []
|
||||||
|
self.prompts = new_settings.prompts or []
|
||||||
|
self.llms = new_settings.llms or []
|
||||||
|
self.tools = new_settings.tools or []
|
||||||
|
self.memories = new_settings.memories or []
|
||||||
|
self.dev = new_settings.dev or False
|
||||||
|
|
||||||
|
|
||||||
def save_settings_to_yaml(settings: Settings, file_path: str):
|
def save_settings_to_yaml(settings: Settings, file_path: str):
|
||||||
with open(file_path, "w") as f:
|
with open(file_path, "w") as f:
|
||||||
|
|
@ -41,9 +52,8 @@ def load_settings_from_yaml(file_path: str) -> Settings:
|
||||||
|
|
||||||
with open(file_path, "r") as f:
|
with open(file_path, "r") as f:
|
||||||
settings_dict = yaml.safe_load(f)
|
settings_dict = yaml.safe_load(f)
|
||||||
a = Settings.parse_obj(settings_dict)
|
|
||||||
|
|
||||||
return a
|
return Settings(**settings_dict)
|
||||||
|
|
||||||
|
|
||||||
settings = load_settings_from_yaml("config.yaml")
|
settings = load_settings_from_yaml("config.yaml")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue