Spaces:
Running
Running
Update config_provider.py
Browse files- config_provider.py +73 -105
config_provider.py
CHANGED
@@ -97,8 +97,7 @@ class LLMConfig(BaseModel):
|
|
97 |
fine_tune_zip: str = ""
|
98 |
|
99 |
class VersionConfig(BaseModel):
|
100 |
-
|
101 |
-
no: Optional[int] = None
|
102 |
caption: Optional[str] = ""
|
103 |
description: Optional[str] = ""
|
104 |
published: bool = False
|
@@ -116,7 +115,6 @@ class VersionConfig(BaseModel):
|
|
116 |
|
117 |
class Config:
|
118 |
extra = "allow"
|
119 |
-
populate_by_name = True
|
120 |
|
121 |
class ProjectConfig(BaseModel):
|
122 |
id: Optional[int] = None
|
@@ -125,10 +123,8 @@ class ProjectConfig(BaseModel):
|
|
125 |
icon: Optional[str] = "folder"
|
126 |
description: Optional[str] = ""
|
127 |
enabled: bool = True
|
128 |
-
|
129 |
-
version_id_counter: int = 1
|
130 |
versions: List[VersionConfig]
|
131 |
-
# Language settings - changed from default_language/supported_languages
|
132 |
default_locale: str = "tr"
|
133 |
supported_locales: List[str] = ["tr"]
|
134 |
timezone: Optional[str] = "Europe/Istanbul"
|
@@ -225,17 +221,21 @@ class ParameterConfig(BaseModel):
|
|
225 |
|
226 |
class IntentConfig(BaseModel):
|
227 |
name: str
|
228 |
-
caption:
|
229 |
dependencies: List[str] = []
|
230 |
-
requiresApproval: bool = False
|
231 |
examples: List[LocalizedExample] = []
|
232 |
-
detection_prompt:
|
233 |
parameters: List[ParameterConfig] = []
|
234 |
action: str
|
235 |
-
requiresApproval: bool = False
|
236 |
fallback_timeout_prompt: Optional[str] = None
|
237 |
fallback_error_prompt: Optional[str] = None
|
238 |
-
|
|
|
|
|
|
|
|
|
|
|
239 |
class Config:
|
240 |
extra = "allow"
|
241 |
|
@@ -667,11 +667,48 @@ class ConfigProvider:
|
|
667 |
@classmethod
|
668 |
def add_activity_log(cls, username: str, action: str, entity_type: str,
|
669 |
entity_id: Optional[int] = None, entity_name: Optional[str] = None,
|
670 |
-
details: Optional[str] = None):
|
671 |
-
"""Add activity log entry"""
|
672 |
if cls._instance is None:
|
673 |
cls.get()
|
674 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
675 |
entry = ActivityLogEntry(
|
676 |
timestamp=datetime.now().isoformat() + "Z",
|
677 |
username=username,
|
@@ -679,7 +716,7 @@ class ConfigProvider:
|
|
679 |
entity_type=entity_type,
|
680 |
entity_id=entity_id,
|
681 |
entity_name=entity_name,
|
682 |
-
details=
|
683 |
)
|
684 |
|
685 |
cls._instance.activity_log.append(entry)
|
@@ -687,6 +724,9 @@ class ConfigProvider:
|
|
687 |
# Keep only last 1000 entries
|
688 |
if len(cls._instance.activity_log) > 1000:
|
689 |
cls._instance.activity_log = cls._instance.activity_log[-1000:]
|
|
|
|
|
|
|
690 |
|
691 |
@classmethod
|
692 |
def update_environment(cls, update_data: dict, username: str) -> None:
|
@@ -747,6 +787,9 @@ class ConfigProvider:
|
|
747 |
if any(p.name == project_data['name'] for p in cls._instance.projects if not p.deleted):
|
748 |
raise ValueError(f"Project name '{project_data['name']}' already exists")
|
749 |
|
|
|
|
|
|
|
750 |
# Create project
|
751 |
new_project = ProjectConfig(
|
752 |
id=cls._instance.project_id_counter,
|
@@ -770,9 +813,7 @@ class ConfigProvider:
|
|
770 |
|
771 |
# Create initial version
|
772 |
initial_version = VersionConfig(
|
773 |
-
|
774 |
-
version_number=1,
|
775 |
-
no=1,
|
776 |
caption="Version 1",
|
777 |
description="Initial version",
|
778 |
published=False,
|
@@ -782,23 +823,21 @@ class ConfigProvider:
|
|
782 |
last_update_date=datetime.now().isoformat() + "Z",
|
783 |
last_update_user=username,
|
784 |
general_prompt="You are a helpful assistant.",
|
|
|
785 |
llm=LLMConfig(
|
786 |
-
repo_id="",
|
787 |
generation_config={
|
788 |
-
"max_new_tokens":
|
789 |
-
"temperature": 0.7
|
790 |
-
"top_p": 0.95
|
791 |
}
|
792 |
),
|
793 |
intents=[]
|
794 |
)
|
795 |
|
796 |
new_project.versions.append(initial_version)
|
797 |
-
new_project.last_version_number = 1
|
798 |
|
799 |
# Add to config
|
800 |
cls._instance.projects.append(new_project)
|
801 |
-
cls._instance.project_id_counter += 1
|
802 |
|
803 |
# Add activity log
|
804 |
cls.add_activity_log(username, "CREATE_PROJECT", "project", new_project.id, new_project.name)
|
@@ -1037,93 +1076,22 @@ class ConfigProvider:
|
|
1037 |
# Import versions
|
1038 |
for idx, version_data in enumerate(project_data.get("versions", [])):
|
1039 |
new_version = VersionConfig(
|
1040 |
-
id=idx + 1,
|
1041 |
-
version_number=idx + 1,
|
1042 |
no=idx + 1,
|
1043 |
-
caption=version_data.get("caption", f"
|
1044 |
description=version_data.get("description", ""),
|
1045 |
-
published=False,
|
1046 |
-
deleted=False,
|
1047 |
-
created_date=datetime.now().isoformat() + "Z",
|
1048 |
-
created_by=username,
|
1049 |
-
last_update_date=datetime.now().isoformat() + "Z",
|
1050 |
-
last_update_user=username,
|
1051 |
general_prompt=version_data.get("general_prompt", ""),
|
1052 |
-
|
1053 |
-
|
1054 |
-
|
1055 |
-
|
1056 |
-
|
1057 |
-
"top_p": 0.95
|
1058 |
-
}
|
1059 |
-
),
|
1060 |
-
intents=[]
|
1061 |
)
|
1062 |
-
|
1063 |
-
# Import intents
|
1064 |
-
for intent_data in version_data.get("intents", []):
|
1065 |
-
intent = IntentConfig(
|
1066 |
-
name=intent_data.get("name", ""),
|
1067 |
-
caption=intent_data.get("caption", ""),
|
1068 |
-
detection_prompt=intent_data.get("detection_prompt", ""),
|
1069 |
-
action=intent_data.get("action", ""),
|
1070 |
-
fallback_timeout_prompt=intent_data.get("fallback_timeout_prompt"),
|
1071 |
-
fallback_error_prompt=intent_data.get("fallback_error_prompt"),
|
1072 |
-
examples=[],
|
1073 |
-
parameters=[]
|
1074 |
-
)
|
1075 |
-
|
1076 |
-
# Convert examples
|
1077 |
-
if "examples" in intent_data:
|
1078 |
-
if isinstance(intent_data["examples"], list):
|
1079 |
-
for example in intent_data["examples"]:
|
1080 |
-
if isinstance(example, str):
|
1081 |
-
# Old format - use project default locale
|
1082 |
-
intent.examples.append(LocalizedExample(
|
1083 |
-
locale_code=new_project.default_locale,
|
1084 |
-
example=example
|
1085 |
-
))
|
1086 |
-
elif isinstance(example, dict):
|
1087 |
-
# New format
|
1088 |
-
intent.examples.append(LocalizedExample(**example))
|
1089 |
-
|
1090 |
-
# Convert parameters
|
1091 |
-
for param_data in intent_data.get("parameters", []):
|
1092 |
-
param = ParameterConfig(
|
1093 |
-
name=param_data.get("name", ""),
|
1094 |
-
type=param_data.get("type", "str"),
|
1095 |
-
required=param_data.get("required", True),
|
1096 |
-
variable_name=param_data.get("variable_name", param_data.get("name", "")),
|
1097 |
-
extraction_prompt=param_data.get("extraction_prompt"),
|
1098 |
-
validation_regex=param_data.get("validation_regex"),
|
1099 |
-
invalid_prompt=param_data.get("invalid_prompt"),
|
1100 |
-
type_error_prompt=param_data.get("type_error_prompt"),
|
1101 |
-
caption=[]
|
1102 |
-
)
|
1103 |
-
|
1104 |
-
# Convert caption
|
1105 |
-
if "caption" in param_data:
|
1106 |
-
if isinstance(param_data["caption"], str):
|
1107 |
-
# Old format
|
1108 |
-
param.caption.append(LocalizedCaption(
|
1109 |
-
locale_code=new_project.default_locale,
|
1110 |
-
caption=param_data["caption"]
|
1111 |
-
))
|
1112 |
-
elif isinstance(param_data["caption"], list):
|
1113 |
-
# New format
|
1114 |
-
for cap in param_data["caption"]:
|
1115 |
-
param.caption.append(LocalizedCaption(**cap))
|
1116 |
-
|
1117 |
-
intent.parameters.append(param)
|
1118 |
-
|
1119 |
-
new_version.intents.append(intent)
|
1120 |
-
|
1121 |
new_project.versions.append(new_version)
|
1122 |
-
|
1123 |
-
|
1124 |
-
|
1125 |
-
|
1126 |
-
|
1127 |
# Save
|
1128 |
cls._instance.save()
|
1129 |
|
|
|
97 |
fine_tune_zip: str = ""
|
98 |
|
99 |
class VersionConfig(BaseModel):
|
100 |
+
no: int # Tek version numarası alanı
|
|
|
101 |
caption: Optional[str] = ""
|
102 |
description: Optional[str] = ""
|
103 |
published: bool = False
|
|
|
115 |
|
116 |
class Config:
|
117 |
extra = "allow"
|
|
|
118 |
|
119 |
class ProjectConfig(BaseModel):
|
120 |
id: Optional[int] = None
|
|
|
123 |
icon: Optional[str] = "folder"
|
124 |
description: Optional[str] = ""
|
125 |
enabled: bool = True
|
126 |
+
version_id_counter: int = 1 # last_version_number yerine sadece bu kullanılacak
|
|
|
127 |
versions: List[VersionConfig]
|
|
|
128 |
default_locale: str = "tr"
|
129 |
supported_locales: List[str] = ["tr"]
|
130 |
timezone: Optional[str] = "Europe/Istanbul"
|
|
|
221 |
|
222 |
class IntentConfig(BaseModel):
|
223 |
name: str
|
224 |
+
caption: Union[str, List[LocalizedCaption]]
|
225 |
dependencies: List[str] = []
|
226 |
+
requiresApproval: bool = False # YENİ ALAN
|
227 |
examples: List[LocalizedExample] = []
|
228 |
+
detection_prompt: str
|
229 |
parameters: List[ParameterConfig] = []
|
230 |
action: str
|
|
|
231 |
fallback_timeout_prompt: Optional[str] = None
|
232 |
fallback_error_prompt: Optional[str] = None
|
233 |
+
deleted: bool = False
|
234 |
+
created_date: Optional[str] = None
|
235 |
+
created_by: Optional[str] = None
|
236 |
+
last_update_date: Optional[str] = None
|
237 |
+
last_update_user: Optional[str] = None
|
238 |
+
|
239 |
class Config:
|
240 |
extra = "allow"
|
241 |
|
|
|
667 |
@classmethod
|
668 |
def add_activity_log(cls, username: str, action: str, entity_type: str,
|
669 |
entity_id: Optional[int] = None, entity_name: Optional[str] = None,
|
670 |
+
details: Optional[Dict[str, Any]] = None) -> None:
|
671 |
+
"""Add activity log entry with detailed context"""
|
672 |
if cls._instance is None:
|
673 |
cls.get()
|
674 |
|
675 |
+
# Build detailed context
|
676 |
+
detail_str = None
|
677 |
+
if details:
|
678 |
+
detail_str = json.dumps(details, ensure_ascii=False)
|
679 |
+
else:
|
680 |
+
# Auto-generate details based on action
|
681 |
+
if action == "CREATE_PROJECT":
|
682 |
+
detail_str = f"Created new project '{entity_name}'"
|
683 |
+
elif action == "UPDATE_PROJECT":
|
684 |
+
detail_str = f"Updated project configuration"
|
685 |
+
elif action == "DELETE_PROJECT":
|
686 |
+
detail_str = f"Soft deleted project '{entity_name}'"
|
687 |
+
elif action == "CREATE_VERSION":
|
688 |
+
detail_str = f"Created new version for {entity_name}"
|
689 |
+
elif action == "PUBLISH_VERSION":
|
690 |
+
detail_str = f"Published version {entity_id}"
|
691 |
+
elif action == "CREATE_INTENT":
|
692 |
+
detail_str = f"Added intent '{entity_name}'"
|
693 |
+
elif action == "UPDATE_INTENT":
|
694 |
+
detail_str = f"Modified intent configuration"
|
695 |
+
elif action == "DELETE_INTENT":
|
696 |
+
detail_str = f"Removed intent '{entity_name}'"
|
697 |
+
elif action == "CREATE_API":
|
698 |
+
detail_str = f"Created API endpoint '{entity_name}'"
|
699 |
+
elif action == "UPDATE_API":
|
700 |
+
detail_str = f"Modified API configuration"
|
701 |
+
elif action == "DELETE_API":
|
702 |
+
detail_str = f"Soft deleted API '{entity_name}'"
|
703 |
+
elif action == "LOGIN":
|
704 |
+
detail_str = f"User logged in"
|
705 |
+
elif action == "LOGOUT":
|
706 |
+
detail_str = f"User logged out"
|
707 |
+
elif action == "IMPORT_PROJECT":
|
708 |
+
detail_str = f"Imported project from file"
|
709 |
+
elif action == "EXPORT_PROJECT":
|
710 |
+
detail_str = f"Exported project '{entity_name}'"
|
711 |
+
|
712 |
entry = ActivityLogEntry(
|
713 |
timestamp=datetime.now().isoformat() + "Z",
|
714 |
username=username,
|
|
|
716 |
entity_type=entity_type,
|
717 |
entity_id=entity_id,
|
718 |
entity_name=entity_name,
|
719 |
+
details=detail_str
|
720 |
)
|
721 |
|
722 |
cls._instance.activity_log.append(entry)
|
|
|
724 |
# Keep only last 1000 entries
|
725 |
if len(cls._instance.activity_log) > 1000:
|
726 |
cls._instance.activity_log = cls._instance.activity_log[-1000:]
|
727 |
+
|
728 |
+
# Save immediately for audit trail
|
729 |
+
cls._instance.save()
|
730 |
|
731 |
@classmethod
|
732 |
def update_environment(cls, update_data: dict, username: str) -> None:
|
|
|
787 |
if any(p.name == project_data['name'] for p in cls._instance.projects if not p.deleted):
|
788 |
raise ValueError(f"Project name '{project_data['name']}' already exists")
|
789 |
|
790 |
+
# Increment global project counter
|
791 |
+
cls._instance.project_id_counter += 1
|
792 |
+
|
793 |
# Create project
|
794 |
new_project = ProjectConfig(
|
795 |
id=cls._instance.project_id_counter,
|
|
|
813 |
|
814 |
# Create initial version
|
815 |
initial_version = VersionConfig(
|
816 |
+
no=1, # Sadece no kullan
|
|
|
|
|
817 |
caption="Version 1",
|
818 |
description="Initial version",
|
819 |
published=False,
|
|
|
823 |
last_update_date=datetime.now().isoformat() + "Z",
|
824 |
last_update_user=username,
|
825 |
general_prompt="You are a helpful assistant.",
|
826 |
+
welcome_prompt=None,
|
827 |
llm=LLMConfig(
|
828 |
+
repo_id="ytu-ce-cosmos/Turkish-Llama-8b-Instruct-v0.1",
|
829 |
generation_config={
|
830 |
+
"max_new_tokens": 256,
|
831 |
+
"temperature": 0.7
|
|
|
832 |
}
|
833 |
),
|
834 |
intents=[]
|
835 |
)
|
836 |
|
837 |
new_project.versions.append(initial_version)
|
|
|
838 |
|
839 |
# Add to config
|
840 |
cls._instance.projects.append(new_project)
|
|
|
841 |
|
842 |
# Add activity log
|
843 |
cls.add_activity_log(username, "CREATE_PROJECT", "project", new_project.id, new_project.name)
|
|
|
1076 |
# Import versions
|
1077 |
for idx, version_data in enumerate(project_data.get("versions", [])):
|
1078 |
new_version = VersionConfig(
|
|
|
|
|
1079 |
no=idx + 1,
|
1080 |
+
caption=version_data.get("caption", f"v{idx + 1}"),
|
1081 |
description=version_data.get("description", ""),
|
1082 |
+
published=version_data.get("published", False),
|
|
|
|
|
|
|
|
|
|
|
1083 |
general_prompt=version_data.get("general_prompt", ""),
|
1084 |
+
welcome_prompt=version_data.get("welcome_prompt"),
|
1085 |
+
llm=LLMConfig(**version_data.get("llm", {})),
|
1086 |
+
intents=[IntentConfig(**i) for i in version_data.get("intents", [])],
|
1087 |
+
created_date=datetime.now().isoformat() + "Z",
|
1088 |
+
created_by=username
|
|
|
|
|
|
|
|
|
1089 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1090 |
new_project.versions.append(new_version)
|
1091 |
+
|
1092 |
+
# Update version counter
|
1093 |
+
new_project.version_id_counter = max(new_project.version_id_counter, new_version.no)
|
1094 |
+
|
|
|
1095 |
# Save
|
1096 |
cls._instance.save()
|
1097 |
|