Skip to main content
@kneeraazon
R&D PrototypeAI & AutomationCase Study

Built for Clinical Research · FastAPI · OpenAI · Pinecone · Cohere

FastKwan TCM Research API

A FastAPI service for Traditional Chinese Medicine research. From a patient's free-text description, a chat model extracts the symptoms, Pinecone finds similar syndromes or prescriptions, Cohere reranks them, and the answer is the retrieved text; a third endpoint finds a disease by partial name in MongoDB Atlas and returns its prevention and lifestyle text. It is a research and decision-support prototype: its answers are retrieved texts, not medical advice.

OUTCOME.01100%Line coverage of the service code (re-run)
OUTCOME.0210Mutation checks, each failing the suite (recorded)
OUTCOME.0368Automated tests (recorded)
2D SYSTEM TOPOLOGY // END-TO-END REQUEST PIPELINE4-STAGE DATA FLOW

NER (OpenAI) → Vector Search → Cohere Rerank → Atlas Lookup

FIG.01 // INTERFACE & SYSTEM CAPTURE1 / 6
The FastKwan Swagger UI: the title, the line 'Traditional Chinese Medicine (TCM) AI API', five operations (GET /, GET /health, POST /syndrome, POST /lifestyle, POST /prescription) and the list of request and response schemas below them.
The FastKwan Swagger UI: the title, the line 'Traditional Chinese Medicine (TCM) AI API', five operations (GET /, GET /health, POST /syndrome, POST /lifestyle, POST /prescription) and the list of request and response schemas below them.

Role

AI Engineer

Duration

Since July 2023

Tech Stack

Python 3.13FastAPIOpenAI API (symptom extraction, embeddings)Pinecone vector searchCohere rerankMongoDB Atlas SearchLangChainpytestRuffuvDocker
SECTION 01 // PROBLEM STATEMENT

The Problem

A service that takes a patient's description and sends it to three outside providers has to be careful in three ways: it must not need live keys just to start, it must not leak what goes wrong inside it, and it must not be open to anyone who finds the URL, because every call costs money and carries a person's description.

The starting point was a single 266-line file. Importing it required four live API keys and a network connection, it bound to every network interface with no access control, the symptom-extraction prompt was pasted twice, the error handlers returned the text of whatever exception had been raised, and the Cohere rerank step read a field that the current Cohere answer no longer carries. It had no tests.

SECTION 02 // SYSTEM ARCHITECTURE & EXECUTION

My Approach

  1. Layers. An app factory, settings, routes, schemas, a security check, the prompt and the text helper are separate modules, and the OpenAI, Pinecone and Cohere clients and the MongoDB connection are created on first use, so the package imports and the server starts with nothing configured.
  2. Configuration. Every setting comes from the environment or a local .env. An endpoint whose services are not configured answers 503 and names the missing variables. There is no default chat or rerank model on purpose, because the right one depends on the account.
  3. Access. When API_KEY is set, every endpoint except the root and the health check needs it in an X-API-Key header, compared in constant time. The server binds to localhost unless told otherwise.
  4. Pipeline. Symptoms are extracted by the chat model, the candidates are searched in the right Pinecone namespace (30 for syndromes, 50 for prescriptions), the top few are reranked by Cohere, and syndrome entries are reordered so the syndrome type comes before its symptoms. If nothing is recognised as a symptom, the description itself is searched.
  5. Failures. An upstream failure answers 502 with a fixed message and the details go to the log; a missing setting answers 503; an unknown disease answers 404.
  6. Rerank fix. The text of each ranked item is looked up by index, because the current Cohere answer carries only an index and a score.
  7. Delivery. 68 tests with every provider faked at its boundary, a non-root Docker image with a health check, a CI workflow, an .env.example that a test keeps complete, and a README that matches the code.
SECTION 03 // VERIFIED OUTCOMES

Results & Impact

Re-run on 2026-11-14 in a scratch copy: ruff check and ruff format --check pass on 22 files, 66 tests pass with 100% line coverage of 227 statements in src/fastkwan (two further tests read the .env.example file, which was not copied there and were deselected), and uv audit reports one known vulnerability: dnspython 2.9.1, a transitive dependency of pymongo, fixed in 2.9.2. In a throwaway copy, uv lock --upgrade-package dnspython changed that one package, the tests still passed and the audit then found none; the repository itself was not changed.

The real app was also run locally that day in two ways. As shipped, with an API key and no provider keys: /health answered 200, a call without the key 401, and a call with the key 503 naming the missing variables. With sample stand-ins in place of the four providers, which run underneath the project's own classes and routes: the syndrome and prescription endpoints returned ranked sample entries, the lifestyle endpoint returned a sample entry or 404, and a failing model call returned the fixed 502 without repeating the error. All text those stand-ins return is labelled sample text.

Recorded in the repository's work-done.md (2026-11-01): ten mutation checks, each putting one defect back and each making the suite fail, among them the wrong rerank text, the API key never checked, the upstream error text returned, prescriptions searched in the syndromes namespace and an eager client; and the real openai and Cohere v2 clients exercised against a local fake HTTP server to confirm the request shape and the parsing.

Not verified: any call to the real OpenAI, Pinecone, Cohere or Atlas services, the answer quality, whether the old model names are still available, and a GitHub Actions run.