File size: 7,552 Bytes
82bf89e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from fastapi import FastAPI, HTTPException, Query
from pydantic import BaseModel
from sse_starlette import EventSourceResponse
import asyncio
# import logging
import uuid

# Handle relative imports
# If relative import fails, try absolute import
from long_review_write import LiteratureReviewTool
from config_logger import logger
from setting_config import settings
from typing import Any
debug_config_args: dict[str, Any] = {"debug": settings.DEBUG_MODE}
if not settings.DEBUG_MODE:
    debug_config_args["openapi_url"] = None
    debug_config_args["docs_url"] = None
    debug_config_args["redoc_url"] = None

app = FastAPI(title="Bio-Agent Literature Review API", version="1.0.0",
              **debug_config_args,)
message_queues = {} 

class StreamMessage(BaseModel):
    type: str
    content: str


@app.get("/health",tags=["check"],operation_id="health_check")
async def health_check():
    """
    Health Check and Service Validation Tool
    
    This MCP tool performs comprehensive health checks and validation for the Review service
    and its dependencies. It verifies the service is ready to handle literature review requests.
    
    **Process:**
    1. **Service Status Check** - Verifies the Review service is running and responsive
    2. **Dependency Validation** - Checks all required APIs and services are available
    3. **Configuration Verification** - Validates environment variables and settings
    4. **Connection Testing** - Tests connections to external services (PubMed, web search)
    
    **Input:**
    - None (no parameters required)
    
    **Output:**
    - Service status information via Server-Sent Events (SSE)
    - Health check results including:
      * Service availability status
      * Dependency connection status
      * Configuration validation results
      * Ready/not ready status for processing requests
    
    **Use Cases:**
    - Service monitoring and diagnostics
    - Pre-flight checks before starting literature reviews
    - System health monitoring
    - Troubleshooting service issues
    
    **Expected Duration:** 5-10 seconds
    
    **Note:** This tool is useful for verifying the Review service is properly configured
    and ready to handle literature review generation requests.
    """
    async def generate_data():
        a = "test"
        for i in a:
            
            yield {
                "data": StreamMessage(
                    type="result", content=f"data {i}\n"
                ).model_dump_json()
            }
    return EventSourceResponse(generate_data())


@app.get("/review_generate",operation_id="review_generate")
async def review_generate(query: str = Query(..., description="Research query for literature review generation")):
    """
    Comprehensive Literature Review Generation Tool
    
    This MCP tool generates comprehensive, research-backed literature reviews on biomedical topics.
    The tool performs an extensive multi-stage research and writing process that typically takes 
    approximately 30 minutes to complete.
    
    **Process Overview:**
    1. **Query Analysis & Plan Generation** (5-8 min)
       - Analyzes the research query and generates a detailed outline
       - Creates multiple sections with specific research objectives
       - Plans the overall structure of the review
    
    2. **Comprehensive Literature Research** (15-20 min)
       - Performs extensive PubMed database searches for relevant scientific papers
       - Conducts web searches for additional context and recent developments
       - Collects and analyzes 50-100+ relevant scientific papers
       - Gathers comprehensive reference materials
    
    3. **Section-by-Section Writing** (8-10 min)
       - Writes detailed content for each planned section
       - Each section typically contains 800-1200 words
       - Integrates findings from multiple research sources
       - Ensures proper citation and academic formatting
    
    4. **Quality Control & Review** (2-3 min)
       - Performs content validation and fact-checking
       - Ensures accuracy of scientific claims
       - Validates references and citations
       - Checks for consistency and coherence
    
    5. **Final Report Assembly** (2-3 min)
       - Combines all sections into a cohesive document
       - Generates comprehensive abstract
       - Creates final bibliography with proper formatting
       - Produces a complete 15,000-word literature review
    
    **Input:**
    - query (string): A research topic or question in biomedical field
      Examples: "CRISPR gene editing in cancer treatment", "COVID-19 vaccine development", 
                "Alzheimer's disease mechanisms", "Stem cell therapy applications"
    
    **Output:**
    - A comprehensive literature review in Markdown format containing:
      * Abstract (200-300 words)
      * Introduction and background
      * Multiple detailed sections (typically 5-8 sections)
      * Discussion and future directions
      * Comprehensive bibliography (50-100+ references)
      * Total length: ~15,000 words
    
    **Key Features:**
    - Real-time progress updates via Server-Sent Events (SSE)
    - Extensive use of PubMed and web search APIs
    - Multi-agent collaboration for different aspects of research
    - Quality control and validation at multiple stages
    - Academic-grade formatting and citation
    - Comprehensive coverage of current research landscape
    
    **Expected Duration:** 30 minutes (may vary based on query complexity)
    
    **Use Cases:**
    - Academic research preparation
    - Grant proposal background research
    - Clinical practice guideline development
    - Drug development literature analysis
    - Medical education content creation
    - Healthcare policy research
    
    **Note:** This tool is specifically designed for biomedical and healthcare topics.
    For best results, provide specific, well-defined research questions.
    """
    if not query or query.strip() == "":
        raise HTTPException(status_code=400, detail="Query parameter is required")
    
    # Create a unique queue for this request
    request_id = str(uuid.uuid4())
    queue = asyncio.Queue()
    message_queues[request_id] = queue
    
    try:
        async def result_callback(result):
            await queue.put(StreamMessage(type="result", content=result))
        
        tool = LiteratureReviewTool(
            thoughts_callback=result_callback,
            results_callback=result_callback,
            verbose=True,
        )
        
        process_task = asyncio.create_task(
            tool.run(query)
        )
        
        async def event_generator():
            try:
                while True:
                    try:
                        message = await asyncio.wait_for(queue.get(), timeout=0.1)
                        yield {"data": message.model_dump_json()}
                    except asyncio.TimeoutError:
                        if process_task.done():
                            break
                        continue
            except Exception as e:
                logger.error(f"stream error: {e}")
            finally:
                yield {
                    "data": StreamMessage(
                        type="done", content="task done"
                    ).model_dump_json()
                }
        
        return EventSourceResponse(event_generator())
    
    finally:
        # Clean up the queue when done
        if request_id in message_queues:
            del message_queues[request_id]