Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/ruby_llm/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def complete(&) # rubocop:disable Metrics/PerceivedComplexity

if @schema && response.content.is_a?(String)
begin
response.content = JSON.parse(response.content)
response.content = JSON.parse(extract_json(response.content))
rescue JSON::ParserError
# If parsing fails, keep content as string
end
Expand Down Expand Up @@ -169,6 +169,13 @@ def instance_variables

private

def extract_json(text)
return text if text.nil? || text.empty?

# Will return the first json object or array embedded in the text
text.match(/[{\[]{1}([,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]|".*?")+[}\]]/)[0]
end

def wrap_streaming_block(&block)
return nil unless block_given?

Expand Down
15 changes: 9 additions & 6 deletions lib/ruby_llm/providers/anthropic/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ def completion_url
'/v1/messages'
end

def render_payload(messages, tools:, temperature:, model:, stream: false, schema: nil) # rubocop:disable Metrics/ParameterLists,Lint/UnusedMethodArgument
def render_payload(messages, tools:, temperature:, model:, stream: false, schema: nil) # rubocop:disable Metrics/ParameterLists
system_messages, chat_messages = separate_messages(messages)
system_content = build_system_content(system_messages)
system_content = build_system_content(system_messages, schema)

build_base_payload(chat_messages, model, stream).tap do |payload|
add_optional_fields(payload, system_content:, tools:, temperature:)
Expand All @@ -24,17 +24,15 @@ def separate_messages(messages)
messages.partition { |msg| msg.role == :system }
end

def build_system_content(system_messages)
return [] if system_messages.empty?

def build_system_content(system_messages, schema)
if system_messages.length > 1
RubyLLM.logger.warn(
"Anthropic's Claude implementation only supports a single system message. " \
'Multiple system messages will be combined into one.'
)
end

system_messages.flat_map do |msg|
messages = system_messages.flat_map do |msg|
content = msg.content

if content.is_a?(RubyLLM::Content::Raw)
Expand All @@ -43,6 +41,11 @@ def build_system_content(system_messages)
Media.format_content(content)
end
end

return messages unless schema

messages << { text: "You should respond with json that follows this schema: #{schema}",
type: 'text' }
end

def build_base_payload(chat_messages, model, stream)
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_llm/providers/bedrock/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def render_payload(messages, tools:, temperature:, model:, stream: false, schema
@model_id = model.id

system_messages, chat_messages = Anthropic::Chat.separate_messages(messages)
system_content = Anthropic::Chat.build_system_content(system_messages)
system_content = Anthropic::Chat.build_system_content(system_messages, schema)

build_base_payload(chat_messages, model).tap do |payload|
Anthropic::Chat.add_optional_fields(payload, system_content:, tools:, temperature:)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading