File size: 5,520 Bytes
14fdd05
 
 
 
 
 
 
 
 
7ffe51d
14fdd05
7ffe51d
676582c
7ffe51d
14fdd05
7ffe51d
676582c
14fdd05
676582c
14fdd05
 
676582c
 
 
 
 
 
 
 
 
 
 
7ffe51d
14fdd05
7ffe51d
676582c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ffe51d
676582c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ffe51d
 
 
676582c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
---
title: TaskFlow API
emoji: 
colorFrom: blue
colorTo: indigo
sdk: docker
pinned: false
license: mit
---

# TaskFlow Backend API

FastAPI backend for TaskFlow task management application with AI chatbot integration.

## Features

- User authentication with JWT and Better Auth
- Task CRUD operations
- **AI Chatbot Assistant** - Conversational AI for task management
- PostgreSQL database with SQLModel ORM
- RESTful API design
- Multi-turn conversation support with context management
- Intent recognition for todo-related requests

## Tech Stack

- **Framework**: FastAPI 0.104.1
- **ORM**: SQLModel 0.0.14
- **Database**: PostgreSQL (Neon Serverless)
- **Authentication**: Better Auth + JWT
- **AI Provider**: Google Gemini (gemini-pro)
- **Migrations**: Alembic 1.13.0

## Environment Variables

Configure these in your `.env` file:

### Database
- `DATABASE_URL`: PostgreSQL connection string (Neon or local)

### Application
- `APP_NAME`: Application name (default: "Task CRUD API")
- `DEBUG`: Debug mode (default: True)
- `CORS_ORIGINS`: Allowed CORS origins (default: "http://localhost:3000")

### Authentication
- `BETTER_AUTH_SECRET`: Secret key for Better Auth (required)
- `JWT_ALGORITHM`: JWT algorithm (default: "HS256")
- `JWT_EXPIRATION_DAYS`: Token expiration in days (default: 7)

### AI Provider Configuration
- `AI_PROVIDER`: AI provider to use (default: "gemini")
- `GEMINI_API_KEY`: Google Gemini API key (required if using Gemini)
- `OPENROUTER_API_KEY`: OpenRouter API key (optional)
- `COHERE_API_KEY`: Cohere API key (optional)

### Conversation Settings
- `MAX_CONVERSATION_MESSAGES`: Maximum messages to keep in history (default: 20)
- `MAX_CONVERSATION_TOKENS`: Maximum tokens to keep in history (default: 8000)

## Setup Instructions

### 1. Install Dependencies

```bash
pip install -r requirements.txt
```

### 2. Configure Environment

Create a `.env` file in the `backend/` directory:

```env
# Database
DATABASE_URL=postgresql://user:password@localhost:5432/todo_db

# Application
APP_NAME=Task CRUD API
DEBUG=True
CORS_ORIGINS=http://localhost:3000

# Authentication
BETTER_AUTH_SECRET=your_secret_key_here
JWT_ALGORITHM=HS256
JWT_EXPIRATION_DAYS=7

# AI Provider
AI_PROVIDER=gemini
GEMINI_API_KEY=your_gemini_api_key_here

# Conversation Settings
MAX_CONVERSATION_MESSAGES=20
MAX_CONVERSATION_TOKENS=8000
```

### 3. Run Database Migrations

```bash
alembic upgrade head
```

### 4. Start the Server

```bash
uvicorn src.main:app --reload --host 0.0.0.0 --port 8000
```

The API will be available at `http://localhost:8000`

## API Documentation

Once running, visit:
- **Interactive Docs**: `http://localhost:8000/docs`
- **ReDoc**: `http://localhost:8000/redoc`

## API Endpoints

### Authentication
- `POST /api/auth/signup` - Register new user
- `POST /api/auth/login` - Login user

### Tasks
- `GET /api/{user_id}/tasks` - Get all tasks for user
- `POST /api/{user_id}/tasks` - Create new task
- `GET /api/{user_id}/tasks/{task_id}` - Get specific task
- `PUT /api/{user_id}/tasks/{task_id}` - Update task
- `DELETE /api/{user_id}/tasks/{task_id}` - Delete task

### AI Chat (New in Phase 1)
- `POST /api/{user_id}/chat` - Send message to AI assistant

#### Chat Request Body
```json
{
  "message": "Can you help me organize my tasks?",
  "conversation_id": 123,  // Optional: null for new conversation
  "temperature": 0.7       // Optional: 0.0 to 1.0
}
```

#### Chat Response
```json
{
  "conversation_id": 123,
  "message": "I'd be happy to help you organize your tasks!",
  "role": "assistant",
  "timestamp": "2026-01-14T10:30:00Z",
  "token_count": 25,
  "model": "gemini-pro"
}
```

## AI Chatbot Features

### Phase 1 (Current)
- ✅ Natural conversation with AI assistant
- ✅ Multi-turn conversations with context retention
- ✅ Intent recognition for todo-related requests
- ✅ Conversation history persistence
- ✅ Automatic history trimming (20 messages / 8000 tokens)
- ✅ Free-tier AI provider support (Gemini)

### Phase 2 (Coming Soon)
- 🔄 MCP tools for task CRUD operations
- 🔄 AI can directly create, update, and delete tasks
- 🔄 Natural language task management

## Error Handling

The API returns standard HTTP status codes:

- `200 OK` - Request successful
- `400 Bad Request` - Invalid request data
- `401 Unauthorized` - Authentication required or failed
- `404 Not Found` - Resource not found
- `429 Too Many Requests` - Rate limit exceeded
- `500 Internal Server Error` - Server error

## Database Schema

### Users Table
- `id`: Primary key
- `email`: Unique email address
- `name`: User's name
- `password`: Hashed password
- `created_at`, `updated_at`: Timestamps

### Tasks Table
- `id`: Primary key
- `user_id`: Foreign key to users
- `title`: Task title
- `description`: Task description
- `completed`: Boolean status
- `created_at`, `updated_at`: Timestamps

### Conversation Table (New)
- `id`: Primary key
- `user_id`: Foreign key to users
- `title`: Conversation title
- `created_at`, `updated_at`: Timestamps

### Message Table (New)
- `id`: Primary key
- `conversation_id`: Foreign key to conversation
- `role`: "user" or "assistant"
- `content`: Message text
- `timestamp`: Message timestamp
- `token_count`: Token count for the message

## Development

### Running Tests
```bash
pytest
```

### Database Migrations

Create a new migration:
```bash
alembic revision -m "description"
```

Apply migrations:
```bash
alembic upgrade head
```

Rollback migration:
```bash
alembic downgrade -1
```

## License

MIT License