Skip to content

Latest commit

 

History

History
227 lines (156 loc) · 4.04 KB

File metadata and controls

227 lines (156 loc) · 4.04 KB

Email Parser API

FastAPI-based email parsing service using LLM for structured data extraction with Oracle Cloud Infrastructure (OCI).


Prerequisites

  • Docker
  • Docker Compose
  • OCI credentials (config + private key)

Project Structure

.
├── Dockerfile
├── docker-compose.yml
├── requirements.txt
├── .dockerignore
├── main.py                  # FastAPI application
├── llm_client.py           # LLM client with OCI integration
├── structured_output.py    # Pydantic models
├── prompt.md               # LLM prompts
└── app/config/
    ├── config               # OCI config file (includes model and compartment_id)
    └── oci_api_key.pem      # OCI private key

Configuration

Download private key and config file

  • Login to OCI Console -> Identity & security -> Users
  • Select user -> API Keys -> Add API Key
  • Generate private Key
  • Dowload both the .pem and the config file
  • Compile the config file's field "key_file" with the path of the .pem you just dowloaded

OCI Configuration (Local Project)

The project expects the OCI configuration to be located inside the repository at:

./app/config

This directory is mounted into the container at:

/app/config

Make sure you have:

  • app/config/config — OCI CLI config file
  • app/config/oci_api_key.pem — OCI private key

OCI config example (app/config/config)

[DEFAULT]
user=ocid1.user.oc1..xxxxx
fingerprint=xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
tenancy=ocid1.tenancy.oc1..xxxxx
region=eu-frankfurt-1
key_file=/app/config/oci_api_key.pem

# LLM specific settings
model=cohere.command-a-03-2025
oci_compartment_id=ocid1.compartment.oc1..xxxxx

Important Notes

  • key_file must use the container path:

    /app/config/oci_api_key.pem
    

Building and Running

Using Docker Compose (Recommended)

docker-compose up -d --build

View logs

docker-compose logs -f

Stop the service

docker-compose down

Running with Docker only

# Build image
docker build -t email-parser-api .

# Run container
docker run -d \
  -p 8080:8080 \
  -v ./app/config:/app/config:ro \
  --name email-parser-api \
  email-parser-api

# Logs
docker logs -f email-parser-api

# Stop & remove
docker stop email-parser-api
docker rm email-parser-api

API Usage

Interactive Documentation

Once running:


Endpoints

  • GET / — Root endpoint
  • GET /health — Health check
  • POST /parse-email — Parse email content

Example Request

curl -X POST "http://localhost:8080/parse-email" \
  -H "Content-Type: application/json" \
  -d '{
    "email_body": "From: john.doe@example.com\nTo: support@company.com\nSubject: Order Issue\nDate: 2024-01-15\n\nHello,\n\nI have an issue with my recent order #12345.\n\nBest regards,\nJohn Doe"
  }'

Example Response

{
  "emails": [
    {
      "CUSTOMER_NAME": "John Doe",
      "MAIL_TYPE": "TO",
      "CUSTOMER_OPERATOR": "john.doe@example.com",
      "TOPIC": "Order Issue",
      "MAIL_DATE": "2024-01-15"
    }
  ],
  "FULL_BODY": "Hello,\n\nI have an issue with my recent order #12345.\n\nBest regards,\nJohn Doe"
}

Development (Without Docker)

python -m venv venv
source venv/bin/activate   # Windows: venv\Scripts\activate

pip install -r requirements.txt

uvicorn main:app --reload --host 0.0.0.0 --port 8080

Security Best Practices

Add to .gitignore:

app/config/config
app/config/oci_api_key.pem

Troubleshooting

OCI Authentication Errors

  • Check app/config/config
  • Check app/config/oci_api_key.pem
  • Verify key_file=/app/config/oci_api_key.pem

Model Not Found

  • Verify model value
  • Check region compatibility

Compartment Errors

  • Verify oci_compartment_id
  • Check permissions on compartment