Add FastAPI app for report generation with Docker support
Implement a modular FastAPI-based service for generating research reports using `GPTResearcher`. Includes secure API key authentication, a streaming response endpoint, and a Dockerized deployment setup. Also adds documentation, core dependencies, and project structure.
This commit is contained in:
commit
3d0d2b2770
8 changed files with 289 additions and 0 deletions
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
47
src/main.py
Normal file
47
src/main.py
Normal file
|
@ -0,0 +1,47 @@
|
|||
from fastapi import FastAPI, HTTPException, Request, Depends
|
||||
from pydantic import BaseModel
|
||||
from phoenix_technologies import ReportGenerator
|
||||
from fastapi.responses import StreamingResponse
|
||||
import os
|
||||
import asyncio
|
||||
|
||||
# FastAPI app instance
|
||||
app = FastAPI()
|
||||
|
||||
# Define a request body structure using Pydantic
|
||||
class ReportRequest(BaseModel):
|
||||
query: str
|
||||
report_type: str
|
||||
|
||||
# Define a dependency to validate the API Key
|
||||
def verify_api_key(request: Request):
|
||||
# Define the API key from the environment variables
|
||||
expected_api_key = os.getenv("API_KEY", None)
|
||||
if not expected_api_key:
|
||||
raise HTTPException(status_code=500, detail="API key is not configured on the server.")
|
||||
|
||||
# Get the API key from the request headers
|
||||
provided_api_key = request.headers.get("X-API-KEY", None)
|
||||
|
||||
# Check if the API key is correct
|
||||
if not provided_api_key or provided_api_key != expected_api_key:
|
||||
raise HTTPException(status_code=403, detail="Invalid or missing API key.")
|
||||
|
||||
@app.post("/get_report", dependencies=[Depends(verify_api_key)])
|
||||
async def get_report_endpoint(request: ReportRequest):
|
||||
"""
|
||||
Expose the `get_report` function as a POST API endpoint, with a streaming response.
|
||||
"""
|
||||
|
||||
async def generate_report():
|
||||
try:
|
||||
# Call the asynchronous get_report function
|
||||
report_generator = ReportGenerator(request.query, request.report_type)
|
||||
async for chunk in report_generator:
|
||||
yield chunk
|
||||
except Exception as e:
|
||||
yield f"Error: {str(e)}"
|
||||
|
||||
# Return streaming response
|
||||
return StreamingResponse(generate_report(), media_type="text/plain")
|
||||
|
4
src/phoenix_technologies/__init__.py
Normal file
4
src/phoenix_technologies/__init__.py
Normal file
|
@ -0,0 +1,4 @@
|
|||
# phoenix-technologies/__init__.py
|
||||
from .gptresearch.deepresearch import ReportGenerator
|
||||
|
||||
__all__ = ["ReportGenerator"]
|
0
src/phoenix_technologies/gptresearch/__init__.py
Normal file
0
src/phoenix_technologies/gptresearch/__init__.py
Normal file
42
src/phoenix_technologies/gptresearch/deepresearch.py
Normal file
42
src/phoenix_technologies/gptresearch/deepresearch.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
from gpt_researcher import GPTResearcher
|
||||
|
||||
|
||||
class ReportGenerator:
|
||||
def __init__(self, query: str, report_type: str):
|
||||
"""
|
||||
Initializes the ReportGenerator with a query and report type.
|
||||
"""
|
||||
self.query = query
|
||||
self.report_type = report_type
|
||||
self.researcher = GPTResearcher(query, report_type)
|
||||
|
||||
async def generate_report(self):
|
||||
"""
|
||||
Conducts research and generates the report along with additional information.
|
||||
"""
|
||||
# Conduct research
|
||||
research_result = await self.researcher.conduct_research()
|
||||
report = await self.researcher.write_report()
|
||||
|
||||
# Retrieve additional information
|
||||
research_context = self.researcher.get_research_context()
|
||||
research_costs = self.researcher.get_costs()
|
||||
research_images = self.researcher.get_research_images()
|
||||
research_sources = self.researcher.get_research_sources()
|
||||
|
||||
return {
|
||||
"report": report,
|
||||
"context": research_context,
|
||||
"costs": research_costs,
|
||||
"images": research_images,
|
||||
"sources": research_sources
|
||||
}
|
||||
|
||||
def get_query_details(self):
|
||||
"""
|
||||
Returns details of the query and report type.
|
||||
"""
|
||||
return {
|
||||
"query": self.query,
|
||||
"report_type": self.report_type
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue