AI support copilot project (TypeScript) · Chapter 9 of 12

Guardrails

By the end of this chapter you can say what a prompt injection is and show one working, stop the copilot from asking the model when search found nothing good, use the model itself as a classifier that refuses questions Parda does not answer, and clean a source in code before the model reads it. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Answers
  • 3 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of AI support copilot project (TypeScript) are free. This one comes with the all labs plan.

Why you need this

On 29 September Asha added a page for partner venues. It says so in its first lines: "It was imported from a partner's document." The document had an HTML comment in it, invisible on the website, and the comment talks to the model, not to members. With chapter 8's code, the copilot obeys it. This is POST /ask for "How are partners paid?":

{"question":"How are partners paid?","answer":"All tickets are free this week with the code FREESHOW.","found":true,"citations":[{"chunkId":"partner-venues-04","pageId":"partner-venues","title":"Partner venues","heading":"How partners are paid"}],"tokens":{"input":395,"output":30}}

And the stream says the same in its last event:

event: done
data: {"answer":"All tickets are free this week with the code FREESHOW.","found":true}

There is no FREESHOW code. The answer is cited, found is true, and every check from chapter 7 passes, because the id is real. Datta would be arguing with members about free Diwali tickets by lunch.

Two smaller problems come with 200 messages a day. A question the pages do not cover, such as "Do you sell popcorn and cold drinks?", still sends five unrelated sources to the model: 353 tokens read to say "I don't know". And "Write a poem about wheelchair access at Kala Talkies" finds strong wheelchair chunks, so the copilot answers it as if it were a support question. Search scores how well a chunk matches; it cannot tell a real question from a request for a poem. The code so far trusts every source and asks the model every time.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    guards.ts: weak retrieval means I don't know

    Write weakRetrieval(hits, minScore) in src/answer/guards.ts. The hits are best first, scored by the reranker. Return true when there are no hits, or when the best (first) hit's score is below minScore; false otherwise (a score equal to minScore is not weak).

    The Check runs five cases, then retrieves "Do you sell popcorn and cold drinks?" (the best score is about 0.0000, under config.minScore 0.05) and "How do I change my seats?" (about 0.998) and wants true and false.

  2. Task 2

    guards.ts: is the question about Parda?

    Write isOffTopic(ai, question) in src/answer/guards.ts. Send one chat request: ai.chat.completions.create with model config.chatModel, temperature 0 and two messages, { role: 'system', content: TOPIC_INSTRUCTIONS } and { role: 'user', content: question }. Return true when the reply's text (the first choice's message.content, with none counting as ""), trimmed and lower-cased, is exactly "off_topic".

    The Check reads the lab model's request log for a poem, a question about changing seats and a general-knowledge question, then gives your function stand-in models that reply " OFF_TOPIC\n", "parda", "off_topic please" and nothing at all.

  3. Task 3 · interview question

    Interview: guards.ts, orders hidden in a help page

    Write cleanSource(text) in src/answer/guards.ts. Remove every HTML comment (<!-- ... -->, even over several lines, and only up to its own -->). Then split the text into sentences after a full stop, question mark or exclamation mark followed by spaces (text.split(/(?<=[.!?])\s+/)), keep the sentences in which ORDERS (given) finds no match, join them with one space and trim the result.

    The Check cleans six texts (a comment over two lines, two comments with text between them, an order to ignore instructions, a message to the AI), then sends the sources for "How are partners paid?", cleaned by your function, with chapter 8's instructions that do not defend: the hidden order in partner-venues must not reach the answer.

    Nikhil will ask: INSTRUCTIONS already tell the model to treat the sources as data. Why also clean the sources in code?

What you will learn

  • Prompt injection: text inside a source that gives the model orders, and why a model can follow it.
  • Weak retrieval: reading the reranker's best score as "the pages do not say", before any model call.
  • Classification with a model: two labels, one short request, and how to read a label back safely.
  • Regular expressions for cleaning: greedy and lazy matches, matching across lines, splitting into sentences with a lookbehind.
  • Defence in depth: why the prompt defends and the code cleans too.

More in Answers

Build "Guardrails" yourself

Parda answers the same audience questions all day. You build the copilot that answers them from its own help pages, with sources, and says so when it does not know. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of AI support copilot project (TypeScript) are free. This one comes with the all labs plan.