ciyidogan commited on
Commit
a914cec
·
verified ·
1 Parent(s): 6939b86

Update parse_llm_blocks.py

Browse files
Files changed (1) hide show
  1. parse_llm_blocks.py +60 -25
parse_llm_blocks.py CHANGED
@@ -1,25 +1,60 @@
1
- import re
2
- import json
3
-
4
- def parse_llm_blocks(response_text):
5
- blocks = {
6
- "intent": "NONE",
7
- "params": {},
8
- "missing": [],
9
- "action_json": {}
10
- }
11
- intent_match = re.search(r"#INTENT:\s*(.+)", response_text)
12
- params_match = re.search(r"#PARAMS:\s*(\{.*?\})", response_text)
13
- missing_match = re.search(r"#MISSING:\s*(\[[^\]]*\])", response_text)
14
- action_match = re.search(r"#ACTION_JSON:\s*(\{.*?\})", response_text)
15
-
16
- if intent_match:
17
- blocks["intent"] = intent_match.group(1).strip()
18
- if params_match:
19
- blocks["params"] = json.loads(params_match.group(1))
20
- if missing_match:
21
- blocks["missing"] = json.loads(missing_match.group(1))
22
- if action_match:
23
- blocks["action_json"] = json.loads(action_match.group(1))
24
-
25
- return blocks
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import json
3
+ from log import log
4
+
5
+ def parse_llm_blocks(response_text):
6
+ blocks = {
7
+ "answer": "",
8
+ "intent": "NONE",
9
+ "params": {},
10
+ "missing": [],
11
+ "action_json": {}
12
+ }
13
+
14
+ try:
15
+ answer_match = re.search(r"#ANSWER:\s*(.+)", response_text)
16
+ if answer_match:
17
+ blocks["answer"] = answer_match.group(1).strip()
18
+ else:
19
+ log(f"⚠️ ANSWER blok bulunamadı. Ham içerik: {response_text}")
20
+
21
+ intent_match = re.search(r"#INTENT:\s*(.+)", response_text)
22
+ if intent_match:
23
+ blocks["intent"] = intent_match.group(1).strip()
24
+ else:
25
+ log(f"⚠️ INTENT blok bulunamadı. Ham içerik: {response_text}")
26
+
27
+ params_match = re.search(r"#PARAMS:\s*(\{.*?\})", response_text)
28
+ if params_match:
29
+ try:
30
+ blocks["params"] = json.loads(params_match.group(1))
31
+ except json.JSONDecodeError:
32
+ log(f"⚠️ PARAMS JSON parse hatası: {params_match.group(1)}")
33
+ blocks["params"] = {}
34
+ else:
35
+ log(f"⚠️ PARAMS blok bulunamadı. Ham içerik: {response_text}")
36
+
37
+ missing_match = re.search(r"#MISSING:\s*(\[[^\]]*\])", response_text)
38
+ if missing_match:
39
+ try:
40
+ blocks["missing"] = json.loads(missing_match.group(1))
41
+ except json.JSONDecodeError:
42
+ log(f"⚠️ MISSING JSON parse hatası: {missing_match.group(1)}")
43
+ blocks["missing"] = []
44
+ else:
45
+ log(f"⚠️ MISSING blok bulunamadı. Ham içerik: {response_text}")
46
+
47
+ action_match = re.search(r"#ACTION_JSON:\s*(\{.*?\})", response_text)
48
+ if action_match:
49
+ try:
50
+ blocks["action_json"] = json.loads(action_match.group(1))
51
+ except json.JSONDecodeError:
52
+ log(f"⚠️ ACTION_JSON parse hatası: {action_match.group(1)}")
53
+ blocks["action_json"] = {}
54
+ else:
55
+ log(f"⚠️ ACTION_JSON blok bulunamadı. Ham içerik: {response_text}")
56
+
57
+ except Exception as e:
58
+ log(f"❌ parse_llm_blocks() genel hatası: {e}")
59
+
60
+ return blocks