Class: LangGraphRB::ChatOpenAI
- Defined in:
- lib/langgraph_rb/chat_openai.rb
Overview
ChatOpenAI wrapper compatible with LLMBase, supporting tool binding
Instance Attribute Summary
Attributes inherited from LLMBase
Instance Method Summary collapse
-
#bind_tools(tools) ⇒ Object
Returns a new instance with tools bound (non-destructive).
-
#call(messages, tools: nil) ⇒ Object
messages: array of { role: ‘system’|‘user’|‘assistant’|‘tool’, content: string, tool_calls?: […] } tools: optional array of tool definitions (objects responding to .to_openai_tool_schema) Returns assistant text string or a tool-call envelope hash when tool calls are produced.
-
#initialize(model:, temperature: 0.0, api_key: ENV['OPENAI_API_KEY'], client: nil) ⇒ ChatOpenAI
constructor
A new instance of ChatOpenAI.
Methods inherited from LLMBase
Constructor Details
#initialize(model:, temperature: 0.0, api_key: ENV['OPENAI_API_KEY'], client: nil) ⇒ ChatOpenAI
Returns a new instance of ChatOpenAI.
11 12 13 14 15 16 17 18 |
# File 'lib/langgraph_rb/chat_openai.rb', line 11 def initialize(model:, temperature: 0.0, api_key: ENV['OPENAI_API_KEY'], client: nil) super(model: model, temperature: temperature) @client = client || OpenAI::Client.new(api_key: api_key) unless @client.respond_to?(:chat) && @client.chat.respond_to?(:completions) raise "LangGraphRB::ChatOpenAI expects 'openai' gem ~> 0.24 (client.chat.completions.create)" end end |
Instance Method Details
#bind_tools(tools) ⇒ Object
Returns a new instance with tools bound (non-destructive)
21 22 23 24 25 26 |
# File 'lib/langgraph_rb/chat_openai.rb', line 21 def bind_tools(tools) dup_instance = self.class.new(model: @model, temperature: @temperature) dup_instance.instance_variable_set(:@client, @client) dup_instance.instance_variable_set(:@bound_tools, Array(tools)) dup_instance end |
#call(messages, tools: nil) ⇒ Object
messages: array of { role: ‘system’|‘user’|‘assistant’|‘tool’, content: string, tool_calls?: […] } tools: optional array of tool definitions (objects responding to .to_openai_tool_schema) Returns assistant text string or a tool-call envelope hash when tool calls are produced
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/langgraph_rb/chat_openai.rb', line 31 def call(, tools: nil) raise ArgumentError, 'messages must be an Array' unless .is_a?(Array) tool_definitions = (tools || @bound_tools) tool_schemas = Array(tool_definitions).flat_map do |tool| if tool.respond_to?(:to_openai_tool_schema) Array(tool.to_openai_tool_schema) else [tool] end end request_payload = { model: @model, temperature: @temperature, messages: () } if tool_schemas && !tool_schemas.empty? request_payload[:tools] = tool_schemas request_payload[:tool_choice] = 'auto' end notify_llm_request({ name: 'OpenAI::ChatCompletion', model: @model, model_parameters: { temperature: @temperature }, input: request_payload[:messages] }) # openai 0.24.0 uses client.chat.completions.create(params) response = @client.chat.completions.create(request_payload) = (response) tool_calls = [:tool_calls] text_content = [:content] usage = extract_usage_from_response(response) notify_llm_response({ output: tool_calls ? { tool_calls: tool_calls } : text_content, prompt_tokens: usage[:prompt_tokens], completion_tokens: usage[:completion_tokens], total_tokens: usage[:total_tokens] }) if tool_calls && !tool_calls.empty? normalized_calls = tool_calls.map do |tc| { id: tc[:id], name: tc[:function][:name], arguments: parse_tool_arguments(tc[:function][:arguments]) } end { tool_calls: normalized_calls } else text_content end rescue => e notify_llm_error({ error: e. }) raise e end |