@@ -17,7 +17,7 @@ class LLMReviewProcessor:
1717 def __init__ (self , cfg : DictConfig ):
1818 self .cfg = cfg
1919 self .llm_handler = self ._init_llm_handler ()
20- self .prompt_builder = PromptBuilder ()
20+ self .prompt_builder = PromptBuilder (top_k = cfg . llmargs . top_k_aspects )
2121
2222 def _init_llm_handler (self ):
2323 config = LLMconfig (
@@ -39,7 +39,6 @@ def find_aspect_indices(aspect: str, sentence_tokens) :
3939 for i in range (len (tokens ) - len (aspect_tokens ) + 1 ):
4040 if tokens [i :i + len (aspect_tokens )] == aspect_tokens : return list (range (i , i + len (aspect_tokens )))
4141
42-
4342 return - 1
4443
4544 def process_reviews (self , reviews : list ):
@@ -49,13 +48,32 @@ def process_reviews(self, reviews: list):
4948 if sample_review .get ('implicit' , [False ])[0 ] is not True : continue
5049
5150 prompt = self .prompt_builder .build_prompt (sample_review )
52- response = self .llm_handler .get_response (prompt )
53- matches = re .findall (r'\{.*?\}' , response , re .DOTALL )
51+
52+ max_retries = 5
53+ valid_json_found = False
54+ matches = []
55+
56+ for attempt in range (max_retries ):
57+ response = self .llm_handler .get_response (prompt )
58+ matches = re .findall (r'\{.*?\}' , response , re .DOTALL )
59+
60+ for json_str in matches :
61+ try :
62+ aspect_data = json .loads (json_str )
63+ if "aspect" in aspect_data and aspect_data ["aspect" ]:
64+ valid_json_found = True
65+ break
66+ except json .JSONDecodeError :
67+ continue
68+
69+ if valid_json_found :
70+ break
71+ else :
72+ print (f"Invalid or no valid JSON with 'aspect' found. Attempt { attempt + 1 } of { max_retries } " )
5473
5574 if not matches :
5675 print ("No JSON object found in response" )
5776 continue
58-
5977 all_aspects = []
6078 seen_aspects = set ()
6179 tokens = [word .strip ().lower () for sentences in sample_review ["sentences" ] for word in sentences ]
0 commit comments