Learn how to ground AI responses in your own content using Gloo AI Grounded Completions with Retrieval-Augmented Generation (RAG).
From time immemorial, well let’s say late 2022, we’ve all had the experience of AI models confidently responding to a question with nonsense or made-up facts. That’s a term known as “hallucinating”. It happens when models lack the knowledge needed and are incentivized to try and be helpful. At other times, their responses are simply vague and unhelpful.The good news? There’s a fix! This tutorial walks you through how to use Gloo AI’s Grounded Completions API with RAG (Retrieval-Augmented Generation). You’ll see how giving your AI the right context makes all the difference.
Key Problem: Without grounding, AI models may hallucinate answers about your organization, products, or services.The Solution: Grounded Completions uses RAG to retrieve relevant content from YOUR publisher before generating responses. This tutorial shows how grounding on your own content transforms generic AI into an accurate, source-backed assistant.
For this tutorial, we’ve cooked up a fictional organization called Bezalel Ministries. They’re a faith-based creative group who produce biblically-accurate artwork and educational resources. They’re delightful.We’ve created 5 sample documents that cover everything from their hiring process to their educational programs and research methodology.Why use a made-up org? Because it lets us show you exactly how the API works without any real-world baggage. Plus, Bezalel Ministries is more interesting than “Company X” or “Acme Corp”.What the comparison reveals:
Step 1 (Non-grounded): Generic hiring advice with no knowledge of Bezalel
Step 2 (Publisher grounded): Accurate details about Bezalel’s 3-phase selection journey from their actual docs
Working Code Sample
Follow along with complete working examples in all 6 languages (JavaScript, TypeScript, Python, PHP, Go, Java). Includes the 5 Bezalel sample content files.Setup and testing instructions are provided later.
import java.net.http.*;import java.net.URI;import com.google.gson.*;public class NonGroundedRequest { public static JsonObject makeNonGroundedRequest(String query, String token) throws Exception { // Standard completion WITHOUT RAG - may hallucinate String apiUrl = "https://platform.ai.gloo.com/ai/v2/chat/completions"; JsonObject message = new JsonObject(); message.addProperty("role", "user"); message.addProperty("content", query); JsonArray messages = new JsonArray(); messages.add(message); JsonObject payload = new JsonObject(); payload.add("messages", messages); payload.addProperty("auto_routing", true); payload.addProperty("max_tokens", 500); HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(apiUrl)) .header("Authorization", "Bearer " + token) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(payload.toString())) .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); return JsonParser.parseString(response.body()).getAsJsonObject(); } // Example query public static void main(String[] args) throws Exception { String query = "What is Bezalel Ministries' hiring process?"; JsonObject result = makeNonGroundedRequest(query, accessToken); String content = result.getAsJsonArray("choices") .get(0).getAsJsonObject() .getAsJsonObject("message") .get("content").getAsString(); System.out.println(content); // Result: Generic hiring advice, not specific to Bezalel }}
What you’ll see: A generic response about hiring processes, or possibly “I don’t have specific information about Bezalel Ministries.” The model doesn’t know about your organization because it wasn’t trained on your content.
import java.net.http.*;import java.net.URI;import com.google.gson.*;public class PublisherGroundedRequest { public static JsonObject makePublisherGroundedRequest( String query, String token, String publisherName, int sourcesLimit) throws Exception { // Grounded on YOUR publisher - accurate, source-backed String apiUrl = "https://platform.ai.gloo.com/ai/v2/chat/completions/grounded"; JsonObject message = new JsonObject(); message.addProperty("role", "user"); message.addProperty("content", query); JsonArray messages = new JsonArray(); messages.add(message); JsonObject payload = new JsonObject(); payload.add("messages", messages); payload.addProperty("auto_routing", true); payload.addProperty("rag_publisher", publisherName); // KEY: This grounds on YOUR content payload.addProperty("sources_limit", sourcesLimit); payload.addProperty("max_tokens", 500); HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(apiUrl)) .header("Authorization", "Bearer " + token) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(payload.toString())) .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); return JsonParser.parseString(response.body()).getAsJsonObject(); } // Same query, now grounded on YOUR publisher public static void main(String[] args) throws Exception { String query = "What is Bezalel Ministries' hiring process?"; JsonObject result = makePublisherGroundedRequest(query, accessToken, "Bezalel", 3); String content = result.getAsJsonArray("choices") .get(0).getAsJsonObject() .getAsJsonObject("message") .get("content").getAsString(); boolean sourcesReturned = result.get("sources_returned").getAsBoolean(); System.out.println(content); System.out.println("Sources used: " + sourcesReturned); // Result: Detailed answer about Bezalel's 3-phase selection journey // Sources used: true }}
What you’ll see: A detailed, accurate response about Bezalel’s specific 3-phase selection journey, pulled directly from their uploaded content. The sources_returned: true flag confirms RAG found and used relevant sources.
Why this query works: The Bezalel publisher contains a document titled “bezalel_hiring_process.txt” that describes their selection journey in detail. When you ground on this publisher, the RAG system retrieves this document before generating the response.For your own use case, design queries that match your uploaded content. If you upload product documentation, ask product questions. If you upload HR policies, ask HR questions.
Query: What is Bezalel Ministries' hiring process?================================================================================🔹 STEP 1: NON-GROUNDED Response (Generic Model Knowledge):--------------------------------------------------------------------------------I cannot provide specific details about the internal hiring process for BezalelMinistries, as that information is not publicly available...📊 Sources used: false================================================================================🔹 STEP 2: GROUNDED on Your Publisher (Your Specific Content):--------------------------------------------------------------------------------Bezalel Ministries views its hiring process as discerning a divine calling,modeling their approach on God's selection of Bezalel in Exodus. Their processincludes three phases: calling alignment through structured interviews aboutspiritual gifts, skills assessment with portfolio reviews, and a discernmentperiod where candidates spend time with the team...📊 Sources used: true📊 Model: gloo-google-gemini-2.5-pro
Notice the difference:
Step 1 (Non-grounded): Generic or “I don’t have specific information”
Step 2 (Publisher grounded): Detailed, accurate answer from Bezalel’s actual documentation
Uploaded relevant documents (PDFs, text files, Word docs, etc.)
Upload content that answers the questions your users will ask
Example: HR policies for HR questions, product docs for product questions
Note your Publisher name (you’ll need this for the code)
Content Quality Matters: The more relevant and well-structured your
content, the better your grounded responses will be. Each document should
focus on a specific topic area.
B. Update QueriesDesign queries that match your uploaded content:
# Before (Bezalel example)queries = [ "What is Bezalel Ministries' hiring process?", "What educational resources does Bezalel Ministries provide?",]# After (your content - example: e-commerce)queries = [ "What is the return policy for damaged items?", "How do I track my order?",]# After (your content - example: technical docs)queries = [ "How do I configure SSL certificates?", "What are the system requirements for installation?",]
Make queries more specific: “What is the pricing?” → “What is the enterprise plan pricing for annual subscriptions?”
Match content structure: If your document is titled “Installation Guide,” ask installation questions
Test incrementally: Start with one query, verify it works, then add more
Check sources: If available in the API response, inspect which documents were retrieved
Iterative Testing: RAG quality improves with iteration. Test different
queries, refine your content, and adjust sources_limit until you get optimal
results.
Clone or browse the complete working examples for all 6 languages (JavaScript,
TypeScript, Python, PHP, Go, Java) with setup instructions and Bezalel content
files.