mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-28 09:38:17 +00:00
Update task.py: try to find json in task output using regex (#491)
* Update task.py: try to find json in task output using regex Sometimes the model replies with a valid and additional text, let's try to extract and validate it first. It's cheaper than calling LLM for that. * Update task.py --------- Co-authored-by: João Moura <joaomdmoura@gmail.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import re
|
||||||
import threading
|
import threading
|
||||||
import uuid
|
import uuid
|
||||||
from typing import Any, Dict, List, Optional, Type
|
from typing import Any, Dict, List, Optional, Type
|
||||||
@@ -246,7 +247,16 @@ class Task(BaseModel):
|
|||||||
return exported_result.model_dump()
|
return exported_result.model_dump()
|
||||||
return exported_result
|
return exported_result
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
# sometimes the response contains valid JSON in the middle of text
|
||||||
|
match = re.search(r"({.*})", result, re.DOTALL)
|
||||||
|
if match:
|
||||||
|
try:
|
||||||
|
exported_result = model.model_validate_json(match.group(0))
|
||||||
|
if self.output_json:
|
||||||
|
return exported_result.model_dump()
|
||||||
|
return exported_result
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
llm = self.agent.function_calling_llm or self.agent.llm
|
llm = self.agent.function_calling_llm or self.agent.llm
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user