refactor: improve clean up observervation and final answer

This commit is contained in:
Lucas Gomide
2025-07-17 17:26:54 -03:00
parent ae9907c8e7
commit fab7c8504a
2 changed files with 123 additions and 22 deletions

View File

@@ -221,29 +221,19 @@ class CrewAgentParser:
return _match_fallback_pair(text)
def _clean_agent_observations(self, text: str) -> str:
"""
Remove agent-written observations from the text.
This handles cases where agents write their own observations instead of
letting the system generate them after tool execution.
"""
observation_pattern = (
r'(Action\s*\d*\s*Input\s*\d*\s*:[^\n]*)' # Capture Action Input line
r'\n\s*Observation\s*:.*?' # Match Observation line
r'(?=\n\s*(?:Thought|Action\s*\d*\s*:|Final\s+Answer:|$))' # Lookahead
def _clean_agent_observations(self, text: str) -> str:
# Pattern: capture Action/Input lines, then Observation block until next Thought or end-of-string
obs_pattern = re.compile(
r'^(\s*Action:.*\n\s*Action Input:.*\n)' # group 1: Action + Action Input
r'\s*Observation:.*?(?=(?:\n\s*Thought:|\Z))', # non-greedy until Thought: or end-of-string
flags=re.DOTALL | re.MULTILINE
)
def clean_text(text: str) -> str:
# Remove agent-written observations while keeping Action Input
text = re.sub(observation_pattern, r'\1', text, flags=re.MULTILINE | re.DOTALL)
# Remove excessive blank lines
text = re.sub(r'\n\s*\n\s*\n+', '\n\n', text)
return text.strip()
if re.search(observation_pattern, text, flags=re.MULTILINE | re.DOTALL):
text = clean_text(text)
if obs_pattern.search(text):
text = obs_pattern.sub(r'\1', text)
# Remove Final Answer and everything following if present
text = re.sub(r'\n\s*Final\s+Answer:.*', '', text, flags=re.DOTALL | re.MULTILINE)
# Normalize blank lines
text = re.sub(r'\n\s*\n\s*\n+', '\n\n', text).strip()
return text

View File

@@ -371,3 +371,114 @@ class MockAgent:
# TODO: ADD TEST TO MAKE SURE ** REMOVAL DOESN'T MESS UP ANYTHING
def test_ensure_agent_action_is_selected_when_model_hallucinates_observation_and_final_answer(parser):
text = """Let's continue our effort to gather comprehensive, well-rounded information about AI in healthcare in 2023 to compile a detailed research report effectively.
Action: Web Search
Action Input: {"search_query": "innovations in AI for healthcare 2023 latest updates and challenges"}
Observation: The search is yielding repeated and abundant information on the fragmented, redundant regulatory frameworks, clinical validation importance, and varied insights about AIs ongoing integration challenges in healthcare. To ensure a rich mix of insights, let's compile, structure, and organize these insights into a coherent report.
Content Synthesis:
- **Innovations and Trends**:
- AI is significantly contributing to personalized medicine, enabling more accurate patient diagnosis and treatment plans.
- Deep learning models, especially in image and pattern recognition, are revolutionizing radiology and pathology.
- AI's role in drug discovery is speeding up research and reducing costs and time for new drugs entering the market.
- AI-driven wearable devices are proving crucial for patient monitoring, predicting potential health issues, and facilitating proactive care.
Thought: I now have ample information to construct a research report detailing innovations, challenges, and opportunities of AI in healthcare in 2023.
Final Answer: The finalized detailed research report on AI in Healthcare, 2023:
Title: Current Innovations, Challenges, and Potential of AI in Healthcare - 2023 Overview
Introduction:
The integration of Artificial Intelligence (AI) in healthcare is heralding a new era of modern medicine. In 2023, substantial technological advancements have brought about transformative changes in healthcare delivery. This report explores the latest AI innovations, identifies prevalent challenges, and discusses the potential opportunities in healthcare.
Potential and Opportunities:
AI's potential in healthcare is vast, presenting numerous opportunities:
- Cost Reduction: AI has the capacity to streamline operations, cutting costs significantly.
- Preventive Healthcare: Utilizing predictive analytics allows for early intervention and prevention, alleviating pressure on emergency and critical care resources.
- Enhanced Surgeries: Robotic surgeries guided by AI improve surgical outcomes and patient recovery times.
- Improved Patient Experience: AI-driven solutions personalize patient interaction, improving engagement and healthcare experiences.
Conclusion:
AI continues to reshape the healthcare landscape in 2023. Facing challenges head-on with robust solutions will unlock unparalleled benefits, positioning AI as a cornerstone for future medical and healthcare advancements. With ongoing improvements in regulations, data quality, and validation processes, the full potential of AI in healthcare stands to be realized.
"""
result = parser.parse(text)
expected_text = """Let's continue our effort to gather comprehensive, well-rounded information about AI in healthcare in 2023 to compile a detailed research report effectively.
Action: Web Search
Action Input: {"search_query": "innovations in AI for healthcare 2023 latest updates and challenges"}
Thought: I now have ample information to construct a research report detailing innovations, challenges, and opportunities of AI in healthcare in 2023.
"""
assert isinstance(result, AgentAction)
assert result.text.strip() == expected_text.strip()
def test_ensure_agent_action_is_selected_when_model_hallucinates_observation_field(parser):
text = """Let's continue our effort to gather comprehensive, well-rounded information about AI in healthcare in 2023 to compile a detailed research report effectively.
Action: Web Search
Action Input: {"search_query": "innovations in AI for healthcare 2023 latest updates and challenges"}
Observation: The search is yielding repeated and abundant information on the fragmented, redundant regulatory frameworks, clinical validation importance, and varied insights about AIs ongoing integration challenges in healthcare. To ensure a rich mix of insights, let's compile, structure, and organize these insights into a coherent report.
Content Synthesis:
- **Innovations and Trends**:
- AI is significantly contributing to personalized medicine, enabling more accurate patient diagnosis and treatment plans.
- Deep learning models, especially in image and pattern recognition, are revolutionizing radiology and pathology.
Final Answer: The finalized detailed research report on AI in Healthcare, 2023:
Title: Current Innovations, Challenges, and Potential of AI in Healthcare - 2023 Overview
Introduction:
The integration of Artificial Intelligence (AI) in healthcare is heralding a new era of modern medicine. In 2023, substantial technological advancements have brought about transformative changes in healthcare delivery. This report explores the latest AI innovations, identifies prevalent challenges, and discusses the potential opportunities in healthcare.
Innovations and Trends:
AI technologies are becoming deeply embedded in various aspects of healthcare operations. Key advancements include:
- Personalized Medicine: AI's analytical capabilities produce precise diagnostic outcomes and tailored treatment plans, fostering personalized medicine.
- Radiology and Pathology: AI, particularly through advanced deep learning models, is improving imaging accuracy, thereby transforming radiological and pathological analyses.
"""
result = parser.parse(text)
expected_text = """Let's continue our effort to gather comprehensive, well-rounded information about AI in healthcare in 2023 to compile a detailed research report effectively.
Action: Web Search
Action Input: {"search_query": "innovations in AI for healthcare 2023 latest updates and challenges"}
"""
assert isinstance(result, AgentAction)
assert result.text.strip() == expected_text.strip()
def test_ensure_agent_finish_is_selected_when_no_action_was_provided(parser):
text = """
```
Thought: The repeated results indicate that there may be a technical issue retrieving new information. I will summarize the available knowledge to complete the task.
Final Answer:
Research Report on AI in Healthcare (2023)
1. Introduction:
AI technologies have become increasingly important in healthcare for their potential to transform patient care, diagnostics, and operational efficiencies. As we progress through 2023, significant advancements are noted alongside various challenges that need addressing.
2. Developments in AI Technologies:
Recent years have seen AI significantly impact medical imaging, precision medicine, drug discovery, and robotic surgery. AI algorithms, such as neural networks and machine learning models, provide breakthroughs in analyzing large datasets to identify disease patterns, optimize treatment plans, and predict outcomes. In 2023, AI continues to be integrated within electronic health records, telemedicine platforms, and virtual health assistants, expanding its access and utility.
3. Challenges:
- **Data Quality and Availability:** AI models require accurate, comprehensive data. However, healthcare data often remains fragmented and inconsistent, limiting AI's efficacy. High-quality data collection and management are crucial.
- **Regulatory Frameworks:** Establishing clear regulations is imperative to ensure AI is used safely in clinical environments. Policymakers need to develop standards for AI research, implementation, and continuous monitoring.
- **Clinical Validation:** Before deploying AI models in healthcare applications, they must undergo rigorous clinical validation to confirm their safety and effectiveness.
- **Privacy and Consent:** Patient data privacy concerns persist. AI systems need robust mechanisms for data protection and maintaining patient consent when using personal health information.
4. Future Potentials:
AI holds the potential to democratize access to healthcare services by making diagnostic tools more accessible and improving personalized treatment plans. Future research and investments are expected to focus on enhancing AI models to process and generate insights from electronic health records, predict patient admissions, and improve monitoring systems in real time.
5. Conclusion:
In 2023, AI in healthcare continues to grow, supported by technological advancements and increased investment, despite ongoing challenges. Addressing these issues could allow AI to revolutionize healthcare, improving patient outcomes, and streamlining the efficiency of healthcare systems worldwide.
```
"""
result = parser.parse(text)
assert isinstance(result, AgentFinish)
assert result.text.strip() == text.strip()