Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Agents/AI Service/agency.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ def __init__(self, role: str, prompt: ChatPromptTemplate):
logger.info(f"Loading {role} model: {model_name}")
self.llm = ChatOllama(model=model_name)

@staticmethod
def _parse_json(text: str) -> Any:
"""Extract and parse the first JSON object found in text."""
start = text.find("{")
if start == -1:
raise json.JSONDecodeError("No JSON object found", text, 0)
decoder = json.JSONDecoder()
obj, _ = decoder.raw_decode(text[start:])
return obj

async def run(self, project_json: str, context: Dict[str, Any], timeout: Optional[float] = 60.0) -> Any:
"""
Invoke the LLM with formatted messages and return parsed JSON.
Expand All @@ -176,7 +186,7 @@ async def run(self, project_json: str, context: Dict[str, Any], timeout: Optiona
timeout,
)
raw = getattr(message, "content", message)
parsed = json.loads(raw)
parsed = self._parse_json(raw)
logger.info(f"{self.role} responded successfully")
return parsed
except asyncio.TimeoutError:
Expand Down