|
| 1 | +#!/usr/bin/env -S -- ruby |
| 2 | +# frozen_string_literal: true |
| 3 | +# typed: strong |
| 4 | + |
| 5 | +require_relative "../lib/openai" |
| 6 | + |
| 7 | +# gets API Key from environment variable `OPENAI_API_KEY` |
| 8 | +client = OpenAI::Client.new |
| 9 | + |
| 10 | +begin |
| 11 | + pp("----- streams are enumerable -----") |
| 12 | + |
| 13 | + stream = client.completions.create_streaming( |
| 14 | + model: :"gpt-3.5-turbo-instruct", |
| 15 | + prompt: "1,2,3,", |
| 16 | + max_tokens: 5, |
| 17 | + temperature: 0.0 |
| 18 | + ) |
| 19 | + |
| 20 | + # the `stream` itself is an `https://rubyapi.org/3.1/o/enumerable` |
| 21 | + # which means that you can work with the stream almost as if it is an array |
| 22 | + all_choices = |
| 23 | + stream |
| 24 | + # calling any of the `enumerable` methods will block until the whole stream is consumed |
| 25 | + # it will also clean up the stream. |
| 26 | + .select do |completion| |
| 27 | + completion.object == :text_completion |
| 28 | + end |
| 29 | + .flat_map do |completion| |
| 30 | + completion.choices |
| 31 | + end |
| 32 | + |
| 33 | + pp(all_choices) |
| 34 | + |
| 35 | + # once the stream has been consumed, it will become "empty" |
| 36 | + pp("this will print an empty array") |
| 37 | + pp(stream.to_a) |
| 38 | +end |
| 39 | + |
| 40 | +begin |
| 41 | + pp("----- streams can be lazy -----") |
| 42 | + |
| 43 | + stream = client.completions.create_streaming( |
| 44 | + model: :"gpt-3.5-turbo-instruct", |
| 45 | + prompt: "1,2,3,", |
| 46 | + max_tokens: 5, |
| 47 | + temperature: 0.0 |
| 48 | + ) |
| 49 | + |
| 50 | + stream_of_choices = |
| 51 | + stream |
| 52 | + # calling `#lazy` will return a deferred `https://rubyapi.org/3.1/o/enumerator/lazy` |
| 53 | + .lazy |
| 54 | + # each successive calls to methods that return another `enumerable` will not consume the stream |
| 55 | + # but rather, return a transformed stream. (see link above) |
| 56 | + .select do |completion| |
| 57 | + completion.object == :text_completion |
| 58 | + end |
| 59 | + .flat_map do |completion| |
| 60 | + completion.choices |
| 61 | + end |
| 62 | + |
| 63 | + # prints the suspended intermediary stream |
| 64 | + pp(stream_of_choices) |
| 65 | + # beware that if the intermediary stream is not used, a call to `stream.close` is required |
| 66 | + # to release the underlying connection |
| 67 | + |
| 68 | + # method calls that do not return another `enumerable` will consume the intermediary stream |
| 69 | + # and perform cleanup |
| 70 | + stream_of_choices.each do |choice| |
| 71 | + pp(choice) |
| 72 | + end |
| 73 | + |
| 74 | + # at this point the stream has been consumed already, so it will return an empty array |
| 75 | + pp(stream_of_choices.to_a) |
| 76 | +end |
0 commit comments