Skip to content

Commit 8461923

Browse files
authored
Merge pull request #7 from instructor-ai/main
missing changes
2 parents c4ec83b + 1902ca9 commit 8461923

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

lib/instructor/openai/patch.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,29 @@ def build_function(model)
118118
{
119119
type: 'function',
120120
function: {
121-
name: model.name.humanize.titleize,
121+
name: generate_function_name(model),
122122
description: generate_description(model),
123123
parameters: model.json_schema
124124
}
125125
}
126126
end
127127

128+
def generate_function_name(model)
129+
model.schema.fetch(:title, model.name)
130+
end
131+
128132
# Generates the description for the function.
129133
#
130134
# @param model [Class] The response model class.
131135
# @return [String] The generated description.
132136
def generate_description(model)
133-
"Correctly extracted `#{model.name}` with all the required parameters with correct types"
137+
if model.respond_to?(:instructions)
138+
raise Instructor::Error, 'The instructions must be a string' unless model.instructions.is_a?(String)
139+
140+
model.instructions
141+
else
142+
"Correctly extracted `#{model.name}` with all the required parameters with correct types"
143+
end
134144
end
135145

136146
# Checks if the response is iterable.

spec/openai/patch_spec.rb

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ def self.name
1313
'User'
1414
end
1515

16+
def self.instructions
17+
"Extract the user's name and age."
18+
end
19+
1620
define_schema do
21+
title 'SomeUser'
1722
property :name, String
1823
property :age, Integer
1924
end
@@ -24,19 +29,39 @@ def self.name
2429
expect(patched_client).to eq(OpenAI::Client)
2530
end
2631

32+
context 'when generating description' do
33+
let(:client) { patched_client.new }
34+
35+
it "returns the model's instructions" do
36+
expect(client.generate_description(user_model)).to eq("Extract the user's name and age.")
37+
end
38+
39+
it 'returns the default description when the model does not have instructions' do
40+
model = Class.new do
41+
include EasyTalk::Model
42+
43+
def self.name
44+
'User'
45+
end
46+
47+
define_schema {}
48+
end
49+
50+
expect(client.generate_description(model)).to eq('Correctly extracted `User` with all the required parameters with correct types')
51+
end
52+
end
53+
2754
context 'with a new instance of the patched client' do
2855
it 'returns an instance of the patched client' do
2956
expect(patched_client.new).to be_an_instance_of(OpenAI::Client)
3057
end
3158

3259
pending 'receives the chat method with the expected arguments' do
33-
client = patched_client.new
3460
client.chat(parameters: {}, response_model: nil)
3561
expect(client).to have_received(:chat).with(parameters: {}, response_model: nil)
3662
end
3763

3864
it 'does not require the response model argument' do
39-
client = patched_client.new
4065
expect { client.chat(parameters: {}) }.not_to raise_error(ArgumentError)
4166
end
4267

@@ -45,6 +70,28 @@ def self.name
4570
expect { client.chat }.to raise_error(ArgumentError, 'missing keyword: :parameters')
4671
end
4772

73+
describe 'when setting the function_name' do
74+
it 'returns the function_name based on the schema title' do
75+
client = patched_client.new
76+
expect(client.generate_function_name(user_model)).to eq('SomeUser')
77+
end
78+
79+
it 'returns the class name when the schema title is not defined' do
80+
model = Class.new do
81+
include EasyTalk::Model
82+
83+
def self.name
84+
'User'
85+
end
86+
87+
define_schema {}
88+
end
89+
90+
client = patched_client.new
91+
expect(client.generate_function_name(model)).to eq('User')
92+
end
93+
end
94+
4895
it 'returns an object with the expected valid attribute values', vcr: 'patching_spec/valid_response' do
4996
client = patched_client.new
5097

0 commit comments

Comments
 (0)