|
| 1 | +# BACKEND GUIDE |
| 2 | + |
| 3 | +## I. **RUNNING APP** |
| 4 | + |
| 5 | +Refer to [INSTALLATION.md](../INSTALLATION.md) for more in depth configuration details |
| 6 | +and dependency installation. This section is purely for running the backend. |
| 7 | + |
| 8 | +1) Ensure that your current working directory is `fastapi` (i.e. run `cd fastapi`) |
| 9 | +2) If you have not already, set up your virtual environment (`python3 -m venv venv`) |
| 10 | +3) Switch to the virtual environment (`source venv/bin/activate` or `venv\bin\activate.bat` depending on OS) |
| 11 | +4) If you have not already, install dependencies (`pip install -r requirements.txt`) |
| 12 | +5) Consider enabling debug logging: |
| 13 | + 1) Create `.env` inside the `fastapi` folder |
| 14 | + 2) Add properties to enable FastAPI and general debug logging: |
| 15 | + ```properties |
| 16 | + LOG_LEVEL=DEBUG |
| 17 | + FASTAPI_DEBUG=True |
| 18 | + ``` |
| 19 | +6) Finally, run the app with Uvicorn (`uvicorn app.main:app --reload`) |
| 20 | + * The app runs on port 8000 by default. To use another port, add `--port <number>` to the command. |
| 21 | + |
| 22 | +You should see logs similar to the following: |
| 23 | + |
| 24 | + |
| 25 | +To test the endpoints, use a tool like POSTMAN or curl in order to make GET or POST requests and receive formatted JSON responses. |
| 26 | + |
| 27 | +## II. **FILE STRUCTURE** |
| 28 | + |
| 29 | +`app` is the root package of the python project. |
| 30 | + |
| 31 | +`app/ai` contains the backend ML functionality. |
| 32 | + |
| 33 | +`app/api` contains the API endpoints exposed by the web server. |
| 34 | + |
| 35 | +`app/model` contains request and response structures for communication between the front and back end. |
| 36 | + |
| 37 | +## III. **CURRENT FUNCTIONALITY** |
| 38 | + |
| 39 | +### `/symmetry/v1/wiki/articles` |
| 40 | + |
| 41 | +**Protocol:** `GET` |
| 42 | + |
| 43 | +**Parameters:** |
| 44 | + * `query`: A Wikipedia article URL or title |
| 45 | + * `lang`: Optional. A language short code (i.e. "en" or "fr") defaulting to "en" for English. |
| 46 | + |
| 47 | +**Response:** |
| 48 | + |
| 49 | +```json |
| 50 | +{ |
| 51 | + "sourceArticle": "<article content>", |
| 52 | + "articleLanguages": [ |
| 53 | + // Article language data |
| 54 | + ] |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +### `/symmetry/v1/articles/compare` |
| 59 | + |
| 60 | +> [!NOTE] |
| 61 | +> This endpoint is a work in progress and currently only returns dummy data. |
| 62 | +
|
| 63 | +**Protocol:** `POST` |
| 64 | + |
| 65 | +**Parameters:** |
| 66 | +```json |
| 67 | +{ |
| 68 | + "article_text_blob_1": "<article 1 content>", |
| 69 | + "article_text_blob_2": "<article 2 content>", |
| 70 | + "article_text_blob_1_language": "<article 1 short language code>", |
| 71 | + "article_text_blob_2_language": "<article 2 short language code>", |
| 72 | + // Float ranging from 0-1 representing similarity percentage. |
| 73 | + "comparison_threshold": 0.8, |
| 74 | + "model_name": "<model name string>" |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +Response: |
| 79 | +```json |
| 80 | +{ |
| 81 | + "comparisons": [ |
| 82 | + // Array of comparison objects. |
| 83 | + { |
| 84 | + "left_article_array": [ |
| 85 | + // Array of article 1 content divided into subsections. |
| 86 | + ], |
| 87 | + "right_article_array": [ |
| 88 | + // Array of article 2 content divided into subsections. |
| 89 | + ], |
| 90 | + "left_article_missing_info_index": [ |
| 91 | + // Array of indices of article 1 subsections that are not present in 2. |
| 92 | + ], |
| 93 | + "right_article_extra_info_index": [ |
| 94 | + // Array of indices of article 2 subsections that are not present in 1. |
| 95 | + ] |
| 96 | + } |
| 97 | + ] |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +## IV. **CONCEPTS & RESEARCH** |
| 102 | + |
| 103 | +### Simple UI |
| 104 | +The UI is supposed to be as simple as possible - the UI team is supposed to focus their |
| 105 | +efforts on improving the user experience, not covering all possible cases. |
| 106 | +That means that the API/middleware is responsible for the bulk of logic related to input |
| 107 | +validation. |
| 108 | + |
| 109 | +For example, the dynamic search box allowing queries by URL or article title is handled |
| 110 | +by the wiki articles endpoint rather than the UI. |
| 111 | + |
| 112 | +### Semantic Comparison Without Translation |
| 113 | +The language comparison model in use does not require that the input texts be in the same language. |
| 114 | + |
| 115 | +### JSON vs XML |
| 116 | +We were tasked with investigating the benefits of using XML over JSON for communications |
| 117 | +as it might be easier for some ML models to work with. |
| 118 | + |
| 119 | +Our findings were that the primary benefit of using XML for some ML models is that it can |
| 120 | +be used to more easily guard against prompt injection. |
| 121 | +It is otherwise largely harder to work with, and is a model-specific detail. |
| 122 | + |
| 123 | +### CORS |
| 124 | +Cross-origin resource sharing is useful for protecting against certain attacks. |
| 125 | +By restricting resources to whitelisted domains, we can prevent this. |
| 126 | + |
| 127 | +However, Symmetry currently operates as a local server. CORS will not be important until |
| 128 | +a future state where a standalone Symmetry server exists. |
| 129 | + |
| 130 | +### Endpoint Naming Conventions |
| 131 | + |
| 132 | +A quick read on RESTful resource naming: https://restfulapi.net/resource-naming/ |
| 133 | + |
| 134 | +The current format is `/symmetry/v1/<path>/<to>/<resource>`. |
| 135 | + |
| 136 | +For a more dense and in-depth view of the RESTful philosophy, the above article |
| 137 | +links Roy Fielding's dissertation: |
| 138 | +https://ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_2_1_1 |
| 139 | + |
| 140 | +## V. **WHAT'S NEXT** |
| 141 | + |
| 142 | +### External Request Rejection |
| 143 | +As Symmetry does run a local web server, it would be a good idea to ensure that all |
| 144 | +remote requests are rejected. |
| 145 | + |
| 146 | +### CORS Configurability |
| 147 | +As stated above, CORS is not particularly necessary for Symmetry in its current state. |
| 148 | +However, in the future, it should likely be configurable to allow other origins. |
| 149 | + |
| 150 | +### Caching |
| 151 | +There is a cache implementation in place for articles. However, it is global and |
| 152 | +designed only for use in Wikipedia article fetching. With a little effort, ArticleCache |
| 153 | +could be repurposed into a more general and reusable content cache. |
| 154 | + |
| 155 | +### Live Comparison Data |
| 156 | +The [comparison endpoint](#symmetryv1articlescompare) currently returns dummy data. |
| 157 | +When the ML team completes their side, this endpoint will need to be connected to |
| 158 | +return real data. |
| 159 | + |
0 commit comments