Step-by-step tutorial for building an AI chatbot that answers questions about the Urantia Book using RAG (Retrieval-Augmented Generation) with the Urantia Papers API and OpenAI.
Use this file to discover all available pages before exploring further.
This tutorial walks through building an AI chatbot that can answer questions about the Urantia Book with accurate citations, using the Urantia Papers API for retrieval and OpenAI for generation.
async function buildPrompt(question: string) { // Search for relevant passages const results = await searchUrantia(question, 5); // Get context for top 3 results const contexts = await Promise.all( results.slice(0, 3).map(r => getContext(r.standardReferenceId, 2)) ); // Format passages for the LLM const passages = contexts.map(ctx => { const paragraphs = ctx.paragraphs .map(p => `[${p.standardReferenceId}] ${p.text}`) .join('\n\n'); return paragraphs; }).join('\n\n---\n\n'); return `You are a knowledgeable assistant about the Urantia Book. Answer the user's question based ONLY on the provided passages. Always cite specific paper references (e.g., Paper 107:0.2) for your claims. If the passages don't contain enough information to answer, say so.## Relevant Passages from the Urantia Book${passages}## User Question${question}`;}
import requestsimport openaidef search_urantia(query, limit=5): r = requests.post("https://api.urantia.dev/search", json={"q": query, "type": "and", "limit": limit}) return r.json()["results"]def get_context(ref, window=3): r = requests.get(f"https://api.urantia.dev/paragraphs/{ref}/context?window={window}") return r.json()def ask_urantia(question): results = search_urantia(question, 5) contexts = [get_context(r["standardReferenceId"], 2) for r in results[:3]] passages = "\n\n---\n\n".join([ "\n\n".join([f'[{p["standardReferenceId"]}] {p["text"]}' for p in ctx["paragraphs"]]) for ctx in contexts ]) response = openai.chat.completions.create( model="gpt-4o", messages=[{ "role": "system", "content": f"""Answer based ONLY on these Urantia Book passages. Cite references.{passages}""" }, { "role": "user", "content": question }], temperature=0.3 ) return response.choices[0].message.contentprint(ask_urantia("What happens after death according to the Urantia Book?"))