Copy this prompt and provide it to your AI coding assistant.
# AI Integration Instructions: Amgap Tech AI Gateway
This document contains instructions for an AI agent to integrate my application with the Amgap Tech AI Gateway.
## 1. Context
The Amgap Tech AI Gateway provides an endpoint to process chat messages via an AI assistant. Your task is to implement the API calls to this gateway in my application.
## 2. API Details
- **Endpoint:** `POST https://yourdomain.com/api/chat`
- **Method:** POST
- **Authentication:** Custom Header (`X-API-Key`)
- **Headers:**
- `X-API-Key: <YOUR_API_KEY>`
- `Accept: application/json`
- `Content-Type: application/json`
- **JSON Payload:**
- `message` (string, required) - The user's message up to 2000 characters.
- `session_id` (string, required) - A unique session identifier (up to 128 characters) to maintain conversation context.
## 3. Implementation Steps
1. Ask me what framework/language I am using (e.g., Laravel, Node.js, Python) if you do not already know.
2. Instruct me to add the API Key to my `.env` file.
```env
AMGAP_GATEWAY_KEY=<YOUR_API_KEY>
```
3. Create a dedicated Service or Client class that handles sending requests to the gateway endpoint.
4. Parse the JSON response.
### Expected JSON Response
```json
{
"reply": "The AI's response text...",
"session_id": "the-session-id-passed",
"requires_approval": false,
"pending_tool": null
}
```
### Laravel PHP Example
```php
$response = Http::withHeaders([
'X-API-Key' => config('services.amgap_gateway.key'),
'Accept' => 'application/json',
])->post('https://yourdomain.com/api/chat', [
'message' => $userMessage,
'session_id' => $sessionId,
]);
if ($response->successful()) {
return $response->json('reply');
}
// Handle error
return 'Sorry, the AI is currently unavailable.';
```
### JavaScript (Node.js / Fetch) Example
```javascript
const response = await fetch('https://yourdomain.com/api/chat', {
method: 'POST',
headers: {
'X-API-Key': process.env.AMGAP_GATEWAY_KEY,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
message: userMessage,
session_id: sessionId
})
});
if (response.ok) {
const data = await response.json();
return data.reply;
}
// Handle error
```
### Python (Requests) Example
```python
import requests
import os
url = 'https://yourdomain.com/api/chat'
headers = {
'X-API-Key': os.environ.get('AMGAP_GATEWAY_KEY'),
'Accept': 'application/json'
}
payload = {
'message': user_message,
'session_id': session_id
}
try:
response = requests.post(url, headers=headers, json=payload, timeout=10)
if response.status_code == 200:
data = response.json()
return data.get('reply')
except requests.exceptions.RequestException:
pass
# Handle error
```
## 4. Fallback Behavior
If the Gateway is down or returns a non-200 response, the application should handle the error gracefully without crashing.