Skip to content

savhascelik/sample-task-fastapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenRouter GPT-4o FastAPI Microservice

A FastAPI-based microservice that accepts user input, sends it to GPT-4o via OpenRouter, logs the response, and triggers an alert in n8n if there's an error.

Features

  • Accepts text input via REST API
  • Forwards requests to OpenRouter API (supporting GPT-4o and similar models)
  • Error handling with detailed logging
  • n8n integration for error alerts
  • Environment variable configuration
  • Token limit management to prevent credit issues

Installation

  1. Clone the repository:
git clone https://github.com/savhascelik/sample-task-fastapi.git
cd sample-task-fastapi
  1. Install the required dependencies:
pip install -r requirements.txt
  1. Create a .env file in the project root:
OPENROUTER_API_KEY=your_openrouter_api_key
N8N_WEBHOOK_URL=your_n8n_webhook_url

Running the Application

To start the application:

uvicorn main:app --reload

The application will run at http://127.0.0.1:8000 by default.

API Usage

Generate Text

curl -X 'POST' \
  'http://127.0.0.1:8000/generate' \
  -H 'Content-Type: application/json' \
  -d '{
  "text": "Hello, how are you?"
}'

Reload Environment Variables

If you change the .env file while the application is running, you can reload the environment variables without restarting:

curl -X 'POST' \
  'http://127.0.0.1:8000/reload-env'

Logging

The application logs are saved in the logs directory, with a filename format of app_YYYY-MM-DD.log. These logs contain detailed information about requests, responses, and any errors that occur.

To view the logs on Windows:

type logs\app_YYYY-MM-DD.log

To view the logs on Linux/Mac:

cat logs/app_YYYY-MM-DD.log

To monitor logs in real-time on Windows:

powershell -command "Get-Content -Path logs\app_YYYY-MM-DD.log -Wait"

To monitor logs in real-time on Linux/Mac:

tail -f logs/app_YYYY-MM-DD.log

API Documentation

FastAPI provides automatic interactive API documentation:

Environment Variables

Variable Description Required
OPENROUTER_API_KEY Your OpenRouter API key Yes
N8N_WEBHOOK_URL URL for n8n webhook to trigger on errors No

Error Handling

The application handles various error scenarios:

  • OpenRouter API errors (authentication, rate limiting, etc.)
  • Network errors
  • Insufficient credits errors
  • General server errors

Each error type has specific handling and appropriate HTTP status codes.

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages