heymenn commited on
Commit
e7e1393
·
verified ·
1 Parent(s): 7c1cffe

Update kig_core/planner.py

Browse files
Files changed (1) hide show
  1. kig_core/planner.py +38 -30
kig_core/planner.py CHANGED
@@ -156,37 +156,45 @@ def generate_structured_issues(state: PlannerState) -> Dict[str, Any]:
156
 
157
  chain = prompt | issue_llm | output_parser
158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  try:
160
- structured_issues_obj = invoke_llm(chain,{
161
- "user_query": user_query,
162
- "context": full_context
163
- })
164
- print(f"structured_issues_obj => type : {type(structured_issues_obj)}, value : {structured_issues_obj}")
165
- structured_issues = structured_issues_obj #.key_issues
166
- key_issues_list = [KeyIssueInvoke(**issue_dict) for issue_dict in structured_issues_obj]
167
- # Ensure IDs are sequential if the LLM didn't assign them correctly
168
- for i, issue in enumerate(structured_issues):
169
- issue.id = i + 1
170
-
171
- logger.info(f"Successfully generated {len(structured_issues)} structured key issues.")
172
- final_message = f"Generated {len(structured_issues)} Key Issues based on the query '{user_query}'."
173
- return {
174
- "key_issues": structured_issues,
175
- "messages": [AIMessage(content=final_message)],
176
- "error": None
177
- }
178
- except Exception as e:
179
- logger.error(f"Failed to generate or parse structured key issues: {e}", exc_info=True)
180
- # Attempt to get raw output for debugging if possible
181
- raw_output = "Could not retrieve raw output."
182
- try:
183
- raw_chain = prompt | issue_llm | StrOutputParser()
184
- raw_output = invoke_llm(raw_chain,{"user_query": user_query, "context": full_context})
185
- logger.debug(f"Raw output from failed JSON parsing:\n{raw_output}")
186
- except Exception as raw_e:
187
- logger.error(f"Could not even get raw output: {raw_e}")
188
-
189
- return {"error": f"Failed to generate structured key issues: {e}. Raw output hint: {raw_output[:500]}..."}
190
 
191
 
192
  # --- Conditional Edges ---
 
156
 
157
  chain = prompt | issue_llm | output_parser
158
 
159
+ try:
160
+ structured_issues_obj = invoke_llm(chain, {
161
+ "user_query": user_query,
162
+ "context": full_context
163
+ })
164
+ print(f"structured_issues_obj => type : {type(structured_issues_obj)}, value : {structured_issues_obj}")
165
+
166
+ # If the output is a dict with a key 'key_issues', extract it
167
+ if isinstance(structured_issues_obj, dict) and 'key_issues' in structured_issues_obj:
168
+ issues_data = structured_issues_obj['key_issues']
169
+ else:
170
+ issues_data = structured_issues_obj # Assume it's already a list of dicts
171
+
172
+ # Always convert to KeyIssueInvoke objects
173
+ key_issues_list = [KeyIssueInvoke(**issue_dict) for issue_dict in issues_data]
174
+
175
+ # Ensure IDs are sequential if the LLM didn't assign them correctly
176
+ for i, issue in enumerate(key_issues_list):
177
+ issue.id = i + 1
178
+
179
+ logger.info(f"Successfully generated {len(key_issues_list)} structured key issues.")
180
+ final_message = f"Generated {len(key_issues_list)} Key Issues based on the query '{user_query}'."
181
+ return {
182
+ "key_issues": key_issues_list,
183
+ "messages": [AIMessage(content=final_message)],
184
+ "error": None
185
+ }
186
+ except Exception as e:
187
+ logger.error(f"Failed to generate or parse structured key issues: {e}", exc_info=True)
188
+ # Attempt to get raw output for debugging if possible
189
+ raw_output = "Could not retrieve raw output."
190
  try:
191
+ raw_chain = prompt | issue_llm | StrOutputParser()
192
+ raw_output = invoke_llm(raw_chain, {"user_query": user_query, "context": full_context})
193
+ logger.debug(f"Raw output from failed JSON parsing:\n{raw_output}")
194
+ except Exception as raw_e:
195
+ logger.error(f"Could not even get raw output: {raw_e}")
196
+
197
+ return {"error": f"Failed to generate structured key issues: {e}. Raw output hint: {raw_output[:500]}..."}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
 
200
  # --- Conditional Edges ---