From 0f9a08203e9712b4e4d9fad7605be007c816c853 Mon Sep 17 00:00:00 2001 From: Mario Miranda Date: Fri, 24 Feb 2023 18:31:16 -0300 Subject: [PATCH 1/5] [chore]: Add edits feature --- lib/openai/client.rb | 26 ++++++++++++++++++++++++++ lib/openai/edit.rb | 7 +++++++ 2 files changed, 33 insertions(+) create mode 100644 lib/openai/edit.rb diff --git a/lib/openai/client.rb b/lib/openai/client.rb index 656c978..f581184 100644 --- a/lib/openai/client.rb +++ b/lib/openai/client.rb @@ -73,6 +73,32 @@ def completions(prompt: nil, max_tokens: nil, temperature: nil, top_p: nil, n: n ) end + def edit(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, diff --git a/lib/openai/edit.rb b/lib/openai/edit.rb new file mode 100644 index 0000000..81abaee --- /dev/null +++ b/lib/openai/edit.rb @@ -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 From a8782eba6245257602cab8c4ece6a8a7259ff792 Mon Sep 17 00:00:00 2001 From: Mario Miranda Date: Mon, 27 Feb 2023 10:09:28 -0300 Subject: [PATCH 2/5] [fix]: Add openai/edit module --- lib/openai.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/openai.rb b/lib/openai.rb index 1dcd8fc..554321d 100644 --- a/lib/openai.rb +++ b/lib/openai.rb @@ -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" From 1b9d3369d8da95975bf6f67ac0a01799261cced9 Mon Sep 17 00:00:00 2001 From: Mario Miranda Date: Mon, 27 Feb 2023 11:53:08 -0300 Subject: [PATCH 3/5] [fix]: Change function name --- lib/openai/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/openai/client.rb b/lib/openai/client.rb index f581184..77d67b1 100644 --- a/lib/openai/client.rb +++ b/lib/openai/client.rb @@ -73,7 +73,7 @@ def completions(prompt: nil, max_tokens: nil, temperature: nil, top_p: nil, n: n ) end - def edit(input: nil, instruction: nil, n: nil, temperature: nil, top_p: nil, engine: default_engine) + def edits(input: nil, instruction: nil, n: nil, temperature: nil, top_p: nil, engine: default_engine) body = { "input" => input, "instruction" => instruction, From 19852f0f6ab3f4af6a6fa4dfd53614a75a4803e5 Mon Sep 17 00:00:00 2001 From: Mario Miranda Date: Mon, 27 Feb 2023 17:05:43 -0300 Subject: [PATCH 4/5] [feat]: Add endpoint for edit - https://api.openai.com/v1/engines/text-davinci-edit-001/edits --- spec/fixtures/edits.json | 1 + spec/openai/client_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 spec/fixtures/edits.json diff --git a/spec/fixtures/edits.json b/spec/fixtures/edits.json new file mode 100644 index 0000000..54c87bb --- /dev/null +++ b/spec/fixtures/edits.json @@ -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"} \ No newline at end of file diff --git a/spec/openai/client_spec.rb b/spec/openai/client_spec.rb index 651767f..79be2c1 100644 --- a/spec/openai/client_spec.rb +++ b/spec/openai/client_spec.rb @@ -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") From 76b00984b63fc7cf05ae3f0db6427cd88f04e8fc Mon Sep 17 00:00:00 2001 From: Mario Miranda Date: Mon, 27 Feb 2023 17:22:29 -0300 Subject: [PATCH 5/5] [fix]: Change version --- lib/openai/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/openai/version.rb b/lib/openai/version.rb index 8f0d59b..c4e6175 100644 --- a/lib/openai/version.rb +++ b/lib/openai/version.rb @@ -1,3 +1,3 @@ module OpenAI - VERSION = "0.3.0".freeze + VERSION = "0.4.0".freeze end