Python, from Zero, for AI · Chapter 07

One interface, several providers

1:40 · Narrated with a synthetic voice. Captions are available in the player.

Read the transcript

Module seven is what a script turns into when it grows. Classes when a dictionary stops being enough. Data classes for the ones that are mostly data. And then the piece of structure that pays for itself fastest in this field: put the one thing that differs between model providers behind a small base class with a single method, and give the rest of the program an object that has a provider rather than one that is a provider. Then switching from a paid model to a free one running on your own laptop is one line, and a fake provider that returns a canned string makes your tests free and instant. The module also teaches generators, context managers that clean up even when the block fails, decorators that add a retry or a stopwatch in one line, a type checker that finds the empty value on the branch your tests never took, and how to make your project installable as a command somebody else can type. It ends by putting a stopwatch on the whole thing. In an artificial intelligence program the answer is almost always the network call and almost never the loop you were about to rewrite. Fourteen minutes, ninety-one per cent of it waiting. Rewriting the loop perfectly saves eight seconds.

Sources and further reading (1)