|
1 | 1 | RSpec.describe OpenAI::Client do
|
2 |
| - describe "#search" do |
3 |
| - context "with a query" do |
4 |
| - let(:query) { "the president" } |
| 2 | + describe "#search", :vcr do |
| 3 | + let(:cassette) { "#{engine} search #{query}".downcase } |
5 | 4 |
|
6 |
| - context "with documents", :vcr do |
7 |
| - let(:documents) { %w[washington hospital school] } |
| 5 | + context "with a file" do |
| 6 | + let(:filename) { "puppy.jsonl" } |
| 7 | + let(:file) { File.join(RSPEC_ROOT, "fixtures/files", filename) } |
| 8 | + let!(:file_id) do |
| 9 | + response = VCR.use_cassette("files upload search") do |
| 10 | + OpenAI::Client.new.files.upload(parameters: { file: file, purpose: "search" }) |
| 11 | + end |
| 12 | + JSON.parse(response.body)["id"] |
| 13 | + end |
8 | 14 |
|
| 15 | + context "with engine: davinci" do |
| 16 | + let(:query) { "happy" } |
| 17 | + let(:engine) { "davinci" } |
9 | 18 | let(:response) do
|
10 | 19 | OpenAI::Client.new.search(
|
11 | 20 | engine: engine,
|
12 |
| - documents: documents, |
| 21 | + file: file_id, |
13 | 22 | query: query
|
14 | 23 | )
|
15 | 24 | end
|
16 |
| - let(:best_match) { JSON.parse(response.body)["data"].max_by { |d| d["score"] }["document"] } |
17 |
| - let(:cassette) { "#{engine} search #{query}".downcase } |
18 | 25 |
|
19 |
| - context "with engine: ada" do |
20 |
| - let(:engine) { "ada" } |
| 26 | + it "finds the best match" do |
| 27 | + VCR.use_cassette(cassette) do |
| 28 | + expect(response.parsed_response['data'][0]["text"]).to eq("puppy A is happy") |
| 29 | + end |
| 30 | + end |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + context "with documents" do |
| 35 | + let(:documents) { %w[washington hospital school] } |
| 36 | + let(:query) { "the president" } |
| 37 | + let(:response) do |
| 38 | + OpenAI::Client.new.search( |
| 39 | + engine: engine, |
| 40 | + documents: documents, |
| 41 | + query: query |
| 42 | + ) |
| 43 | + end |
| 44 | + let(:best_match) { JSON.parse(response.body)["data"].max_by { |d| d["score"] }["document"] } |
| 45 | + let(:cassette) { "#{engine} search #{query}".downcase } |
| 46 | + |
| 47 | + context "with engine: ada" do |
| 48 | + let(:engine) { "ada" } |
21 | 49 |
|
22 |
| - it "finds the best match" do |
23 |
| - VCR.use_cassette(cassette) do |
24 |
| - expect(documents[best_match]).to eq("washington") |
25 |
| - end |
| 50 | + it "finds the best match" do |
| 51 | + VCR.use_cassette(cassette) do |
| 52 | + expect(documents[best_match]).to eq("washington") |
26 | 53 | end
|
27 | 54 | end
|
| 55 | + end |
28 | 56 |
|
29 |
| - context "with engine: babbage" do |
30 |
| - let(:engine) { "babbage" } |
| 57 | + context "with engine: babbage" do |
| 58 | + let(:engine) { "babbage" } |
31 | 59 |
|
32 |
| - it "finds the best match" do |
33 |
| - VCR.use_cassette(cassette) do |
34 |
| - expect(documents[best_match]).to eq("washington") |
35 |
| - end |
| 60 | + it "finds the best match" do |
| 61 | + VCR.use_cassette(cassette) do |
| 62 | + expect(documents[best_match]).to eq("washington") |
36 | 63 | end
|
37 | 64 | end
|
| 65 | + end |
38 | 66 |
|
39 |
| - context "with engine: curie" do |
40 |
| - let(:engine) { "curie" } |
| 67 | + context "with engine: curie" do |
| 68 | + let(:engine) { "curie" } |
41 | 69 |
|
42 |
| - it "finds the best match" do |
43 |
| - VCR.use_cassette(cassette) do |
44 |
| - expect(documents[best_match]).to eq("washington") |
45 |
| - end |
| 70 | + it "finds the best match" do |
| 71 | + VCR.use_cassette(cassette) do |
| 72 | + expect(documents[best_match]).to eq("washington") |
46 | 73 | end
|
47 | 74 | end
|
| 75 | + end |
48 | 76 |
|
49 |
| - context "with engine: davinci" do |
50 |
| - let(:engine) { "davinci" } |
| 77 | + context "with engine: davinci" do |
| 78 | + let(:engine) { "davinci" } |
51 | 79 |
|
52 |
| - it "finds the best match" do |
53 |
| - VCR.use_cassette(cassette) do |
54 |
| - expect(documents[best_match]).to eq("washington") |
55 |
| - end |
| 80 | + it "finds the best match" do |
| 81 | + VCR.use_cassette(cassette) do |
| 82 | + expect(documents[best_match]).to eq("washington") |
56 | 83 | end
|
57 | 84 | end
|
58 | 85 | end
|
|
0 commit comments