File size: 4,547 Bytes
eceb45a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from agency_swarm.agents import Agent
from agency_swarm.tools import CodeInterpreter
import os
import logging
from .tools.SearchAndScrape import SearchAndScrape

class ValidationAgent(Agent):
    def __init__(self):
        super().__init__(
            name="ValidationAgent",
            description="This agent validates market research reports using AI and ensures data completeness.",
            instructions="./instructions.md",
            files_folder="./files",
            schemas_folder="./schemas",
            tools=[SearchAndScrape],
            tools_folder="./tools",
            temperature=0.3,
            model="groq/llama-3.3-70b-versatile", 
            max_prompt_tokens=25000,
        )

    def validate_data(self, report_data):
        """Validate the report using AI and fill gaps if needed."""
        validation_prompt = f"""
        Analyze this market research report for quality and completeness:

        {report_data}

        Please check for:
        1. Missing key information
        2. Data accuracy and consistency
        3. Logical flow and structure
        4. Completeness of sections:
           - Market Size & Growth
           - Competitive Landscape
           - Consumer Analysis
           - Technology & Innovation
           - Future Outlook

        Provide a detailed assessment with:
        1. Quality score (0-100)
        2. List of missing or incomplete sections
        3. Specific recommendations for improvement
        4. Additional data points needed

        Format: JSON with these keys:
        {
            "quality_score": int,
            "missing_sections": list,
            "recommendations": list,
            "additional_data_needed": list,
            "is_complete": boolean
        }
        """

        try:
            model = self._get_model()  # Updated method to get model
            response = model.generate_content(validation_prompt)
            validation_result = response.text
            
            # If validation shows missing data, scrape for it
            if '"is_complete": false' in validation_result.lower():
                missing_data = self._fill_missing_data(validation_result)
                if missing_data:
                    # Combine original report with new data
                    updated_report = self._merge_reports(report_data, missing_data)
                    # Validate again
                    return self.validate_data(updated_report)
            
            return validation_result

        except Exception as e:
            logging.error(f"Validation error: {str(e)}")
            return {"error": str(e)}

    def _fill_missing_data(self, validation_result):
        """Fill missing data based on validation results."""
        try:
            # Extract missing sections from validation result
            import json
            result = json.loads(validation_result)
            missing_sections = result.get("missing_sections", [])
            
            additional_data = []
            for section in missing_sections:
                # Create specific search query for missing section
                search_query = f"{section} market research data analysis"
                tool = SearchAndScrape(query=search_query)
                section_data = tool.run()
                if section_data:
                    additional_data.append({
                        "section": section,
                        "content": section_data
                    })
            
            return additional_data if additional_data else None

        except Exception as e:
            logging.error(f"Error filling missing data: {str(e)}")
            return None

    def _merge_reports(self, original_report, new_data):
        """Merge original report with newly scraped data."""
        merge_prompt = f"""
        Merge this original report with new data:

        Original Report:
        {original_report}

        New Data to Add:
        {new_data}

        Please create a cohesive, well-structured report that incorporates all information without duplication.
        Ensure proper flow and transitions between sections.
        """

        try:
            model = self._get_model()  # Updated method to get model
            response = model.generate_content(merge_prompt)
            return response.text
        except Exception as e:
            logging.error(f"Error merging reports: {str(e)}")
            return original_report

    def response_validator(self, message):
        return message