Class: Langchain::Assistant::LLM::Adapters::OpenAI
- Defined in:
- lib/langchain/assistant/llm/adapters/openai.rb
Instance Method Summary collapse
-
#allowed_tool_choices ⇒ Object
Get the allowed assistant.tool_choice values for OpenAI.
-
#available_tool_names(tools) ⇒ Object
Get the available tool names for OpenAI.
-
#build_chat_params(messages:, instructions:, tools:, tool_choice:, parallel_tool_calls:) ⇒ Hash
Build the chat parameters for the OpenAI LLM.
-
#build_message(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) ⇒ Messages::OpenAIMessage
Build a OpenAI message.
-
#build_tools(tools) ⇒ Object
Build the tools for the OpenAI LLM.
-
#extract_tool_call_args(tool_call:) ⇒ Array
Extract the tool call information from the OpenAI tool call hash.
- #support_system_message? ⇒ Boolean
- #tool_role ⇒ Object
Instance Method Details
permalink #allowed_tool_choices ⇒ Object
Get the allowed assistant.tool_choice values for OpenAI
72 73 74 |
# File 'lib/langchain/assistant/llm/adapters/openai.rb', line 72 def allowed_tool_choices ["auto", "none"] end |
permalink #available_tool_names(tools) ⇒ Object
Get the available tool names for OpenAI
77 78 79 |
# File 'lib/langchain/assistant/llm/adapters/openai.rb', line 77 def available_tool_names(tools) build_tools(tools).map { |tool| tool.dig(:function, :name) } end |
permalink #build_chat_params(messages:, instructions:, tools:, tool_choice:, parallel_tool_calls:) ⇒ Hash
Build the chat parameters for the OpenAI LLM
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/langchain/assistant/llm/adapters/openai.rb', line 16 def build_chat_params( messages:, instructions:, tools:, tool_choice:, parallel_tool_calls: ) params = {messages: } if tools.any? params[:tools] = build_tools(tools) params[:tool_choice] = build_tool_choice(tool_choice) # Temporary fix because OpenAI o1/o3/reasoning models don't support `parallel_tool_calls` parameter. # Set `Assistant.new(parallel_tool_calls: nil, ...)` to avoid the error. params[:parallel_tool_calls] = parallel_tool_calls unless parallel_tool_calls.nil? end params end |
permalink #build_message(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) ⇒ Messages::OpenAIMessage
Build a OpenAI message
42 43 44 |
# File 'lib/langchain/assistant/llm/adapters/openai.rb', line 42 def (role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) Messages::OpenAIMessage.new(role: role, content: content, image_url: image_url, tool_calls: tool_calls, tool_call_id: tool_call_id) end |
permalink #build_tools(tools) ⇒ Object
Build the tools for the OpenAI LLM
67 68 69 |
# File 'lib/langchain/assistant/llm/adapters/openai.rb', line 67 def build_tools(tools) tools.map { |tool| tool.class.function_schemas.to_openai_format }.flatten end |
permalink #extract_tool_call_args(tool_call:) ⇒ Array
Extract the tool call information from the OpenAI tool call hash
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/langchain/assistant/llm/adapters/openai.rb', line 50 def extract_tool_call_args(tool_call:) tool_call_id = tool_call.dig("id") function_name = tool_call.dig("function", "name") tool_name, method_name = function_name.split("__") tool_arguments = tool_call.dig("function", "arguments") tool_arguments = if tool_arguments.is_a?(Hash) Langchain::Utils::HashTransformer.symbolize_keys(tool_arguments) else JSON.parse(tool_arguments, symbolize_names: true) end [tool_call_id, tool_name, method_name, tool_arguments] end |
permalink #support_system_message? ⇒ Boolean
85 86 87 |
# File 'lib/langchain/assistant/llm/adapters/openai.rb', line 85 def Messages::OpenAIMessage::ROLES.include?("system") end |
permalink #tool_role ⇒ Object
[View source]
81 82 83 |
# File 'lib/langchain/assistant/llm/adapters/openai.rb', line 81 def tool_role Messages::OpenAIMessage::TOOL_ROLE end |