[BACK-AI-05] FastAPI Service Development for AI Text Structuring via Qwen Model

AI Components (AI Service)

Published

October 14, 2026

NoteTask Brief
  • Repository / Component: ai_voice_text_parser.
  • Language: Python 3.10+ (FastAPI, Pydantic, Requests).
  • Task Type: Backend (AI Structuring).
  • Status: Ready for Implementation

Task Description

The raw text received after transcribing an audio recording (via Whisper) is a solid, unformatted string. It is necessary to implement a FastAPI-based microservice that forwards this text to the local qwen2.5:3b model via the Ollama API to extract product names, weights, and portions, and generate a valid JSON structure.

Step-by-Step Instructions (Python)

  1. Implement the Request DTO Model: Design a Pydantic model named PromptRequest containing a mandatory string field prompt (where the Go service will pass the raw text along with the system prompt instruction). Fix the model="qwen2.5:3b", stream=False, and format="json" fields by default.
  2. Routing and Processing: Create a POST /api/generate endpoint. Implement request forwarding to Ollama with a strict timeout of 25 seconds (timeout=25).
  3. Cleaning and Deserialization: Apply markdown tag cleaning for ```json code blocks using re.sub, then parse the sanitized string into a true JSON object using json.loads.
  4. Failure Handling and Moderation Routing (Fallback):
    • When catching json.JSONDecodeError exceptions or timeouts, invoke the fallback_response function.
    • Modify the default response contract: return a structure containing parameters "status": "invalid", "receipt_no": "VOICE-PARSER-FAIL", and a text description for "moderation_reason". This allows the Go backend to recognize that the AI failed and route the transaction to a queue for manual verification.