Nat TaylorBlog, AI, Product Management & Tinkering

Test Drive: Instructor

Published on .

Today I’m test driving Instructor. I actually have a bit of experience here so this is a repeat test drive–and I already know its amazing :) I’m sure it influenced the design of OpenAI Structured Output. I’ll use it to extract some data about a company from the description. The model might be able to just do this on it’s own, but Instructor guarantees to return a Pydantic model.

The process involves:

  1. Define your Pydantic model
  2. Patch the client
  3. Generate a completion.

Perhaps the only interesting thing I’m doing here is using an enum for the market cap size. (It can do much more too, with validation and other such things, but this is just a test drive.)

desc = """Cummins Inc. designs, manufactures, distributes and services diesel and natural gas engines and engine-related component products. The Company's segments include Engine, Distribution, Components and Power Systems. The Engine segment manufactures and markets a range of diesel and natural gas powered engines under the Cummins brand name, as well as certain customer brand names, for the heavy and medium-duty truck, bus, recreational vehicle (RV), light-duty automotive and agricultural markets. The Distribution segment consists of the product lines, which service and/or distribute a range of products and services, including parts, engines, power generation and service. The Components segment supplies products, including aftertreatment systems, turbochargers, filtration products and fuel systems for commercial diesel applications. The Power Systems segment consists of businesses, including Power generation, Industrial and Generator technologies."""

import instructor
from pydantic import BaseModel
from openai import OpenAI
from typing import Literal
from rich import print

class CompanyInfo(BaseModel):
    name: str
    sector: str
    industry: str
    market_cap_category: Literal['small', 'mid', 'large']

client = instructor.from_openai(OpenAI())

company = client.chat.completions.create(
    model="gpt-4o-mini",
    response_model=CompanyInfo,
    messages=[
        {"role": "user", "content": f"Extract the info about the company\n{desc}"}],
)

print(company)
Code language: Python (python)

This produces the following output:

CompanyInfo(
  name='Cummins Inc.',
  sector='Industrial',
  industry='Manufacturing',
  market_cap_category='large'
)Code language: Python (python)

Popular Posts

Post Navigation

«
»