POST /api/chat
Streams responses from the AI assistant powered by Ollama. Supports automatic context injection based on question content.Request Body
The Ollama model to use (e.g., “llama2”, “mistral”, “medllama2”)
The user’s question or message to the AI
Whether to inject medical context. If not specified, automatically determined based on keywords in the prompt
Custom system instruction for the AI. Defaults to: “Responda com base EXCLUSIVA no contexto fornecido…”
Maximum number of context items to include (when context is used)
Maximum characters of context to include (when context is used)
Response
The response is streamed using Server-Sent Events (SSE) format withContent-Type: application/octet-stream.
Each line contains a JSON object with the AI’s response chunk:
Partial text response from the AI model
Indicates if the response is complete
Example Request
cURL
JavaScript
Example Response Stream
Automatic Context Detection
The system automatically detects if the question would benefit from medical context based on keywords: Context Keywords:- CID-related: “cid”, “código”, “doença”, “diagnóstico”, “classificação”
- News-related: “notícia”, “news”, “governo”, “saúde pública”, “ministério”
- Medical skills: “habilidade”, “médica”, “especialidade”, “cfm”, “resolução”
- Vigilance: “vigilância”, “guia”, “termo”, “definição”
- General medical: “sintomas”, “tratamento”, “prevenção”, “epidemiologia”
- Builds relevant context from the medical knowledge base
- Injects it into the prompt before sending to Ollama
- Instructs the AI to answer based on the provided context
Implementation Details
FromOllamaChatController.java:27-106:
Use Cases
- Medical consultation assistance
- Patient education and information
- Disease and symptom lookup
- Treatment information
- Medical news and updates
- General health questions
For explicitly providing full medical context, use
POST /api/chat/with-context instead.Error Responses
| Status Code | Description |
|---|---|
| 200 | Streaming response in progress |
| 400 | Invalid request (missing model or prompt) |
| 500 | Error connecting to Ollama or streaming failed |
The streaming response allows for real-time display of the AI’s answer as it’s being generated, providing a better user experience.