omniai-anthropic 1.8.0 → 1.8.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40dd84f42676e7000caed3c06c942ab1bca0d3d10763c279e084e5d378930946
4
- data.tar.gz: 2233860b1769b297af92fe5c1156fb08ffb61a88452ba39d8ad79e131ae861be
3
+ metadata.gz: 692299f3cf52f0f131202156f1fc5d0f973ec0488eff51f1e0186d3e88befa5d
4
+ data.tar.gz: 2d77787b55f8aa59acc9a33c92627451089fc63c988723963f0bf967dc30df40
5
5
  SHA512:
6
- metadata.gz: d4c99af8092f169086859edf43d6a9164d410caa0166a43fd2ff0b76156fb28cedca8fbbd84251e3405406336a371a068fd00968d93dc723050950dae4da7c80
7
- data.tar.gz: f022585d04e3983ed15ec31cfc3b125bc5e6396be2d417a9cc10af0e827e32a0a19c324062987a83be5cfd306c4659d5e702ae7c931511189518c546b4e31489
6
+ metadata.gz: 97364e1f0f92ac5984dadaa4ff3e19b71463f2bc8acccc939b2f215e3d28daad8f4032d0b251f9253932134dc4c6808e71c80ab796f5c325c6108e4d76535019
7
+ data.tar.gz: 5697f26b5817a949aa4d5cf9e760de598a48c4ed298f9822bd659738ed3e0631b1cbaa98d8bad019e66501f6b331896e637a9f13608a07aa433be64749e54fff
@@ -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
@@ -29,6 +29,8 @@ module OmniAI
29
29
 
30
30
  # @return [Context]
31
31
  CONTEXT = Context.build do |context|
32
+ context.serializers[:tool] = ToolSerializer.method(:serialize)
33
+
32
34
  context.serializers[:file] = MediaSerializer.method(:serialize)
33
35
  context.serializers[:url] = MediaSerializer.method(:serialize)
34
36
 
@@ -38,9 +40,15 @@ module OmniAI
38
40
  context.serializers[:tool_call] = ToolCallSerializer.method(:serialize)
39
41
  context.deserializers[:tool_call] = ToolCallSerializer.method(:deserialize)
40
42
 
43
+ context.serializers[:tool_call_result] = ToolCallResultSerializer.method(:serialize)
44
+ context.deserializers[:tool_call_result] = ToolCallResultSerializer.method(:deserialize)
45
+
41
46
  context.serializers[:function] = FunctionSerializer.method(:serialize)
42
47
  context.deserializers[:function] = FunctionSerializer.method(:deserialize)
43
48
 
49
+ context.serializers[:message] = MessageSerializer.method(:serialize)
50
+ context.deserializers[:message] = MessageSerializer.method(:deserialize)
51
+
44
52
  context.deserializers[:content] = ContentSerializer.method(:deserialize)
45
53
  context.deserializers[:payload] = PayloadSerializer.method(:deserialize)
46
54
  end
@@ -83,17 +91,20 @@ module OmniAI
83
91
  CONTEXT
84
92
  end
85
93
 
94
+ # @return [Array<Message>]
95
+ def build_tool_call_messages(tool_call_list)
96
+ content = tool_call_list.map do |tool_call|
97
+ ToolCallResult.new(tool_call_id: tool_call.id, content: execute_tool_call(tool_call))
98
+ end
99
+
100
+ [Message.new(role: OmniAI::Chat::Role::USER, content:)]
101
+ end
102
+
86
103
  private
87
104
 
88
105
  # @return [Array<Hash>, nil]
89
106
  def tools_payload
90
- @tools&.map do |tool|
91
- {
92
- name: tool.name,
93
- description: tool.description,
94
- input_schema: tool.parameters&.prepare,
95
- }.compact
96
- end
107
+ @tools.map { |tool| tool.serialize(context:) } if @tools&.any?
97
108
  end
98
109
  end
99
110
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module OmniAI
4
4
  module Anthropic
5
- VERSION = '1.8.0'
5
+ VERSION = '1.8.1'
6
6
  end
7
7
  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.0
4
+ version: 1.8.1
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-08-16 00:00:00.000000000 Z
11
+ date: 2024-08-19 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