zenaight commited on
Commit
a204bcd
·
1 Parent(s): 3de6228

Refactor intent extraction logic in AI chat

Browse files

- Moved the check for non-JSON responses to after content cleanup in the `extract_and_update_intent` function, improving clarity and ensuring that the response is properly processed before validation.
- Enhanced logging for non-JSON responses to aid in debugging and provide better context for the user experience.

Files changed (1) hide show
  1. ai_chat.py +6 -5
ai_chat.py CHANGED
@@ -155,11 +155,6 @@ async def extract_and_update_intent(state):
155
  response = await llm.ainvoke([{"role":"user","content":extraction_prompt}])
156
  print(f"LLM response for intent extraction: {response.content}")
157
 
158
- # Check if response is an explanatory answer rather than JSON
159
- if not response.content.strip().startswith("{"):
160
- state["response"] = response.content
161
- return state
162
-
163
  import json
164
  try:
165
  # Clean up the response content (remove markdown formatting if present)
@@ -175,6 +170,12 @@ async def extract_and_update_intent(state):
175
  content = content.strip()
176
  print(f"Final cleaned content: {repr(content)}")
177
 
 
 
 
 
 
 
178
  extracted = json.loads(content)
179
  print(f"Successfully parsed intent JSON: {extracted}")
180
  except Exception as e:
 
155
  response = await llm.ainvoke([{"role":"user","content":extraction_prompt}])
156
  print(f"LLM response for intent extraction: {response.content}")
157
 
 
 
 
 
 
158
  import json
159
  try:
160
  # Clean up the response content (remove markdown formatting if present)
 
170
  content = content.strip()
171
  print(f"Final cleaned content: {repr(content)}")
172
 
173
+ # Check if cleaned content is JSON
174
+ if not content.startswith("{"):
175
+ print("Cleaned content is not JSON, treating as explanatory answer")
176
+ state["response"] = response.content
177
+ return state
178
+
179
  extracted = json.loads(content)
180
  print(f"Successfully parsed intent JSON: {extracted}")
181
  except Exception as e: