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
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Configuration Models for Flare Platform
|
3 |
+
"""
|
4 |
+
from typing import Optional, List, Dict, Any
|
5 |
+
from datetime import datetime
|
6 |
+
from pydantic import BaseModel, Field
|
7 |
+
|
8 |
+
|
9 |
+
# ===================== User & Auth =====================
|
10 |
+
class UserConfig(BaseModel):
|
11 |
+
username: str
|
12 |
+
password_hash: str
|
13 |
+
salt: Optional[str] = None
|
14 |
+
|
15 |
+
|
16 |
+
# ===================== Provider Models =====================
|
17 |
+
class ProviderDefinition(BaseModel):
|
18 |
+
type: str # llm, tts, stt
|
19 |
+
name: str
|
20 |
+
display_name: str
|
21 |
+
requires_endpoint: bool
|
22 |
+
requires_api_key: bool
|
23 |
+
requires_repo_info: Optional[bool] = False
|
24 |
+
description: str
|
25 |
+
|
26 |
+
|
27 |
+
class ProviderSettings(BaseModel):
|
28 |
+
name: str
|
29 |
+
api_key: Optional[str] = None
|
30 |
+
endpoint: Optional[str] = None
|
31 |
+
settings: Optional[Dict[str, Any]] = Field(default_factory=dict)
|
32 |
+
|
33 |
+
|
34 |
+
# ===================== Global Config =====================
|
35 |
+
class GlobalConfig(BaseModel):
|
36 |
+
llm_provider: ProviderSettings
|
37 |
+
tts_provider: ProviderSettings
|
38 |
+
stt_provider: ProviderSettings
|
39 |
+
providers: List[ProviderDefinition] = Field(default_factory=list)
|
40 |
+
users: List[UserConfig] = Field(default_factory=list)
|
41 |
+
|
42 |
+
def is_cloud_mode(self) -> bool:
|
43 |
+
"""Check if running in cloud mode"""
|
44 |
+
import os
|
45 |
+
return bool(os.environ.get("SPACE_ID"))
|
46 |
+
|
47 |
+
def get_provider_config(self, provider_type: str, provider_name: str) -> Optional[ProviderDefinition]:
|
48 |
+
"""Get provider definition by type and name"""
|
49 |
+
return next(
|
50 |
+
(p for p in self.providers if p.type == provider_type and p.name == provider_name),
|
51 |
+
None
|
52 |
+
)
|
53 |
+
|
54 |
+
|
55 |
+
# ===================== Localization Models =====================
|
56 |
+
class LocalizedExample(BaseModel):
|
57 |
+
locale_code: str
|
58 |
+
example: str
|
59 |
+
|
60 |
+
|
61 |
+
class LocalizedCaption(BaseModel):
|
62 |
+
locale_code: str
|
63 |
+
caption: str
|
64 |
+
|
65 |
+
|
66 |
+
# ===================== Parameter Models =====================
|
67 |
+
class ParameterConfig(BaseModel):
|
68 |
+
name: str
|
69 |
+
caption: List[LocalizedCaption]
|
70 |
+
type: str # str, int, float, bool, date
|
71 |
+
required: bool = True
|
72 |
+
variable_name: str
|
73 |
+
extraction_prompt: Optional[str] = None
|
74 |
+
validation_regex: Optional[str] = None
|
75 |
+
invalid_prompt: Optional[str] = None
|
76 |
+
type_error_prompt: Optional[str] = None
|
77 |
+
|
78 |
+
def canonical_type(self) -> str:
|
79 |
+
"""Get canonical type name"""
|
80 |
+
return self.type.lower()
|
81 |
+
|
82 |
+
def get_caption_for_locale(self, locale: str) -> str:
|
83 |
+
"""Get caption for specific locale"""
|
84 |
+
for cap in self.caption:
|
85 |
+
if cap.locale_code == locale:
|
86 |
+
return cap.caption
|
87 |
+
# Fallback to first caption
|
88 |
+
return self.caption[0].caption if self.caption else self.name
|
89 |
+
|
90 |
+
|
91 |
+
# ===================== Intent Models =====================
|
92 |
+
class IntentConfig(BaseModel):
|
93 |
+
name: str
|
94 |
+
caption: str
|
95 |
+
requiresApproval: Optional[bool] = False
|
96 |
+
dependencies: List[str] = Field(default_factory=list)
|
97 |
+
examples: List[LocalizedExample] = Field(default_factory=list)
|
98 |
+
detection_prompt: str
|
99 |
+
parameters: List[ParameterConfig] = Field(default_factory=list)
|
100 |
+
action: str
|
101 |
+
fallback_timeout_prompt: Optional[str] = None
|
102 |
+
fallback_error_prompt: Optional[str] = None
|
103 |
+
|
104 |
+
|
105 |
+
# ===================== LLM Configuration =====================
|
106 |
+
class GenerationConfig(BaseModel):
|
107 |
+
max_new_tokens: int = 512
|
108 |
+
temperature: float = 0.7
|
109 |
+
top_p: float = 0.9
|
110 |
+
top_k: Optional[int] = None
|
111 |
+
repetition_penalty: Optional[float] = None
|
112 |
+
do_sample: Optional[bool] = True
|
113 |
+
num_beams: Optional[int] = None
|
114 |
+
length_penalty: Optional[float] = None
|
115 |
+
early_stopping: Optional[bool] = None
|
116 |
+
|
117 |
+
|
118 |
+
class LLMConfiguration(BaseModel):
|
119 |
+
repo_id: str
|
120 |
+
generation_config: GenerationConfig = Field(default_factory=GenerationConfig)
|
121 |
+
use_fine_tune: bool = False
|
122 |
+
fine_tune_zip: Optional[str] = ""
|
123 |
+
|
124 |
+
|
125 |
+
# ===================== Version Models =====================
|
126 |
+
class VersionConfig(BaseModel):
|
127 |
+
id: int
|
128 |
+
no: int
|
129 |
+
caption: str
|
130 |
+
description: Optional[str] = None
|
131 |
+
published: bool = False
|
132 |
+
deleted: bool = False
|
133 |
+
general_prompt: str
|
134 |
+
welcome_prompt: Optional[str] = None
|
135 |
+
llm: LLMConfiguration
|
136 |
+
intents: List[IntentConfig] = Field(default_factory=list)
|
137 |
+
created_date: datetime
|
138 |
+
created_by: str
|
139 |
+
publish_date: Optional[datetime] = None
|
140 |
+
published_by: Optional[str] = None
|
141 |
+
|
142 |
+
|
143 |
+
# ===================== Project Models =====================
|
144 |
+
class ProjectConfig(BaseModel):
|
145 |
+
id: int
|
146 |
+
name: str
|
147 |
+
caption: str
|
148 |
+
icon: Optional[str] = "folder"
|
149 |
+
description: Optional[str] = None
|
150 |
+
enabled: bool = True
|
151 |
+
default_locale: str = "tr"
|
152 |
+
supported_locales: List[str] = Field(default_factory=lambda: ["tr"])
|
153 |
+
timezone: str = "Europe/Istanbul"
|
154 |
+
region: str = "tr-TR"
|
155 |
+
versions: List[VersionConfig] = Field(default_factory=list)
|
156 |
+
version_id_counter: int = 1
|
157 |
+
deleted: bool = False
|
158 |
+
created_date: datetime
|
159 |
+
created_by: str
|
160 |
+
last_update_date: Optional[datetime] = None
|
161 |
+
last_update_user: Optional[str] = None
|
162 |
+
|
163 |
+
|
164 |
+
# ===================== API Models =====================
|
165 |
+
class RetryConfig(BaseModel):
|
166 |
+
retry_count: int = 3
|
167 |
+
backoff_seconds: int = 2
|
168 |
+
strategy: str = "static" # static, exponential
|
169 |
+
|
170 |
+
|
171 |
+
class AuthConfig(BaseModel):
|
172 |
+
enabled: bool
|
173 |
+
token_endpoint: Optional[str] = None
|
174 |
+
response_token_path: Optional[str] = None
|
175 |
+
token_request_body: Optional[Dict[str, Any]] = None
|
176 |
+
token_refresh_endpoint: Optional[str] = None
|
177 |
+
token_refresh_body: Optional[Dict[str, Any]] = None
|
178 |
+
|
179 |
+
|
180 |
+
class ResponseMapping(BaseModel):
|
181 |
+
variable_name: str
|
182 |
+
type: str # str, int, float, bool, date
|
183 |
+
json_path: str
|
184 |
+
caption: str
|
185 |
+
|
186 |
+
|
187 |
+
class APIConfig(BaseModel):
|
188 |
+
name: str
|
189 |
+
url: str
|
190 |
+
method: str = "POST"
|
191 |
+
headers: Dict[str, str] = Field(default_factory=dict)
|
192 |
+
body_template: Dict[str, Any] = Field(default_factory=dict)
|
193 |
+
timeout_seconds: int = 10
|
194 |
+
retry: RetryConfig = Field(default_factory=RetryConfig)
|
195 |
+
proxy: Optional[str] = None
|
196 |
+
auth: Optional[AuthConfig] = None
|
197 |
+
response_prompt: Optional[str] = None
|
198 |
+
response_mappings: List[ResponseMapping] = Field(default_factory=list)
|
199 |
+
deleted: bool = False
|
200 |
+
created_date: Optional[datetime] = None
|
201 |
+
created_by: Optional[str] = None
|
202 |
+
last_update_date: Optional[datetime] = None
|
203 |
+
last_update_user: Optional[str] = None
|
204 |
+
|
205 |
+
|
206 |
+
# ===================== Activity Log =====================
|
207 |
+
class ActivityLogEntry(BaseModel):
|
208 |
+
id: int
|
209 |
+
timestamp: datetime
|
210 |
+
username: str
|
211 |
+
action: str
|
212 |
+
entity_type: str
|
213 |
+
entity_id: Optional[int] = None
|
214 |
+
entity_name: Optional[str] = None
|
215 |
+
details: Optional[str] = None
|
216 |
+
|
217 |
+
|
218 |
+
# ===================== Root Configuration =====================
|
219 |
+
class ServiceConfig(BaseModel):
|
220 |
+
config: GlobalConfig = Field(alias="config")
|
221 |
+
projects: List[ProjectConfig] = Field(default_factory=list)
|
222 |
+
apis: List[APIConfig] = Field(default_factory=list)
|
223 |
+
activity_log: List[ActivityLogEntry] = Field(default_factory=list)
|
224 |
+
project_id_counter: int = 1
|
225 |
+
last_update_date: Optional[datetime] = None
|
226 |
+
last_update_user: Optional[str] = None
|
227 |
+
|
228 |
+
class Config:
|
229 |
+
populate_by_name = True
|
230 |
+
|
231 |
+
def build_index(self) -> None:
|
232 |
+
"""Build indexes for quick lookup"""
|
233 |
+
# This method can be extended to build various indexes
|
234 |
+
pass
|
235 |
+
|
236 |
+
def get_api(self, api_name: str) -> Optional[APIConfig]:
|
237 |
+
"""Get API config by name"""
|
238 |
+
return next((api for api in self.apis if api.name == api_name), None)
|
239 |
+
|
240 |
+
|
241 |
+
# For backward compatibility - alias
|
242 |
+
GlobalConfiguration = GlobalConfig
|