Class: Bristow::Providers::Openai
- Defined in:
- lib/bristow/providers/openai.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #chat(params) ⇒ Object
- #default_model ⇒ Object
- #format_function_response(response, result) ⇒ Object
- #format_functions(functions) ⇒ Object
- #function_arguments(response) ⇒ Object
- #function_name(response) ⇒ Object
-
#initialize(api_key:) ⇒ Openai
constructor
A new instance of Openai.
- #is_function_call?(response) ⇒ Boolean
- #stream_chat(params, &block) ⇒ Object
Constructor Details
#initialize(api_key:) ⇒ Openai
Returns a new instance of Openai.
4 5 6 7 |
# File 'lib/bristow/providers/openai.rb', line 4 def initialize(api_key:) super @client = OpenAI::Client.new(access_token: api_key) end |
Instance Method Details
#chat(params) ⇒ Object
9 10 11 12 |
# File 'lib/bristow/providers/openai.rb', line 9 def chat(params) response = @client.chat(parameters: params) response.dig("choices", 0, "message") end |
#default_model ⇒ Object
65 66 67 |
# File 'lib/bristow/providers/openai.rb', line 65 def default_model "gpt-4o-mini" end |
#format_function_response(response, result) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/bristow/providers/openai.rb', line 81 def format_function_response(response, result) = { "role" => "function", "name" => response["function_call"]["name"], "content" => result.to_json } end |
#format_functions(functions) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/bristow/providers/openai.rb', line 58 def format_functions(functions) { functions: functions.map(&:to_schema), function_call: "auto" } end |
#function_arguments(response) ⇒ Object
77 78 79 |
# File 'lib/bristow/providers/openai.rb', line 77 def function_arguments(response) JSON.parse(response["function_call"]["arguments"]) end |
#function_name(response) ⇒ Object
73 74 75 |
# File 'lib/bristow/providers/openai.rb', line 73 def function_name(response) response["function_call"]["name"] end |
#is_function_call?(response) ⇒ Boolean
69 70 71 |
# File 'lib/bristow/providers/openai.rb', line 69 def is_function_call?(response) response["function_call"] end |
#stream_chat(params, &block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/bristow/providers/openai.rb', line 14 def stream_chat(params, &block) full_content = "" function_name = nil function_args = "" stream_proc = proc do |chunk| delta = chunk.dig("choices", 0, "delta") next unless delta if delta["function_call"] # Building function call if delta.dig("function_call", "name") function_name = delta.dig("function_call", "name") end if delta.dig("function_call", "arguments") function_args += delta.dig("function_call", "arguments") end elsif delta["content"] # Regular content full_content += delta["content"] yield delta["content"] end end params[:stream] = stream_proc @client.chat(parameters: params) if function_name { "role" => "assistant", "function_call" => { "name" => function_name, "arguments" => function_args } } else { "role" => "assistant", "content" => full_content } end end |