├── .github └── workflows │ └── python-package.yml ├── .gitignore ├── LICENSE ├── README.md ├── null.devcontainer ├── Dockerfile └── devcontainer.json ├── project-root ├── app │ ├── __init__.py │ ├── api │ │ ├── __init__.py │ │ ├── advanced_reports.py │ │ └── endpoints.py │ ├── core │ │ ├── __init__.py │ │ └── config.py │ ├── main.py │ ├── models │ │ ├── __init__.py │ │ └── report.py │ ├── services │ │ ├── __init__.py │ │ ├── report_generator-sync.py │ │ └── report_generator.py │ └── utils │ │ ├── __init__.py │ │ └── exa_search.py ├── install.sh ├── start.py ├── start.sh └── tests │ ├── __init__.py │ ├── test_main.py │ └── test_report_generator.py ├── reports-example.ipynb ├── requirements.txt ├── sample-reports ├── README.md ├── advanced-logic-and-reasoning.md ├── ai-in-agriculture.md ├── ai-in-cybersecurity.md ├── ai-in-education.md ├── ai-in-environmental-conservation.md ├── ai-in-finance.md ├── ai-in-healthcare.md ├── ai-in-manufacturing.md ├── ai-in-retail.md ├── ai-in-smart-cities.md ├── ai-in-transportation.md ├── combining-approaches.md ├── graph-structures.md ├── multi-hop-prompting.md └── recursive-prompting.md ├── scripts ├── fix-path.sh └── setup_env.sh ├── setup.py ├── start.py └── start.sh /.github/workflows/python-package.yml: -------------------------------------------------------------------------------- 1 | name: Python package 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v2 12 | - name: Set up Python 13 | uses: actions/setup-python@v2 14 | with: 15 | python-version: '3.x' 16 | - name: Install dependencies 17 | run: | 18 | python -m pip install --upgrade pip 19 | pip install flake8 pytest 20 | if [ -f requirements.txt ]; then pip install -r requirements.txt; fi 21 | - name: Lint with flake8 22 | run: | 23 | # stop the build if there are Python syntax errors or undefined names 24 | flake8 . 25 | - name: Test with pytest 26 | run: | 27 | pytest 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/latest/usage/project/#working-with-version-control 110 | .pdm.toml 111 | .pdm-python 112 | .pdm-build/ 113 | 114 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 115 | __pypackages__/ 116 | 117 | # Celery stuff 118 | celerybeat-schedule 119 | celerybeat.pid 120 | 121 | # SageMath parsed files 122 | *.sage.py 123 | 124 | # Environments 125 | .env 126 | .venv 127 | env/ 128 | venv/ 129 | ENV/ 130 | env.bak/ 131 | venv.bak/ 132 | 133 | # Spyder project settings 134 | .spyderproject 135 | .spyproject 136 | 137 | # Rope project settings 138 | .ropeproject 139 | 140 | # mkdocs documentation 141 | /site 142 | 143 | # mypy 144 | .mypy_cache/ 145 | .dmypy.json 146 | dmypy.json 147 | 148 | # Pyre type checker 149 | .pyre/ 150 | 151 | # pytype static type analyzer 152 | .pytype/ 153 | 154 | # Cython debug symbols 155 | cython_debug/ 156 | 157 | # PyCharm 158 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 159 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 160 | # and can be added to the global gitignore or merged into this file. For a more nuclear 161 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 162 | #.idea/ 163 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 rUv 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Agentic Reports 2 | ``` 3 | _ ___ ___ _ _ _____ ___ ___ ___ ___ ___ ___ ___ _____ ___ 4 | /_\ / __| __| \| |_ _|_ _/ __| | _ | __| _ \/ _ \| _ |_ _/ __| 5 | / _ | (_ | _|| .` | | | | | (__ | | _|| _| (_) | / | | \__ \ 6 | /_/ \_\___|___|_|\_| |_| |___\___| |_|_|___|_| \___/|_|_\ |_| |___/ 7 | 8 | A Comprehensive Python Library for Generating Research Reports 9 | 10 | ``` 11 | Welcome to Agentic Reports, a Python library designed to simplify the process of generating comprehensive research reports. This library leverages the power of FastAPI, Pydantic, Pandas, and Exa to provide users with an efficient and streamlined way to create detailed reports based on various data sources. Agentic Reports uses real data from the internet, considering parameters such as time, source, domain, and other relevant factors to ensure the information is current and accurate. 12 | 13 | ### Technical Overview 14 | 15 | Agentic Reports uses a multi-step process to deliver detailed research reports from a variety of sources: 16 | 17 | 1. **User Query Submission**: Users start by submitting a topic they wish to research. This can be done through a simple API request. 18 | 19 | 2. **Subquery Generation**: The system automatically generates a set of subqueries related to the main topic. These subqueries break down the broad topic into specific areas of focus, ensuring a comprehensive analysis. 20 | 21 | 3. **Data Collection**: Using the generated subqueries, the system searches for relevant information across various sources, including databases, external APIs, and other repositories. Web data features include filtering by time, source, domain, and other parameters to ensure the information is current and relevant. 22 | 23 | 4. **Data Compilation**: The collected data is compiled into a cohesive and structured report. The system organizes the information, providing detailed analysis, insights, and findings based on the initial topic and subqueries. 24 | 25 | 5. **Report Delivery**: The final report, complete with citations and data sources, is delivered back to the user in a well-organized format. This ensures that users receive a thorough and reliable resource for their research needs. 26 | 27 | By following these steps, Agentic Reports provides users with a streamlined and efficient way to generate comprehensive research reports, making it an invaluable tool for in-depth analysis and information gathering. 28 | 29 | ### Features 30 | 31 | - **AI-Enhanced Report Generation**: Harnesses the power of advanced AI models to create detailed and precise reports, ensuring accuracy and depth in every analysis. 32 | - **Robust Data Analysis**: Utilizes powerful search capabilities combined with Pandas for extensive data manipulation and insightful analysis. 33 | - **Web Data Features**: Incorporates real-time data from the internet, allowing filtering by time, source, domain, and other parameters to ensure the most current and relevant information. 34 | - **Flexible Report Customization**: Provides users with the ability to tailor reports to their specific needs, offering a high degree of customization to fit various requirements. 35 | 36 | ### How to Install 37 | 38 | To install Agentic Reports, simply use pip: 39 | 40 | ```bash 41 | pip install agentic-reports 42 | ``` 43 | 44 | This command will install the library and all its dependencies, making it ready for use in your projects. 45 | 46 | ### Setting Up API Keys for Agentic Reports 47 | 48 | To use Agentic Reports, you need to set your OpenAI and Exa API keys. These keys can be set from the command line or will be prompted the first time you start the library. 49 | 50 | #### Obtaining API Keys 51 | 52 | - **OpenAI API Key**: To get your OpenAI API key, visit the [OpenAI API page](https://platform.openai.com/account/api-keys) and follow the instructions to create and retrieve your API key. 53 | - **Exa API Key**: To get your Exa API key, visit the [Exa API page](https://docs.exa.ai/) and follow the instructions to sign up and obtain your API key. 54 | 55 | #### Automatic Prompt on First Start 56 | 57 | When you first start the Agentic Reports library, it will check for the necessary API keys. If they are not set, it will prompt you to enter them: 58 | 59 | ```bash 60 | agentic-reports 61 | ``` 62 | 63 | You will see prompts like: 64 | 65 | ```plaintext 66 | Enter your OPENAI_API_KEY: 67 | Enter your EXA_API_KEY: 68 | ``` 69 | 70 | This ensures that you have the required keys set up before the application starts. Once entered, these keys will be used for subsequent runs of the application. 71 | 72 | By following these steps, you can easily set up and use your API keys to get the most out of Agentic Reports. For more detailed information, refer to the official documentation provided with the library. 73 | 74 | #### Alternative Command Line Setup 75 | 76 | 1. **Set API Keys Manually**: You can manually set your API keys using the `export` command in your terminal: 77 | 78 | ```bash 79 | export OPENAI_API_KEY="your_openai_api_key" 80 | export EXA_API_KEY="your_exa_api_key" 81 | ``` 82 | 83 | 2. **Check if API Keys are Set**: To verify that your API keys are set, you can use the `echo` command: 84 | 85 | ```bash 86 | echo $OPENAI_API_KEY 87 | echo $EXA_API_KEY 88 | ``` 89 | 90 | ### How `Agentic Reports` Works 91 | 92 | `Agentic Reports` is designed to make the process of generating detailed research reports seamless and user-friendly. 93 | 94 | #### Step 1: Submitting a Topic 95 | 96 | 1. **Submit a Topic**: The user starts by submitting a topic they want to research. This can be done through a simple API request, where the user provides the main topic of interest. For example, "Latest AI advancements". 97 | 98 | #### Step 2: Generating Subqueries 99 | 100 | 2. **Automatic Subquery Generation**: Once the main topic is submitted, the system automatically generates a set of detailed subqueries related to the main topic. These subqueries help in breaking down the broad topic into specific areas of focus, ensuring a comprehensive analysis. 101 | 102 | #### Step 3: Data Collection 103 | 104 | 3. **Data Gathering**: The system then uses these subqueries to search for relevant information across various sources. This includes fetching data from databases, external APIs, and other repositories. The goal is to gather as much pertinent information as possible. 105 | 106 | #### Step 4: Compiling the Report 107 | 108 | 4. **Report Compilation**: After collecting the data, the system compiles all the gathered information into a cohesive and structured report. This report includes detailed analysis, insights, and findings based on the provided topic and its subqueries. 109 | 110 | #### Step 5: Delivering the Report 111 | 112 | 5. **Report Delivery**: The final report is delivered back to the user in a well-organized format. The user can then review the comprehensive report, which includes citations, data sources, and detailed explanations, providing a thorough understanding of the topic. 113 | 114 | ### User Experience Highlights 115 | 116 | - **Ease of Use**: Users only need to provide a single topic to get started. The rest of the process, including generating subqueries and gathering data, is handled automatically. 117 | - **Comprehensive Analysis**: By breaking down the main topic into subqueries, the system ensures a deep and thorough exploration of the subject matter. 118 | - **Time Efficiency**: Automating the research process saves users significant time and effort, providing them with detailed reports quickly. 119 | - **Detailed Insights**: The final report includes citations and sources, offering users a reliable and informative resource for their research needs. 120 | 121 | Agentic Reports, through `Agentic Reports`, streamlines the entire process of creating detailed research reports, making it an invaluable tool for anyone needing comprehensive and accurate information on a specific topic. 122 | 123 | ### API Reference 124 | 125 | Agentic Reports provides several endpoints for generating reports and processing data: 126 | 127 | - **/generate-report**: Generates a comprehensive report based on a given topic. 128 | - Parameters: `topic` (string) 129 | - Example Request: `{"topic": "Latest AI advancements"}` 130 | 131 | - **/generate-subqueries**: Generates subqueries from a given topic for detailed analysis. 132 | - Parameters: `topic` (string), `num_subqueries` (int) 133 | - Example Request: `{"topic": "Latest AI advancements", "num_subqueries": 5}` 134 | 135 | - **/search-subqueries**: Searches for information based on provided subqueries. 136 | - Parameters: `subqueries` (list of strings) 137 | - Example Request: `{"subqueries": ["AI in healthcare", "AI in finance"]}` 138 | 139 | - **/advanced-search**: Performs an advanced search with customizable parameters. 140 | - Parameters: `query` (string), `start_published_date` (string), `end_published_date` (string), etc. 141 | - Example Request: `{"query": "AI", "start_published_date": "2021-01-01", "end_published_date": "2021-12-31"}` 142 | 143 | - **/find-similar-links**: Finds similar links to a provided URL. 144 | - Parameters: `url` (string), `num_results` (int) 145 | - Example Request: `{"url": "https://cnn.com", "num_results": 10}` 146 | 147 | - **/generate-report-advanced**: Generates an advanced, comprehensive report based on detailed subqueries and provided prompts. 148 | - Parameters: 149 | - `query` (string): The main topic for the report. 150 | - `primary_prompt` (string): The primary prompt for the report generation. 151 | - `subqueries_prompt` (string): The prompt for generating subqueries. 152 | - `report_prompt` (string): The detailed prompt for generating the final report. 153 | - `start_published_date` (string, optional): The start date for filtering published articles. 154 | - `end_published_date` (string, optional): The end date for filtering published articles. 155 | - `include_domains` (list of strings, optional): Domains to include in the search. 156 | - `exclude_domains` (list of strings, optional): Domains to exclude from the search. 157 | - `highlights` (dict, optional): Parameters for highlighting text. 158 | - `text` (dict, optional): Parameters for including HTML tags in the text. 159 | - `num_subqueries` (int): The number of subqueries to generate. 160 | - `batch_size` (int, optional): The size of each batch for processing subqueries. 161 | 162 | ### Sample Request JSON 163 | 164 | ``` 165 | { 166 | "query": "string", // The main topic for the report 167 | "primary_prompt": "string", // The primary prompt for the report generation 168 | "subqueries_prompt": "string", // The prompt for generating subqueries 169 | "report_prompt": "string", // The detailed prompt for generating the final report 170 | "start_published_date": "string (YYYY-MM-DD)", // Optional: The start date for filtering published articles 171 | "end_published_date": "string (YYYY-MM-DD)", // Optional: The end date for filtering published articles 172 | "include_domains": ["string"], // Optional: Domains to include in the search 173 | "exclude_domains": ["string"], // Optional: Domains to exclude from the search 174 | "highlights": { 175 | "num_sentences": "int" // Optional: Parameters for highlighting text 176 | }, 177 | "text": { 178 | "include_html_tags": "boolean" // Optional: Parameters for including HTML tags in the text 179 | }, 180 | "num_subqueries": "int", // The number of subqueries to generate 181 | "batch_size": "int" // Optional: The size of each batch for processing subqueries 182 | } 183 | 184 | ``` 185 | - remove "// descriptions" 186 | 187 | ### Advanced Uses 188 | 189 | Agentic Reports can be used in a variety of advanced scenarios, such as: 190 | 191 | - **Automated Generation of Research Papers and Articles**: Automatically generate in-depth research papers and articles by leveraging AI models that create comprehensive and well-structured content based on user-defined topics. 192 | 193 | - **Data Analysis and Visualization for Business Intelligence**: Utilize the library to perform sophisticated data analysis and create visualizations that aid in business decision-making and strategy formulation. 194 | 195 | - **Custom Report Generation for Specific Industries or Topics**: Tailor reports to meet the unique needs of different industries or specific research topics, providing highly relevant and targeted information. 196 | 197 | ### Advanced Report Generation 198 | 199 | The `@router.post("/generate-report-advanced")` endpoint is designed for generating comprehensive and detailed reports based on advanced queries and parameters. This endpoint allows for a high degree of customization, enabling users to specify detailed prompts, subqueries, and other parameters to tailor the report generation process to their specific needs. 200 | 201 | #### Endpoint Purpose and Functionality 202 | 203 | The endpoint aims to provide users with the ability to generate reports that require a deeper level of analysis and customization than standard reports. It supports a wide range of parameters that can be used to define the scope, focus, and structure of the generated report. 204 | 205 | #### Parameters 206 | 207 | - `query` (string): The main query or topic for the report. 208 | - `primary_prompt` (string): The primary prompt guiding the report's focus. 209 | - `subqueries_prompt` (string): Prompts for generating subqueries related to the main topic. 210 | - `report_prompt` (string): The prompt for structuring the final report. 211 | - `start_published_date` (string, optional): The start date for filtering published data. 212 | - `end_published_date` (string, optional): The end date for filtering published data. 213 | - `include_domains` (list of strings, optional): Domains to include in the search. 214 | - `exclude_domains` (list of strings, optional): Domains to exclude from the search. 215 | - `highlights` (dict, optional): Parameters for highlighting key sentences. 216 | - `text` (dict, optional): Parameters for text content retrieval. 217 | - `num_subqueries` (int): The number of subqueries to generate. 218 | 219 | #### Example JSON Request 220 | 221 | ```json 222 | { 223 | "query": "AI Stocks", 224 | "primary_prompt": "Generate a detailed report on the current advancements, performance, and market trends of AI stocks. The report should include an analysis of recent developments, major players, market performance, challenges faced by AI stocks, and future prospects. Ensure that the report is well-structured and professional.", 225 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on AI stocks. These subqueries should cover various aspects of the topic, including recent advancements, market performance, major players, challenges, and future prospects.", 226 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about AI stocks based on the provided information. Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. Reset the citations index and ignore the order of citations in the provided information.", 227 | "start_published_date": "2024-01-01", 228 | "end_published_date": "2024-06-03", 229 | "highlights": { 230 | "num_sentences": 5 231 | }, 232 | "text": { 233 | "include_html_tags": false 234 | }, 235 | "num_subqueries": 5 236 | } 237 | ``` 238 | 239 | #### Expected Response 240 | 241 | The response will include a structured report based on the provided parameters. The report will be comprehensive, including analysis, insights, and findings as specified in the prompts and subqueries. The response structure will also include any highlights or specific text content retrieval parameters as requested. 242 | 243 | By utilizing this advanced report generation feature, developers and researchers can create detailed and customized reports tailored to their specific research needs and interests. 244 | 245 | ### Advanced Overview: Prompt Engineering Approaches 246 | 247 | Agentic Reports incorporates advanced prompt engineering techniques to enhance the quality and depth of generated reports. These approaches include multi-hop prompting, recursive strategies, graph structures, and advanced logic and reasoning. 248 | 249 | #### Multi-Hop Prompting 250 | 251 | Multi-hop prompting involves breaking down complex queries into a series of intermediate steps or subqueries. Each subquery aims to gather specific pieces of information that, when combined, provide a comprehensive answer to the original query. This approach ensures that the final report is detailed and well-supported by relevant data. 252 | 253 | **Example**: To generate a report on "The impact of AI in healthcare," the system might break it down into subqueries such as: 254 | 1. Historical development of AI in healthcare. 255 | 2. Current applications of AI in diagnostics and treatment. 256 | 3. Case studies on AI-driven healthcare improvements. 257 | 4. Future trends and potential of AI in healthcare. 258 | 259 | #### Recursive Prompting 260 | 261 | Recursive prompting involves iterative querying, where the system refines its queries based on previous responses. This technique ensures that the gathered information is accurate and relevant, progressively narrowing down the search to provide precise and comprehensive results. 262 | 263 | **Example**: For a topic like "AI's role in climate change mitigation," the initial query might focus on general applications of AI. Based on the gathered data, subsequent queries will delve deeper into specific areas such as AI in renewable energy optimization, AI in carbon footprint reduction, and AI-driven climate modeling. 264 | 265 | #### Graph Structures 266 | 267 | Graph-based prompting leverages graph structures to map relationships between different pieces of information. By creating a network of interconnected data points, the system can identify and explore intricate connections, providing a more holistic and nuanced report. 268 | 269 | **Example**: When researching "AI in financial markets," a graph structure can help map out the relationships between AI technologies, market trends, regulatory impacts, and economic outcomes. This interconnected approach allows for a comprehensive analysis of how AI influences various aspects of financial markets. 270 | 271 | #### Advanced Logic and Reasoning 272 | 273 | Advanced logic and reasoning techniques are used to enhance the depth and accuracy of the reports. These techniques involve critical thinking, hypothesis testing, and evidence-based analysis to ensure that the generated content is logical, coherent, and well-supported by data. 274 | 275 | **Example**: For a report on "Ethical implications of AI," the system uses advanced reasoning to weigh different ethical considerations, analyze case studies, and provide balanced arguments. It critically examines various perspectives, ensuring a thorough and unbiased analysis. 276 | 277 | ### Combining Approaches 278 | 279 | Agentic Reports often combines these prompt engineering techniques to maximize the quality and comprehensiveness of the generated reports. By integrating multi-hop prompting, recursive strategies, graph structures, and advanced logic and reasoning, the system can tackle complex topics with precision and depth. 280 | 281 | **Example**: Generating a report on "The future of AI in smart cities" might involve: 282 | - **Multi-hop prompting** to break down the topic into subqueries like AI in traffic management, AI in energy efficiency, and AI in public safety. 283 | - **Recursive prompting** to iteratively refine these subqueries based on initial findings. 284 | - **Graph structures** to map out the relationships between different AI applications and their impacts on smart city development. 285 | - **Advanced logic and reasoning** to critically analyze the data and provide well-rounded insights and projections. 286 | 287 | By leveraging these advanced prompt engineering techniques, Agentic Reports ensures that users receive highly detailed, accurate, and insightful reports tailored to their specific research needs. 288 | 289 | Here's the updated README with the link pointing to `./sample-reports/`: 290 | 291 | # Advanced Reporting Techniques in Agentic Reports 292 | 293 | This README provides an overview of advanced reporting techniques utilized in Agentic Reports, showcasing examples and JSON configurations for each method. These techniques enhance the depth, accuracy, and comprehensiveness of generated reports, tailored to specific research needs. 294 | 295 | ## Recursive Prompting 296 | 297 | Recursive prompting involves iterative querying to refine searches based on previous responses, ensuring accurate and relevant information. 298 | 299 | [Learn more about Recursive Prompting](./sample-reports/recursive-prompting.md) 300 | 301 | ## Graph Structures 302 | 303 | Graph-based prompting uses graph structures to map relationships between information, identifying intricate connections for a holistic report. 304 | 305 | [Learn more about Graph Structures](./sample-reports/graph-structures.md) 306 | 307 | ## Advanced Logic and Reasoning 308 | 309 | This technique employs critical thinking and evidence-based analysis to ensure logical, coherent content supported by data. 310 | 311 | [Learn more about Advanced Logic and Reasoning](./sample-reports/advanced-logic-and-reasoning.md) 312 | 313 | ## Combining Approaches 314 | 315 | Agentic Reports often combine these techniques to maximize report quality, tackling complex topics with precision. 316 | 317 | [Learn more about Combining Approaches](./sample-reports/combining-approaches.md) 318 | 319 | ## JSON Example for Advanced Report Generation 320 | 321 | ```json 322 | { 323 | "query": "The future of AI in smart cities", 324 | "primary_prompt": "Generate a detailed report on the future of AI in smart cities, focusing on applications such as traffic management, energy efficiency, and public safety.", 325 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on the future of AI in smart cities.", 326 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about the future of AI in smart cities based on the provided information.", 327 | "start_published_date": "2024-01-01", 328 | "end_published_date": "2024-06-03", 329 | "highlights": { 330 | "num_sentences": 5 331 | }, 332 | "text": { 333 | "include_html_tags": false 334 | }, 335 | "num_subqueries": 5 336 | } 337 | ``` 338 | 339 | For more detailed examples and JSON configurations, refer to the respective `.md` files linked above. 340 | 341 | ## Sample Outputs 342 | ``` 343 | Starting report generation for topic: Agentic Engineering and the coming agent landscape 344 | 🌿 Generating subqueries from topic: Agentic Engineering and the coming agent landscape 345 | 346 | Raw response content: Certainly! Below are ten diverse and interesting search queries that can help you generate a comprehensive report on "Agentic 347 | 348 | Engineering and the coming agent landscape": 349 | 350 | 1. **"Agentic Engineering: Definitions and Key Concepts"** 351 | - Understand the foundational ideas and terminologies related to agentic engineering. 352 | 353 | 2. **"Historical Evolution and Milestones in Agentic Engineering"** 354 | - Explore the development and significant breakthroughs in the field. 355 | 356 | 3. **"Agentic Engineering in Artificial Intelligence and Robotics"** 357 | - Investigate the application of agentic engineering within AI and robotics. 358 | 359 | 4. **"Ethical Considerations and Challenges in Agentic Engineering"** 360 | - Delve into the ethical implications and potential societal impacts. 361 | 362 | 5. **"Future Landscape of Autonomous Agents in Industry and Everyday Life"** 363 | - Analyze future projections and the expected integration of autonomous agents in various sectors. 364 | 365 | 6. **"Case Studies of Successful Implementation of Agentic Engineering"** 366 | - Look at real-world examples and their outcomes to understand practical applications. 367 | 368 | 7. **"Agentic Engineering: Innovations and Upcoming Technologies"** 369 | - Identify the latest innovations and technologies that are shaping the field. 370 | 371 | 8. **"Comparative Analysis of Agentic Engineering vs Traditional Engineering"** 372 | - Compare and contrast agentic engineering with other engineering disciplines. 373 | 374 | 9. **"Regulatory Frameworks and Policies Around Agentic Engineering"** 375 | - Investigate the current and proposed regulations governing the development and deployment of autonomous agents. 376 | 377 | 10. **"Impact of Agentic Engineering on Employment and Workforce Dynamics"** 378 | - Examine how agentic engineering and autonomous agents are transforming job markets and skills requirements. 379 | 380 | These queries should provide a well-rounded basis for your research and reporting on the subject. 381 | Parsed subqueries: 382 | 383 | ['**"Agentic Engineering: Definitions and Key Concepts"**', '**"Historical Evolution and Milestones in Agentic Engineering"**', '**"Agentic Engineering in Artificial Intelligence and Robotics"**', '**"Ethical Considerations and Challenges in Agentic Engineering"**', '**"Future Landscape of Autonomous Agents in Industry and Everyday Life"**', '**"Case Studies of Successful Implementation of Agentic Engineering"**', '**"Agentic Engineering: Innovations and Upcoming Technologies"**', '**"Comparative Analysis of Agentic Engineering vs Traditional Engineering"**', '**"Regulatory Frameworks and Policies Around Agentic Engineering"**', '**"Impact of Agentic Engineering on Employment and Workforce Dynamics"**'] 384 | 385 | ⌛ Searching each subquery 386 | 🔍 Searching for subquery: **"Agentic Engineering: Definitions and Key Concepts"** 387 | 🔍 Searching for subquery: **"Historical Evolution and Milestones in Agentic Engineering"** 388 | 🔍 Searching for subquery: **"Agentic Engineering in Artificial Intelligence and Robotics"** 389 | 🔍 Searching for subquery: **"Ethical Considerations and Challenges in Agentic Engineering"** 390 | 🔍 Searching for subquery: **"Future Landscape of Autonomous Agents in Industry and Everyday Life"** 391 | 🔍 Searching for subquery: **"Case Studies of Successful Implementation of Agentic Engineering"** 392 | 🔍 Searching for subquery: **"Agentic Engineering: Innovations and Upcoming Technologies"** 393 | 🔍 Searching for subquery: **"Comparative Analysis of Agentic Engineering vs Traditional Engineering"** 394 | 🔍 Searching for subquery: **"Regulatory Frameworks and Policies Around Agentic Engineering"** 395 | 🔍 Searching for subquery: **"Impact of Agentic Engineering on Employment and Workforce Dynamics"** 396 | ✅ Search successful for subquery: **"Agentic Engineering: Definitions and Key Concepts"** 397 | ✅ Search successful for subquery: **"Historical Evolution and Milestones in Agentic Engineering"** 398 | ✅ Search successful for subquery: **"Agentic Engineering in Artificial Intelligence and Robotics"** 399 | ✅ Search successful for subquery: **"Ethical Considerations and Challenges in Agentic Engineering"** 400 | ✅ Search successful for subquery: **"Future Landscape of Autonomous Agents in Industry and Everyday Life"** 401 | ✅ Search successful for subquery: **"Case Studies of Successful Implementation of Agentic Engineering"** 402 | ✅ Search successful for subquery: **"Agentic Engineering: Innovations and Upcoming Technologies"** 403 | ✅ Search successful for subquery: **"Comparative Analysis of Agentic Engineering vs Traditional Engineering"** 404 | ✅ Search successful for subquery: **"Regulatory Frameworks and Policies Around Agentic Engineering"** 405 | ✅ Search successful for subquery: **"Impact of Agentic Engineering on Employment and Workforce Dynamics"** 406 | 🏁 Completed search for all subqueries 407 | Generating report from Exa results for topic: Agentic Engineering and the coming agent landscape 408 | ⌨️ Formatting Exa results for LLM 409 | ``` 410 | 411 | ## Sample Report 412 | ### Research Report: Agentic Engineering and the Coming Agent Landscape 413 | 414 | Agentic Engineering is an evolving field that seeks to integrate principles of agency into engineered systems, emphasizing attributes such as autonomy, functionality, intentionality, and meaning. Foundational concepts in naturalized accounts of agency highlight the interrelation of these attributes and their evolution over time, showcasing the complex properties that constitute autonomous agents[1]. This approach recognizes the importance of scientific inquiry and continuous formulation of questions to advance the understanding and capabilities of agentic systems. Autonomy and interaction are particularly emphasized as key drivers in developing robust and adaptable agents capable of significantly enhancing human-machine collaboration[1]. 415 | 416 | Recent advancements in AI and machine learning have catalyzed the development of agentic systems capable of implementing complex workflows and multi-agent collaborations. A course by Andrew Ng and collaborators showcases AutoGen, a framework that enables the creation of specialized agents designed for tasks such as multi-agent collaboration, tool use, and planning[2]. By leveraging such tools, developers can build conversational agents with capabilities ranging from playing chess to generating detailed financial reports with minimal manual intervention. These innovations are not merely confined to digital realms; they are poised to revolutionize physical tasks through integrations with robotics, as seen with AI systems that can autonomously manage real-world tasks such as self-driving cars[3]. 417 | 418 | One of the most significant impacts of Agentic Engineering is on the workforce and employment dynamics. While there is concern about the potential for AI to displace human jobs, there is also a significant opportunity for enhancing productivity and innovation. AI employees, like the Ema multi-agent system, are designed to augment human roles by automating repetitive and complex tasks[4]. These AI personas integrate with various enterprise applications, enabling users to deploy AI-driven workflows with minimal setup. This shift not only promises to optimize operational efficiency but also necessitates new regulatory frameworks and training programs to ensure that human workers can effectively collaborate with AI counterparts while maintaining data security and ethical standards[4][5]. 419 | 420 | ### References 421 | 422 | [1] https://www.sciencedirect.com/science/article/pii/S0732118X09000464 (Published: November 2140) 423 | 424 | [2] https://x.com/AndrewYNg/status/1795845101979406490 (Published: May 2024) 425 | 426 | [3] https://gizmodo.com/ai-agents-openai-chatgpt-google-gemini-reality-sci-fi-1851500474 (Published: May 2024) 427 | 428 | [4] https://medium.com/emaunlimited/the-guide-to-ai-employees-how-ema-is-revolutionizing-enterprise-automation-with-agentic-systems-16df43d8e8d8 (Published: May 2024) 429 | 430 | [5] https://superagi.com/autonomous-software-development/ (Published: May 2024) -------------------------------------------------------------------------------- /null.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.9-slim 2 | 3 | WORKDIR /workspace 4 | 5 | COPY requirements.txt . 6 | RUN pip install --no-cache-dir -r requirements.txt 7 | 8 | COPY . . 9 | 10 | CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"] 11 | -------------------------------------------------------------------------------- /null.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Agentic Reports", 3 | "dockerFile": "Dockerfile", 4 | "settings": { 5 | "terminal.integrated.shell.linux": "/bin/bash" 6 | }, 7 | "extensions": [ 8 | "ms-python.python", 9 | "ms-azuretools.vscode-docker" 10 | ], 11 | "postCreateCommand": "pip install -r requirements.txt && pip install . && chmod +x /workspaces/agentic-reports/scripts/setup_env.sh", 12 | "postStartCommand": "agentic-reports" 13 | } 14 | -------------------------------------------------------------------------------- /project-root/app/__init__.py: -------------------------------------------------------------------------------- 1 | # This file initializes the app package 2 | -------------------------------------------------------------------------------- /project-root/app/api/__init__.py: -------------------------------------------------------------------------------- 1 | # This file initializes the api sub-package 2 | -------------------------------------------------------------------------------- /project-root/app/api/advanced_reports.py: -------------------------------------------------------------------------------- 1 | from fastapi import APIRouter, HTTPException, status, Body 2 | from typing import List, Optional, Dict 3 | from app.services.report_generator import generate_advanced_report 4 | 5 | router = APIRouter() 6 | 7 | @router.post("/generate-report-advanced") 8 | async def generate_report_advanced_endpoint( 9 | query: str = Body(..., example="quantum computing"), 10 | primary_prompt: str = Body(..., example="Generate a detailed report on the current advancements and applications of quantum computing."), 11 | subqueries_prompt: str = Body(..., example="Please create subqueries related to quantum computing advancements and its various applications."), 12 | report_prompt: str = Body(..., example="Write a comprehensive and professional in french, five-paragraph, 800 word research report about quantum computing based on the provided information."), 13 | start_published_date: Optional[str] = Body(None, example="2023-01-01"), 14 | end_published_date: Optional[str] = Body(None, example="2023-12-31"), 15 | include_domains: Optional[List[str]] = Body(None, example=["example.com", "another-example.com"]), 16 | exclude_domains: Optional[List[str]] = Body(None, example=["excluded.com"]), 17 | highlights: Optional[Dict] = Body(None, example={"num_sentences": 5}), 18 | text: Optional[Dict] = Body(None, example={"include_html_tags": False}), 19 | num_subqueries: int = Body(10, example=10) 20 | ): 21 | try: 22 | report = await generate_advanced_report( 23 | query=query, 24 | primary_prompt=primary_prompt, 25 | subqueries_prompt=subqueries_prompt, 26 | report_prompt=report_prompt, 27 | start_published_date=start_published_date, 28 | end_published_date=end_published_date, 29 | include_domains=include_domains, 30 | exclude_domains=exclude_domains, 31 | highlights=highlights, 32 | text=text, 33 | num_subqueries=num_subqueries 34 | ) 35 | return {"report": report} 36 | except Exception as e: 37 | raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) 38 | -------------------------------------------------------------------------------- /project-root/app/api/endpoints.py: -------------------------------------------------------------------------------- 1 | # endpoints.py is the place where you define the FastAPI endpoints. 2 | 3 | from fastapi import APIRouter, HTTPException, status, Request, Body 4 | from app.services.report_generator import generate_report, generate_subqueries_from_topic, exa_search_each_subquery 5 | from app.core.config import settings 6 | from app.utils.exa_search import advanced_search_exa, find_similar_exa 7 | from typing import Optional, List, Dict 8 | from .advanced_reports import router as advanced_reports_router 9 | 10 | router = APIRouter() 11 | 12 | @router.post("/generate-report") 13 | async def generate_report_endpoint(topic: str, request: Request): 14 | try: 15 | report = await generate_report(topic) 16 | return {"report": report} 17 | except Exception as e: 18 | raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) 19 | 20 | @router.post("/generate-subqueries") 21 | async def generate_subqueries_endpoint( 22 | topic: str = Body(..., embed=True, example="Latest AI jobs in Toronto"), 23 | num_subqueries: int = Body(10, embed=True, example=10) 24 | ): 25 | try: 26 | subqueries = await generate_subqueries_from_topic(topic, num_subqueries) 27 | return {"subqueries": subqueries} 28 | except Exception as e: 29 | raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) 30 | 31 | @router.post("/search-subqueries") 32 | async def search_subqueries_endpoint( 33 | subqueries: List[str] = Body(..., embed=True, example=["AI job market in Canada", "Top AI companies hiring in Toronto"]) 34 | ): 35 | try: 36 | list_of_query_exa_pairs = await exa_search_each_subquery(subqueries) 37 | return {"results": list_of_query_exa_pairs} 38 | except Exception as e: 39 | raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) 40 | 41 | @router.post("/advanced-search") 42 | async def advanced_search_endpoint( 43 | query: str = Body("New York City condos", embed=True), 44 | start_published_date: Optional[str] = Body("2023-01-01", embed=True), 45 | end_published_date: Optional[str] = Body("2023-12-31", embed=True), 46 | include_domains: Optional[List[str]] = Body(None, embed=True), 47 | exclude_domains: Optional[List[str]] = Body(None, embed=True), 48 | highlights: Optional[Dict] = Body({"num_sentences": 3}, embed=True), 49 | text: Optional[Dict] = Body({"include_html_tags": False}, embed=True) 50 | ): 51 | try: 52 | # Ensure only one of include_domains or exclude_domains is specified 53 | if include_domains and exclude_domains: 54 | raise HTTPException( 55 | status_code=status.HTTP_400_BAD_REQUEST, 56 | detail="Only one of include_domains or exclude_domains can be specified." 57 | ) 58 | 59 | results = advanced_search_exa( 60 | query=query, 61 | start_published_date=start_published_date, 62 | end_published_date=end_published_date, 63 | include_domains=include_domains, 64 | exclude_domains=exclude_domains, 65 | highlights=highlights, 66 | text=text 67 | ) 68 | return {"results": results} 69 | except HTTPException as e: 70 | raise e 71 | except Exception as e: 72 | raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) 73 | 74 | @router.post("/find-similar-links", summary="Find similar links to the provided URL") 75 | async def find_similar_links_endpoint( 76 | url: str = Body(..., embed=True, example="https://drudgereport.com"), 77 | num_results: Optional[int] = Body(10, embed=True, example=10), 78 | include_domains: Optional[List[str]] = Body(None, embed=True, example=["example.com"]), 79 | exclude_domains: Optional[List[str]] = Body(None, embed=True, example=["excluded.com"]), 80 | start_crawl_date: Optional[str] = Body(None, embed=True, example="2023-01-01"), 81 | end_crawl_date: Optional[str] = Body(None, embed=True, example="2023-12-31"), 82 | start_published_date: Optional[str] = Body(None, embed=True, example="2023-01-01"), 83 | end_published_date: Optional[str] = Body(None, embed=True, example="2023-12-31"), 84 | category: Optional[str] = Body(None, embed=True, example="news") 85 | ): 86 | try: 87 | # Ensure only one of include_domains or exclude_domains is specified 88 | if include_domains and exclude_domains: 89 | raise HTTPException( 90 | status_code=status.HTTP_400_BAD_REQUEST, 91 | detail="Only one of include_domains or exclude_domains can be specified." 92 | ) 93 | 94 | results = find_similar_exa( 95 | url=url, 96 | num_results=num_results, 97 | include_domains=include_domains, 98 | exclude_domains=exclude_domains, 99 | start_crawl_date=start_crawl_date, 100 | end_crawl_date=end_crawl_date, 101 | start_published_date=start_published_date, 102 | end_published_date=end_published_date, 103 | category=category 104 | ) 105 | return {"results": results} 106 | except HTTPException as e: 107 | raise e 108 | except Exception as e: 109 | raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) 110 | 111 | # Include the advanced_reports router 112 | router.include_router(advanced_reports_router, prefix="/api/v1", tags=["advanced-reports"]) -------------------------------------------------------------------------------- /project-root/app/core/__init__.py: -------------------------------------------------------------------------------- 1 | # This file initializes the core sub-package 2 | -------------------------------------------------------------------------------- /project-root/app/core/config.py: -------------------------------------------------------------------------------- 1 | # config.py: Configuration settings for the application. This file contains the Settings class, which is used to define the configuration settings for the application. The settings are loaded from environment variables using the pydantic_settings library. The describe_fields method is used to provide a description of the configuration settings. 2 | import os 3 | from pydantic_settings import BaseSettings 4 | 5 | class Settings(BaseSettings): 6 | """ 7 | Settings class for application configuration. 8 | 9 | System OS Secrets: 10 | - EXA_API_KEY: API key for Exa service 11 | - OPENAI_API_KEY: API key for OpenAI service 12 | """ 13 | exa_api_key: str = os.getenv("EXA_API_KEY", "Please_set_EXA_API_KEY_system_OS_secret") 14 | openai_api_key: str = os.getenv("OPENAI_API_KEY", "Please_set_OPENAI_API_KEY_system_OS_secret") 15 | 16 | class Config: 17 | env_file = ".env" 18 | env_file_encoding = 'utf-8' 19 | 20 | @staticmethod 21 | def describe_fields(): 22 | return { 23 | 'exa_api_key': 'API key for Exa service retrieved from system OS secrets', 24 | 'openai_api_key': 'API key for OpenAI service retrieved from system OS secrets' 25 | } 26 | 27 | settings = Settings() -------------------------------------------------------------------------------- /project-root/app/main.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI 2 | from app.api import endpoints 3 | from fastapi.responses import RedirectResponse 4 | 5 | app = FastAPI() 6 | app = FastAPI(title="Agentic Reports (v0.01)") 7 | 8 | @app.get("/", include_in_schema=False) 9 | async def docs_redirect(): 10 | return RedirectResponse(url='/docs') 11 | 12 | app.include_router(endpoints.router) 13 | -------------------------------------------------------------------------------- /project-root/app/models/__init__.py: -------------------------------------------------------------------------------- 1 | # This file initializes the models package 2 | -------------------------------------------------------------------------------- /project-root/app/models/report.py: -------------------------------------------------------------------------------- 1 | from pydantic import BaseModel 2 | 3 | class Report(BaseModel): 4 | topic: str 5 | content: str 6 | -------------------------------------------------------------------------------- /project-root/app/services/__init__.py: -------------------------------------------------------------------------------- 1 | # This file initializes the services sub-package 2 | -------------------------------------------------------------------------------- /project-root/app/services/report_generator-sync.py: -------------------------------------------------------------------------------- 1 | # project-root/app/services/report_generator-sync.py is a synchronous version of the report_generator.py service. 2 | # It uses the synchronous version of the OpenAI API client and synchronous Exa search functions. 3 | # This version is suitable for applications that require synchronous processing or do not support asynchronous operations. 4 | # The functions in this module perform the following tasks: 5 | # - Initialize the Exa client for searching the Exa service. 6 | # - Generate subqueries from a given topic using the GPT-4 model. 7 | # - Search Exa for each subquery to retrieve relevant information. 8 | # - Format the Exa search results for input to the GPT-4 model. 9 | # - Generate a research report based on the Exa search results using the GPT-4 model. 10 | # - Perform a search with retries and exponential backoff. 11 | # - Provide a synchronous version of the report generation process. 12 | 13 | import os 14 | import json 15 | import time 16 | 17 | import asyncio 18 | import aiohttp 19 | 20 | from datetime import datetime, timedelta 21 | from litellm import acompletion 22 | from app.core.config import settings 23 | from app.utils.exa_search import search_exa 24 | 25 | # Set up API keys using environment variables 26 | os.environ["OPENAI_API_KEY"] = os.getenv('OPENAI_API_KEY', settings.openai_api_key) 27 | 28 | async def generate_report(topic: str) -> str: 29 | print(f"Starting report generation for topic: {topic}") 30 | subqueries = await generate_subqueries_from_topic(topic) 31 | list_of_query_exa_pairs = exa_search_each_subquery(subqueries) 32 | report = await generate_report_from_exa_results(topic, list_of_query_exa_pairs) 33 | return report 34 | 35 | async def generate_report_without_exa(topic): 36 | print(f"🚀 Generating report without Exa for topic: {topic}") 37 | content = f"Write a comprehensive and professional three-paragraph research report about {topic}. Include citations with source, month, and year." 38 | try: 39 | completion = await acompletion( 40 | model='gpt-4o', 41 | messages=[{"role": "user", "content": content}], 42 | stream=False 43 | ) 44 | report = completion['choices'][0]['message']['content'] 45 | except Exception as e: 46 | report = f"Failed to generate report: {str(e)}" 47 | return report 48 | 49 | async def generate_subqueries_from_topic(topic, num_subqueries=6): 50 | print(f"🌿 Generating subqueries from topic: {topic}") 51 | content = f"I'm going to give you a topic I want to research. I want you to generate {num_subqueries} interesting, diverse search queries that would be useful for generating a report on my main topic. Here is the main topic: {topic}." 52 | try: 53 | completion = await acompletion( 54 | model='gpt-4o', 55 | messages=[{"role": "user", "content": content}], 56 | stream=False 57 | ) 58 | response_content = completion['choices'][0]['message']['content'] 59 | print(f"Raw response content: {response_content}") # Print raw response for debugging 60 | 61 | # Split the response into individual lines and strip leading/trailing spaces and numbers 62 | subqueries = [] 63 | for line in response_content.split('\n'): 64 | if line.strip(): 65 | # Attempt to split by ". " and take the second part 66 | try: 67 | subquery = line.strip(' "').split('. ', 1)[1] 68 | subqueries.append(subquery) 69 | except IndexError: 70 | print(f"Skipping improperly formatted line: {line}") 71 | 72 | # Output the parsed subqueries for debugging 73 | print(f"Parsed subqueries: {subqueries}") 74 | 75 | except Exception as e: 76 | print(f"Unexpected error: {str(e)}") # Output unexpected error details 77 | subqueries = [f"Failed to generate subqueries: {str(e)}"] 78 | return subqueries 79 | 80 | def exa_search_each_subquery(subqueries): 81 | print(f"⌛ Searching each subquery") 82 | list_of_query_exa_pairs = [] 83 | 84 | # Initialize the Exa client (assuming you have the initialization code) 85 | exa_client = initialize_exa_client() # Add your initialization logic here 86 | 87 | if exa_client is None: 88 | print("⚠️ Exa client is not initialized. Check your initialization logic.") 89 | return [] 90 | 91 | def perform_search(query, retries=5, delay=2): 92 | """Helper function to perform search with retries and exponential backoff.""" 93 | for attempt in range(retries): 94 | try: 95 | # Perform the search using Exa API 96 | search_response = exa_client.search_and_contents( 97 | query=query, 98 | type='neural' 99 | ) 100 | # Check if response contains data 101 | if search_response and hasattr(search_response, 'results') and search_response.results: 102 | return search_response 103 | else: 104 | print(f"⚠️ No data found for subquery: {query}") 105 | return None 106 | except Exception as e: 107 | print(f"⚠️ Attempt {attempt + 1} failed for query '{query}': {str(e)}") 108 | if "502" in str(e) and attempt < retries - 1: 109 | backoff = delay * (2 ** attempt) # Exponential backoff 110 | print(f"Retrying in {backoff} seconds...") 111 | time.sleep(backoff) 112 | else: 113 | print(f"⚠️ All {retries} attempts failed for query '{query}'") 114 | return None 115 | 116 | for query in subqueries: 117 | if "Failed to generate subqueries" in query: 118 | print(f"⚠️ Skipping invalid subquery: {query}") 119 | continue 120 | 121 | print(f"🔍 Searching for subquery: {query}") 122 | search_response = perform_search(query) 123 | if search_response: 124 | print(f"✅ Search successful for subquery: {query}") 125 | list_of_query_exa_pairs.append({ 126 | 'subquery': query, 127 | 'results': search_response.results 128 | }) 129 | 130 | print(f"🏁 Completed search for all subqueries") 131 | return list_of_query_exa_pairs 132 | 133 | def format_exa_results_for_llm(list_of_query_exa_pairs): 134 | print(f"⌨️ Formatting Exa results for LLM") 135 | formatted_string = "" 136 | for pair in list_of_query_exa_pairs: 137 | formatted_string += f"[{pair['subquery']}]:\n" 138 | for result in pair['results']: 139 | content = getattr(result, 'text', "No text available") 140 | publish_date = getattr(result, 'published_date', "No date available") 141 | url = getattr(result, 'url', "No URL available") 142 | formatted_string += f"URL: {url}\nContent: {content}\nPublish Date: {publish_date}\n" 143 | formatted_string += "\n" 144 | return formatted_string 145 | 146 | async def generate_report_from_exa_results(topic, list_of_query_exa_pairs): 147 | print(f"Generating report from Exa results for topic: {topic}") 148 | formatted_exa_content = format_exa_results_for_llm(list_of_query_exa_pairs) 149 | content = (f"Write a comprehensive and professional three-paragraph research report about {topic} based on the provided information. " 150 | f"Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section " 151 | f"that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. " 152 | f"Reset the citations index and ignore the order of citations in the provided information. Here is the information: {formatted_exa_content}.") 153 | try: 154 | completion = await acompletion( 155 | model='gpt-4o', 156 | messages=[{"role": "user", "content": content}], 157 | stream=False 158 | ) 159 | report = completion['choices'][0]['message']['content'] 160 | except Exception as e: 161 | report = f"Failed to generate report: {str(e)}" 162 | return report 163 | 164 | def initialize_exa_client(): 165 | from exa_py import Exa 166 | try: 167 | exa_client = Exa(api_key=settings.exa_api_key) 168 | return exa_client 169 | except Exception as e: 170 | print(f"⚠️ Failed to initialize Exa client: {str(e)}") 171 | return None 172 | 173 | # For synchronous version if needed 174 | def generate_subqueries_from_topic_sync(topic, num_subqueries=6): 175 | print(f"🌿 Generating subqueries from topic: {topic}") 176 | content = f"I'm going to give you a topic I want to research. I want you to generate {num_subqueries} interesting, diverse search queries that would be useful for generating a report on my main topic. Here is the main topic: {topic}." 177 | try: 178 | completion = openai.ChatCompletion.create( 179 | model='gpt-4o', 180 | messages=[{"role": "user", "content": content}] 181 | ) 182 | response_content = completion['choices'][0]['message']['content'] 183 | print(f"Raw response content: {response_content}") # Print raw response for debugging 184 | 185 | # Split the response into individual lines and strip leading/trailing spaces and numbers 186 | subqueries = [] 187 | for line in response_content.split('\n'): 188 | if line.strip(): 189 | # Attempt to split by ". " and take the second part 190 | try: 191 | subquery = line.strip(' "').split('. ', 1)[1] 192 | subqueries.append(subquery) 193 | except IndexError: 194 | print(f"Skipping improperly formatted line: {line}") 195 | 196 | # Output the parsed subqueries for debugging 197 | print(f"Parsed subqueries: {subqueries}") 198 | 199 | except Exception as e: 200 | print(f"Unexpected error: {str(e)}") # Output unexpected error details 201 | subqueries = [f"Failed to generate subqueries: {str(e)}"] 202 | return subqueries 203 | 204 | def generate_report_sync(topic): 205 | print(f"Starting report generation for topic: {topic}") 206 | subqueries = generate_subqueries_from_topic_sync(topic) 207 | list_of_query_exa_pairs = exa_search_each_subquery(subqueries) 208 | report = generate_report_from_exa_results_sync(topic, list_of_query_exa_pairs) 209 | return report 210 | 211 | def generate_report_from_exa_results_sync(topic, list_of_query_exa_pairs): 212 | print(f"Generating report from Exa results for topic: {topic}") 213 | formatted_exa_content = format_exa_results_for_llm(list_of_query_exa_pairs) 214 | content = (f"Write a comprehensive and professional three-paragraph research report about {topic} based on the provided information. " 215 | f"Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section " 216 | f"that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. " 217 | f"Reset the citations index and ignore the order of citations in the provided information. Here is the information: {formatted_exa_content}.") 218 | completion = openai.ChatCompletion.create( 219 | model='gpt-4o', 220 | messages=[{"role": "user", "content": content}] 221 | ) 222 | report = completion.choices[0].message.content 223 | return report 224 | -------------------------------------------------------------------------------- /project-root/app/services/report_generator.py: -------------------------------------------------------------------------------- 1 | import os 2 | import json 3 | import time 4 | import asyncio 5 | import aiohttp 6 | 7 | from datetime import datetime, timedelta 8 | from typing import Optional, List, Dict 9 | from litellm import acompletion 10 | from app.core.config import settings 11 | from app.utils.exa_search import search_exa 12 | 13 | # Set up API keys using environment variables 14 | os.environ["OPENAI_API_KEY"] = os.getenv('OPENAI_API_KEY', settings.openai_api_key) 15 | 16 | async def generate_report(topic: str) -> str: 17 | print(f"Starting report generation for topic: {topic}") 18 | subqueries = await generate_subqueries_from_topic(topic) 19 | list_of_query_exa_pairs = await exa_search_each_subquery(subqueries) 20 | report = await generate_report_from_exa_results(topic, list_of_query_exa_pairs) 21 | return report 22 | 23 | async def generate_report_without_exa(topic): 24 | print(f"🚀 Generating report without Exa for topic: {topic}") 25 | content = f"Write a comprehensive and professional three-paragraph research report about {topic}. Include citations with source, month, and year." 26 | try: 27 | completion = await acompletion( 28 | model='gpt-4o', 29 | messages=[{"role": "user", "content": content}], 30 | stream=False, 31 | timeout=600 32 | ) 33 | report = completion['choices'][0]['message']['content'] 34 | except Exception as e: 35 | report = f"Failed to generate report: {str(e)}" 36 | return report 37 | 38 | async def generate_subqueries_from_topic(topic, num_subqueries=10): 39 | print(f"🌿 Generating subqueries from topic: {topic}") 40 | content = f"I'm going to give you a topic I want to research. I want you to generate {num_subqueries} interesting, diverse search queries that would be useful for generating a report on my main topic. Here is the main topic: {topic}." 41 | try: 42 | completion = await acompletion( 43 | model='gpt-4o', 44 | messages=[{"role": "user", "content": content}], 45 | stream=False, 46 | timeout=600 47 | ) 48 | response_content = completion['choices'][0]['message']['content'] 49 | print(f"Raw response content: {response_content}") # Print raw response for debugging 50 | 51 | # Split the response into individual lines and strip leading/trailing spaces and numbers 52 | subqueries = [] 53 | for line in response_content.split('\n'): 54 | line = line.strip() 55 | if line and line[0].isdigit(): 56 | # Attempt to split by ". " and take the second part 57 | try: 58 | subquery = line.split('. ', 1)[1].strip(' "') 59 | subqueries.append(subquery) 60 | except IndexError: 61 | print(f"Skipping improperly formatted line: {line}") 62 | 63 | # Output the parsed subqueries for debugging 64 | print(f"Parsed subqueries: {subqueries}") 65 | 66 | except Exception as e: 67 | print(f"Unexpected error: {str(e)}") # Output unexpected error details 68 | subqueries = [f"Failed to generate subqueries: {str(e)}"] 69 | return subqueries 70 | 71 | async def exa_search_each_subquery(subqueries): 72 | print(f"⌛ Searching each subquery") 73 | list_of_query_exa_pairs = [] 74 | 75 | async def perform_search(query, retries=5, delay=2): 76 | """Helper function to perform search with retries and exponential backoff.""" 77 | for attempt in range(retries): 78 | try: 79 | # Perform the search using Exa API 80 | search_response = search_exa( 81 | subqueries=[query], 82 | api_key=settings.exa_api_key 83 | ) 84 | if search_response and 'results' in search_response[0]: 85 | return search_response[0] 86 | else: 87 | print(f"⚠️ No data found for subquery: {query}") 88 | return None 89 | except Exception as e: 90 | print(f"⚠️ Attempt {attempt + 1} failed for query '{query}': {str(e)}") 91 | if "502" in str(e) and attempt < retries - 1: 92 | backoff = delay * (2 ** attempt) # Exponential backoff 93 | print(f"Retrying in {backoff} seconds...") 94 | await asyncio.sleep(backoff) 95 | else: 96 | print(f"⚠️ All {retries} attempts failed for query '{query}'") 97 | return None 98 | 99 | tasks = [] 100 | for query in subqueries: 101 | if "Failed to generate subqueries" in query: 102 | print(f"⚠️ Skipping invalid subquery: {query}") 103 | continue 104 | 105 | print(f"🔍 Searching for subquery: {query}") 106 | tasks.append(perform_search(query)) 107 | 108 | results = await asyncio.gather(*tasks) 109 | 110 | for query, search_response in zip(subqueries, results): 111 | if search_response and 'results' in search_response: 112 | print(f"✅ Search successful for subquery: {query}") 113 | list_of_query_exa_pairs.append({ 114 | 'subquery': query, 115 | 'results': search_response['results'] 116 | }) 117 | 118 | print(f"🏁 Completed search for all subqueries") 119 | return list_of_query_exa_pairs 120 | 121 | def format_exa_results_for_llm(list_of_query_exa_pairs): 122 | print(f"⌨️ Formatting Exa results for LLM") 123 | formatted_string = "" 124 | for pair in list_of_query_exa_pairs: 125 | formatted_string += f"[{pair['subquery']}]:\n" 126 | for result in pair['results']: 127 | content = getattr(result, 'text', "No text available") 128 | publish_date = getattr(result, 'published_date', "No date available") 129 | url = getattr(result, 'url', "No URL available") 130 | formatted_string += f"URL: {url}\nContent: {content}\nPublish Date: {publish_date}\n" 131 | formatted_string += "\n" 132 | return formatted_string 133 | 134 | async def generate_report_from_exa_results(topic, list_of_query_exa_pairs): 135 | print(f"Generating report from Exa results for topic: {topic}") 136 | formatted_exa_content = format_exa_results_for_llm(list_of_query_exa_pairs) 137 | content = (f"Write a comprehensive and professional three-paragraph research report about {topic} based on the provided information. " 138 | f"Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section " 139 | f"that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. " 140 | f"Reset the citations index and ignore the order of citations in the provided information. Here is the information: {formatted_exa_content}.") 141 | try: 142 | completion = await acompletion( 143 | model='gpt-4o', 144 | messages=[{"role": "user", "content": content}], 145 | stream=False, 146 | timeout=600 147 | ) 148 | report = completion['choices'][0]['message']['content'] 149 | except Exception as e: 150 | report = f"Failed to generate report: {str(e)}" 151 | return report 152 | 153 | async def generate_advanced_subqueries_from_topic(topic, subqueries_prompt, num_subqueries=20): 154 | print(f"🌿 Generating {num_subqueries} advanced subqueries from topic: {topic}") 155 | content = f"{subqueries_prompt} Here is the main topic: {topic}. Generate {num_subqueries} subqueries." 156 | try: 157 | completion = await acompletion( 158 | model='gpt-4o', 159 | messages=[{"role": "user", "content": content}], 160 | stream=False, 161 | timeout=600 162 | ) 163 | response_content = completion['choices'][0]['message']['content'] 164 | print(f"Raw response content: {response_content}") # Print raw response for debugging 165 | 166 | # Split the response into individual lines and strip leading/trailing spaces and numbers 167 | subqueries = [] 168 | for line in response_content.split('\n'): 169 | line = line.strip() 170 | if line and line[0].isdigit(): 171 | # Attempt to split by ". " and take the second part 172 | try: 173 | subquery = line.split('. ', 1)[1].strip(' "') 174 | subqueries.append(subquery) 175 | except IndexError: 176 | print(f"Skipping improperly formatted line: {line}") 177 | 178 | # Output the parsed subqueries for debugging 179 | print(f"Parsed subqueries: {subqueries}") 180 | 181 | except Exception as e: 182 | print(f"Unexpected error: {str(e)}") # Output unexpected error details 183 | subqueries = [f"Failed to generate subqueries: {str(e)}"] 184 | return subqueries 185 | 186 | 187 | async def generate_advanced_report_from_exa_results(topic, report_prompt, list_of_query_exa_pairs): 188 | print(f"Generating advanced report from Exa results for topic: {topic}") 189 | formatted_exa_content = format_exa_results_for_llm(list_of_query_exa_pairs) 190 | content = ( 191 | f"{report_prompt} " 192 | f"Here is the information: {formatted_exa_content}." 193 | ) 194 | 195 | try: 196 | completion = await acompletion( 197 | model='gpt-4o', 198 | messages=[{"role": "user", "content": content}], 199 | stream=False, 200 | timeout=6000 201 | ) 202 | report = completion['choices'][0]['message']['content'] 203 | except Exception as e: 204 | report = f"Failed to generate report: {str(e)}" 205 | return report 206 | 207 | async def generate_advanced_report( 208 | query: str, 209 | primary_prompt: str, 210 | subqueries_prompt: str, 211 | report_prompt: str, 212 | start_published_date: Optional[str], 213 | end_published_date: Optional[str], 214 | include_domains: Optional[List[str]], 215 | exclude_domains: Optional[List[str]], 216 | highlights: Optional[Dict], 217 | text: Optional[Dict], 218 | num_subqueries: int 219 | ): 220 | print(f"Generating advanced report for query: {query}") 221 | 222 | # Step 1: Generate Subqueries 223 | subqueries = await generate_advanced_subqueries_from_topic(query, subqueries_prompt, num_subqueries) 224 | 225 | # Step 2: Perform Exa Search for Each Subquery 226 | list_of_query_exa_pairs = await exa_search_each_subquery(subqueries) 227 | 228 | # Step 3: Generate Report from Exa Results using provided report_prompt 229 | report = await generate_advanced_report_from_exa_results(query, report_prompt, list_of_query_exa_pairs) 230 | 231 | return report 232 | 233 | 234 | def initialize_exa_client(): 235 | from exa_py import Exa 236 | try: 237 | exa_client = Exa(api_key=settings.exa_api_key) 238 | return exa_client 239 | except Exception as e: 240 | print(f"⚠️ Failed to initialize Exa client: {str(e)}") 241 | return None 242 | 243 | # For synchronous version if needed 244 | def generate_subqueries_from_topic_sync(topic, num_subqueries=10): 245 | print(f"🌿 Generating subqueries from topic: {topic}") 246 | content = f"I'm going to give you a topic I want to research. I want you to generate {num_subqueries} interesting, diverse search queries that would be useful for generating a report on my main topic. Here is the main topic: {topic}." 247 | try: 248 | completion = openai.ChatCompletion.create( 249 | model='gpt-4o', 250 | messages=[{"role": "user", "content": content}] 251 | ) 252 | response_content = completion['choices'][0]['message']['content'] 253 | print(f"Raw response content: {response_content}") # Print raw response for debugging 254 | 255 | # Split the response into individual lines and strip leading/trailing spaces and numbers 256 | subqueries = [] 257 | for line in response_content.split('\n'): 258 | if line.strip(): 259 | # Attempt to split by ". " and take the second part 260 | try: 261 | subquery = line.strip(' "').split('. ', 1)[1] 262 | subqueries.append(subquery) 263 | except IndexError: 264 | print(f"Skipping improperly formatted line: {line}") 265 | 266 | # Output the parsed subqueries for debugging 267 | print(f"Parsed subqueries: {subqueries}") 268 | 269 | except Exception as e: 270 | print(f"Unexpected error: {str(e)}") # Output unexpected error details 271 | subqueries = [f"Failed to generate subqueries: {str(e)}"] 272 | return subqueries 273 | 274 | def generate_report_sync(topic): 275 | print(f"Starting report generation for topic: {topic}") 276 | subqueries = generate_subqueries_from_topic_sync(topic) 277 | list_of_query_exa_pairs = exa_search_each_subquery(subqueries) 278 | report = generate_report_from_exa_results_sync(topic, list_of_query_exa_pairs) 279 | return report 280 | 281 | def generate_report_from_exa_results_sync(topic, list_of_query_exa_pairs): 282 | print(f"Generating report from Exa results for topic: {topic}") 283 | formatted_exa_content = format_exa_results_for_llm(list_of_query_exa_pairs) 284 | formatted_exa_content = format_exa_results_for_llm(list_of_query_exa_pairs) 285 | content = (f"Write a comprehensive and professional three-paragraph research report about {topic} based on the provided information. " 286 | f"Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section " 287 | f"that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. " 288 | f"Reset the citations index and ignore the order of citations in the provided information. Here is the information: {formatted_exa_content}." 289 | f"only use data, never reference anything not directly provided.") 290 | completion = openai.ChatCompletion.create( 291 | model='gpt-4o', 292 | messages=[{"role": "user", "content": content}] 293 | ) 294 | report = completion.choices[0].message.content 295 | return report 296 | 297 | # Ensure to call the function within an async context 298 | # Example usage: 299 | # asyncio.run(generate_report("latest ai jobs in Toronto")) -------------------------------------------------------------------------------- /project-root/app/utils/__init__.py: -------------------------------------------------------------------------------- 1 | # This file initializes the utils sub-package 2 | -------------------------------------------------------------------------------- /project-root/app/utils/exa_search.py: -------------------------------------------------------------------------------- 1 | # project-root/app/utils/exa_search.py 2 | 3 | from exa_py import Exa 4 | from app.core.config import settings 5 | from datetime import datetime, timedelta 6 | from typing import Optional, List, Dict 7 | 8 | # Initialize the Exa client 9 | exa = Exa(api_key=settings.exa_api_key) 10 | 11 | def search_exa(subqueries: list, api_key: str, start_published_date: Optional[str] = None, end_published_date: Optional[str] = None, include_domains: Optional[List[str]] = None, exclude_domains: Optional[List[str]] = None, highlights: Optional[Dict] = None, text: Optional[Dict] = None) -> list: 12 | """ 13 | Searches Exa service for each subquery using the provided API key with advanced search options. 14 | 15 | Args: 16 | subqueries (list): List of subqueries to search. 17 | api_key (str): API key for the Exa service. 18 | start_published_date (Optional[str]): Start date for published date filter. 19 | end_published_date (Optional[str]): End date for published date filter. 20 | include_domains (Optional[List[str]]): Domains to include in the search. 21 | exclude_domains (Optional[List[str]]): Domains to exclude from the search. 22 | highlights (Optional[Dict]): Configuration for highlights. 23 | text (Optional[Dict]): Configuration for text content retrieval. 24 | 25 | Returns: 26 | list: List of search results. 27 | 28 | Examples: 29 | - Basic search: search_exa(["query"], "api_key") 30 | - Search with date filters: search_exa(["query"], "api_key", start_published_date="2020-01-01", end_published_date="2020-01-31") 31 | - Search with domain filters: search_exa(["query"], "api_key", include_domains=["example.com"], exclude_domains=["example.net"]) 32 | - Search with highlights and text content options: search_exa(["query"], "api_key", highlights={"num_sentences": 5}, text={"include_html_tags": True}) 33 | """ 34 | results = [] 35 | one_week_ago = (datetime.now() - timedelta(days=7)).strftime("%Y-%m-%d") 36 | 37 | for subquery in subqueries: 38 | try: 39 | search_response = exa.search_and_contents( 40 | query=subquery, 41 | num_results=5, 42 | use_autoprompt=True, 43 | start_published_date=start_published_date or one_week_ago, 44 | end_published_date=end_published_date, 45 | include_domains=include_domains, 46 | exclude_domains=exclude_domains, 47 | highlights=highlights, 48 | text=text 49 | ) 50 | results.append({'subquery': subquery, 'results': search_response.results}) 51 | except Exception as e: 52 | print(f"Error making API call to Exa for '{subquery}': {e}") 53 | results.append({'subquery': subquery, 'error': str(e)}) 54 | # print results for debugging subqueries 55 | # print(f"Search results for subqueries: {results}") 56 | return results 57 | 58 | def advanced_search_exa(query: str, start_published_date: Optional[str] = None, end_published_date: Optional[str] = None, include_domains: Optional[List[str]] = None, exclude_domains: Optional[List[str]] = None, highlights: Optional[Dict] = None, text: Optional[Dict] = None) -> dict: 59 | """ 60 | Performs an advanced search on Exa with customizable options. 61 | 62 | Args: 63 | query (str): The search query. 64 | start_published_date (Optional[str]): Start date for published date filter. 65 | end_published_date (Optional[str]): End date for published date filter. 66 | include_domains (Optional[List[str]]): Domains to include in the search. 67 | exclude_domains (Optional[List[str]]): Domains to exclude from the search. 68 | highlights (Optional[Dict]): Configuration for highlights. 69 | text (Optional[Dict]): Configuration for text content retrieval. 70 | 71 | Returns: 72 | dict: The search results including any specified content retrieval options. 73 | 74 | Examples: 75 | - Advanced search with all options: advanced_search_exa("query", start_published_date="2020-01-01", end_published_date="2020-01-31", include_domains=["example.com"], exclude_domains=["example.net"], highlights={"num_sentences": 5}, text={"include_html_tags": True}) 76 | """ 77 | try: 78 | search_response = exa.search_and_contents( 79 | query=query, 80 | num_results=5, 81 | use_autoprompt=True, 82 | start_published_date=start_published_date, 83 | end_published_date=end_published_date, 84 | include_domains=include_domains, 85 | exclude_domains=exclude_domains, 86 | highlights=highlights, 87 | text=text 88 | ) 89 | return {'query': query, 'results': search_response.results} 90 | except Exception as e: 91 | print(f"Error making API call to Exa for '{query}': {e}") 92 | return {'query': query, 'error': str(e)} 93 | 94 | 95 | def find_similar_exa(url: str, num_results: Optional[int] = 10, include_domains: Optional[List[str]] = None, exclude_domains: Optional[List[str]] = None, start_crawl_date: Optional[str] = None, end_crawl_date: Optional[str] = None, start_published_date: Optional[str] = None, end_published_date: Optional[str] = None, category: Optional[str] = None) -> dict: 96 | """ 97 | Finds similar links to the provided URL using the Exa service with customizable options. 98 | 99 | Args: 100 | url (str): The URL to find similar links for. 101 | num_results (Optional[int]): Number of search results to return. 102 | include_domains (Optional[List[str]]): Domains to include in the search. 103 | exclude_domains (Optional[List[str]]): Domains to exclude from the search. 104 | start_crawl_date (Optional[str]): Start date for crawl date filter. 105 | end_crawl_date (Optional[str]): End date for crawl date filter. 106 | start_published_date (Optional[str]): Start date for published date filter. 107 | end_published_date (Optional[str]): End date for published date filter. 108 | category (Optional[str]): Category to focus on. 109 | contents (Optional[Dict]): Configuration for contents retrieval. 110 | 111 | Returns: 112 | dict: The search results including any specified content retrieval options. 113 | """ 114 | try: 115 | search_response = exa.find_similar( 116 | url=url, 117 | num_results=num_results, 118 | include_domains=include_domains, 119 | exclude_domains=exclude_domains, 120 | start_crawl_date=start_crawl_date, 121 | end_crawl_date=end_crawl_date, 122 | start_published_date=start_published_date, 123 | end_published_date=end_published_date, 124 | category=category 125 | ) 126 | return {'url': url, 'results': search_response.results} 127 | except Exception as e: 128 | print(f"Error making API call to Exa for '{url}': {e}") 129 | return {'url': url, 'error': str(e)} -------------------------------------------------------------------------------- /project-root/install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Ensure script is run as root 4 | if [ "$(id -u)" != "0" ]; then 5 | echo "This script must be run as root" 1>&2 6 | exit 1 7 | fi 8 | 9 | # Update and upgrade the system 10 | echo "Updating and upgrading the system..." 11 | apt-get update && apt-get upgrade -y 12 | 13 | # Install Python3 and pip 14 | echo "Installing Python3 and pip..." 15 | apt-get install python3 python3-pip -y 16 | 17 | # Install Docker 18 | echo "Installing Docker..." 19 | apt-get install docker.io -y 20 | 21 | # Install Docker Compose 22 | echo "Installing Docker Compose..." 23 | curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose 24 | chmod +x /usr/local/bin/docker-compose 25 | 26 | # Clone the repository 27 | echo "Cloning the repository..." 28 | git clone project-root 29 | cd project-root 30 | 31 | # Install project dependencies 32 | echo "Installing project dependencies..." 33 | pip3 install -r requirements.txt 34 | 35 | # Build and run the Docker container 36 | echo "Building and running the Docker container..." 37 | docker-compose up --build -d 38 | 39 | echo "Installation complete. The application is now running." 40 | -------------------------------------------------------------------------------- /project-root/start.py: -------------------------------------------------------------------------------- 1 | import os 2 | import subprocess 3 | import sys 4 | import importlib.util 5 | 6 | def check_and_install_python3_10(): 7 | try: 8 | # Check if Python 3.10 is installed 9 | subprocess.run(['python3.10', '--version'], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 10 | except (subprocess.CalledProcessError, FileNotFoundError): 11 | # Python 3.10 is not installed, attempt to install it 12 | print("❗ Python 3.10 is not installed. Attempting to install Python 3.10...") 13 | try: 14 | if sys.platform.startswith('linux'): 15 | subprocess.run(['sudo', 'apt-get', 'update'], check=True) 16 | subprocess.run(['sudo', 'apt-get', 'install', '-y', 'python3.10', 'python3.10-venv', 'python3.10-dev'], check=True) 17 | print("✅ Python 3.10 installed successfully.") 18 | else: 19 | print("❗ Automatic installation is not supported on this OS. Please install Python 3.10 manually.") 20 | sys.exit(1) 21 | except Exception as e: 22 | print(f"❗ Failed to install Python 3.10: {e}") 23 | sys.exit(1) 24 | 25 | def check_and_install_requirements(python_executable): 26 | print("📦 Installing required packages...") 27 | required_packages = ['fastapi', 'pydantic', 'pandas', 'exa_py', 'pytest', 'uvicorn', 'pydantic-settings', 'litellm'] 28 | subprocess.run([python_executable, '-m', 'pip', 'install'] + required_packages, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 29 | print("✅ Packages installed.") 30 | 31 | def prompt_for_api_key(env_var_name): 32 | api_key = input(f"🔑 Enter your {env_var_name}: ") 33 | os.environ[env_var_name] = api_key 34 | subprocess.run(['export', f'{env_var_name}={api_key}'], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) 35 | print(f"✅ {env_var_name} set.") 36 | 37 | def main(): 38 | print(r""" 39 | _____ _ _ _____ _ 40 | | _ |___ ___ ___| |_|_|___ ___| __ |___ ___ ___ ___| |_ ___ 41 | | | . | -_| | _| | _|___| -| -_| . | . | _| _|_ -| 42 | |__|__|_ |___|_|_|_| |_|___| |__|__|___| _|___|_| |_| |___| 43 | |___| |_| 44 | 45 | A Comprehensive Python Library for Generating Research Reports 46 | """) 47 | 48 | print("🚀 Starting the Agentic Reports application setup and server...") 49 | 50 | # Check for Python 3.10 installation 51 | check_and_install_python3_10() 52 | 53 | # Check for OPENAI_API_KEY 54 | if not os.getenv('OPENAI_API_KEY'): 55 | prompt_for_api_key('OPENAI_API_KEY') 56 | 57 | # Check for EXA_API_KEY 58 | if not os.getenv('EXA_API_KEY'): 59 | prompt_for_api_key('EXA_API_KEY') 60 | 61 | # Get the directory of the current script 62 | script_dir = os.path.dirname(os.path.abspath(__file__)) 63 | print(f"📂 Script directory: {script_dir}") 64 | 65 | # Set PYTHONPATH to include the project root directory 66 | os.environ['PYTHONPATH'] = os.environ.get('PYTHONPATH', '') + f':{script_dir}' 67 | print("✅ PYTHONPATH set.") 68 | 69 | # Ensure the correct working directory 70 | os.chdir(script_dir) 71 | print("✅ Working directory set.") 72 | 73 | # Create a virtual environment using Python 3.10 if it doesn't exist 74 | venv_dir = os.path.join(script_dir, 'venv') 75 | if not os.path.exists(venv_dir): 76 | print("🐍 Creating virtual environment with Python 3.10...") 77 | subprocess.run(['python3.10', '-m', 'venv', 'venv'], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 78 | print("✅ Virtual environment created.") 79 | 80 | # Use the Python executable from the virtual environment 81 | venv_python = os.path.join(venv_dir, 'bin', 'python') 82 | if sys.platform == 'win32': 83 | venv_python = os.path.join(venv_dir, 'Scripts', 'python') 84 | print(f"🐍 Using Python executable: {venv_python}") 85 | 86 | # Check and install required packages 87 | check_and_install_requirements(venv_python) 88 | 89 | # Run Uvicorn with the necessary parameters and show its output 90 | print("🌐 Starting Uvicorn server...") 91 | subprocess.run([venv_python, '-m', 'uvicorn', 'app.main:app', '--reload', '--host', '0.0.0.0', '--port', '8000', '--reload-dir', script_dir]) 92 | print("🚀 Uvicorn server started.") 93 | 94 | if __name__ == "__main__": 95 | main() 96 | -------------------------------------------------------------------------------- /project-root/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo " 4 | _____ _ _ _____ _ 5 | | _ |___ ___ ___| |_|_|___ ___| __ |___ ___ ___ ___| |_ ___ 6 | | | . | -_| | _| | _|___| -| -_| . | . | _| _|_ -| 7 | |__|__|_ |___|_|_|_| |_|___| |__|__|___| _|___|_| |_| |___| 8 | |___| |_| 9 | 10 | A Comprehensive Python Library for Generating Research Reports 11 | " 12 | 13 | # Starting message 14 | echo "Starting the Agentic Reports application setup and server..." 15 | 16 | # Check for OPENAI_API_KEY 17 | if [ -z "$OPENAI_API_KEY" ]; then 18 | read -p "Enter your OPENAI_API_KEY: " openai_key 19 | export OPENAI_API_KEY=$openai_key 20 | fi 21 | 22 | # Check for EXA_API_KEY 23 | if [ -z "$EXA_API_KEY" ]; then 24 | read -p "Enter your EXA_API_KEY: " exa_key 25 | export EXA_API_KEY=$exa_key 26 | fi 27 | 28 | # Set PYTHONPATH to include the project root directory 29 | export PYTHONPATH=$PYTHONPATH:/workspaces/agentic-reports/project-root 30 | 31 | # Ensure the correct working directory 32 | cd /workspaces/agentic-reports/project-root 33 | 34 | # Create and activate a virtual environment 35 | python3 -m venv venv 36 | source venv/bin/activate 37 | 38 | # Install required dependencies quietly 39 | pip install -r ../requirements.txt -q 40 | 41 | # Run Uvicorn with the necessary parameters 42 | uvicorn app.main:app --reload --host 0.0.0.0 --port 8000 --reload-dir /workspaces/agentic-reports/project-root 43 | -------------------------------------------------------------------------------- /project-root/tests/__init__.py: -------------------------------------------------------------------------------- 1 | # This file initializes the tests package 2 | -------------------------------------------------------------------------------- /project-root/tests/test_main.py: -------------------------------------------------------------------------------- 1 | from fastapi.testclient import TestClient 2 | from app.main import app 3 | 4 | client = TestClient(app) 5 | 6 | def test_generate_report(): 7 | response = client.post("/generate-report", json={"topic": "AI"}) 8 | assert response.status_code == 200 9 | assert "report" in response.json() 10 | -------------------------------------------------------------------------------- /project-root/tests/test_report_generator.py: -------------------------------------------------------------------------------- 1 | from app.services.report_generator import generate_report 2 | 3 | def test_generate_report(): 4 | topic = "AI" 5 | report = generate_report(topic) 6 | assert isinstance(report, str) 7 | -------------------------------------------------------------------------------- /reports-example.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "view-in-github" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": { 16 | "id": "z8fsb44MMHfF" 17 | }, 18 | "source": [ 19 | "# 📄 Agentic Report Generator\n", 20 | "\n", 21 | "Welcome to the Agentic Report Generator! This tool leverages the power of OpenAI's GPT-4 and Exa's search capabilities to generate comprehensive research reports on any topic you choose.\n", 22 | "\n", 23 | "## How It Works\n", 24 | "1. **Generate Subqueries**: The tool will create several subqueries based on your main topic to ensure a thorough investigation.\n", 25 | "2. **Search with Exa**: Each subquery is searched using Exa to gather the most relevant and recent information.\n", 26 | "3. **Format Results**: The search results are formatted for easy consumption by the language model.\n", 27 | "4. **Generate Report**: A professional, three-paragraph research report is generated, complete with citations.\n", 28 | "\n", 29 | "## Getting Started\n", 30 | "1. **Add Your API Keys**:\n", 31 | " - **Exa API Key**: Obtain your API key from [Exa.ai](https://exa.ai) and store it securely in Google Colab's Secrets.\n", 32 | " - **OpenAI API Key**: Obtain your API key from [OpenAI](https://openai.com) and store it securely in Google Colab's Secrets.\n", 33 | "2. **Run the Cells**: Execute the cells in the notebook to start generating your report.\n", 34 | "\n", 35 | "Let's get started!" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "metadata": { 42 | "cellView": "form", 43 | "id": "-LKUeN72Imim" 44 | }, 45 | "outputs": [], 46 | "source": [ 47 | "# @title Install Requirements\n", 48 | "!pip install openai exa_py" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": { 55 | "colab": { 56 | "base_uri": "https://localhost:8080/" 57 | }, 58 | "id": "_49IDO56IV6M", 59 | "outputId": "5f2e861f-7909-48e2-a897-e3ec0f0d445f" 60 | }, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "Welcome to the Research Report Generator!\n", 67 | "Please select an option:\n", 68 | "1. Generate a report\n", 69 | "2. Exit\n", 70 | "Enter your choice: 1\n", 71 | "Please enter the topic you want to research: latest stories\n", 72 | "Starting report generation for topic: latest stories\n", 73 | " \n", 74 | "🌿 Generating subqueries from topic: latest stories\n", 75 | "\n", 76 | "⌛ Searching each subquery\n", 77 | "Generating report from Exa results for topic: latest stories\n", 78 | "\n", 79 | "⌨️ Formatting Exa results for LLM\n", 80 | "📃 Final Report: ## Research Report\n", 81 | "\n", 82 | "In recent developments in Alabama's parole system, there has been a notable decision involving Kenneth Wayne McCroskey, who was initially imprisoned for a third-degree robbery in 1996. McCroskey, despite serving a life sentence for the violation of parole terms related to his previous offenses, was paroled on Wednesday after the state revisited his case [1]. Alabama's parole system has faced criticism for being overly stringent, with McCroskey's case shedding light on its severity. The present parole board's increased rate of approvals might suggest a potential shift in the system, indicating a move towards more leniency and mercy in certain cases.\n", 83 | "\n", 84 | "The tech industry recently observed a significant ethical lapse regarding AI in journalism. Hoodline, a local news site, has supplemented its reporting with AI-generated content, posing significant questions about transparency and credibility in journalism. While AI can assist in handling large data sets and facilitate research, Hoodline faced serious backlash for using fake biographies and AI-generated headshots to give an illusion of human-authored content. This controversial move underscores the media's struggle with ethical AI usage, particularly as it risks public trust and exacerbates misinformation [2]. Despite employing multiple editors to oversee the AI's contributions, the integrity of AI-generated content remains a contentious issue.\n", 85 | "\n", 86 | "A landmark incident in American history occurred when former President Donald J. Trump was convicted of multiple felony counts related to falsifying business records. This conviction arises from his efforts to cover up a scandal that potentially threatened his 2016 presidential campaign. Trump's case is set against the backdrop of an electorate that remains deeply divided; however, this conviction could significantly impact his political future and the wider politico-legal landscape in the United States [3]. The impending sentence, which could range from probation to a potential four-year prison term, exemplifies the judiciary's potent role in upholding legal accountability for even the country's highest officeholders.\n", 87 | "\n", 88 | "## References\n", 89 | "[1] , May 2024.\n", 90 | "[2] , May 2024.\n", 91 | "[3] , May 2024.\n", 92 | "Would you like to ask a follow-up question? (yes/no): yes\n", 93 | "Please enter your follow-up question: test\n", 94 | "Follow-up question: test\n", 95 | "Starting report generation for topic: test\n", 96 | " \n", 97 | "🌿 Generating subqueries from topic: test\n", 98 | "\n", 99 | "⌛ Searching each subquery\n", 100 | "Generating report from Exa results for topic: test\n", 101 | "\n", 102 | "⌨️ Formatting Exa results for LLM\n" 103 | ] 104 | } 105 | ], 106 | "source": [ 107 | "# @title 📄 Agentic Report Generator\n", 108 | "import os\n", 109 | "import json\n", 110 | "import openai\n", 111 | "from exa_py import Exa\n", 112 | "from datetime import datetime, timedelta\n", 113 | "from google.colab import userdata\n", 114 | "\n", 115 | "# Access secrets stored in Google Colab\n", 116 | "EXA_API_KEY = userdata.get('EXA_API_KEY')\n", 117 | "OPENAI_API_KEY = userdata.get('OPENAI_API_KEY')\n", 118 | "\n", 119 | "# Set up API keys using environment variables\n", 120 | "os.environ['EXA_API_KEY'] = EXA_API_KEY\n", 121 | "os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY\n", 122 | "\n", 123 | "# Set up API keys\n", 124 | "openai.api_key = OPENAI_API_KEY\n", 125 | "exa = Exa(api_key=EXA_API_KEY)\n", 126 | "\n", 127 | "def display_menu():\n", 128 | " print(\"Welcome to the Research Report Generator!\")\n", 129 | " print(\"Please select an option:\")\n", 130 | " print(\"1. Generate a report\")\n", 131 | " print(\"2. Exit\")\n", 132 | "\n", 133 | "def get_user_input(prompt):\n", 134 | " return input(prompt)\n", 135 | "\n", 136 | "def generate_report_without_exa(topic):\n", 137 | " print(f\"🚀 Generating report without Exa for topic: {topic}\")\n", 138 | " print(f\"\")\n", 139 | " content = f\"Write a comprehensive and professional three paragraph research report about {topic}. Include citations with source, month, and year.\"\n", 140 | " completion = openai.chat.completions.create(\n", 141 | " model='gpt-4o',\n", 142 | " messages=[{\"role\": \"user\", \"content\": content}],\n", 143 | " temperature=0\n", 144 | " )\n", 145 | " report = completion.choices[0].message.content\n", 146 | " return report\n", 147 | "\n", 148 | "def create_custom_function(num_subqueries):\n", 149 | " properties = {}\n", 150 | " for i in range(1, num_subqueries + 1):\n", 151 | " key = f'subquery_{i}'\n", 152 | " properties[key] = {\n", 153 | " 'type': 'string',\n", 154 | " 'description': 'Search queries that would be useful for generating a report on my main topic'\n", 155 | " }\n", 156 | "\n", 157 | " custom_function = {\n", 158 | " 'name': 'generate_exa_search_queries',\n", 159 | " 'description': 'Generates Exa search queries to investigate the main topic',\n", 160 | " 'parameters': {\n", 161 | " 'type': 'object',\n", 162 | " 'properties': properties\n", 163 | " }\n", 164 | " }\n", 165 | "\n", 166 | " return [custom_function]\n", 167 | "\n", 168 | "def generate_subqueries_from_topic(topic, num_subqueries=6):\n", 169 | " print(f\" \")\n", 170 | " print(f\"🌿 Generating subqueries from topic: {topic}\")\n", 171 | " content = f\"I'm going to give you a topic I want to research. I want you to generate {num_subqueries} interesting, diverse search queries that would be useful for generating a report on my main topic. Here is the main topic: {topic}.\"\n", 172 | " custom_functions = create_custom_function(num_subqueries)\n", 173 | " completion = openai.chat.completions.create(\n", 174 | " model='gpt-4o',\n", 175 | " messages=[{\"role\": \"user\", \"content\": content}],\n", 176 | " temperature=0,\n", 177 | " functions=custom_functions,\n", 178 | " function_call='auto'\n", 179 | " )\n", 180 | " json_response = json.loads(completion.choices[0].message.function_call.arguments)\n", 181 | " subqueries = list(json_response.values())\n", 182 | " return subqueries\n", 183 | "\n", 184 | "def exa_search_each_subquery(subqueries):\n", 185 | " print(f\"\")\n", 186 | " print(f\"⌛ Searching each subquery\")\n", 187 | " list_of_query_exa_pairs = []\n", 188 | " one_week_ago = (datetime.now() - timedelta(days=7)).strftime(\"%Y-%m-%d\")\n", 189 | " for query in subqueries:\n", 190 | " search_response = exa.search_and_contents(\n", 191 | " query,\n", 192 | " num_results=5,\n", 193 | " use_autoprompt=True,\n", 194 | " start_published_date=one_week_ago,\n", 195 | " text=True,\n", 196 | " highlights={\"num_sentences\": 5},\n", 197 | " )\n", 198 | " query_object = {\n", 199 | " 'subquery': query,\n", 200 | " 'results': search_response.results\n", 201 | " }\n", 202 | " list_of_query_exa_pairs.append(query_object)\n", 203 | " return list_of_query_exa_pairs\n", 204 | "\n", 205 | "def format_exa_results_for_llm(list_of_query_exa_pairs):\n", 206 | " print(f\"\")\n", 207 | " print(f\"⌨️ Formatting Exa results for LLM\")\n", 208 | " formatted_string = \"\"\n", 209 | " for i in list_of_query_exa_pairs:\n", 210 | " formatted_string += f\"[{i['subquery']}]:\\n\"\n", 211 | " for result in i['results']:\n", 212 | " content = result.text if result.text else \" \".join(result.highlights)\n", 213 | " publish_date = result.published_date\n", 214 | " formatted_string += f\"URL: {result.url}\\nContent: {content}\\nPublish Date: {publish_date}\\n\"\n", 215 | " formatted_string += \"\\n\"\n", 216 | " return formatted_string\n", 217 | "\n", 218 | "def generate_report_from_exa_results(topic, list_of_query_exa_pairs):\n", 219 | " print(f\"Generating report from Exa results for topic: {topic}\")\n", 220 | " formatted_exa_content = format_exa_results_for_llm(list_of_query_exa_pairs)\n", 221 | " content = (f\"Write a comprehensive and professional three paragraph research report about {topic} based on the provided information. \"\n", 222 | " f\"Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section \"\n", 223 | " f\"that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. \"\n", 224 | " f\"Reset the citations index and ignore the order of citations in the provided information. Here is the information: {formatted_exa_content}.\")\n", 225 | " completion = openai.chat.completions.create(\n", 226 | " model='gpt-4o',\n", 227 | " messages=[{\"role\": \"user\", \"content\": content}]\n", 228 | " )\n", 229 | " report = completion.choices[0].message.content\n", 230 | " return report\n", 231 | "\n", 232 | "def generate_report(topic):\n", 233 | " print(f\"Starting report generation for topic: {topic}\")\n", 234 | " subqueries = generate_subqueries_from_topic(topic)\n", 235 | " list_of_query_exa_pairs = exa_search_each_subquery(subqueries)\n", 236 | " report = generate_report_from_exa_results(topic, list_of_query_exa_pairs)\n", 237 | " return report\n", 238 | "\n", 239 | "def main():\n", 240 | " while True:\n", 241 | " display_menu()\n", 242 | " choice = get_user_input(\"Enter your choice: \")\n", 243 | " if choice == '1':\n", 244 | " example_topic = get_user_input(\"Please enter the topic you want to research: \")\n", 245 | " report = generate_report(example_topic)\n", 246 | " print(f\"📃 Final Report: {report}\")\n", 247 | " while True:\n", 248 | " follow_up = get_user_input(\"Would you like to ask a follow-up question? (yes/no): \")\n", 249 | " if follow_up.lower() == 'yes':\n", 250 | " follow_up_question = get_user_input(\"Please enter your follow-up question: \")\n", 251 | " print(f\"Follow-up question: {follow_up_question}\")\n", 252 | " report = generate_report(follow_up_question)\n", 253 | " print(f\"📃 Final Report: {report}\")\n", 254 | " else:\n", 255 | " print(\"Thank you for using the Research Report Generator!\")\n", 256 | " break\n", 257 | " elif choice == '2':\n", 258 | " print(\"Exiting the program. Goodbye!\")\n", 259 | " break\n", 260 | " else:\n", 261 | " print(\"Invalid choice. Please try again.\")\n", 262 | "\n", 263 | "\n", 264 | "\n", 265 | "if __name__ == \"__main__\":\n", 266 | " main()\n" 267 | ] 268 | } 269 | ], 270 | "metadata": { 271 | "colab": { 272 | "authorship_tag": "ABX9TyPJLj5Z57McuCqtT8ZPz9zC", 273 | "include_colab_link": true, 274 | "provenance": [] 275 | }, 276 | "kernelspec": { 277 | "display_name": "Python 3", 278 | "name": "python3" 279 | }, 280 | "language_info": { 281 | "name": "python" 282 | } 283 | }, 284 | "nbformat": 4, 285 | "nbformat_minor": 0 286 | } 287 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | fastapi 2 | pydantic 3 | pandas 4 | exa_py 5 | pytest 6 | uvicorn 7 | pydantic_settings 8 | litellm 9 | virtualenv -------------------------------------------------------------------------------- /sample-reports/README.md: -------------------------------------------------------------------------------- 1 | # Advanced Reporting Techniques in Agentic Reports 2 | 3 | This README provides an overview of advanced reporting techniques utilized in Agentic Reports, showcasing examples and JSON configurations for each method. These techniques enhance the depth, accuracy, and comprehensiveness of generated reports, tailored to specific research needs. 4 | 5 | ## Recursive Prompting 6 | 7 | Recursive prompting involves iterative querying to refine searches based on previous responses, ensuring accurate and relevant information. 8 | 9 | [Learn more about Recursive Prompting](recursive-prompting.md) 10 | 11 | ## Graph Structures 12 | 13 | Graph-based prompting uses graph structures to map relationships between information, identifying intricate connections for a holistic report. 14 | 15 | [Learn more about Graph Structures](graph-structures.md) 16 | 17 | ## Advanced Logic and Reasoning 18 | 19 | This technique employs critical thinking and evidence-based analysis to ensure logical, coherent content supported by data. 20 | 21 | [Learn more about Advanced Logic and Reasoning](advanced-logic-and-reasoning.md) 22 | 23 | ## Combining Approaches 24 | 25 | Agentic Reports often combines these techniques to maximize report quality, tackling complex topics with precision. 26 | 27 | [Learn more about Combining Approaches](combining-approaches.md) 28 | 29 | ## JSON Example for Advanced Report Generation 30 | 31 | ```json 32 | { 33 | "query": "The future of AI in smart cities", 34 | "primary_prompt": "Generate a detailed report on the future of AI in smart cities, focusing on applications such as traffic management, energy efficiency, and public safety.", 35 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on the future of AI in smart cities.", 36 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about the future of AI in smart cities based on the provided information.", 37 | "start_published_date": "2024-01-01", 38 | "end_published_date": "2024-06-03", 39 | "highlights": { 40 | "num_sentences": 5 41 | }, 42 | "text": { 43 | "include_html_tags": false 44 | }, 45 | "num_subqueries": 5 46 | } 47 | ``` 48 | 49 | For more detailed examples and JSON configurations, refer to the respective `.md` files linked above. 50 | -------------------------------------------------------------------------------- /sample-reports/advanced-logic-and-reasoning.md: -------------------------------------------------------------------------------- 1 | # Advanced Logic and Reasoning in Prompt Engineering 2 | 3 | Advanced Logic and Reasoning in prompt engineering involves the use of sophisticated logical and analytical techniques to enhance the depth and accuracy of the reports generated. This approach employs critical thinking, hypothesis testing, and evidence-based analysis to ensure that the content is logical, coherent, and well-supported by data. 4 | 5 | ## Example: Ethical Implications of AI 6 | 7 | For a report on "Ethical implications of AI," advanced reasoning is used to weigh different ethical considerations, analyze case studies, and provide balanced arguments. It critically examines various perspectives, ensuring a thorough and unbiased analysis. 8 | 9 | ### JSON Example for Advanced Logic and Reasoning 10 | 11 | ```json 12 | { 13 | "query": "Ethical implications of AI", 14 | "primary_prompt": "Generate a detailed report on the ethical implications of AI, including considerations such as privacy, bias, and autonomy. The report should critically examine various perspectives and provide balanced arguments.", 15 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on the ethical implications of AI. These subqueries should cover various aspects of the topic, including privacy, bias, and autonomy.", 16 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about the ethical implications of AI based on the provided information. Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. Reset the citations index and ignore the order of citations in the provided information.", 17 | "start_published_date": "2024-01-01", 18 | "end_published_date": "2024-06-03", 19 | "highlights": { 20 | "num_sentences": 5 21 | }, 22 | "text": { 23 | "include_html_tags": false 24 | }, 25 | "num_subqueries": 5 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /sample-reports/ai-in-agriculture.md: -------------------------------------------------------------------------------- 1 | # AI Applications in Agriculture 2 | 3 | The integration of Artificial Intelligence (AI) in agriculture is revolutionizing the sector by enhancing efficiency, productivity, and sustainability. AI technologies are being employed to optimize various agricultural processes, from crop monitoring and disease detection to precision farming and supply chain management. 4 | 5 | ## Example: Precision Farming with AI 6 | 7 | Precision farming is a prime example of how AI can be applied in agriculture. By utilizing data from satellites, drones, and ground sensors, AI algorithms can analyze soil health, crop health, and environmental factors to make informed decisions about planting, watering, and harvesting. 8 | 9 | ### JSON Example for AI in Agriculture 10 | 11 | ```json 12 | { 13 | "query": "AI applications in agriculture", 14 | "primary_prompt": "Generate a detailed report on the current advancements and applications of AI in agriculture. The report should include an analysis of precision farming, crop monitoring, disease detection, and supply chain optimization.", 15 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on AI applications in agriculture. These subqueries should cover various aspects of the topic, including precision farming, crop monitoring, disease detection, and supply chain optimization.", 16 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about AI applications in agriculture based on the provided information. Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. Reset the citations index and ignore the order of citations in the provided information.", 17 | "start_published_date": "2024-01-01", 18 | "end_published_date": "2024-06-03", 19 | "highlights": { 20 | "num_sentences": 5 21 | }, 22 | "text": { 23 | "include_html_tags": false 24 | }, 25 | "num_subqueries": 5 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /sample-reports/ai-in-cybersecurity.md: -------------------------------------------------------------------------------- 1 | # AI's Role in Enhancing Cybersecurity 2 | 3 | The integration of Artificial Intelligence (AI) in cybersecurity represents a significant advancement in the ongoing battle against cyber threats. AI technologies offer unparalleled capabilities in detecting, analyzing, and responding to potential security breaches with speed and efficiency far beyond human capabilities. 4 | 5 | ## AI-Driven Threat Detection 6 | 7 | AI algorithms can analyze vast amounts of data to identify patterns and anomalies that may indicate a cybersecurity threat. This proactive approach allows for the early detection of potential breaches, significantly reducing the risk of data loss or damage. 8 | 9 | ### JSON Example for AI-Driven Threat Detection 10 | 11 | ```json 12 | { 13 | "query": "AI in Cybersecurity", 14 | "primary_prompt": "Generate a detailed report on the role of AI in enhancing cybersecurity, focusing on AI-driven threat detection, predictive analytics, and automated response systems.", 15 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on AI's role in cybersecurity. These subqueries should cover various aspects of the topic, including threat detection, predictive analytics, and automated response systems.", 16 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about AI's role in enhancing cybersecurity based on the provided information. Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. Reset the citations index and ignore the order of citations in the provided information.", 17 | "start_published_date": "2024-01-01", 18 | "end_published_date": "2024-06-03", 19 | "highlights": { 20 | "num_sentences": 5 21 | }, 22 | "text": { 23 | "include_html_tags": false 24 | }, 25 | "num_subqueries": 5 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /sample-reports/ai-in-education.md: -------------------------------------------------------------------------------- 1 | # AI's Impact on Education 2 | 3 | The integration of Artificial Intelligence (AI) in the educational sector has revolutionized the way teaching and learning processes are conducted. AI technologies have introduced a range of innovative tools and methodologies that have significantly enhanced educational experiences, making learning more accessible, personalized, and efficient. 4 | 5 | ## Example: Personalized Learning Environments 6 | 7 | AI-driven systems can analyze individual student performance and learning habits to create personalized learning environments. These systems adapt teaching materials to suit each student's learning pace and style, ensuring that all students can achieve their full potential. 8 | 9 | ### JSON Example for AI in Education 10 | 11 | ```json 12 | { 13 | "query": "AI's impact on education", 14 | "primary_prompt": "Generate a detailed report on the current advancements and applications of AI in education. The report should include an analysis of AI-driven personalized learning environments, intelligent tutoring systems, and the use of AI for administrative tasks.", 15 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on AI's impact on education. These subqueries should cover various aspects of the topic, including personalized learning environments, intelligent tutoring systems, and AI in educational administration.", 16 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about AI's impact on education based on the provided information. Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. Reset the citations index and ignore the order of citations in the provided information.", 17 | "start_published_date": "2024-01-01", 18 | "end_published_date": "2024-06-03", 19 | "highlights": { 20 | "num_sentences": 5 21 | }, 22 | "text": { 23 | "include_html_tags": false 24 | }, 25 | "num_subqueries": 5 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /sample-reports/ai-in-environmental-conservation.md: -------------------------------------------------------------------------------- 1 | # AI in Environmental Conservation 2 | 3 | The integration of Artificial Intelligence (AI) in environmental conservation efforts represents a transformative approach to tackling some of the most pressing ecological challenges of our time. From monitoring deforestation and wildlife populations to predicting climate change impacts, AI technologies offer innovative solutions for preserving our planet's natural resources. 4 | 5 | ## Example: AI-Driven Wildlife Monitoring 6 | 7 | One of the critical applications of AI in environmental conservation is in wildlife monitoring. By leveraging AI-powered image recognition and data analysis, conservationists can track animal populations, monitor their health, and detect poaching activities more efficiently than ever before. 8 | 9 | ### JSON Example for AI-Driven Wildlife Monitoring 10 | 11 | ```json 12 | { 13 | "query": "AI in wildlife monitoring", 14 | "primary_prompt": "Generate a detailed report on the use of AI technologies in wildlife monitoring, including the latest advancements, applications, and outcomes. The report should cover how AI is used to track animal populations, monitor their health, and combat poaching.", 15 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on AI's role in wildlife monitoring. These subqueries should explore different aspects of the topic, such as image recognition technologies and data analysis methods.", 16 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about AI's role in wildlife monitoring based on the provided information. Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. Reset the citations index and ignore the order of citations in the provided information.", 17 | "start_published_date": "2024-01-01", 18 | "end_published_date": "2024-06-03", 19 | "highlights": { 20 | "num_sentences": 5 21 | }, 22 | "text": { 23 | "include_html_tags": false 24 | }, 25 | "num_subqueries": 5 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /sample-reports/ai-in-finance.md: -------------------------------------------------------------------------------- 1 | # AI in Financial Markets 2 | 3 | The integration of Artificial Intelligence (AI) in financial markets has revolutionized the way financial institutions operate, offering unprecedented opportunities for efficiency and innovation. This report explores the multifaceted role of AI in financial markets, highlighting its impact on market trends, regulatory impacts, and economic outcomes. 4 | 5 | ## AI Technologies in Financial Markets 6 | 7 | AI technologies, including machine learning algorithms and neural networks, have been instrumental in analyzing vast amounts of financial data. These technologies enable the prediction of market trends, risk assessment, and the automation of trading strategies, thereby enhancing market efficiency and reducing operational costs. 8 | 9 | ## Market Trends 10 | 11 | The adoption of AI in financial markets has led to the emergence of algorithmic trading, high-frequency trading, and robo-advisors. These AI-driven approaches have significantly increased the speed and volume of trading activities, contributing to greater liquidity and more dynamic markets. 12 | 13 | ## Regulatory Impacts 14 | 15 | Regulatory bodies have started to adapt to the rise of AI in financial markets. New regulations are being developed to address the challenges posed by AI, including issues related to transparency, accountability, and ethical considerations in automated decision-making processes. 16 | 17 | ## Economic Outcomes 18 | 19 | AI's impact on financial markets extends to economic outcomes, influencing investment patterns, capital allocation, and financial stability. By enabling more informed decision-making and efficient market operations, AI contributes to the overall health and growth of the economy. 20 | 21 | ### JSON Example for AI in Financial Markets Report Generation 22 | 23 | ```json 24 | { 25 | "query": "AI in financial markets", 26 | "primary_prompt": "Generate a detailed report on the current advancements, performance, and market trends of AI in financial markets. The report should include an analysis of AI technologies, market trends, regulatory impacts, and economic outcomes.", 27 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on AI in financial markets. These subqueries should cover various aspects of the topic, including AI technologies, market trends, regulatory impacts, and economic outcomes.", 28 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about AI in financial markets based on the provided information. Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. Reset the citations index and ignore the order of citations in the provided information.", 29 | "start_published_date": "2024-01-01", 30 | "end_published_date": "2024-06-03", 31 | "highlights": { 32 | "num_sentences": 5 33 | }, 34 | "text": { 35 | "include_html_tags": false 36 | }, 37 | "num_subqueries": 5 38 | } 39 | ``` 40 | -------------------------------------------------------------------------------- /sample-reports/ai-in-healthcare.md: -------------------------------------------------------------------------------- 1 | # AI's Role in Healthcare 2 | 3 | The integration of Artificial Intelligence (AI) in healthcare is revolutionizing the medical field, offering unprecedented opportunities for enhancing patient care, improving diagnostic accuracy, and streamlining operations. 4 | 5 | ## AI in Diagnostics 6 | 7 | AI algorithms are increasingly used in diagnostic processes, where they analyze medical images with high precision, often detecting conditions earlier and more accurately than human practitioners. 8 | 9 | ### JSON Example for AI in Diagnostics 10 | 11 | ```json 12 | { 13 | "query": "AI in Diagnostics", 14 | "primary_prompt": "Generate a detailed report on the use of AI in diagnostics, including its impact on detection accuracy, patient outcomes, and healthcare efficiency.", 15 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on AI's role in diagnostics. These subqueries should cover various aspects of the topic, including impact on detection accuracy, patient outcomes, and healthcare efficiency.", 16 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about AI's role in diagnostics based on the provided information. Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. Reset the citations index and ignore the order of citations in the provided information.", 17 | "start_published_date": "2024-01-01", 18 | "end_published_date": "2024-06-03", 19 | "highlights": { 20 | "num_sentences": 5 21 | }, 22 | "text": { 23 | "include_html_tags": false 24 | }, 25 | "num_subqueries": 5 26 | } 27 | ``` 28 | 29 | ## AI in Patient Care 30 | 31 | AI technologies are also transforming patient care, enabling personalized treatment plans and continuous monitoring through wearable devices and mobile apps. 32 | 33 | ### JSON Example for AI in Patient Care 34 | 35 | ```json 36 | { 37 | "query": "AI in Patient Care", 38 | "primary_prompt": "Explore the advancements of AI in patient care, focusing on personalized treatment plans, continuous monitoring, and the overall impact on patient satisfaction and recovery times.", 39 | "subqueries_prompt": "Identify key areas where AI is making a significant impact in patient care, including personalized treatment and continuous monitoring.", 40 | "report_prompt": "Provide an in-depth analysis of AI's contributions to patient care, supported by data and case studies that illustrate its effectiveness in improving patient outcomes.", 41 | "start_published_date": "2024-01-01", 42 | "end_published_date": "2024-06-03", 43 | "highlights": { 44 | "num_sentences": 5 45 | }, 46 | "text": { 47 | "include_html_tags": false 48 | }, 49 | "num_subqueries": 5 50 | } 51 | ``` 52 | -------------------------------------------------------------------------------- /sample-reports/ai-in-manufacturing.md: -------------------------------------------------------------------------------- 1 | # AI's Impact on Manufacturing 2 | 3 | The integration of Artificial Intelligence (AI) in manufacturing is revolutionizing the industry by enhancing efficiency, reducing operational costs, and improving product quality. This report delves into the various applications of AI in manufacturing, highlighting the transformative potential of these technologies. 4 | 5 | ## AI Applications in Manufacturing 6 | 7 | 1. **Predictive Maintenance**: AI algorithms analyze data from machinery to predict potential failures before they occur, minimizing downtime and maintenance costs. 8 | 2. **Quality Control**: Machine learning models are used to inspect and ensure product quality, identifying defects more accurately and swiftly than human inspectors. 9 | 3. **Supply Chain Optimization**: AI optimizes supply chain operations by forecasting demand, managing inventory, and identifying the most efficient delivery routes. 10 | 4. **Robotics and Automation**: AI-driven robots perform tasks with precision and flexibility, from assembly lines to intricate product design and development. 11 | 12 | ## Challenges and Future Prospects 13 | 14 | While AI offers significant benefits, challenges such as high initial investment costs, data privacy concerns, and the need for skilled workforce remain. However, continuous advancements in AI technologies and increasing adoption indicate a promising future for AI in manufacturing. 15 | 16 | ## JSON Example for AI in Manufacturing Report Generation 17 | 18 | ```json 19 | { 20 | "query": "AI in Manufacturing", 21 | "primary_prompt": "Generate a detailed report on the current advancements and applications of AI in manufacturing. The report should include an analysis of predictive maintenance, quality control, supply chain optimization, and robotics and automation.", 22 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on AI in manufacturing. These subqueries should cover various aspects of the topic, including predictive maintenance, quality control, supply chain optimization, and robotics and automation.", 23 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about AI in manufacturing based on the provided information. Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. Reset the citations index and ignore the order of citations in the provided information.", 24 | "start_published_date": "2024-01-01", 25 | "end_published_date": "2024-06-03", 26 | "highlights": { 27 | "num_sentences": 5 28 | }, 29 | "text": { 30 | "include_html_tags": false 31 | }, 32 | "num_subqueries": 5 33 | } 34 | ``` 35 | -------------------------------------------------------------------------------- /sample-reports/ai-in-retail.md: -------------------------------------------------------------------------------- 1 | # AI Innovations in Retail 2 | 3 | The integration of Artificial Intelligence (AI) in the retail sector has revolutionized the way businesses operate, offering enhanced customer experiences and operational efficiencies. This report delves into the various applications of AI in retail, highlighting key innovations and their impacts on the industry. 4 | 5 | ## AI-Driven Personalization 6 | 7 | AI technologies enable retailers to offer personalized shopping experiences to customers by analyzing their preferences, purchase history, and browsing behavior. This level of personalization increases customer satisfaction and loyalty, driving sales growth. 8 | 9 | ## Inventory Management 10 | 11 | AI-powered inventory management systems can predict stock levels accurately, automate restocking processes, and optimize warehouse organization. This reduces the risk of overstocking or stockouts, ensuring that products are available when customers need them. 12 | 13 | ## Customer Service Automation 14 | 15 | Chatbots and virtual assistants powered by AI provide 24/7 customer service, handling inquiries, and resolving issues promptly. This not only improves customer experience but also reduces the workload on human customer service representatives. 16 | 17 | ## Fraud Detection 18 | 19 | AI algorithms can detect unusual patterns and potential fraudulent transactions in real-time, enhancing the security of online retail transactions. This builds trust with customers and protects businesses from financial losses. 20 | 21 | ### JSON Example for AI Innovations in Retail Report Generation 22 | 23 | ```json 24 | { 25 | "query": "AI Innovations in Retail", 26 | "primary_prompt": "Generate a detailed report on the current advancements and applications of AI in the retail sector. The report should include an analysis of AI-driven personalization, inventory management, customer service automation, and fraud detection.", 27 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on AI innovations in retail. These subqueries should cover various aspects of the topic, including personalization, inventory management, customer service, and fraud detection.", 28 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about AI innovations in retail based on the provided information. Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. Reset the citations index and ignore the order of citations in the provided information.", 29 | "start_published_date": "2024-01-01", 30 | "end_published_date": "2024-06-03", 31 | "highlights": { 32 | "num_sentences": 5 33 | }, 34 | "text": { 35 | "include_html_tags": false 36 | }, 37 | "num_subqueries": 5 38 | } 39 | ``` 40 | -------------------------------------------------------------------------------- /sample-reports/ai-in-smart-cities.md: -------------------------------------------------------------------------------- 1 | # The Future of AI in Smart Cities 2 | 3 | The integration of Artificial Intelligence (AI) in smart cities is transforming urban landscapes, making them more efficient, sustainable, and livable. This report delves into the future of AI in smart cities, focusing on its applications in traffic management, energy efficiency, and public safety. 4 | 5 | ## AI in Traffic Management 6 | 7 | AI technologies are revolutionizing traffic management systems. By analyzing real-time data from traffic sensors and cameras, AI algorithms can predict traffic congestion and adjust signal timings to optimize traffic flow, reducing travel times and emissions. 8 | 9 | ## AI in Energy Efficiency 10 | 11 | In the realm of energy management, AI is instrumental in optimizing the use of resources. Smart grids, powered by AI, can forecast energy demand and distribute power efficiently, integrating renewable energy sources and reducing overall energy consumption. 12 | 13 | ## AI in Public Safety 14 | 15 | AI enhances public safety through advanced surveillance systems and predictive policing. By analyzing data from various sources, AI can identify potential threats and allocate resources proactively, improving emergency response times and reducing crime rates. 16 | 17 | ### JSON Example for AI in Smart Cities Report Generation 18 | 19 | ```json 20 | { 21 | "query": "The future of AI in smart cities", 22 | "primary_prompt": "Generate a detailed report on the future of AI in smart cities, focusing on applications such as traffic management, energy efficiency, and public safety. The report should include an analysis of current technologies, ongoing projects, challenges, and future prospects.", 23 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on the future of AI in smart cities. These subqueries should cover various aspects of the topic, including current technologies, ongoing projects, challenges, and future prospects.", 24 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about the future of AI in smart cities based on the provided information. Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. Reset the citations index and ignore the order of citations in the provided information.", 25 | "start_published_date": "2024-01-01", 26 | "end_published_date": "2024-06-03", 27 | "highlights": { 28 | "num_sentences": 5 29 | }, 30 | "text": { 31 | "include_html_tags": false 32 | }, 33 | "num_subqueries": 5 34 | } 35 | ``` 36 | -------------------------------------------------------------------------------- /sample-reports/ai-in-transportation.md: -------------------------------------------------------------------------------- 1 | # AI Advancements in Transportation 2 | 3 | The integration of Artificial Intelligence (AI) in transportation is revolutionizing the way we commute, manage traffic, and ensure safety on the roads. This report delves into the current advancements of AI in transportation, highlighting its impact on improving efficiency and reducing congestion. 4 | 5 | ## AI in Traffic Management 6 | 7 | AI technologies are being employed to optimize traffic flow and reduce congestion. Through the use of real-time data analysis and predictive modeling, AI systems can adjust traffic signals, predict traffic patterns, and provide alternate routes to drivers, significantly reducing travel time and improving overall traffic management. 8 | 9 | ## AI in Autonomous Vehicles 10 | 11 | Autonomous vehicles, powered by AI, are set to transform the transportation landscape. These vehicles are equipped with sensors and machine learning algorithms that enable them to navigate roads safely without human intervention. This section explores the progress in autonomous vehicle technology, including the challenges faced and the potential impact on society. 12 | 13 | ## AI in Public Transportation 14 | 15 | AI is also making strides in public transportation systems. From optimizing bus routes to predicting maintenance needs, AI technologies are enhancing the efficiency and reliability of public transit, offering a more convenient and sustainable option for commuters. 16 | 17 | ## JSON Example for AI in Transportation Report Generation 18 | 19 | ```json 20 | { 21 | "query": "AI advancements in transportation", 22 | "primary_prompt": "Generate a detailed report on the current advancements of AI in transportation, focusing on traffic management, autonomous vehicles, and public transportation. The report should include an analysis of technologies, ongoing projects, challenges, and future prospects.", 23 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on AI advancements in transportation. These subqueries should cover various aspects of the topic, including traffic management, autonomous vehicles, and public transportation.", 24 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about AI advancements in transportation based on the provided information. Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. Reset the citations index and ignore the order of citations in the provided information.", 25 | "start_published_date": "2024-01-01", 26 | "end_published_date": "2024-06-03", 27 | "highlights": { 28 | "num_sentences": 5 29 | }, 30 | "text": { 31 | "include_html_tags": false 32 | }, 33 | "num_subqueries": 5 34 | } 35 | ``` 36 | -------------------------------------------------------------------------------- /sample-reports/combining-approaches.md: -------------------------------------------------------------------------------- 1 | # Combining Approaches in Advanced Reporting 2 | 3 | Combining different prompt engineering techniques can significantly enhance the quality and comprehensiveness of generated reports. By integrating multi-hop prompting, recursive strategies, graph structures, and advanced logic and reasoning, Agentic Reports can tackle complex topics with precision and depth. 4 | 5 | ## Example: The Future of AI in Smart Cities 6 | 7 | Generating a report on "The future of AI in smart cities" might involve: 8 | 9 | - **Multi-hop prompting** to break down the topic into subqueries like AI in traffic management, AI in energy efficiency, and AI in public safety. 10 | - **Recursive prompting** to iteratively refine these subqueries based on initial findings. 11 | - **Graph structures** to map out the relationships between different AI applications and their impacts on smart city development. 12 | - **Advanced logic and reasoning** to critically analyze the data and provide well-rounded insights and projections. 13 | 14 | ### JSON Example for Combining Approaches 15 | 16 | ```json 17 | { 18 | "query": "The future of AI in smart cities", 19 | "primary_prompt": "Generate a detailed report on the future of AI in smart cities, focusing on applications such as traffic management, energy efficiency, and public safety. The report should include an analysis of current technologies, ongoing projects, challenges, and future prospects.", 20 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on the future of AI in smart cities. These subqueries should cover various aspects of the topic, including current technologies, ongoing projects, challenges, and future prospects.", 21 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about the future of AI in smart cities based on the provided information. Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. Reset the citations index and ignore the order of citations in the provided information.", 22 | "start_published_date": "2024-01-01", 23 | "end_published_date": "2024-06-03", 24 | "highlights": { 25 | "num_sentences": 5 26 | }, 27 | "text": { 28 | "include_html_tags": false 29 | }, 30 | "num_subqueries": 5 31 | } 32 | ``` 33 | -------------------------------------------------------------------------------- /sample-reports/graph-structures.md: -------------------------------------------------------------------------------- 1 | # Graph Structures in Advanced Reporting 2 | 3 | Graph Structures in advanced reporting leverage the power of graph theory to map out and analyze the relationships between various pieces of information. This technique is particularly useful for exploring complex topics where understanding the interconnections between data points can provide deeper insights. 4 | 5 | ## Example: AI in Financial Markets 6 | 7 | In the context of "AI in financial markets," graph structures can be employed to visualize and analyze the intricate relationships between AI technologies, market trends, regulatory impacts, and economic outcomes. This approach allows for a comprehensive analysis of how AI influences different aspects of financial markets. 8 | 9 | ### JSON Example for Graph Structures 10 | 11 | ```json 12 | { 13 | "query": "AI in financial markets", 14 | "primary_prompt": "Generate a detailed report on the current advancements, performance, and market trends of AI in financial markets. The report should include an analysis of AI technologies, market trends, regulatory impacts, and economic outcomes.", 15 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on AI in financial markets. These subqueries should cover various aspects of the topic, including AI technologies, market trends, regulatory impacts, and economic outcomes.", 16 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about AI in financial markets based on the provided information. Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. Reset the citations index and ignore the order of citations in the provided information.", 17 | "start_published_date": "2024-01-01", 18 | "end_published_date": "2024-06-03", 19 | "highlights": { 20 | "num_sentences": 5 21 | }, 22 | "text": { 23 | "include_html_tags": false 24 | }, 25 | "num_subqueries": 5 26 | } 27 | ``` 28 | 29 | This JSON example outlines how to structure a request for generating a report using graph structures. It demonstrates the method of breaking down a complex topic into interconnected subtopics for a thorough exploration. 30 | -------------------------------------------------------------------------------- /sample-reports/multi-hop-prompting.md: -------------------------------------------------------------------------------- 1 | # Multi-Hop Prompting in Advanced Reporting 2 | 3 | Multi-Hop Prompting is an advanced prompt engineering technique that breaks down complex topics into manageable subqueries. This approach allows for a more detailed and comprehensive exploration of the subject matter by tackling it in stages. 4 | 5 | ## Example: The Future of AI in Smart Cities 6 | 7 | Generating a report on "The future of AI in smart cities" might involve the following steps: 8 | 9 | 1. **Initial Query**: Start with a broad query on the role of AI in smart cities. 10 | 2. **First Hop**: Break down the initial query into subqueries focusing on specific applications of AI in smart cities, such as traffic management, energy efficiency, and public safety. 11 | 3. **Second Hop**: For each application identified in the first hop, generate further subqueries to explore current technologies, ongoing projects, challenges, and future prospects. 12 | 4. **Synthesis**: Combine the insights gathered from all hops to construct a comprehensive report that covers the initial query in depth. 13 | 14 | This multi-hop approach ensures that each aspect of the complex topic is thoroughly investigated, leading to a report that is both detailed and insightful. 15 | 16 | ```json 17 | { 18 | "query": "The future of AI in smart cities", 19 | "primary_prompt": "Generate a detailed report on the future of AI in smart cities, focusing on applications such as traffic management, energy efficiency, and public safety. The report should include an analysis of current technologies, ongoing projects, challenges, and future prospects. Ensure that the report is well-structured and professional.", 20 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on the future of AI in smart cities. These subqueries should cover various aspects of the topic, including current technologies, ongoing projects, challenges, and future prospects.", 21 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about the future of AI in smart cities based on the provided information. Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. Reset the citations index and ignore the order of citations in the provided information.", 22 | "start_published_date": "2024-01-01", 23 | "end_published_date": "2024-06-03", 24 | "highlights": { 25 | "num_sentences": 5 26 | }, 27 | "text": { 28 | "include_html_tags": false 29 | }, 30 | "num_subqueries": 5 31 | } 32 | ``` 33 | -------------------------------------------------------------------------------- /sample-reports/recursive-prompting.md: -------------------------------------------------------------------------------- 1 | # Recursive Prompting in Advanced Reporting 2 | 3 | Recursive Prompting is a sophisticated technique used in advanced reporting to refine and deepen the inquiry process. It involves iteratively querying or prompting for information, where each subsequent query is informed by the response to the previous one. This method ensures that the information gathered is both relevant and comprehensive, progressively narrowing down the focus to yield precise results. 4 | 5 | ## Example: AI's Role in Climate Change Mitigation 6 | 7 | Consider the topic "AI's role in climate change mitigation." The initial query might focus on general applications of AI in environmental science. Based on the initial responses, subsequent queries could delve deeper into specific areas such as AI in renewable energy optimization, AI in carbon footprint reduction, and AI-driven climate modeling. 8 | 9 | ### JSON Example for Recursive Prompting 10 | 11 | ```json 12 | { 13 | "query": "AI's role in climate change mitigation", 14 | "primary_prompt": "Generate a detailed report on the current advancements and applications of AI in climate change mitigation. The report should include an analysis of AI in renewable energy optimization, carbon footprint reduction, and climate modeling.", 15 | "subqueries_prompt": "Generate 2 interesting, diverse search queries that would be useful for generating a detailed report on AI's role in climate change mitigation. These subqueries should cover various aspects of the topic, including renewable energy optimization, carbon footprint reduction, and climate modeling.", 16 | "report_prompt": "Write a comprehensive and professional in English, five-paragraph, 200-word research report about AI's role in climate change mitigation based on the provided information. Include citations in the text using footnote notation ([citation #]), for example [2]. First provide the report, followed by a single `References` section that only lists the URLs (and their published date) used, in the format [#] . For the published date, only include the month and year. Reset the citations index and ignore the order of citations in the provided information.", 17 | "start_published_date": "2024-01-01", 18 | "end_published_date": "2024-06-03", 19 | "highlights": { 20 | "num_sentences": 5 21 | }, 22 | "text": { 23 | "include_html_tags": false 24 | }, 25 | "num_subqueries": 5 26 | } 27 | ``` 28 | 29 | This JSON example outlines how to structure a request for generating a report using recursive prompting. It demonstrates the progression from a broad initial query to more focused subqueries, ensuring a thorough exploration of the topic. 30 | -------------------------------------------------------------------------------- /scripts/fix-path.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Set PYTHONPATH to include the project root directory 4 | export PYTHONPATH=$PYTHONPATH:/workspaces/agentic-reports/project-root 5 | 6 | # Ensure the correct working directory 7 | cd /workspaces/agentic-reports/project-root 8 | 9 | # Create and activate a virtual environment 10 | python3 -m venv venv 11 | source venv/bin/activate 12 | 13 | # Install dependencies 14 | pip install -r ../requirements.txt 15 | 16 | # Run Uvicorn with the necessary parameters 17 | uvicorn app.main:app --reload --host 0.0.0.0 --port 8000 --reload-dir /workspaces/agentic-reports/project-root 18 | -------------------------------------------------------------------------------- /scripts/setup_env.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | export PYTHONPATH=$PYTHONPATH:/workspaces/agentic-reports/project-root 3 | cd /workspaces/agentic-reports/project-root 4 | uvicorn app.main:app --reload --host 0.0.0.0 --port 8000 --reload-dir /workspaces/agentic-reports/project-root 5 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | from pathlib import Path 3 | 4 | def read_long_description(): 5 | this_directory = Path(__file__).parent 6 | long_description = (this_directory / "README.md").read_text() 7 | return long_description 8 | 9 | setup( 10 | name='agentic_reports', 11 | version='0.1.5', 12 | packages=find_packages(where='project-root'), 13 | package_dir={'': 'project-root'}, 14 | py_modules=['start'], # Include the start module 15 | install_requires=[ 16 | 'twine', 17 | 'setuptools', 18 | 'wheel', 19 | 'flake8', 20 | 'black', 21 | 'pytest', 22 | 'pip-upgrader', 23 | 'fastapi', 24 | 'uvicorn', 25 | 'pandas', 26 | 'pydantic', 27 | 'exa_py', 28 | 'pydantic-settings', 29 | 'virtualenv', 30 | 'litellm' 31 | ], 32 | python_requires='>=3.10', 33 | author='rUv', 34 | author_email='null@ruv.net', 35 | description='Agent Reporting using Exa.ai API.', 36 | long_description=read_long_description(), 37 | long_description_content_type='text/markdown', 38 | url='https://github.com/ruvnet/agentic-reports', 39 | license='Apache License 2.0', 40 | classifiers=[ 41 | 'Development Status :: 3 - Alpha', 42 | 'Intended Audience :: Developers', 43 | 'Topic :: Software Development :: Libraries :: Application Frameworks', 44 | 'License :: OSI Approved :: Apache Software License', 45 | 'Programming Language :: Python :: 3', 46 | 'Programming Language :: Python :: 3.10', 47 | ], 48 | entry_points={ 49 | 'console_scripts': [ 50 | 'agentic-reports=start:main', 51 | ], 52 | }, 53 | ) 54 | -------------------------------------------------------------------------------- /start.py: -------------------------------------------------------------------------------- 1 | import os 2 | import subprocess 3 | 4 | def prompt_for_api_key(env_var_name): 5 | api_key = input(f"Enter your {env_var_name}: ") 6 | os.environ[env_var_name] = api_key 7 | 8 | def main(): 9 | # Check for OPENAI_API_KEY 10 | if not os.getenv('OPENAI_API_KEY'): 11 | prompt_for_api_key('OPENAI_API_KEY') 12 | 13 | # Check for EXA_API_KEY 14 | if not os.getenv('EXA_API_KEY'): 15 | prompt_for_api_key('EXA_API_KEY') 16 | 17 | # Run the shell script 18 | subprocess.run(['bash', './start.sh'], check=True) 19 | 20 | if __name__ == "__main__": 21 | main() 22 | -------------------------------------------------------------------------------- /start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo " 4 | _____ _ _ _____ _ 5 | | _ |___ ___ ___| |_|_|___ ___| __ |___ ___ ___ ___| |_ ___ 6 | | | . | -_| | _| | _|___| -| -_| . | . | _| _|_ -| 7 | |__|__|_ |___|_|_|_| |_|___| |__|__|___| _|___|_| |_| |___| 8 | |___| |_| 9 | 10 | A Comprehensive Python Library for Generating Research Reports 11 | " 12 | 13 | # Starting message 14 | echo "Starting the Agentic Reports application setup and server..." 15 | 16 | # Check for OPENAI_API_KEY 17 | if [ -z "$OPENAI_API_KEY" ]; then 18 | read -p "Enter your OPENAI_API_KEY: " openai_key 19 | export OPENAI_API_KEY=$openai_key 20 | fi 21 | 22 | # Check for EXA_API_KEY 23 | if [ -z "$EXA_API_KEY" ]; then 24 | read -p "Enter your EXA_API_KEY: " exa_key 25 | export EXA_API_KEY=$exa_key 26 | fi 27 | 28 | # Set PYTHONPATH to include the project root directory 29 | export PYTHONPATH=$PYTHONPATH:/workspaces/agentic-reports/project-root 30 | 31 | # Ensure the correct working directory 32 | cd /workspaces/agentic-reports/project-root 33 | 34 | # Create and activate a virtual environment 35 | python3 -m venv venv 36 | source venv/bin/activate 37 | 38 | # Install required dependencies quietly 39 | pip install -r ../requirements.txt -q 40 | 41 | # Run Uvicorn with the necessary parameters 42 | uvicorn app.main:app --reload --host 0.0.0.0 --port 8000 --reload-dir /workspaces/agentic-reports/project-root 43 | --------------------------------------------------------------------------------