LangChainor LlamaIndex without actually understanding how the retrieval loop functions, which makes debugging a nightmare when the LLM starts hallucinating. I decided to strip away the abstractions and build a basic RAG pipeline using only raw API calls and a vector store to see where the actual latency and retrieval failures happen.
The goal was to implement a real-world AI workflow that handles document chunking, embedding generation, and context injection without a heavy framework.
The Implementation Logic #
-
Chunking: I split the raw text into 500-token segments with a 50-token overlap. Doing this manually revealed how much the chunk size affects the precision of the retrieval.
-
Embedding: I used an OpenAI embedding model to convert these chunks into vectors.
-
Vector Search: I implemented a simple cosine similarity check to find the top-k most relevant chunks based on the user's query vector.
-
Augmentation: The retrieved text is then stuffed into the system prompt as "Context," and the LLM generates the answer based strictly on that data.
The "Gotcha" I Hit #
During the process, I ran into a consistent issue where the model ignored the provided context and relied on its internal training data. The "error" wasn't a code crash, but a prompt failure.
The fix was a strict prompt engineering adjustment. Instead of saying "Use the context to answer," I had to be explicit:
You are a strict retrieval assistant. Answer the question ONLY using the provided context. If the answer is not in the context, state "I do not have enough information." Do not use outside knowledge.
By removing the framework, it's much easier to see that the "magic" of RAG is really just a sophisticated string concatenation problem. For anyone wanting a deep dive into LLM agents, starting from scratch is the only way to truly understand the data flow.
Next Ollama Cloud: Stuck on Phone Verification →