PAYAL / API_TEST_EXAMPLES.md
abeerrai01
INTIAL
382ea2d
# API Test Examples - Payal Farmer Advisory Chatbot
Complete JSON examples for testing all API endpoints.
## Base URL
```
http://localhost:8000
```
## 1. Crop Advice
### POST `/chat/crop`
**English Example:**
```json
{
"query": "What crops should I grow in my area?",
"lat": 28.6139,
"lon": 77.2090,
"uid": "user123",
"session_id": "",
"language": "en"
}
```
**Hindi Example:**
```json
{
"query": "मेरे क्षेत्र में कौन सी फसलें उगानी चाहिए?",
"lat": 17.3850,
"lon": 78.4867,
"uid": "user123",
"session_id": "",
"language": "hi"
}
```
**Telugu Example (with existing session):**
```json
{
"query": "నా ప్రాంతంలో ఏ పంటలు పండించాలి?",
"lat": 17.3850,
"lon": 78.4867,
"uid": "user123",
"session_id": "session_20241201_123456",
"language": "te"
}
```
**Punjabi Example (auto-detect language):**
```json
{
"query": "ਮੇਰੇ ਖੇਤਰ ਵਿੱਚ ਕਿਹੜੀਆਂ ਫਸਲਾਂ ਉਗਾਉਣੀਆਂ ਚਾਹੀਦੀਆਂ ਹਨ?",
"lat": 31.6340,
"lon": 74.8723,
"uid": "user123",
"session_id": "",
"language": ""
}
```
---
## 2. Fertilizer Advice
### POST `/chat/fertilizer`
**English Example:**
```json
{
"crop": "Wheat",
"stage": "Vegetative",
"soil_test": "Nitrogen: Low, Phosphorus: Medium",
"uid": "user123",
"session_id": "",
"language": "en"
}
```
**Hindi Example:**
```json
{
"crop": "गेहूं",
"stage": "वानस्पतिक अवस्था",
"soil_test": "",
"uid": "user123",
"session_id": "session_20241201_123456",
"language": "hi"
}
```
**Tamil Example:**
```json
{
"crop": "நெல்",
"stage": "",
"soil_test": "",
"uid": "user123",
"session_id": "",
"language": "ta"
}
```
---
## 3. Soil Health Advice
### POST `/chat/soil`
**English Example:**
```json
{
"soil_info": "pH level is 6.5, organic matter is low, clay soil type",
"uid": "user123",
"session_id": "",
"language": "en"
}
```
**Hindi Example:**
```json
{
"soil_info": "मिट्टी का pH 6.5 है, कार्बनिक पदार्थ कम है, मिट्टी चिकनी है",
"uid": "user123",
"session_id": "session_20241201_123456",
"language": "hi"
}
```
**Bengali Example (auto-detect):**
```json
{
"soil_info": "মাটির pH 6.5, জৈব পদার্থ কম, মাটি কাদাময়",
"uid": "user123",
"session_id": "",
"language": ""
}
```
---
## 4. Weather Advice
### POST `/chat/weather`
**English Example:**
```json
{
"lat": 28.6139,
"lon": 77.2090,
"days": 3,
"uid": "user123",
"session_id": "",
"language": "en"
}
```
**Hindi Example:**
```json
{
"lat": 17.3850,
"lon": 78.4867,
"days": 5,
"uid": "user123",
"session_id": "session_20241201_123456",
"language": "hi"
}
```
**Marathi Example:**
```json
{
"lat": 18.5204,
"lon": 73.8567,
"days": 3,
"uid": "user123",
"session_id": "",
"language": "mr"
}
```
---
## 5. Pest Detection
### GET `/chat/pest`
**With farmer ID:**
```
GET /chat/pest?farmer_id=farmer_001&uid=user123&language=hi
```
**Without farmer ID:**
```
GET /chat/pest?uid=user123&language=en
```
**With session ID:**
```
GET /chat/pest?farmer_id=farmer_001&uid=user123&session_id=session_20241201_123456&language=te
```
---
## 6. Phishing URL Check
### POST `/chat/phishing`
**English Example:**
```json
{
"url": "https://www.google.com",
"uid": "user123",
"session_id": "",
"language": "en"
}
```
**Hindi Example:**
```json
{
"url": "http://suspicious-site.com",
"uid": "user123",
"session_id": "session_20241201_123456",
"language": "hi"
}
```
**Gujarati Example (auto-detect):**
```json
{
"url": "https://www.example.com",
"uid": "user123",
"session_id": "",
"language": ""
}
```
---
## 7. Sentiment Analysis
### POST `/chat/sentiment`
**English Example (Positive):**
```json
{
"query": "I am very happy with the crop yield this year!",
"uid": "user123",
"session_id": "",
"language": "en"
}
```
**Hindi Example (Negative):**
```json
{
"query": "इस साल फसल की पैदावार बहुत खराब है",
"uid": "user123",
"session_id": "session_20241201_123456",
"language": "hi"
}
```
**Telugu Example (Mixed):**
```json
{
"query": "ఈ సంవత్సరం పంట దిగుబడి బాగుంది కానీ కీటకాల సమస్య ఉంది",
"uid": "user123",
"session_id": "",
"language": "te"
}
```
---
## 8. Government Schemes
### POST `/chat/schemes`
**English Example:**
```json
{
"text": "I need information about farming subsidies and government schemes",
"uid": "user123",
"session_id": "",
"language": "en"
}
```
**Hindi Example:**
```json
{
"text": "कृषि ऋण और सब्सिडी योजनाओं के बारे में जानकारी चाहिए",
"uid": "user123",
"session_id": "session_20241201_123456",
"language": "hi"
}
```
**Punjabi Example (auto-detect):**
```json
{
"text": "ਮੈਨੂੰ ਖੇਤੀਬਾੜੀ ਸਬਸਿਡੀ ਅਤੇ ਸਰਕਾਰੀ ਯੋਜਨਾਵਾਂ ਬਾਰੇ ਜਾਣਕਾਰੀ ਚਾਹੀਦੀ ਹੈ",
"uid": "user123",
"session_id": "",
"language": ""
}
```
---
## 9. Conversation History
### GET `/chat/history`
**Get current session history:**
```
GET /chat/history?uid=user123
```
**Get specific session history:**
```
GET /chat/history?uid=user123&session_id=session_20241201_123456
```
---
## 10. Start New Chat
### POST `/chat/new`
**Start a new chat session:**
```
POST /chat/new?uid=user123
```
**Response:**
```json
{
"message": "New chat session started",
"session_id": "session_20241201_234567",
"uid": "user123"
}
```
---
## Complete Test Flow Example
### Step 1: Start New Chat
```bash
curl -X POST "http://localhost:8000/chat/new?uid=user123"
```
**Response:**
```json
{
"message": "New chat session started",
"session_id": "session_20241201_123456",
"uid": "user123"
}
```
### Step 2: First Question (session_id auto-created)
```json
POST /chat/crop
{
"query": "What crops should I grow?",
"lat": 28.6139,
"lon": 77.2090,
"uid": "user123",
"session_id": "",
"language": "en"
}
```
**Response:**
```json
{
"response": "...",
"session_id": "session_20241201_123456",
"uid": "user123",
"language": "en"
}
```
### Step 3: Follow-up Question (reuse session_id)
```json
POST /chat/crop
{
"query": "Tell me more about wheat",
"lat": 28.6139,
"lon": 77.2090,
"uid": "user123",
"session_id": "session_20241201_123456",
"language": "en"
}
```
**Note:** Chatbot remembers previous conversation!
### Step 4: Check History
```bash
curl -X GET "http://localhost:8000/chat/history?uid=user123&session_id=session_20241201_123456"
```
---
## cURL Commands
### Crop Advice
```bash
curl -X POST "http://localhost:8000/chat/crop" \
-H "Content-Type: application/json" \
-d '{
"query": "What crops should I grow?",
"lat": 28.6139,
"lon": 77.2090,
"uid": "user123",
"session_id": "",
"language": "en"
}'
```
### Fertilizer Advice
```bash
curl -X POST "http://localhost:8000/chat/fertilizer" \
-H "Content-Type: application/json" \
-d '{
"crop": "Wheat",
"stage": "Vegetative",
"soil_test": "Nitrogen: Low",
"uid": "user123",
"session_id": "",
"language": "en"
}'
```
### Weather Advice
```bash
curl -X POST "http://localhost:8000/chat/weather" \
-H "Content-Type: application/json" \
-d '{
"lat": 28.6139,
"lon": 77.2090,
"days": 3,
"uid": "user123",
"session_id": "",
"language": "hi"
}'
```
### Pest Detection
```bash
curl -X GET "http://localhost:8000/chat/pest?farmer_id=farmer_001&uid=user123&language=hi"
```
### Phishing Check
```bash
curl -X POST "http://localhost:8000/chat/phishing" \
-H "Content-Type: application/json" \
-d '{
"url": "https://www.google.com",
"uid": "user123",
"session_id": "",
"language": "en"
}'
```
### Sentiment Analysis
```bash
curl -X POST "http://localhost:8000/chat/sentiment" \
-H "Content-Type: application/json" \
-d '{
"query": "I am very happy with the crop yield!",
"uid": "user123",
"session_id": "",
"language": "en"
}'
```
### Government Schemes
```bash
curl -X POST "http://localhost:8000/chat/schemes" \
-H "Content-Type: application/json" \
-d '{
"text": "I need information about farming subsidies",
"uid": "user123",
"session_id": "",
"language": "en"
}'
```
### Conversation History
```bash
curl -X GET "http://localhost:8000/chat/history?uid=user123"
```
### Start New Chat
```bash
curl -X POST "http://localhost:8000/chat/new?uid=user123"
```
---
## Python Requests Examples
```python
import requests
BASE_URL = "http://localhost:8000"
# Crop Advice
response = requests.post(f"{BASE_URL}/chat/crop", json={
"query": "What crops should I grow?",
"lat": 28.6139,
"lon": 77.2090,
"uid": "user123",
"session_id": "",
"language": "en"
})
print(response.json())
# Get session_id from response
session_id = response.json()["session_id"]
# Follow-up question (reuse session_id)
response = requests.post(f"{BASE_URL}/chat/crop", json={
"query": "Tell me more about wheat",
"lat": 28.6139,
"lon": 77.2090,
"uid": "user123",
"session_id": session_id,
"language": "en"
})
print(response.json())
```
---
## Language Codes
- `hi` - Hindi
- `en` - English
- `pa` - Punjabi
- `te` - Telugu
- `ta` - Tamil
- `bn` - Bengali
- `mr` - Marathi
- `gu` - Gujarati
**Note:** If `language` is empty or not provided, the system will auto-detect the language from the input text.
---
## Important Notes
1. **Session ID Management:**
- First request with `uid`: `session_id` is auto-created and returned
- Subsequent requests: Use the `session_id` from previous response
- Session persists for 24 hours, then automatically creates a new one
- Use `/chat/new` endpoint to explicitly start a new chat
2. **Language Detection:**
- If `language` field is empty, system auto-detects from input text
- Supported languages: hi, en, pa, te, ta, bn, mr, gu
3. **Required Fields:**
- `uid`: Required for session management and conversation history
- `session_id`: Optional (auto-created if not provided)
- `language`: Optional (auto-detected if not provided)
4. **Response Format:**
All endpoints return:
```json
{
"response": "...",
"session_id": "session_XXXXXX",
"uid": "user123",
"language": "hi"
}
```