Refactor codebase to implement MCP server for GPT Researcher

Replaced FastAPI app with an MCP server implementation, enhancing flexibility and modularity for research operations. Deprecated `phoenix_technologies` package, updated server logic, added utility functions, and revised dependencies in `requirements.txt`. Updated Dockerfile and README to align with the new architecture.
This commit is contained in:
ThomasTaroni 2025-04-26 17:54:43 +02:00
parent 73e929ca00
commit 44b91b9375
10 changed files with 481 additions and 281 deletions

223
README.md
View file

@ -1,175 +1,72 @@
# README for FastAPI-Based Report GPT Generation Service
## Overview
This repository contains the implementation of a **FastAPI**-based service designed to generate research reports. The service processes user-provided queries and report types, performing advanced research powered by `GPTResearcher` and responding with comprehensive results, including details, cost, context, images, and other associated metadata.
# Project Overview
## Description
This project is a server-side application built with Python that facilitates research-related operations. It provides functionalities to manage researchers, handle resources, process queries, and generate in-depth research reports. The application features reusable utility functions to streamline responses, handle exceptions gracefully, and format data for client consumption. A `Dockerfile` is provided for easy containerization and deployment.
## Features
### Server Functionality
The main server functionalities are defined in `server.py`, which includes:
- **research_resource**: Management of research resources.
- **deep_research**: Conducts detailed research operations.
- **write_report**: Creates comprehensive reports based on researched data.
- **get_research_sources**: Retrieves information sources for research.
- **get_research_context**: Provides contextual information tied to research.
- **research_query**: Handles incoming research-related queries.
- **run_server**: Initializes and runs the server.
- **RESTful API** to handle user queries and generate reports.
- **Streaming responses** to deliver research output in chunks.
- **Secure API access** with API Key authentication.
- Completely containerized setup with Docker.
- Built with modular design for easier scalability and maintenance.
### Utility Functions
The `utils.py` file provides additional support, including:
- **Response Handling**:
- `create_error_response`
- `create_success_response`
---
- **Error & Exception Management**:
- `handle_exception`
## System Architecture
- **Data Operations**:
- `get_researcher_by_id`
- `format_sources_for_response`
- `format_context_with_sources`
- `store_research_results`
- `create_research_prompt`
### Core Components
### Docker Support
The included `Dockerfile` allows for simple containerized deployment:
- Uses a lightweight Python 3.13 image.
- Installs required dependencies from `requirements.txt`.
- Configures the application to run via `server.py` on port `8000` using `CMD ["python", "server.py"]`.
1. **FastAPI App (`main.py`)**:
- Hosts the API endpoints.
- Handles API Key authentication for secure use.
- Accepts user inputs (query and report type) and generates a chunked streaming response.
## Setup and Usage
### Prerequisites
- Python 3.13 or later.
- `pip` for dependency management.
- Docker (optional, for containerized deployment).
2. **Research Logic (`deepresearch.py`)**:
- Encapsulates research and report generation.
- Utilizes `GPTResearcher` to conduct research, generate reports, and retrieve extended data like images, contexts, or costs.
3. **Docker Integration**:
- The application is containerized with a well-defined `Dockerfile`.
- Includes dependency installation, environment setup, and FastAPI server configuration for rapid deployment.
---
## Prerequisites
Before running the application, ensure the following are installed on your system:
- **Docker**: Version 24.0+
- **Python**: Version 3.13+
- **pip**: Pre-installed Python package manager.
---
## Running the Application Locally
### Cloning the Repository
Clone the repository to a directory of your choice:
```shell script
git clone https://git.kvant.cloud/phoenix/gpt-researcher.git
cd gpt-researcher
### Installation
1. Clone this repository.
2. Install dependencies:
``` bash
pip install -r requirements.txt
```
### Environment Variable Configuration
Create a `.env` file in the root of the project and define:
1. Run the application:
``` bash
python server.py
```
API_KEY=your_api_key # Replace "your_api_key" with your desired key
OPENAI_BASE_URL=
OPENAI_API_KEY=
EMBEDDING=
FAST_LLM=
SMART_LLM=
STRATEGIC_LLM=
OPENAI_API_VERSION=
SERPER_API_KEY=
RETRIEVER=serper
### Using Docker
Build and run the application as a Docker container:
1. Build the Docker image:
``` bash
docker build -t research-app .
```
### Installing Dependencies
Install the required Python modules based on the generated `requirements.txt`.
```shell script
pip install --no-cache-dir -r requirements.txt
1. Run the Docker container:
``` bash
docker run -p 8000:8000 research-app
```
### Running the App
Run the FastAPI app locally:
```shell script
uvicorn main:app --host 0.0.0.0 --port 8000
The application will be accessible at `http://localhost:8000`.
## Folder Structure
```
|-- src/
|-- server.py # Main server logic
|-- utils.py # Reusable utility functions
|-- Dockerfile # Containerization setup
|-- requirements.txt # Dependencies file
|-- README.md # Documentation (this file)
```
After running, your app will be available at `http://127.0.0.1:8000`.
---
## Using Docker for Deployment
### Building the Docker Image
Build the Docker image using the **Dockerfile** provided:
```shell script
docker build -t fastapi-report-service .
```
### Running the Docker Container
Spin up a container and map FastAPI's default port, `8000`:
```shell script
docker run --env-file .env -p 8000:8000 fastapi-report-service
```
---
## API Usage
### 1. **`/get_report`**
- **Method**: `POST`
- **Description**: Generates a report based on user input.
- **Headers**:
- `X-API-KEY`: API Key for authentication.
- **Request Body** (`JSON`):
```json
{
"query": "Research on AI in healthcare",
"report_type": "research_report|resource_report|outline_report|custom_report|detailed_report|subtopic_report|deep"
}
```
- **Streaming Response**: Research and report are provided in chunks.
---
## Code Structure
```
├── Dockerfile # Configuration for Dockerizing the application
├── requirements.txt # Python dependencies list
├── main.py # FastAPI server entry point
├── deepresearch.py # Research-related logic and GPTResearcher integration
└── src/ # Other project files and assets
```
---
## Features Under the Hood
1. **Authentication**:
- An API key mechanism ensures that only authorized users can access endpoints.
2. **Streaming Response**:
- Large research reports are sent incrementally using `StreamingResponse` for better experience and efficiency.
3. **Modular Research Logic**:
- Research and generation tasks are handled by a dedicated class (`ReportGenerator`), making the application extensible.
---
## Future Enhancements
- **Asynchronous Enhancements**:
- Improve async handling for long-running queries.
- **Database Integration**:
- Save request history for auditing and reference purposes.
- **Web Interface**:
- A user-friendly web application for interacting with the API.
---
## Contributing
Contributions are welcome! Feel free to fork the repository, make updates, and submit a pull request.