CSC-105

LibraryAI in your app

AI in your app

Some apps need a model at their core: to estimate a meal from a photo, find what's holding a group project back, match a résumé to jobs, or check someone's writing. The course gives your app its own address for that, so your app can ask a model without a key in its code.

When you need it

Use a model when your app has to understand, write, judge or see something people give it, and no simple rule could do it.

If rules can do the job, use rules: sorting, filtering, adding up, matching exact words. Rules are instant, free, and give the same answer every time. Many apps use both: rules for what's certain, and a model for the part that takes judgment.

How it works

Your app sends a request to its address. The course passes it to a small, fast model and sends back the answer, usually in a few seconds. Your agent puts your app's address in your code for you.

const AI = "https://…/api/repos/ai/…" // your app's own address

const response = await fetch(AI, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    instructions: "You help college students cook from what they have. Suggest simple recipes.",
    prompt: "Here's what's in my fridge. What can I make tonight?",
    images: [photo], // up to three pictures, as data URLs
  }),
})
const { text, error } = await response.json()
  • instructions say the model's job and its rules. They stay the same on every call.
  • prompt is what this person asked or gave.
  • images are pictures the model should look at, such as a photo someone took with your app.

Answers in a shape

When your app needs to use the answer (show it in a list, add up numbers, sort it), ask for a shape with a schema. The answer comes back as data, ready to use:

body: JSON.stringify({
  instructions: "Suggest up to three recipes from the food in the photo.",
  prompt: "What can I make tonight?",
  images: [photo],
  schema: {
    type: "object",
    properties: {
      recipes: {
        type: "array",
        items: {
          type: "object",
          properties: { name: { type: "string" }, minutes: { type: "number" }, uses: { type: "array", items: { type: "string" } } },
          required: ["name", "minutes", "uses"],
        },
      },
    },
    required: ["recipes"],
  },
})
// → { data: { recipes: [{ name: "Veggie fried rice", minutes: 20, uses: ["rice", "eggs", "peppers"] }] } }

Design for a model

A model is slower than a rule and sometimes wrong, so the experience around it matters as much as the answer.

  • Show it's working. A clear "Looking at your photo…" while people wait.
  • Show an answer people can correct. An estimate they can adjust beats a number they have to trust.
  • Handle errors kindly. If error comes back, say what happened in plain words and let them try again.
  • Keep sample answers for testing. Your prototype can still run on sample data when you want a test to focus on something else.

Watch your testers: do they trust the answer, check it, or ignore it? Write what you see in docs/tests.md.

Limits and privacy

  • 200 calls a day for your app, shared by everyone who uses it. When they run out, your app gets an error until the next day.
  • Up to three pictures a call, each up to 4 MB.
  • Answers up to about 3,000 words. A longer one is cut off, and your app gets an error that says so. Ask for what you'll show: ten matches with a sentence each, not fifty with a page each.
  • What your app sends goes to a model provider. Tell people what your app sends, and never ask for passwords, ID numbers, or anything they wouldn't want shared. Test with sample data, like a made-up résumé, when you can.
  • No keys in your code, ever. Your app's address is the only thing it needs.