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.
Prerequisites
Before starting, ensure you have:- A Gloo AI Studio account
- Your Client ID and Client Secret from the API Credentials page
- A Publisher created with content uploaded in Gloo Studio
What You’ll Build
A 2-step comparison that shows you how RAG grounding works:- Non-grounded (Step 1): Generic model knowledge, may hallucinate
- Grounded on your publisher (Step 2): Your specific content, accurate and source-backed
Example: Bezalel Ministries
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.
Step 1: Understanding the Hallucination Problem
Without grounding, when you ask about specific organizational information, the model relies on general knowledge and may:- Provide generic answers that don’t match your reality
- Make up plausible-sounding but incorrect information
- Say “I don’t have information” even though you’ve uploaded relevant content
The Non-Grounded Request
This is a standard Completions V2 call:Step 2: Grounding on Your Publisher
Now let’s use the/grounded endpoint with the rag_publisher parameter to ground on YOUR specific content.
The Publisher Grounded Request
This adds two key changes from Step 1: the/grounded endpoint and the rag_publisher parameter:
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.
Side-by-Side Comparison
| Step | Endpoint | rag_publisher | sources_returned | Response Quality |
|---|---|---|---|---|
| 1. Non-grounded | /ai/v2/chat/completions | N/A | N/A | Generic/may hallucinate |
| 2. Publisher grounded | /ai/v2/chat/completions/grounded | Your publisher | true | Accurate, specific |
Quick Reference: Publisher & Query Examples
Different content types require different query approaches:| Content Type | Publisher Example | Good Query Examples | Why It Works |
|---|---|---|---|
| Organizational Docs | ”Bezalel" | "What is the hiring process?" "What benefits do employees get?” | Documents contain policy/process descriptions |
| Bible Study Resources | ”FaithLibrary" | "What study guides are available on the Book of Romans?" "How do I start a small group Bible study?” | Study materials answer how-to and topical questions |
| Pastoral Care Support | ”CareMinistry" | "What counseling services are available?" "How do I request prayer support?” | Ministry docs answer member care questions |
| Educational Content | ”CourseCatalog" | "What courses are available on Python?" "What are the prerequisites for Course 101?” | Course descriptions match query topics |
Step 3: Comparing Side-by-Side
The most powerful way to demonstrate RAG’s value is to compare both approaches. Here’s how to build a comparison function:Example Output
- Step 1 (Non-grounded): Generic or “I don’t have specific information”
- Step 2 (Publisher grounded): Detailed, accurate answer from Bezalel’s actual documentation
Testing Your Implementation
This tutorial provides two testing paths:- Quick Start (Recommended): Test the Bezalel example to validate your setup
- Your Own Content: Adapt the code to use your publisher and content
Phase 1: Quick Start with Bezalel Example
First, validate your setup works by running the provided Bezalel example.Setup
- Install dependencies for your language (see cookbook READMEs)
- Configure
.envwith your credentials: - Create the Bezalel publisher in Gloo Studio:
- Create a new publisher named “Bezalel” (exact name, case-sensitive)
- Upload the 5 sample content files from the cookbook’s
content/directory - Wait for all documents to show “Completed” status before proceeding
Run the Comparison
Each language implementation includes a ready-to-run comparison demo:What to Look For
When testing with ANY publisher (Bezalel or your own): ✅ Step 1 (Non-grounded):- Generic response not specific to your content
- Example: “A typical hiring process involves…” (for any hiring query)
- Specific, accurate information from YOUR uploaded documents
sources_returned: true(if this is false, see troubleshooting)- Example: For Bezalel → “3-phase selection journey with calling alignment…”
- Example: For your product docs → “The enterprise plan is $X/month and includes…”
Phase 2: Adapt to Your Own Content
Once the Bezalel example works, adapt it to your own use case.Step 1: Prepare Your Content
Before modifying code, ensure you have:- Created a Publisher in Gloo Studio
- See Upload Files to Data Engine recipe for detailed instructions
- 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)
Step 2: Update the Code
Modify two areas in the code: A. Update Environment Variables In your.env file:
Step 3: Run and Validate
Run your modified code using the same commands from Phase 1. Interpreting Results: ✅ Success indicators:- Step 2 shows
sources_returned: true - Step 2 response contains specific information from your documents
- Step 2 is noticeably more accurate than Step 1
- Check that your publisher name matches exactly (case-sensitive)
- Verify content is uploaded and indexed in Studio
- Ensure your queries are relevant to your content
- Try increasing
sources_limit(change 3 to 5 or 10)
Step 3: Refine Your Queries
If results aren’t accurate:- 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
Working Code Sample
View Complete Code
Clone or browse the complete working examples for all 6 languages (JavaScript,
TypeScript, Python, PHP, Go, Java) with setup instructions and Bezalel content
files.
Next Steps
Now that you understand grounded completions:Use Your Own Content
See Phase 2: Adapt to Your Own Content above for detailed instructions on:- Preparing and uploading your content
- Updating the code for your publisher
- Designing effective queries
- Validating and refining your results
Build a Production Chatbot
Extend this recipe:- Add streaming for real-time responses
- Implement conversation history
- Extract and display source citations
- Handle edge cases (no sources found, publisher not found)
- Add user feedback collection
Explore Advanced Features
- Adjust source limits: Use
sources_limitto control how many documents are retrieved - Search without generation: Use the Search API for RAG without completion
- Multi-turn conversations: Maintain context across multiple grounded queries
- Hybrid approaches: Mix grounded and non-grounded based on query type
Troubleshooting
Publisher Not Found Error
- Verify publisher name is exact match (case-sensitive) to Studio
- Confirm publisher exists at Gloo Studio
- Check you’re using the correct account/credentials
No Sources Returned
- Verify content is uploaded to the publisher in Studio
- Check that your query is relevant to uploaded content
- Increase
sources_limit(try 5-10 for complex queries) - Ensure content has finished indexing in Studio
Authentication Errors
- Verify
GLOO_CLIENT_IDandGLOO_CLIENT_SECRETare correct - Get fresh credentials from API Credentials page
- Check token expiration—implementation auto-refreshes tokens
Generic Responses in Grounded Mode
If Step 2 (publisher grounded) responses still seem generic:- Check
sources_returnedflag—should betruefor Step 2 - Verify you’re using the
rag_publisherparameter - Confirm the right content is uploaded to your publisher in Studio
- Try more specific queries that match your content
- Review content in Studio to ensure it’s indexed
Step 2 Isn’t Using My Content
If Step 2 returnssources_returned: false or generic responses:
Check Publisher Name:
- Publisher names are case-sensitive: “MyPublisher” ≠ “mypublisher”
- Verify exact name in Gloo Studio
- Check for typos in your code or
.envfile
- Log into Gloo Studio
- Navigate to your Publisher
- Confirm documents show “Completed” status (not “Pending”)
- Wait 5-10 minutes after upload before testing
- Your query might not match your content
- Example: Asking “What is the pricing?” when you only uploaded a “User Guide”
- Solution: Ask questions your documents can answer
- Default is 3 sources
- Try 5-10 for complex queries:
sources_limit: 10 - More sources = broader retrieval, better coverage
- Scanned PDFs may have poor OCR
- Very short documents (<100 words) may not be retrieved
- Highly technical jargon may need more context
Additional Resources
- Completions V2 API Guide - Complete API documentation
- Grounded Completions API Reference - Full parameter details
- Upload Files Tutorial - Setting up Publishers and uploading documents
- Search API Reference - RAG without generation
- Authentication Tutorial - OAuth2 setup details

