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:
ftoppi
2024-05-02 08:26:34 +02:00
committed by GitHub
parent f7e856a16d
commit 19fe093ce9

View File

@@ -1,3 +1,4 @@
import re
import threading
import uuid
from typing import Any, Dict, List, Optional, Type
@@ -246,7 +247,16 @@ class Task(BaseModel):
return exported_result.model_dump()
return exported_result
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