- April 8, 2024
- 2 min read
Building Your RAG Application on LlamaIndex with Friendli Inference: A Step-by-Step Guide

So you're ready to delve into the exciting world of Retrieval-Augmented Generation (RAG)? While the possibilities are endless, choosing the right components can feel overwhelming. This blog post will equip you with the knowledge and code to confidently deploy RAG on LlamaIndex with Friendli Inference, known for its blazing-fast performance and cost-effectiveness.
1. Setting Up Your Environment:
Before diving in, ensure you have an OpenAI API key and a Friendli Personal Access Token. You can obtain them from:
- OpenAI API key: https://platform.openai.com/api-keys
- Friendli Personal Access Token: https://friendli.ai/suite/~/setting/tokens
You can obtain the python notebook code for this article at arxiv_reranker.ipynb.
You can install the required libraries and export relevant variables as:
Here's a Python script snippet to get you started:
2. Preparing Your Document:
For this example, we'll use our research paper "Orca: A Distributed Serving System for Transformer-Based Generative Models", which describes our iterative (continuous) batching technique for inference serving. Feel free to substitute this with any relevant document to better suit your needs to enhance model accuracy and reduce biases.
3. Storing the Document in a Vector Store:
The next step involves parsing the document and saving it to a vector store. Let’s first read the PDF file and parse them into chunked documents.
Here, we'll save the parsed documents to the vector store using LlamaIndex, which uses a simple in-memory dictionary by default. In the later sections of the article, we also demonstrate using ElasticSearch for larger datasets:
4. Exploring RAG Retrieval Methods:
4.1. Simple RAG:
This basic approach retrieves documents based on cosine similarity between the query embedding and document embeddings.
As a sample output, you can expect results like:
4.2. Advanced Usage: Utilizing ElasticSearch for Large-Scale Data:
For extensive datasets, consider switching from the default in-memory storage to ElasticSearch. Here's how to set it up and use it with LlamaIndex:
First, run ElasticSearch using docker
You can check that it’s running with:
Install the ElasticSearch-LlamaIndex integration package
We can load the storage context from our documents like
You can check the index named ‘demo’ with the embeddings of documents, stored as a dense vector type
Based on the information, one can run queries as follows
By following these steps and incorporating the provided code, you'll be well on your way to implementing RAG in your applications. Remember, this is just a starting point – feel free to experiment and customize the process to suit your specific needs.
Written by
FriendliAI Tech & Research
Share
General FAQ
What is FriendliAI?
FriendliAI is the Frontier Inference Cloud for Agents, delivering high throughput, low latency, and reliability at scale for agentic workloads. Through vertically optimized inference infrastructure, it delivers 2–5× faster output token speed and a 99.99% uptime SLA for high-volume production traffic.
How does FriendliAI reduce inference costs?
FriendliAI reduces inference costs through higher GPU utilization and optimized inference performance. FriendliAI's patented continuous batching technique, along with quantization, speculative decoding, KV cache offloading, multi-LoRA serving, and autoscaling, helps you serve more tokens with fewer GPUs, lowering your infrastructure costs without sacrificing performance.
Why should I choose FriendliAI over other inference providers?
FriendliAI is built for production AI agents, combining speed, reliability, and efficiency at scale. It delivers low-latency streaming, reliable long-context inference, and robust tool calling without compromising stability. According to independent OpenRouter benchmarks, FriendliAI consistently ranks among the top providers for throughput, latency, and reliability across leading open-weight models. See why customers choose FriendliAI
Which open-weight models does FriendliAI support?
Run today’s frontier open-weight models—including GLM, MiniMax, Kimi, DeepSeek, Qwen, Gemma, and more—with a simple API call. FriendliAI Model API gives you instant access to the latest models with optimized inference performance for production workloads. Explore models and pricing
How do I get started?
Getting started takes just a few minutes. [1] Sign up for FriendliAI, [2] Generate your API key, and [3] Make your first inference request with frontier open-weight models.
Still have questions?
If you want a customized solution for that key issue that is slowing your growth, support@friendli.ai or click Talk to an engineer — our engineers (not a bot) will reply within one business day.

