Wen1201 commited on
Commit
8415d51
·
verified ·
1 Parent(s): cdf1501

Upload bayesian_llm_assistant.py

Browse files
Files changed (1) hide show
  1. bayesian_llm_assistant.py +18 -9
bayesian_llm_assistant.py CHANGED
@@ -243,6 +243,8 @@ Format responses with proper markdown for better readability.
243
  error_msg = f"❌ Error: {str(e)}\n\nPlease check your API key and try again."
244
  return error_msg, None
245
 
 
 
246
  def _extract_and_render_dag(self, text):
247
  """
248
  從文字中提取 Graphviz 代碼並渲染成圖片
@@ -253,17 +255,23 @@ Format responses with proper markdown for better readability.
253
  Returns:
254
  PIL Image 或 None
255
  """
256
- # 使用正則表達式提取 Graphviz 代碼
257
- pattern = r'```graphviz\s*\n(.*?)\n```'
258
- matches = re.findall(pattern, text, re.DOTALL)
259
 
260
- if not matches:
261
- return None
262
-
263
- try:
264
- # 取第一個 Graphviz 代碼塊
265
  dot_code = matches[0]
 
 
 
 
266
 
 
 
 
 
 
 
267
  # 使用 Graphviz 渲染
268
  graph = graphviz.Source(dot_code)
269
  png_bytes = graph.pipe(format='png')
@@ -275,7 +283,8 @@ Format responses with proper markdown for better readability.
275
 
276
  except Exception as e:
277
  print(f"Failed to render DAG: {e}")
278
- return None
 
279
 
280
  def _prepare_context(self, results):
281
  """準備分析結果的上下文資訊"""
 
243
  error_msg = f"❌ Error: {str(e)}\n\nPlease check your API key and try again."
244
  return error_msg, None
245
 
246
+
247
+
248
  def _extract_and_render_dag(self, text):
249
  """
250
  從文字中提取 Graphviz 代碼並渲染成圖片
 
255
  Returns:
256
  PIL Image 或 None
257
  """
258
+ # 方法 1: 嘗試提取 ```graphviz ... ``` 格式
259
+ pattern1 = r'```graphviz\s*\n(.*?)\n```'
260
+ matches = re.findall(pattern1, text, re.DOTALL)
261
 
262
+ if matches:
 
 
 
 
263
  dot_code = matches[0]
264
+ else:
265
+ # 方法 2: 嘗試提取 digraph ... } 格式(沒有 markdown 包裹)
266
+ pattern2 = r'(digraph\s+\w+\s*\{.*?\n\})'
267
+ matches = re.findall(pattern2, text, re.DOTALL)
268
 
269
+ if not matches:
270
+ return None
271
+
272
+ dot_code = matches[0]
273
+
274
+ try:
275
  # 使用 Graphviz 渲染
276
  graph = graphviz.Source(dot_code)
277
  png_bytes = graph.pipe(format='png')
 
283
 
284
  except Exception as e:
285
  print(f"Failed to render DAG: {e}")
286
+ return None
287
+
288
 
289
  def _prepare_context(self, results):
290
  """準備分析結果的上下文資訊"""