# yusuke710/nanoperplexityai architecture

> Architecture diagram of the GitHub repository yusuke710/nanoperplexityai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/yusuke710/nanoperplexityai
- Repository: https://github.com/yusuke710/nanoperplexityai

## Overview

##### Project Overview & Type
- This project is an open-source, lightweight implementation of perplexity.ai. It is a single-script tool (around 200 lines of Python) that takes a user query, optionally performs a Google search to extract context from web pages, builds a prompt, and calls an LLM API (such as OpenAI’s) to generate a response. The responses are then streamed and saved into a markdown file.
- As an open-source tool, it focuses on rapid prototyping and minimalism without a complex GUI. The primary goal is to quickly generate LLM responses enriched by contextual data retrieved via a search.

##### Analyzing the File Structure
- Top-Level Files:
- The main executable is nanoPerplexityAI.py.
- playground.md serves as the dynamic output/visualization file.
- example_outputs/ holds pre-generated markdown answer examples.
- Directories like assets contain images and demo gifs that help illustrate the project.
- The experiments/ directory shows that some explorative code and optimization notes exist, which suggests active iterative development.
- Configuration files such as .gitignore and LICENSE.txt are present alongside the README that explains project usage and installation.

##### Core Architecture (as described in the README)
The README enumerates the following key steps:
a. Capturing the user query.
b. Deciding whether to trigger a Google search and fetching webpage content.
c. Composing a prompt by combining a system prompt, fetched context, and the user's query.
d. Making a call to the LLM API for generating the answer.
e. Streaming the answer to a markdown file (playground.md) for real-time output.

- User Interface (CLI Input):
- Mention that it’s a CLI-based input system that prompts the user.

- Query Processing Module:
- This module handles analyzing the user query and deciding if a Google search is required.

- Google Search Integration:
- An external component that, if triggered, calls the Google Search API via libraries like googlesearch-python, requests, BeautifulSoup4, etc.

- Prompt Builder:
- This component compiles the system prompt, the fetched webpage context, and the original query.

- LLM API Caller:
- External integration with the large language model (e.g., OpenAI API).

- Markdown Output Generator:
- This is in charge of streaming LLM responses and saving them in a markdown file (e.g., playground.md).

- File Storage & Assets:
- Although auxiliary, illustrate that the system outputs data (responses in markdown file) and also accesses static assets (assets/) for UI elements if needed.
- Optionally, add a component for “File System / Asset Storage” indicating usage of example_outputs and assets folders.

b. Depicting Relationships and Data Flow:

c. Architectural Patterns:
- Even though it is a small script, annotate that the architecture reflects a modular design:
- Separation between input handling, data fetching, processing, and external API integration.
- It employs a “pipeline” or “chain-of-responsibility” pattern to build and execute the query.

- Details about external dependencies (e.g., googlesearch-python, requests, beautifulsoup4, openai) alongside the respective components.

d. Technologies & Frameworks:
- Mention that the project is built in Python and uses specific third-party libraries as per the README install instructions.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph InHouseModules [In-House Modules]
        U["User Query Input (CLI)"]
        Q["Query Processing Module\n(Analyzes Query & Decides on Search)"]
        P["Prompt Builder\n(Compiles System Prompt, Context, and Query)"]
        M["Markdown Output Generator\n(Streams LLM Response to playground.md)"]
    end

    subgraph ExternalServices [External Services]
        G["Google Search Integration\n(googlesearch-python, requests, BeautifulSoup4)"]
        L["LLM API Caller\n(OpenAI API or similar)"]
    end

    subgraph FileAssets [File Storage & Assets]
        F["File System / Asset Storage\n(example_outputs, assets/)"]
    end

    U -->|"Sends User Query" | Q
    Q -->|"Decides & Triggers Search if needed" | G
    G -->|"Returns Webpage Context" | Q
    Q -->|"Sends Aggregated Data" | P
    P -->|"API Call: send prompt, receive streaming answer" | L
    L -->|"Streams LLM Response" | M
    M -->|"Saves to Markdown File" | F

    click U "https://github.com/yusuke710/nanoperplexityai/blob/main/nanoPerplexityAI.py"
    click Q "https://github.com/yusuke710/nanoperplexityai/blob/main/nanoPerplexityAI.py"
    click G "https://github.com/yusuke710/nanoperplexityai/blob/main/nanoPerplexityAI.py"
    click P "https://github.com/yusuke710/nanoperplexityai/blob/main/nanoPerplexityAI.py"
    click L "https://github.com/yusuke710/nanoperplexityai/blob/main/nanoPerplexityAI.py"
    click M "https://github.com/yusuke710/nanoperplexityai/blob/main/playground.md"
    click F "https://github.com/yusuke710/nanoperplexityai/tree/main/assets"
    click F "https://github.com/yusuke710/nanoperplexityai/tree/main/example_outputs"

    classDef internal fill:#DDEBF7,stroke:#5B9BD5,stroke-width:2px;
    classDef external fill:#F8CBAD,stroke:#ED7D31,stroke-width:2px;
    classDef asset fill:#C6E0B4,stroke:#70AD47,stroke-width:2px;

    class U,Q,P,M internal;
    class G,L external;
    class F asset;
```
