|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require_relative "../lib/openai" |
| 5 | + |
| 6 | +class Location < OpenAI::BaseModel |
| 7 | + required :address, String |
| 8 | + required :city, String, doc: "City name" |
| 9 | + required :postal_code, String, nil?: true |
| 10 | +end |
| 11 | + |
| 12 | +# Participant model with an optional last_name and an enum for status |
| 13 | +class Participant < OpenAI::BaseModel |
| 14 | + required :first_name, String |
| 15 | + required :last_name, String, nil?: true |
| 16 | + required :status, OpenAI::EnumOf[:confirmed, :unconfirmed, :tentative] |
| 17 | +end |
| 18 | + |
| 19 | +# CalendarEvent model with a list of participants. |
| 20 | +class CalendarEvent < OpenAI::BaseModel |
| 21 | + required :name, String |
| 22 | + required :date, String |
| 23 | + required :participants, OpenAI::ArrayOf[Participant] |
| 24 | + required :is_virtual, OpenAI::Boolean |
| 25 | + required :location, |
| 26 | + OpenAI::UnionOf[String, Location], |
| 27 | + nil?: true, |
| 28 | + doc: "Event location" |
| 29 | +end |
| 30 | + |
| 31 | +client = OpenAI::Client.new |
| 32 | + |
| 33 | +response = client.responses.create( |
| 34 | + model: "gpt-4o-2024-08-06", |
| 35 | + input: [ |
| 36 | + {role: :system, content: "Extract the event information."}, |
| 37 | + { |
| 38 | + role: :user, |
| 39 | + content: <<~CONTENT |
| 40 | + Alice Shah and Lena are going to a science fair on Friday at 123 Main St. in San Diego. |
| 41 | + They have also invited Jasper Vellani and Talia Groves - Jasper has not responded and Talia said she is thinking about it. |
| 42 | + CONTENT |
| 43 | + } |
| 44 | + ], |
| 45 | + text: CalendarEvent |
| 46 | +) |
| 47 | + |
| 48 | +response |
| 49 | + .output |
| 50 | + .flat_map { _1.content } |
| 51 | + # filter out refusal responses |
| 52 | + .grep_v(OpenAI::Models::Responses::ResponseOutputRefusal) |
| 53 | + .each do |content| |
| 54 | + pp(content.parsed) |
| 55 | + end |
0 commit comments