Langgraph rewrite user intent milvus

Can anyone help if this is right approach I have milvus database where I want to query using filters

I want to use past history and output and add with current filters and then use llm to rewrite user intent

Is there any better solution or this is a good one

I have CSV records embedded in milvus one row each

def rewrite_node(state: State) → State:

— 1. Get user query —

last_user = next((m for m in reversed(state[“messages”]) if m[“role”] == “user”), None)
if not last_user:
return state

user_query = last_user["content"]

# --- 2. Prepare conversation history (last 5 messages) ---
context_msgs = [m["content"] for m in state["messages"][-5:]]
history_text = "\n".join(context_msgs)

# --- 3. Prepare filter history (last 5 structured filters) ---
recent_filters = state.get("filter_history", [])[-5:]
filter_lines = [
    f"- Query: {f['raw_query']} | Filters: {json.dumps(f['filters'])}"
    for f in recent_filters
]
filters_text = "\n".join(filter_lines) if filter_lines else "None"

# --- 4. Candidate schema props from Milvus (semantic hints) ---
candidates = get_candidate_props_from_schema(user_query, k=5)
cand_text = "\n".join(
    f"- {c['Prop']} ({c['Section']}) score={c['score']:.3f}"
    for c in candidates if c["Prop"]
) or "None"

# --- 5. Allowed schema lists ---
props_list = ", ".join(SCHEMA["props"])
sections_list = ", ".join(SCHEMA["sections"])

# --- 6. System Prompt includes both history types ---
system_prompt = f"""

You are a query rewriter for a Milvus-based search over structured form data.

Conversation history

{history_text}

Previous filters used

{filters_text}

Allowed props

{props_list}

Allowed sections

{sections_list}

Top schema candidates (semantic search)

{cand_text}

Given all this, rewrite the current user query into:
{{ “query”: “…”, “filters”: {{ “Prop”: “…”, “Section”: “…”, “crfId”: “…” }} }}

Rules:

  • Reuse relevant past filters if user says things like “same client”, “same form”, etc.

  • Choose Prop/Section only from the allowed lists.

  • Return pure JSON, no extra text.
    “”"

    — 7. Call LLM —

    resp = rewrite_llm.invoke([
    {“role”: “system”, “content”: system_prompt},
    {“role”: “user”, “content”: user_query},
    ])

    raw = resp.content.strip()
    try:
    data = json.loads(raw)
    except Exception:
    data = {“query”: user_query, “filters”: {}}

    — 8. Validate against schema —

    query = data.get(“query”, user_query)
    filters = data.get(“filters”, {}) or {}
    if filters.get(“Prop”) not in SCHEMA[“props”]:
    filters.pop(“Prop”, None)
    if filters.get(“Section”) not in SCHEMA[“sections”]:
    filters.pop(“Section”, None)

    — 9. Update state —

    state[“query”] = query
    state[“filters”] = filters
    state[“filter_history”].append({
    “raw_query”: user_query,
    “query”: query,
    “filters”: filters,
    })

    — 10. Record assistant message —

    state[“messages”].append({
    “role”: “assistant”,
    “content”: f"[rewrite] query=‘{query}’, filters={filters}"
    })

    return state