Skip to content

How to Build a Real-Time Research Assistant with FastAPI and GPT

Notifications You must be signed in to change notification settings

Moe230391/research-assistant

Repository files navigation

How to Build a Real-Time Research Assistant with FastAPI and GPT

A step-by-step guide to creating a powerful, AI-driven web application that leverages GPT models and Perplexity API to provide real-time, multi-source research results. This project demonstrates how to integrate advanced AI models with a scalable backend using FastAPI.


Features

  • Real-Time Research: Get instant answers to user queries with dynamic updates.
  • Multi-Source Integration: Combines GPT and Perplexity API for comprehensive results.
  • Related Questions Generator: Automatically generates follow-up questions for deeper insights.
  • User-Friendly Interface: Intuitive frontend with search history and downloadable results.
  • Scalable Architecture: Asynchronous processing with WebSockets for real-time communication.

Setup Instructions

1. Clone the Repository

git clone https://github.com/Moe230391/research-assistant.git
cd research-assistant

2. Install Dependencies

pip install -r requirements.txt

3. Set Up Environment Variables

Create a .env file in the root directory:

OPENAI_API_KEY=your_openai_api_key
PERPLEXITY_API_KEY=your_perplexity_api_key

4. Run the Application

Start the FastAPI server:

uvicorn app:app --reload

Open the app in your browser at http://127.0.0.1:8000.


File Structure

├── app.py                # Main entry point for the application
├── config.py             # Configuration for environment variables
├── websocket_handler.py  # Handles WebSocket connections
├── search_handler.py     # AI-related logic (GPT and Perplexity integration)
├── models.py             # Pydantic models for data validation
├── utils.py              # Utility functions for timeouts and response formatting
├── static/               # Static files (HTML, CSS, JS)
│   ├── index.html        # Frontend interface
│   ├── style.css         # Styling for the app
│   └── script.js         # Frontend JavaScript logic
├── tests/                # Test files for validating app functionality
│   ├── test.txt          # Sample input queries
├── .env                  # Environment variables (ignored in version control)
├── requirements.txt      # List of dependencies
└── README.md             # Project documentation

Technologies Used

  • FastAPI: Backend framework for building APIs and WebSocket support.
  • WebSockets: Real-time updates for research results.
  • GPT Models (OpenAI): Generative AI for related question generation and synthesis.
  • Perplexity API: Multi-source search for robust answers.
  • Python Libraries: asyncio, Pydantic, Termcolor.
  • Frontend: HTML, CSS, JavaScript.

How It Works

  1. User Query:

    • User enters a query in the search bar.
    • WebSocket sends the query to the backend.
  2. Backend Processing:

    • GPT generates related questions.
    • Perplexity API fetches answers for related questions.
    • Results are synthesized into a comprehensive final answer.
  3. Real-Time Updates:

    • WebSocket streams partial results as they are processed.
    • Final synthesized answer is displayed when all results are ready.

Testing

1. Run Tests

Use test.txt to validate the app with sample queries:

What will be the NVIDIA stock price at the end of 2024?
Explain quantum computing in simple terms.

2. Verify Results

Ensure the app:

  • Generates accurate answers.
  • Handles timeouts and errors gracefully.
  • Provides a clear final synthesized response.

Contributing

Contributions are welcome! To contribute:

  1. Fork the repository.
  2. Create a feature branch: git checkout -b feature-name.
  3. Commit changes: git commit -m "Add feature".
  4. Push to the branch: git push origin feature-name.
  5. Submit a pull request.

License

This project is licensed under the MIT License.


Future Improvements

  • Add user authentication for personalized research history.
  • Integrate additional APIs for broader data coverage.
  • Enhance frontend design with modern frameworks like React or Vue.js.

About

How to Build a Real-Time Research Assistant with FastAPI and GPT

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages