AI Chatbot Implementation
AI Chatbot Integration
The HCG AI application features an integrated chatbot powered by the OpenAI API. This component provides users with real-time, conversational support for interpreting HCG blood test results, understanding ultrasound measurements, and receiving general first-trimester guidance.
Configuration
To enable the chatbot, you must provide a valid OpenAI API key in your environment configuration.
- Obtain API Key: Create an account and generate a secret key at the OpenAI Platform.
- Update Environment: Add the key to your
.envfile:OPENAI_API_KEY=your_openai_api_key_here
[!IMPORTANT] Without a valid API key, the "AI Chat" tab will remain visible in the UI, but message requests will return a connection error.
Features
The chatbot is specifically tuned to assist with the medical context of this application:
- HCG Data Interpretation: Explain the significance of specific β-hCG levels based on gestational age.
- Ultrasound Support: Define terminology such as Gestational Sac (GS), Yolk Sac (YS), and Crown-Rump Length (CRL).
- Medical Risk Context: Provide context for the "Low/Medium/High" risk assessments generated by the application's internal algorithms.
Usage in the UI
Users can access the chatbot through the main navigation:
- Click the AI Chat tab in the navigation bar.
- Type a query into the message input (e.g., "What does a CRL of 5mm mean?").
- The assistant will respond with context-aware information based on standard medical guidelines.
API Reference
The frontend communicates with the AI via a POST request to the backend server.
POST /api/chat
Sends a user message to the OpenAI-powered backend and returns a conversational response.
Request Headers
Content-Type: application/jsonCookie: [Session Cookie](Requires authentication)
Request Body
{
"message": "string"
}
Success Response (200 OK)
{
"response": "string"
}
Error Responses
401 Unauthorized: User is not logged in.500 Internal Server Error: OpenAI API key is missing or the service is unavailable.
Technical Notes
- Internal Role: The server uses a system prompt to constrain the AI's behavior to first-trimester pregnancy support, ensuring responses remain relevant to the application's purpose.
- Rate Limiting: Usage is subject to the rate limits associated with your OpenAI API tier.
- Data Privacy: Messages sent to the chatbot are processed by OpenAI. Ensure users are aware that no sensitive PII (Personally Identifiable Information) should be shared in the chat interface.