Class: OmniAgent::Providers::OpenAI
- Defined in:
- lib/omni_agent/providers/openai.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
Methods inherited from Base
#initialize, #validate_messages!
Constructor Details
This class inherits a constructor from OmniAgent::Providers::Base
Instance Method Details
#chat(messages:, tools: [], **options) ⇒ Object
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 |
# File 'lib/omni_agent/providers/openai.rb', line 15 def chat(messages:, tools: [], **) (, allowed_roles: %i[system user assistant tool]) = .map do |msg| clean_msg = msg.reject { |_, v| v.nil? } if !clean_msg.key?(:tool_calls) && !clean_msg[:content].is_a?(String) clean_msg[:content] = "" end if clean_msg[:tool_calls].is_a?(Array) clean_msg[:tool_calls].each do |tc| if tc.dig("function", "arguments").is_a?(Hash) || tc.dig(:function, :arguments).is_a?(Hash) func = tc["function"] || tc[:function] args_key = func.key?("arguments") ? "arguments" : :arguments func[args_key] = func[args_key].to_json end end end clean_msg end openai_tools = tools.map { |tool| format_tool(tool) } payload = { model: model, messages: } payload[:tools] = openai_tools if openai_tools.any? payload.merge!() if .any? response = client.chat.completions.create(**payload) parse_response(response, payload) rescue => e raise OmniAgent::Error, "Error during OpenAI chat: #{e.}" end |