All Python modules

Module 4 — Python for AI Work

Read model answers, shape prompts, and turn results into something usable.

2 lessons · about 30 min · difficulty 4 of 5

  1. 1. Model answers are just dictionaries

    An AI response is nested data you walk through key by key.

    Use `json.loads()` to turn returned text into Python data.

    Then read down the path: `data["choices"][0]["message"]["content"]`.

    Think of it like: Opening nested boxes: the useful part is the small one at the centre, and you have to name each lid.

    At NoblePort: Stephanie.ai's draft text arrives from the model exactly this way. NobleHub reads one path out of the response, saves it to the job jacket, and holds it at a governance gate.

    Try it yourself: Change the raw JSON so `choices` is an empty list, run it, then make the code print 'No draft returned' instead of crashing.

    Common mistake: Assuming the shape is always there. A failed call returns an error object, so check before indexing deep.

    Why it matters: Every AI integration you build starts by reading this shape correctly.

  2. 2. Building prompts with f-strings

    f-strings drop your variables straight into a sentence.

    Put an `f` before the quote and wrap variables in curly braces.

    Keep the template in one place so every prompt stays consistent.

    Think of it like: A pre-printed change order form — the wording is fixed, only the blanks change.

    At NoblePort: Every Stephanie.ai draft uses one stored template with the role, job jacket facts, and tone filled in. Consistent wording is what makes her output reviewable at a gate instead of a surprise each time.

    Try it yourself: Build a prompt template with three blanks — role, job, and audience — then print it for two different jobs.

    Common mistake: Pasting customer data straight into a prompt without checking it. Redact private details before they leave your systems.

    Why it matters: Consistent prompts are the difference between a demo and a product.

Check what stuck

4 quick questions, with an explanation after each answer.

Take the module quiz