Module: OpenAI::Helpers::StructuredOutput::ChatCompletionParser Private
- Defined in:
- lib/openai/helpers/structured_output/chat_completion_parser.rb,
sig/openai/helpers/structured_output/chat_completion_parser.rbs
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Request preparation and response coercion for Chat Completions.
Class Method Summary collapse
- .build_tools(tools, tool_models) ⇒ Object private
- .build_unwrap(model, tool_models) ⇒ Object private
- .get_models(parsed) ⇒ Object private
Class Method Details
.build_tools(tools, tool_models) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/openai/helpers/structured_output/chat_completion_parser.rb', line 117 def self.build_tools(tools, tool_models) return [] if tools.nil? tools.map do |tool| next tool unless tool[:type] == :function function_name = tool.dig(:function, :name) model = tool_models[function_name] model ? tool.merge(model: model) : tool end end |
.build_unwrap(model, tool_models) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
11 12 13 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 |
# File 'lib/openai/helpers/structured_output/chat_completion_parser.rb', line 11 def self.build_unwrap(model, tool_models) # rubocop:disable Metrics/BlockLength -> (raw) do if model.is_a?(OpenAI::StructuredOutput::JsonSchemaConverter) raw[:choices]&.each do |choice| = choice.fetch(:message) begin content = .fetch(:content) parsed = content.nil? ? nil : JSON.parse(content, symbolize_names: true) rescue JSON::ParserError => e parsed = e end coerced = OpenAI::Internal::Type::Converter.coerce(model, parsed) .store(:parsed, coerced) end end raw[:choices]&.each do |choice| choice.dig(:message, :tool_calls)&.each do |tool_call| func = tool_call.fetch(:function) next if (model = tool_models[func.fetch(:name)]).nil? begin arguments = func.fetch(:arguments) parsed = arguments.nil? ? nil : JSON.parse(arguments, symbolize_names: true) rescue JSON::ParserError => e parsed = e end coerced = OpenAI::Internal::Type::Converter.coerce(model, parsed) func.store(:parsed, coerced) end end raw end # rubocop:enable Metrics/BlockLength end |
.get_models(parsed) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/openai/helpers/structured_output/chat_completion_parser.rb', line 52 def self.get_models(parsed) model = nil tool_models = {} case parsed in {response_format: OpenAI::StructuredOutput::JsonSchemaConverter => model} parsed.update( response_format: { type: :json_schema, json_schema: { strict: true, name: model.name.split("::").last, schema: model.to_json_schema } } ) in { response_format: {type: :json_schema, json_schema: OpenAI::StructuredOutput::JsonSchemaConverter => model} } parsed.fetch(:response_format).update( json_schema: { strict: true, name: model.name.split("::").last, schema: model.to_json_schema } ) in { response_format: { type: :json_schema, json_schema: {schema: OpenAI::StructuredOutput::JsonSchemaConverter => model} } } parsed.dig(:response_format, :json_schema).store(:schema, model.to_json_schema) in {tools: Array => tools} mapped = tools.map do |tool| case tool in OpenAI::StructuredOutput::JsonSchemaConverter name = tool.name.split("::").last tool_models.store(name, tool) { type: :function, function: { strict: true, name: name, parameters: tool.to_json_schema } } in {function: {parameters: OpenAI::StructuredOutput::JsonSchemaConverter => params}} func = tool.fetch(:function) name = func[:name] ||= params.name.split("::").last tool_models.store(name, params) func.update(parameters: params.to_json_schema) tool else tool end end tools.replace(mapped) else end [model, tool_models] end |