omniai-anthropic 1.8.0 → 1.8.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/omniai/anthropic/chat/message_serializer.rb +49 -0
- data/lib/omniai/anthropic/chat/tool_call_result_serializer.rb +29 -0
- data/lib/omniai/anthropic/chat/tool_serializer.rb +20 -0
- data/lib/omniai/anthropic/chat.rb +30 -11
- data/lib/omniai/anthropic/version.rb +1 -1
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 139e028a0693e4dfdb45dd1ef6ff5fe4706cf34801eb7154671968f3460cb786
|
4
|
+
data.tar.gz: e50f5502ff0ff81c37bd42ce23dafce2e70843cae7a414061cbbd38edd8abeea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 751b0f552007b98d17d03820150afacab7dc98044f1d558140b5f460d58ffe091b757f3cf6e3e559e894ee96e0c7a10928a3f86cc653e4bfd9b886b44d3311d4
|
7
|
+
data.tar.gz: cccd3f87fffdab99a9e5d0369a293439c953a8d3db5bfa1347b2274b5efe94c9faf17a34fcf1bfc0e841d3e9db5decca842fd5e8a0e77c940344fa3cd266ac4b
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniAI
|
4
|
+
module Anthropic
|
5
|
+
class Chat
|
6
|
+
# Overrides message serialize / deserialize.
|
7
|
+
module MessageSerializer
|
8
|
+
# @param message [OmniAI::Chat::Message]
|
9
|
+
# @param context [OmniAI::Context]
|
10
|
+
# @return [Hash]
|
11
|
+
def self.serialize(message, context:)
|
12
|
+
role = message.role
|
13
|
+
parts = arrayify(message.content) + arrayify(message.tool_call_list)
|
14
|
+
content = parts.map do |part|
|
15
|
+
case part
|
16
|
+
when String then { type: 'text', text: part }
|
17
|
+
else part.serialize(context:)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
{ role:, content: }
|
22
|
+
end
|
23
|
+
|
24
|
+
# @param data [Hash]
|
25
|
+
# @param context [OmniAI::Context]
|
26
|
+
# @return [OmniAI::Chat::Message]
|
27
|
+
def self.deserialize(data, context:)
|
28
|
+
role = data['role']
|
29
|
+
parts = arrayify(data['content']).map do |content|
|
30
|
+
ContentSerializer.deserialize(content, context:)
|
31
|
+
end
|
32
|
+
|
33
|
+
tool_call_list = parts.select { |part| part.is_a?(OmniAI::Chat::ToolCall) }
|
34
|
+
content = parts.reject { |part| part.is_a?(OmniAI::Chat::ToolCall) }
|
35
|
+
|
36
|
+
OmniAI::Chat::Message.new(content:, role:, tool_call_list:)
|
37
|
+
end
|
38
|
+
|
39
|
+
# @param content [Object]
|
40
|
+
# @return [Array<Object>]
|
41
|
+
def self.arrayify(content)
|
42
|
+
return [] if content.nil?
|
43
|
+
|
44
|
+
content.is_a?(Array) ? content : [content]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniAI
|
4
|
+
module Anthropic
|
5
|
+
class Chat
|
6
|
+
# Overrides tool-call response serialize / deserialize.
|
7
|
+
module ToolCallResultSerializer
|
8
|
+
# @param tool_call_result [OmniAI::Chat::ToolCallResult]
|
9
|
+
# @return [Hash]
|
10
|
+
def self.serialize(tool_call_result, *)
|
11
|
+
{
|
12
|
+
type: 'tool_result',
|
13
|
+
tool_use_id: tool_call_result.tool_call_id,
|
14
|
+
content: tool_call_result.content,
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
# @param data [Hash]
|
19
|
+
# @return [OmniAI::Chat::ToolCallResult]
|
20
|
+
def self.deserialize(data, *)
|
21
|
+
tool_call_id = data['tool_use_id']
|
22
|
+
content = data['content']
|
23
|
+
|
24
|
+
OmniAI::Chat::ToolCallResult.new(content:, tool_call_id:)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniAI
|
4
|
+
module Anthropic
|
5
|
+
class Chat
|
6
|
+
# Overrides tool serialize / deserialize.
|
7
|
+
module ToolSerializer
|
8
|
+
# @param tool [OmniAI::Tool]
|
9
|
+
# @return [Hash]
|
10
|
+
def self.serialize(tool, *)
|
11
|
+
{
|
12
|
+
name: tool.name,
|
13
|
+
description: tool.description,
|
14
|
+
input_schema: tool.parameters.is_a?(Tool::Parameters) ? tool.parameters.serialize : tool.parameters,
|
15
|
+
}.compact
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -16,19 +16,29 @@ module OmniAI
|
|
16
16
|
CLAUDE_INSTANT_1_0 = 'claude-instant-1.2'
|
17
17
|
CLAUDE_2_0 = 'claude-2.0'
|
18
18
|
CLAUDE_2_1 = 'claude-2.1'
|
19
|
-
|
19
|
+
|
20
20
|
CLAUDE_3_HAIKU_20240307 = 'claude-3-haiku-20240307'
|
21
|
+
CLAUDE_3_OPUS_20240229 = 'claude-3-opus-20240229'
|
22
|
+
CLAUDE_3_SONNET_20240209 = 'claude-3-sonnet-20240229'
|
21
23
|
CLAUDE_3_SONNET_20240307 = 'claude-3-sonnet-20240307'
|
22
24
|
CLAUDE_3_5_SONNET_20240620 = 'claude-3-5-sonnet-20240620'
|
23
|
-
|
24
|
-
|
25
|
-
|
25
|
+
CLAUDE_3_5_SONNET_20241022 = 'claude-3-5-sonnet-20241022'
|
26
|
+
|
27
|
+
CLAUDE_3_HAIKU_LATEST = CLAUDE_3_HAIKU_20240307
|
28
|
+
CLAUDE_3_OPUS_LATEST = 'claude-3-opus-latest'
|
29
|
+
CLAUDE_3_5_SONNET_LATEST = 'claude-3-5-sonnet-latest'
|
30
|
+
|
31
|
+
CLAUDE_HAIKU = CLAUDE_3_HAIKU_LATEST
|
32
|
+
CLAUDE_OPUS = CLAUDE_3_OPUS_LATEST
|
33
|
+
CLAUDE_SONNET = CLAUDE_3_5_SONNET_LATEST
|
26
34
|
end
|
27
35
|
|
28
36
|
DEFAULT_MODEL = Model::CLAUDE_SONNET
|
29
37
|
|
30
38
|
# @return [Context]
|
31
39
|
CONTEXT = Context.build do |context|
|
40
|
+
context.serializers[:tool] = ToolSerializer.method(:serialize)
|
41
|
+
|
32
42
|
context.serializers[:file] = MediaSerializer.method(:serialize)
|
33
43
|
context.serializers[:url] = MediaSerializer.method(:serialize)
|
34
44
|
|
@@ -38,9 +48,15 @@ module OmniAI
|
|
38
48
|
context.serializers[:tool_call] = ToolCallSerializer.method(:serialize)
|
39
49
|
context.deserializers[:tool_call] = ToolCallSerializer.method(:deserialize)
|
40
50
|
|
51
|
+
context.serializers[:tool_call_result] = ToolCallResultSerializer.method(:serialize)
|
52
|
+
context.deserializers[:tool_call_result] = ToolCallResultSerializer.method(:deserialize)
|
53
|
+
|
41
54
|
context.serializers[:function] = FunctionSerializer.method(:serialize)
|
42
55
|
context.deserializers[:function] = FunctionSerializer.method(:deserialize)
|
43
56
|
|
57
|
+
context.serializers[:message] = MessageSerializer.method(:serialize)
|
58
|
+
context.deserializers[:message] = MessageSerializer.method(:deserialize)
|
59
|
+
|
44
60
|
context.deserializers[:content] = ContentSerializer.method(:deserialize)
|
45
61
|
context.deserializers[:payload] = PayloadSerializer.method(:deserialize)
|
46
62
|
end
|
@@ -83,17 +99,20 @@ module OmniAI
|
|
83
99
|
CONTEXT
|
84
100
|
end
|
85
101
|
|
102
|
+
# @return [Array<Message>]
|
103
|
+
def build_tool_call_messages(tool_call_list)
|
104
|
+
content = tool_call_list.map do |tool_call|
|
105
|
+
ToolCallResult.new(tool_call_id: tool_call.id, content: execute_tool_call(tool_call))
|
106
|
+
end
|
107
|
+
|
108
|
+
[Message.new(role: OmniAI::Chat::Role::USER, content:)]
|
109
|
+
end
|
110
|
+
|
86
111
|
private
|
87
112
|
|
88
113
|
# @return [Array<Hash>, nil]
|
89
114
|
def tools_payload
|
90
|
-
@tools
|
91
|
-
{
|
92
|
-
name: tool.name,
|
93
|
-
description: tool.description,
|
94
|
-
input_schema: tool.parameters&.prepare,
|
95
|
-
}.compact
|
96
|
-
end
|
115
|
+
@tools.map { |tool| tool.serialize(context:) } if @tools&.any?
|
97
116
|
end
|
98
117
|
end
|
99
118
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: omniai-anthropic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.8.
|
4
|
+
version: 1.8.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Sylvestre
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: event_stream_parser
|
@@ -67,10 +67,13 @@ files:
|
|
67
67
|
- lib/omniai/anthropic/chat/content_serializer.rb
|
68
68
|
- lib/omniai/anthropic/chat/function_serializer.rb
|
69
69
|
- lib/omniai/anthropic/chat/media_serializer.rb
|
70
|
+
- lib/omniai/anthropic/chat/message_serializer.rb
|
70
71
|
- lib/omniai/anthropic/chat/payload_serializer.rb
|
71
72
|
- lib/omniai/anthropic/chat/stream.rb
|
72
73
|
- lib/omniai/anthropic/chat/text_serializer.rb
|
74
|
+
- lib/omniai/anthropic/chat/tool_call_result_serializer.rb
|
73
75
|
- lib/omniai/anthropic/chat/tool_call_serializer.rb
|
76
|
+
- lib/omniai/anthropic/chat/tool_serializer.rb
|
74
77
|
- lib/omniai/anthropic/client.rb
|
75
78
|
- lib/omniai/anthropic/config.rb
|
76
79
|
- lib/omniai/anthropic/version.rb
|
@@ -96,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
99
|
- !ruby/object:Gem::Version
|
97
100
|
version: '0'
|
98
101
|
requirements: []
|
99
|
-
rubygems_version: 3.5.
|
102
|
+
rubygems_version: 3.5.18
|
100
103
|
signing_key:
|
101
104
|
specification_version: 4
|
102
105
|
summary: A generalized framework for interacting with Anthropic
|