Welcome to Answering Questions From Your Own Documents! A language model writes fluent answers, but it doesn’t take them from your documents. Ask it about something only your files describe, and it can state a rule, a policy, or a detail that no page of yours contains.
In this course, your code finds the right pages first, sends the model only those, and asks it to answer from them, name its sources, and say so when your documents don’t cover the question. Then your code checks every source the model cites.
What You’ll Build
The course follows a neighborhood board-game café whose players argue about the rules at the tables. The café keeps the rulebooks of its eight most-played games as plain-text files, and it wants a rules desk where a player types a question and the desk answers from those rulebooks. A player acts on whatever the desk says right away, so a wrong rule is worse than no answer.
Each answer names the game and page it came from, so a player can open the page to confirm. A model can cite a page it never saw, so your code also checks that every cited page was really sent to it. That shows the page was sent, not that the answer reads it right.
Here’s a first taste from one real run of the finished desk with OpenAI, with the long lines wrapped for display. Table 3 asked whether a runner on the summit path can take the baton in Summit Relay, and table 1 asked about a seventh player in Lantern Harbor. Your wording, token counts, and cost will differ:
$ node --env-file=.env desk-question.js \
"Can a runner who's already on the summit path still take the baton in Summit Relay?"
Yes, but only if the runner is on space 12, the wide ledge halfway up.
The usual hand-off rule still applies: the receiving runner must be on
the same space as the baton holder or one space ahead.
Sources: Summit Relay, pages 4-5
(625 input, 71 output tokens, 0.196 cents)
$ node --env-file=.env desk-question.js \
"Can a seventh player join Lantern Harbor if they share a boat with someone?"
The rulebooks don't say. Please ask a staff member.
(593 input, 57 output tokens, 0.176 cents)
The desk answered table 3 from Summit Relay’s pages 4 and 5 and cited them. No rulebook covers a seventh player, so the desk said the rulebooks don’t say instead of making up a rule.
What You’ll Learn
By the end of this course, you’ll be able to split documents into chunks, embed them locally and search them by meaning. You’ll send the model only what the search found and ask it to answer from that, cite its sources, and say when the documents don’t cover the question.
Step by step, you’ll:
- Compare texts by meaning by turning text into vectors with a small model that runs on your own machine, and ranking rules by how close they are to a player’s question
- Search the rulebooks by splitting them into chunks that keep their game and page, embedding every chunk once, and catching exact names and card numbers that a search by meaning can miss
- Answer from the rulebooks by sending the model only the top chunks, asking it to cite them and to say when they don’t cover the question, checking every citation, and debugging a wrong answer by looking at what was sent first
- Put it all together in a project that builds the café’s whole rules desk
Before You Start
This is the fourth of five courses on building AI features in JavaScript. It builds on a small model/ module that calls your model provider. Its ask() function sends instructions and an input and returns the reply with its token usage, and its askForJson() function returns a record checked against a schema, or the reason it failed.
The first lesson recaps what this course uses. If you’re starting here, you don’t need to have built the module. The course’s code download has the starting project with model/ ready, along with the café’s rulebooks and a dozen players’ questions.
You should already know async functions with await, how to handle a failed request with retries, JSON, and Node.js scripts with npm packages. You should also know that a call to a model is billed by the token.
You’ll need:
- A current Node LTS, a terminal, and any editor. The course’s runs used Node v26.14.0,
@huggingface/transformers4.3.2, and Zod 4.6.7. - A key for one model provider, OpenAI or Anthropic, for the lessons and the project that call a real model. The starting project’s
.env.exampleshows where the key goes.
The embeddings run on your machine with a small open model, so they need no key and cost nothing. The first run downloads that model, about 90 MB, once. Only the answering step calls your provider: it sends the question and the few chunks the search picked, never the whole set of rulebooks.
In the course’s runs, a question to the finished desk cost about 0.2 cents with the stronger of the two OpenAI models that model/ is set up to use, and all twelve of the café’s questions cost about 2.3 cents together. Anthropic counts tokens differently, so read your own cost line. A low monthly spending limit with your provider, such as five dollars, leaves plenty of room to rerun the examples.
How the Course Works
- Text lessons carry most of the course, and you build the desk’s pieces in the café’s project as you type along. The code is the same for either provider, because
model/handles the difference. The model output in the lessons comes from real OpenAI runs, so with Anthropic, answers read differently. - Four video lessons show the search ranking texts and chunks, the desk saying the rulebooks don’t say, and a wrong answer traced back to the chunks that were sent.
- Four coding exercises run in your browser on a stand-in model, so they need no key. Their tests check your code. In the last one, you write the whole desk.
- A project gives you the desk’s brief, which you build in that last exercise, then bring into your own project and run against your real provider with your key. Next, a mentor chat, opened from a button in its lesson, has you explain your desk’s code and make a change you haven’t seen. Then a quiz checks what you’ve learned. A short summary video closes the course.
- Downloads include the starting project and the finished code.
The next lesson, Ask the Model a Rules Question, sets up the café’s project and sends a player’s question to a model with no rulebook at all.