Skip to content

Commit

Permalink
Changed the name of completions class method from generate to create
Browse files Browse the repository at this point in the history
  • Loading branch information
kladaFOX committed Sep 13, 2024
1 parent 76a072b commit 5d02751
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/spectre/openai/completions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Completions
# @return [String] the generated completion text
# @raise [APIKeyNotConfiguredError] if the API key is not set
# @raise [RuntimeError] for general API errors or unexpected issues
def self.generate(user_prompt:, system_prompt: "You are a helpful assistant.", assistant_prompt: nil, model: DEFAULT_MODEL, json_schema: nil, max_tokens: nil)
def self.create(user_prompt:, system_prompt: "You are a helpful assistant.", assistant_prompt: nil, model: DEFAULT_MODEL, json_schema: nil, max_tokens: nil)
api_key = Spectre.api_key
raise APIKeyNotConfiguredError, "API key is not configured" unless api_key

Expand Down
18 changes: 9 additions & 9 deletions spec/spectre/openai/completions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

it 'raises an APIKeyNotConfiguredError' do
expect {
described_class.generate(user_prompt: user_prompt, system_prompt: system_prompt)
described_class.create(user_prompt: user_prompt, system_prompt: system_prompt)
}.to raise_error(Spectre::APIKeyNotConfiguredError, 'API key is not configured')
end
end
Expand All @@ -34,7 +34,7 @@
end

it 'returns the completion text' do
result = described_class.generate(user_prompt: user_prompt, system_prompt: system_prompt, assistant_prompt: assistant_prompt)
result = described_class.create(user_prompt: user_prompt, system_prompt: system_prompt, assistant_prompt: assistant_prompt)
expect(result).to eq(completion)
end
end
Expand All @@ -47,7 +47,7 @@

it 'raises an error with the API response' do
expect {
described_class.generate(user_prompt: user_prompt, system_prompt: system_prompt)
described_class.create(user_prompt: user_prompt, system_prompt: system_prompt)
}.to raise_error(RuntimeError, /OpenAI API Error/)
end
end
Expand All @@ -60,7 +60,7 @@

it 'raises a JSON Parse Error' do
expect {
described_class.generate(user_prompt: user_prompt, system_prompt: system_prompt)
described_class.create(user_prompt: user_prompt, system_prompt: system_prompt)
}.to raise_error(RuntimeError, /JSON Parse Error/)
end
end
Expand All @@ -73,7 +73,7 @@

it 'raises a Request Timeout error' do
expect {
described_class.generate(user_prompt: user_prompt, system_prompt: system_prompt)
described_class.create(user_prompt: user_prompt, system_prompt: system_prompt)
}.to raise_error(RuntimeError, /Request Timeout/)
end
end
Expand All @@ -88,7 +88,7 @@

it 'raises an incomplete response error' do
expect {
described_class.generate(user_prompt: user_prompt, system_prompt: system_prompt)
described_class.create(user_prompt: user_prompt, system_prompt: system_prompt)
}.to raise_error(RuntimeError, /Incomplete response: The completion was cut off due to token limit./)
end
end
Expand All @@ -102,7 +102,7 @@
end

it 'sends the max_tokens parameter in the request' do
described_class.generate(user_prompt: user_prompt, system_prompt: system_prompt, max_tokens: max_tokens)
described_class.create(user_prompt: user_prompt, system_prompt: system_prompt, max_tokens: max_tokens)

expect(a_request(:post, Spectre::Openai::Completions::API_URL)
.with(body: hash_including(max_tokens: max_tokens))).to have_been_made
Expand All @@ -118,7 +118,7 @@
end

it 'sends the json_schema in the request' do
described_class.generate(user_prompt: user_prompt, system_prompt: system_prompt, json_schema: json_schema)
described_class.create(user_prompt: user_prompt, system_prompt: system_prompt, json_schema: json_schema)

expect(a_request(:post, Spectre::Openai::Completions::API_URL)
.with { |req| JSON.parse(req.body)['response_format']['json_schema'] == JSON.parse(json_schema.to_json) }).to have_been_made.once
Expand All @@ -137,7 +137,7 @@

it 'raises a refusal error' do
expect {
described_class.generate(user_prompt: user_prompt, system_prompt: system_prompt)
described_class.create(user_prompt: user_prompt, system_prompt: system_prompt)
}.to raise_error(RuntimeError, /Refusal: I'm sorry, I cannot assist with that request./)
end
end
Expand Down

0 comments on commit 5d02751

Please sign in to comment.