Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/openai.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "openai/choice"
require "openai/client"
require "openai/completion"
require "openai/edit"
require "openai/engine"
require "openai/logprobs"
require "openai/search_result"
Expand Down
26 changes: 26 additions & 0 deletions lib/openai/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,32 @@ def completions(prompt: nil, max_tokens: nil, temperature: nil, top_p: nil, n: n
)
end

def edits(input: nil, instruction: nil, n: nil, temperature: nil, top_p: nil, engine: default_engine)
body = {
"input" => input,
"instruction" => instruction,
"n" => n,
"temperature" => temperature,
"top_p" => top_p
}.compact

edit = post("/v1/engines/#{engine}/edits", body: body)

choices = edit[:choices]&.map do |choice|
Choice.new(
index: choice[:index],
text: choice[:text]
)
end

Edit.new(
choices: choices,
created: edit[:created],
usage: edit[:usage],
object: edit[:object],
)
end

def search(documents:, query:, engine: default_engine)
body = {
"documents" => documents,
Expand Down
7 changes: 7 additions & 0 deletions lib/openai/edit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module OpenAI
Edit = Struct.new(:choices, :created, :usage, :object, keyword_init: true) do
def created_at
Time.at(created)
end
end
end
2 changes: 1 addition & 1 deletion lib/openai/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module OpenAI
VERSION = "0.3.0".freeze
VERSION = "0.4.0".freeze
end
1 change: 1 addition & 0 deletions spec/fixtures/edits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"choices":[{"finish_reason":null,"index":0,"logprobs":null,"text":"What day of the week is it?\n"}],"created":1677528012,"usage":{"prompt_tokens":25,"completion_tokens":28,"total_tokens":53},"object":"edit"}
15 changes: 15 additions & 0 deletions spec/openai/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@
end
end

describe "#edits" do
before do
stub_request(:post, "https://api.openai.com/v1/engines/text-davinci-edit-001/edits")
.with(headers: request_headers)
.to_return(body: File.read("spec/fixtures/edits.json"), headers: response_headers)
end

it "creates an edit" do
input = "What day of the wek is it?"
instruction = "Fix the spelling mistakes"
results = client.edits(input: input, instruction: instruction, engine: "text-davinci-edit-001")
expect(results).to be_a(OpenAI::Edit)
end
end

describe "#search" do
before do
stub_request(:post, "https://api.openai.com/v1/engines/ada/search")
Expand Down