XiangJinYu commited on
Commit
91e9775
·
verified ·
1 Parent(s): 5189325

modify save yaml

Browse files
Files changed (1) hide show
  1. app.py +35 -27
app.py CHANGED
@@ -13,15 +13,15 @@ from metagpt.ext.spo.components.optimizer import PromptOptimizer
13
  from metagpt.ext.spo.utils.llm_client import SPO_LLM, RequestType
14
 
15
 
16
-
17
  def get_user_workspace():
18
  if "user_id" not in st.session_state:
19
  st.session_state.user_id = str(uuid.uuid4())
20
-
21
  workspace_dir = Path("workspace") / st.session_state.user_id
22
  workspace_dir.mkdir(parents=True, exist_ok=True)
23
  return workspace_dir
24
 
 
25
  def cleanup_workspace(workspace_dir: Path) -> None:
26
  try:
27
  if workspace_dir.exists():
@@ -30,6 +30,7 @@ def cleanup_workspace(workspace_dir: Path) -> None:
30
  except Exception as e:
31
  _logger.error(f"Error cleaning up workspace: {e}")
32
 
 
33
  def get_template_path(template_name: str, is_new_template: bool = False) -> str:
34
  """
35
  Get template file path
@@ -48,23 +49,24 @@ def get_template_path(template_name: str, is_new_template: bool = False) -> str:
48
  # Use root settings path for existing templates
49
  return f"{template_name}.yaml"
50
 
 
51
  def get_all_templates() -> List[str]:
52
  """
53
  Get list of all available templates (both default and user-specific)
54
  :return: List of template names
55
  """
56
  settings_path = Path("metagpt/ext/spo/settings")
57
-
58
  # Get default templates
59
  templates = [f.stem for f in settings_path.glob("*.yaml")]
60
-
61
  # Get user-specific templates if user_id exists
62
  if "user_id" in st.session_state:
63
  user_path = settings_path / st.session_state.user_id
64
  if user_path.exists():
65
  user_templates = [f"{st.session_state.user_id}/{f.stem}" for f in user_path.glob("*.yaml")]
66
  templates.extend(user_templates)
67
-
68
  return sorted(list(set(templates)))
69
 
70
 
@@ -75,22 +77,25 @@ def load_yaml_template(template_path: Path) -> Dict:
75
  return {"prompt": "", "requirements": "", "count": None, "qa": [{"question": "", "answer": ""}]}
76
 
77
 
78
- def save_yaml_template(template_path: Path, data: Dict) -> None:
79
- template_format = {
80
- "prompt": str(data.get("prompt", "")),
81
- "requirements": str(data.get("requirements", "")),
82
- "count": data.get("count"),
83
- "qa": [
84
- {"question": str(qa.get("question", "")).strip(), "answer": str(qa.get("answer", "")).strip()}
85
- for qa in data.get("qa", [])
86
- ],
87
- }
88
-
89
- template_path.parent.mkdir(parents=True, exist_ok=True)
90
-
91
- with open(template_path, "w", encoding="utf-8") as f:
92
- yaml.dump(template_format, f, allow_unicode=True, sort_keys=False, default_flow_style=False, indent=2)
93
-
 
 
 
94
 
95
  def display_optimization_results(result_data):
96
  for result in result_data:
@@ -148,7 +153,7 @@ def main():
148
  template_mode = st.radio("Template Mode", ["Use Existing", "Create New"])
149
 
150
  existing_templates = get_all_templates()
151
-
152
  if template_mode == "Use Existing":
153
  template_name = st.selectbox("Select Template", existing_templates)
154
  is_new_template = False
@@ -190,7 +195,7 @@ def main():
190
  settings_path = Path("metagpt/ext/spo/settings")
191
 
192
  template_path = settings_path / template_real_name
193
-
194
  template_data = load_yaml_template(template_path)
195
 
196
  if "current_template" not in st.session_state or st.session_state.current_template != template_name:
@@ -233,7 +238,7 @@ def main():
233
  if st.button("Save Template"):
234
  new_template_data = {"prompt": prompt, "requirements": requirements, "count": None, "qa": new_qas}
235
 
236
- save_yaml_template(template_path, new_template_data)
237
 
238
  st.session_state.qas = new_qas
239
  st.success(f"Template saved to {template_path}")
@@ -271,9 +276,12 @@ def main():
271
  try:
272
  # Initialize LLM
273
  SPO_LLM.initialize(
274
- optimize_kwargs={"model": opt_model, "temperature": opt_temp, "base_url": base_url, "api_key": api_key},
275
- evaluate_kwargs={"model": eval_model, "temperature": eval_temp, "base_url": base_url, "api_key": api_key},
276
- execute_kwargs={"model": exec_model, "temperature": exec_temp, "base_url": base_url, "api_key": api_key},
 
 
 
277
  )
278
 
279
  # Create optimizer instance
 
13
  from metagpt.ext.spo.utils.llm_client import SPO_LLM, RequestType
14
 
15
 
 
16
  def get_user_workspace():
17
  if "user_id" not in st.session_state:
18
  st.session_state.user_id = str(uuid.uuid4())
19
+
20
  workspace_dir = Path("workspace") / st.session_state.user_id
21
  workspace_dir.mkdir(parents=True, exist_ok=True)
22
  return workspace_dir
23
 
24
+
25
  def cleanup_workspace(workspace_dir: Path) -> None:
26
  try:
27
  if workspace_dir.exists():
 
30
  except Exception as e:
31
  _logger.error(f"Error cleaning up workspace: {e}")
32
 
33
+
34
  def get_template_path(template_name: str, is_new_template: bool = False) -> str:
35
  """
