Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 56f54b5

Browse files
committed
xml tools are back now
1 parent adc6c6e commit 56f54b5

File tree

7 files changed

+413
-302
lines changed

7 files changed

+413
-302
lines changed

lib/completions/anthropic_message_processor.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def process_streamed_message(parsed)
7070

7171
def process_message(payload)
7272
result = ""
73-
parsed = JSON.parse(payload, symbolize_names: true)
73+
parsed = payload
74+
parsed = JSON.parse(payload, symbolize_names: true) if payload.is_a?(String)
7475

7576
content = parsed.dig(:content)
7677
if content.is_a?(Array)

lib/completions/endpoints/anthropic.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ def final_log_update(log)
119119
log.response_tokens = processor.output_tokens if processor.output_tokens
120120
end
121121

122-
def native_tool_support?
123-
@native_tool_support
124-
end
125-
126122
def partials_from(decoded_chunk)
127123
decoded_chunk.split("\n").map { |line| line.split("data: ", 2)[1] }.compact
128124
end

lib/completions/endpoints/aws_bedrock.rb

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,22 @@ def prepare_request(payload)
117117
end
118118
end
119119

120-
def decode(chunk)
120+
def decode_chunk(partial_data)
121+
bedrock_decode(partial_data).map do |decoded_partial_data|
122+
@raw_response ||= +""
123+
@raw_response << decoded_partial_data
124+
@raw_response << "\n"
125+
126+
parsed_json = JSON.parse(decoded_partial_data, symbolize_names: true)
127+
processor.process_streamed_message(parsed_json)
128+
end.compact
129+
end
130+
131+
def decode(response_data)
132+
processor.process_message(response_data)
133+
end
134+
135+
def bedrock_decode(chunk)
121136
@decoder ||= Aws::EventStream::Decoder.new
122137

123138
decoded, _done = @decoder.decode_chunk(chunk)
@@ -147,12 +162,13 @@ def decode(chunk)
147162
Aws::EventStream::Errors::MessageChecksumError,
148163
Aws::EventStream::Errors::PreludeChecksumError => e
149164
Rails.logger.error("#{self.class.name}: #{e.message}")
150-
nil
165+
[]
151166
end
152167

153168
def final_log_update(log)
154169
log.request_tokens = processor.input_tokens if processor.input_tokens
155170
log.response_tokens = processor.output_tokens if processor.output_tokens
171+
log.raw_response_payload = @raw_response
156172
end
157173

158174
def processor
@@ -164,20 +180,13 @@ def add_to_function_buffer(function_buffer, partial: nil, payload: nil)
164180
processor.to_xml_tool_calls(function_buffer) if !partial
165181
end
166182

167-
def extract_completion_from(response_raw)
168-
processor.process_message(response_raw)
169-
end
170183

171184
def has_tool?(_response_data)
172185
processor.tool_calls.present?
173186
end
174187

175-
def partials_from(decoded_chunks)
176-
decoded_chunks
177-
end
178-
179-
def native_tool_support?
180-
@native_tool_support
188+
def xml_tools_enabled?
189+
!@native_tool_support
181190
end
182191

183192
def chunk_to_string(chunk)

0 commit comments

Comments
 (0)