-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0.4.0 Anthropic prompt caching support
- Loading branch information
Showing
10 changed files
with
381 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/core_ext/module/delegation" | ||
|
||
module Raix | ||
module MessageAdapters | ||
# Transforms messages into the format expected by the OpenAI API | ||
class Base | ||
attr_accessor :context | ||
|
||
delegate :cache_at, :model, to: :context | ||
|
||
def initialize(context) | ||
@context = context | ||
end | ||
|
||
def transform(message) | ||
return message if message[:role].present? | ||
|
||
if message[:function].present? | ||
{ role: "assistant", name: message.dig(:function, :name), content: message.dig(:function, :arguments).to_json } | ||
elsif message[:result].present? | ||
{ role: "function", name: message[:name], content: message[:result] } | ||
else | ||
content(message) | ||
end | ||
end | ||
|
||
protected | ||
|
||
def content(message) | ||
case message | ||
in { system: content } | ||
{ role: "system", content: } | ||
in { user: content } | ||
{ role: "user", content: } | ||
in { assistant: content } | ||
{ role: "assistant", content: } | ||
else | ||
raise ArgumentError, "Invalid message format: #{message.inspect}" | ||
end.tap do |msg| | ||
# convert to anthropic multipart format if model is claude-3 and cache_at is set | ||
if model["anthropic/claude-3"] && cache_at && msg[:content].length > cache_at.to_i | ||
msg[:content] = [{ type: "text", text: msg[:content], cache_control: { type: "ephemeral" } }] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Raix | ||
VERSION = "0.3.2" | ||
VERSION = "0.4.0" | ||
end |
Oops, something went wrong.