# shadow writer

## Local Development

### Setup Environment

```bash
python3 -m venv .venv
source .venv/bin/activate
pip install poetry
poetry install
```

### Configure Environment

```bash
cp .env.example .env  # then edit .env
```

### Initialize Database
```bash
flask db upgrade
```

### Run Server
```bash
flask run --reload
# flask run --help
```

### (Optional) Add new dependency to this Python project
```bash
source .venv/bin/activate
poetry add <package_name>
git commit -am 'pkgs: <add package_name>'
git push
```


## Remote Development on Server

### Build Docker Image
```bash
docker build --platform=linux/amd64 --no-cache -t sykp241095/shadow-writer .
```

### Push Docker Image
```bash
docker push sykp241095/shadow-writer  # or to your own namespace on Docker Hub
```

### Deploy docker image on remote server
```bash
docker run --pull=always -p 5000:5000 -v /home/ubuntu/.env:/app/.env sykp241095/shadow-writer
```


## Documentation of API

API Host: `https://seo.tidb.ai`

```bash
curl --location 'https://seo.tidb.ai/api/rag/write' \
--header 'Content-Type: application/json' \
--data '{
    "article_spec": "Why we need Serverless Databases?\n\n1. Benefits of Serverless MySQL\n2. How Serverless MySQL Works\n3. Use Cases for Serverless MySQL\n\n\nWhat is Serverless MySQL?\n\n1. Serverless MySQL vs Traditional MySQL(Cost Comparison, Scalability and Elasticity, Management and Maintenance)\n2. several notable Serverless MySQL\n\nWhy TiDB Serverless Stands Out\nTiDB'\''s Innovative Architecture\nReal-World Success Story about serverless mysql"
}'
```

```bash
curl --location 'https://seo.tidb.ai/api/rag/edit' \
--header 'Content-Type: application/json' \
--data '{
    "article_content": "<The whole article content, returned by the previous /api/rag/write call>",
    "opinions": "Use more tutorials in xxx section",
    "paragraph_to_revise": "**Real-World Success Story:**  AmzTrends, a SaaS company specializing in data analytics for Amazon sellers, migrated to TiDB Serverless and achieved an 80% cost reduction while improving performance and scalability.  This is just one example of how TiDB Serverless is empowering businesses to achieve their goals"
}'
```

## Add new Table to Database

After you add new models to `shadow_writer/models.py`, you need to run the following command to generate a new migration file:

```bash
flask db migrate -m "add new table"
```
NOTE: this command will generate a new migration file in `migrations/versions/` folder, you need to manually modify the generated migration file to add the new table to the database.

Then you need to run the following command to apply the migration:

```bash
flask db upgrade
```


## Architecture

![architecture](/misc/media/shadow_writer.png)
