OpenAI Chatbot Setup
OpenAI Chatbot Integration
The HCG AI application features an integrated AI assistant designed to help users interpret their pregnancy tracking data, understand medical terminology, and receive guidance based on their HCG and ultrasound entries.
1. Configuration
The chatbot requires a valid OpenAI API key to function. If the key is missing, the chatbot interface will be disabled or return an error during interaction.
- Obtain an API Key: Sign in to the OpenAI Platform and generate a new secret key.
- Update Environment Variables: Add the key to your
.envfile in the root directory:
OPENAI_API_KEY=sk-your-unique-api-key-here
2. User Access
The chatbot is a protected feature and is only accessible to authenticated users.
- Navigation: Click the AI Chat tab in the main navigation bar.
- Contextual Awareness: The assistant is designed to provide insights based on the medical data you have already entered in the Entries tab (e.g., β-hCG levels and gestational age).
- Interface: Users can type natural language questions regarding their pregnancy progress or the analysis results provided by the app.
3. API Reference
If you are extending the application or interacting with the chatbot via the backend, use the following endpoint:
POST /api/chat
Sends a message to the AI assistant and retrieves a response.
Authentication Required: Yes (Session-based)
Request Body:
| Field | Type | Description |
| :--- | :--- | :--- |
| message | string | The user's question or prompt. |
Example Request:
curl -X POST http://localhost:5000/api/chat \
-H "Content-Type: application/json" \
-d '{"message": "What does a doubling time of 48 hours indicate for my HCG levels?"}'
Example Response:
{
"reply": "A doubling time of 48-72 hours is generally considered normal in early pregnancy (under 6,000 mIU/mL). It suggests that the pregnancy is progressing as expected at this stage...",
"status": "success"
}
4. Troubleshooting
- "Missing API Key" Error: Ensure that
OPENAI_API_KEYis correctly defined in.envand that the server was restarted after the key was added. - Rate Limiting: If you receive a 429 error, you have exceeded your OpenAI quota or rate limits. Check your usage dashboard on the OpenAI website.
- Unauthorized (401): Ensure you are logged into the application before attempting to access the chat interface.