|
25 | 25 | end
|
26 | 26 | end
|
27 | 27 |
|
| 28 | + describe ".to_json_stream" do |
| 29 | + context "with a proc" do |
| 30 | + let(:user_proc) { proc { |x| x } } |
| 31 | + let(:stream) { OpenAI::Client.send(:to_json_stream, user_proc: user_proc) } |
| 32 | + |
| 33 | + it "returns a proc" do |
| 34 | + expect(stream).to be_a(Proc) |
| 35 | + end |
| 36 | + |
| 37 | + context "when called with a string containing a single JSON object" do |
| 38 | + it "calls the user proc with the data parsed as JSON" do |
| 39 | + expect(user_proc).to receive(:call).with(JSON.parse('{"foo": "bar"}')) |
| 40 | + stream.call('data: { "foo": "bar" }') |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + context "when called with string containing more than one JSON object" do |
| 45 | + it "calls the user proc for each data parsed as JSON" do |
| 46 | + expect(user_proc).to receive(:call).with(JSON.parse('{"foo": "bar"}')) |
| 47 | + expect(user_proc).to receive(:call).with(JSON.parse('{"baz": "qud"}')) |
| 48 | + |
| 49 | + stream.call(<<-CHUNK) |
| 50 | + data: { "foo": "bar" } |
| 51 | +
|
| 52 | + data: { "baz": "qud" } |
| 53 | +
|
| 54 | + data: [DONE] |
| 55 | +
|
| 56 | + CHUNK |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + context "when called with a string that does not even resemble a JSON object" do |
| 61 | + let(:bad_examples) { ["", "foo", "data: ", "data: foo"] } |
| 62 | + |
| 63 | + it "does not call the user proc" do |
| 64 | + bad_examples.each do |chunk| |
| 65 | + expect(user_proc).to_not receive(:call) |
| 66 | + stream.call(chunk) |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + context "when called with a string containing that looks like a JSON object but is invalid" do |
| 72 | + let(:chunk) do |
| 73 | + <<-CHUNK |
| 74 | + data: { "foo": "bar" } |
| 75 | + data: { BAD ]:-> JSON } |
| 76 | + CHUNK |
| 77 | + end |
| 78 | + |
| 79 | + it "does not raise an error" do |
| 80 | + expect(user_proc).to receive(:call).with(JSON.parse('{"foo": "bar"}')) |
| 81 | + |
| 82 | + expect do |
| 83 | + stream.call(chunk) |
| 84 | + end.to_not raise_error(JSON::ParserError) |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + context "when called with a string containing an error" do |
| 89 | + let(:chunk) do |
| 90 | + <<-CHUNK |
| 91 | + data: { "foo": "bar" } |
| 92 | + error: { "message": "A bad thing has happened!" } |
| 93 | + CHUNK |
| 94 | + end |
| 95 | + |
| 96 | + it "does not raise an error" do |
| 97 | + expect(user_proc).to receive(:call).with(JSON.parse('{ "foo": "bar" }')) |
| 98 | + expect(user_proc).to receive(:call).with( |
| 99 | + JSON.parse('{ "message": "A bad thing has happened!" }') |
| 100 | + ) |
| 101 | + |
| 102 | + expect do |
| 103 | + stream.call(chunk) |
| 104 | + end.to_not raise_error(JSON::ParserError) |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + end |
| 109 | + |
28 | 110 | describe ".to_json" do
|
29 | 111 | context "with a jsonl string" do
|
30 | 112 | let(:body) { "{\"prompt\":\":)\"}\n{\"prompt\":\":(\"}\n" }
|
|
0 commit comments