36
  Get template file path
 
49
  # Use root settings path for existing templates
50
  return f"{template_name}.yaml"
51
 
52
+
53
  def get_all_templates() -> List[str]:
54
  """
55
  Get list of all available templates (both default and user-specific)
56
  :return: List of template names
57
  """
58
  settings_path = Path("metagpt/ext/spo/settings")
59
+
60
  # Get default templates
61
  templates = [f.stem for f in settings_path.glob("*.yaml")]
62
+
63
  # Get user-specific templates if user_id exists
64
  if "user_id" in st.session_state:
65
  user_path = settings_path / st.session_state.user_id
66
  if user_path.exists():
67
  user_templates = [f"{st.session_state.user_id}/{f.stem}" for f in user_path.glob("*.yaml")]
68
  templates.extend(user_templates)
69
+
70
  return sorted(list(set(templates)))
71
 
72
 
 
77
  return {"prompt": "", "requirements": "", "count": None, "qa": [{"question": "", "answer": ""}]}
78
 
79
 
80
+ def save_yaml_template(template_path: Path, data: Dict, is_new: bool) -> None:
81
+
82
+ if is_new:
83
+ template_format = {
84
+ "prompt": str(data.get("prompt", "")),
85
+ "requirements": str(data.get("requirements", "")),
86
+ "count": data.get("count"),
87
+ "qa": [
88
+ {"question": str(qa.get("question", "")).strip(), "answer": str(qa.get("answer", "")).strip()}
89
+ for qa in data.get("qa", [])
90
+ ],
91
+ }
92
+
93
+ template_path.parent.mkdir(parents=True, exist_ok=True)
94
+
95
+ with open(template_path, "w", encoding="utf-8") as f:
96
+ yaml.dump(template_format, f, allow_unicode=True, sort_keys=False, default_flow_style=False, indent=2)
97
+ else:
98
+ pass
99
 
100
  def display_optimization_results(result_data):
101
  for result in result_data:
 
153
  template_mode = st.radio("Template Mode", ["Use Existing", "Create New"])
154
 
155
  existing_templates = get_all_templates()
156
+
157
  if template_mode == "Use Existing":
158
  template_name = st.selectbox("Select Template", existing_templates)
159
  is_new_template = False
 
195
  settings_path = Path("metagpt/ext/spo/settings")
196
 
197
  template_path = settings_path / template_real_name
198
+
199
  template_data = load_yaml_template(template_path)
200
 
201
  if "current_template" not in st.session_state or st.session_state.current_template != template_name:
 
238
  if st.button("Save Template"):
239
  new_template_data = {"prompt": prompt, "requirements": requirements, "count": None, "qa": new_qas}
240
 
241
+ save_yaml_template(template_path, new_template_data, is_new_template)
242
 
243
  st.session_state.qas = new_qas
244
  st.success(f"Template saved to {template_path}")
 
276
  try:
277
  # Initialize LLM
278
  SPO_LLM.initialize(
279
+ optimize_kwargs={"model": opt_model, "temperature": opt_temp, "base_url": base_url,
280
+ "api_key": api_key},
281
+ evaluate_kwargs={"model": eval_model, "temperature": eval_temp, "base_url": base_url,
282
+ "api_key": api_key},
283
+ execute_kwargs={"model": exec_model, "temperature": exec_temp, "base_url": base_url,
284
+ "api_key": api_key},
285
  )
286
 
287
  # Create optimizer instance