-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebui_config.py
77 lines (59 loc) · 3.04 KB
/
webui_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from typing import NamedTuple, List, IO, Union
import yaml
# Embedding model configuration.
class EmbeddingModelConfig(NamedTuple):
provider: str # Provider of the embedding model i.e. huggingface
endpoint: str # Endpoint for the embedding model.
# Create a new embedding model configuration from a dictionary.
@classmethod
def new_embedding_config(cls, config: dict):
try:
# Extract provider and endpoint from the config dictionary.
model_provider = config["provider"].lower() # Ensure lowercase provider name.
model_endpoint = config["endpoint"]
except ValueError as ex:
raise ValueError("Error while parsing config") from ex
# Return a new instance of EmbeddingModelConfig.
return cls(provider=model_provider, endpoint=model_endpoint)
# LLM model configuration.
class LlmModelConfig(NamedTuple):
provider: str # Provider of the LLM model. i.e. huggingface tgi
endpoint: str # Endpoint for the LLM model.
model_path: str # Path to the model
# Create a new LLM model configuration from a dictionary.
@classmethod
def new_llm_config(cls, config: dict):
try:
# Extract provider and endpoint from the config dictionary.
model_provider = config["provider"].lower() # Ensure lowercase provider name.
model_endpoint = config.get("endpoint", None)
model_path = config.get("path", None)
except ValueError as ex:
raise ValueError("Error while parsing config") from ex
# Return a new instance of LlmModelConfig.
return cls(provider=model_provider, endpoint=model_endpoint, model_path=model_path)
# UI configuration
class UiConfig:
def __init__(self, config: dict):
# Initialize embedding_model attribute as None.
self.embedding_model: EmbeddingModelConfig
# Get embedding model configuration from the input dictionary.
_embedding_model_config = config.get("embedding_model", None)
# If embedding model configuration exists, create a new EmbeddingModelConfig instance.
if _embedding_model_config:
self.embedding_model = EmbeddingModelConfig.new_embedding_config(_embedding_model_config)
# Initialize llm_models attribute as an empty list.
self.llm_models: LlmModelConfig
# Get LLM models configurations from the input dictionary.
_llm_config = config.get("llm_models", [])
# Loop through each LLM configuration and create LlmModelConfig instances.
for _llm in _llm_config:
self.llm_models = LlmModelConfig.new_llm_config(_llm)
self.document_folder: str = config.get("document-folder", "doc")
# Method to load UI configuration from a file
@classmethod
def load_config_from_file(cls, config: IO[str]):
# Load YAML configuration from the input file
_config = yaml.safe_load(config)
# Return a new instance of UiConfig with loaded configuration
return cls(_config)