DEV Community

Omar Lashin
Omar Lashin

Posted on

Taming Unpredictable User Input: Building a RAG Triage Agent in Node.js

The Problem with Raw User Data
When building the backend for an urban infrastructure platform, the biggest bottleneck isn't the database, it's the users. A citizen reporting a problem rarely uses structured terminology. They don't say, "Electrical failure, Priority 1, Jurisdiction: City Council." They say, "The streetlamp on 5th avenue is sparking and my dog is terrified."

Passing this raw text directly into a database requires a human administrator to manually read and route every single ticket. To automate this, we need to extract structured JSON from unstructured panic.

The RAG Solution
Instead of relying on a raw LLM (which might hallucinate categories that our database doesn't accept), we implemented a lightweight Retrieval-Augmented Generation (RAG) pipeline in Node.js. By feeding the LLM our strict database schemas and city department rules alongside the user prompt, we force it to act as a deterministic triage agent.

Here is a simplified version of the extraction logic using the OpenAI API:

async function categorizeIssue(userReport, cityRulesContext) {
  const systemPrompt = `
    You are a strict city infrastructure triage agent. 
    Analyze the user report against the provided City Rules context.
    You must return ONLY a JSON object with the following keys:
    - category (string: must be one of the approved categories in context)
    - severity (integer: 1-5)
    - department (string: target routing department)
  `;

  const response = await openai.chat.completions.create({
    model: "gpt-4-turbo",
    response_format: { type: "json_object" },
    messages: [
      { role: "system", content: systemPrompt },
      { role: "user", content: `Context: ${cityRulesContext}\n\nReport: ${userReport}` }
    ]
  });

  return JSON.parse(response.choices[0].message.content);
}

Enter fullscreen mode Exit fullscreen mode

By utilizing the response_format: { type: "json_object" } flag, we ensure the output is ready for our Express.js backend to ingest. The Node server doesn't know it's talking to an AI; it just receives a perfectly formatted JSON payload, completely bridging the gap between natural language and strict database requirements.

Top comments (0)