-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathchat_with_schema
More file actions
executable file
·42 lines (36 loc) · 1.26 KB
/
chat_with_schema
File metadata and controls
executable file
·42 lines (36 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env ruby
# frozen_string_literal: true
require "omniai/openai"
client = OmniAI::OpenAI::Client.new
format = OmniAI::Schema.format(name: "Contact", schema: OmniAI::Schema.object(
description: "A contact with a name, relationship, and addresses.",
properties: {
name: OmniAI::Schema.string,
relationship: OmniAI::Schema.string(enum: %w[friend family]),
addresses: OmniAI::Schema.array(
items: OmniAI::Schema.object(
title: "Address",
description: "An address with street, city, state, and zip code.",
properties: {
aptsuite: OmniAI::Schema.string(nullable: true),
street: OmniAI::Schema.string,
city: OmniAI::Schema.string,
state: OmniAI::Schema.string,
zip: OmniAI::Schema.string,
},
required: %i[aptsuite street city state zip]
)
),
},
required: %i[name relationship addresses]
))
response = client.chat(format:) do |prompt|
prompt.user <<~TEXT
Parse the following contact:
NAME: George Harrison
RELATIONSHIP: friend
HOME: 123 Main St, Springfield, IL, 12345
WORK: 456 Elm St, Springfield, IL, 12345
TEXT
end
puts format.parse(response.text) # => { name: "George Harrison", relationship: "friend", addresses: [{ ... }] }