Skip to content

Commit 887caef

Browse files
committed
� This is the 1st commit message:
Provides schema in anthropic system prompts � This is the commit message #2: Adds explanation of regexp Adds explanation of regexp Remove errant pry require Updated cassettes
1 parent cf27430 commit 887caef

File tree

30 files changed

+1269
-1465
lines changed

30 files changed

+1269
-1465
lines changed

lib/ruby_llm/chat.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def complete(&) # rubocop:disable Metrics/PerceivedComplexity
137137

138138
if @schema && response.content.is_a?(String)
139139
begin
140-
response.content = JSON.parse(response.content)
140+
response.content = JSON.parse(extract_json(response.content))
141141
rescue JSON::ParserError
142142
# If parsing fails, keep content as string
143143
end
@@ -169,6 +169,13 @@ def instance_variables
169169

170170
private
171171

172+
def extract_json(text)
173+
return text if text || text.empty?
174+
175+
# Will return the first json object or array embedded in the text
176+
text.match(/[{\[]{1}([,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]|".*?")+[}\]]/)[0]
177+
end
178+
172179
def wrap_streaming_block(&block)
173180
return nil unless block_given?
174181

lib/ruby_llm/providers/anthropic/chat.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ def completion_url
1111
'/v1/messages'
1212
end
1313

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

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

27-
def build_system_content(system_messages)
28-
return [] if system_messages.empty?
29-
27+
def build_system_content(system_messages, schema)
3028
if system_messages.length > 1
3129
RubyLLM.logger.warn(
3230
"Anthropic's Claude implementation only supports a single system message. " \
3331
'Multiple system messages will be combined into one.'
3432
)
3533
end
3634

37-
system_messages.flat_map do |msg|
35+
messages = system_messages.flat_map do |msg|
3836
content = msg.content
3937

4038
if content.is_a?(RubyLLM::Content::Raw)
@@ -43,6 +41,11 @@ def build_system_content(system_messages)
4341
Media.format_content(content)
4442
end
4543
end
44+
45+
return messages unless schema
46+
47+
messages << { text: "You should respond with json that follows this schema: #{schema}",
48+
type: 'text' }
4649
end
4750

4851
def build_base_payload(chat_messages, model, stream)

lib/ruby_llm/providers/bedrock/chat.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def render_payload(messages, tools:, temperature:, model:, stream: false, schema
4343
@model_id = model.id
4444

4545
system_messages, chat_messages = Anthropic::Chat.separate_messages(messages)
46-
system_content = Anthropic::Chat.build_system_content(system_messages)
46+
system_content = Anthropic::Chat.build_system_content(system_messages, schema)
4747

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

spec/fixtures/vcr_cassettes/chat_basic_chat_functionality_anthropic_claude-3-5-haiku-20241022_can_handle_multi-turn_conversations.yml

Lines changed: 46 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/chat_basic_chat_functionality_anthropic_claude-3-5-haiku-20241022_can_have_a_basic_conversation.yml

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)