Dynamic Context Assembly
Construct precise context blocks for e-commerce agents using structured graph retrieval.
Problem Statement
Generic RAG dumps unstructured chunks of text into the context window. But complex agents need structure. For a shopping assistant, an agent needs to differentiate between a Product (entity) and a Purchase (event), and traverse relationships (e.g., "Find other products by this Brand").
Architecture: Structured Retrieval
Instead of a single "get relevant stuff" call, we assemble the context from three distinct retrieval strategies:
Implementation Steps
1. Ontology-Driven Ingestion
We define a schema so the memory system understands our specific domain entities.
2. Retrieval Strategy: Graph Traversal (BFS)
The user asked about "socks for these" (referring to shoes in cart). We use graph traversal to find the brand of the shoes, then find socks by that brand.
3. Context Assembly
Finally, we format the retrieved data into XML-tagged blocks for the LLM. This makes the data strictly readable and reduces hallucination.
Why This Matters
By using Graph Traversal instead of just vector search, we found socks that are logically related (same brand) to the cart item, even if the user didn't mention the brand name. The Structured Context ensures the LLM knows exactly which item is in the cart versus which items are recommendations.