omniai-anthropic 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b6103ed1e3c87ab2d1f2767dc4dce991756eefb558886ab8436632083058aba
4
- data.tar.gz: aa75e2707afdb2bdffb7f74716e1afa31c027141dbfd114c45a77c00abe52045
3
+ metadata.gz: a9ae15992c71cef2c9e50527a2d46ec71232177c5360aa2e37dff56317245a59
4
+ data.tar.gz: a2425bfd1981cf37d12792ce39a9318bd9c8a68d25459970904b027d2c5bdf36
5
5
  SHA512:
6
- metadata.gz: b06128d930cd70b61d2cea0ee2bb0318342d9114f7c9fe46ddaf8a1778f3570cb6e49748efd3a626d95daac9df1ecaf5680bb05180c500a4933e95fd682c942e
7
- data.tar.gz: a18cce1b18ca45810dc9677856ef96e8af5d5faa71bf6160a981a5584d1f76157f45679481fc69e946d2837681cb9a40d8860fa6f52e879206c0af44612f5095
6
+ metadata.gz: de02489dfb4d5154914901a302b22541356883bd217904acb0be7f379bccfafd4f56f68f10160a9826bcf702f4dc491061ec9aebae098918b2529bf9896e280a
7
+ data.tar.gz: 126bb9cd63cd7aa0cb1de8b7e6cbc2d6c2946bee22894ed979c89c6290b0614e73e14efb9ec157c555dbeb577ad62eafcfec1e53d36673c015866f7226358b40
data/README.md CHANGED
@@ -44,21 +44,10 @@ completion.choice.message.content # 'Why did the chicken cross the road? To get
44
44
  ```
45
45
 
46
46
  ```ruby
47
- completion = client.chat({
48
- role: OmniAI::Chat::Role::USER,
49
- content: 'Is it wise to jump off a bridge?'
50
- })
51
- completion.choice.message.content # 'No.'
52
- ```
53
-
54
- ```ruby
55
- completion = client.chat([
56
- {
57
- role: OmniAI::Chat::Role::SYSTEM,
58
- content: 'You are a helpful assistant.'
59
- },
60
- 'What is the capital of Canada?',
61
- ])
47
+ completion = client.chat do |prompt|
48
+ prompt.system('You are a helpful assistant.')
49
+ prompt.user('What is the capital of Canada?')
50
+ end
62
51
  completion.choice.message.content # 'The capital of Canada is Ottawa.'
63
52
  ```
64
53
 
@@ -102,9 +91,9 @@ client.chat('Be poetic.', stream:)
102
91
  `format` takes an optional symbol (`:json`) and modifies requests to send additional system text requesting JSON:
103
92
 
104
93
  ```ruby
105
- completion = client.chat([
106
- { role: OmniAI::Chat::Role::USER, content: 'What is the name of the drummer for the Beatles?' }
107
- ], format: :json)
94
+ completion = client.chat(format: :json) do |prompt|
95
+ prompt.system(OmniAI::Chat::JSON_PROMPT)
96
+ prompt.user('What is the name of the drummer for the Beatles?')
108
97
  JSON.parse(completion.choice.message.content) # { "name": "Ringo" }
109
98
  ```
110
99
 
@@ -46,8 +46,8 @@ module OmniAI
46
46
 
47
47
  # Handler for Type::MESSAGE_STOP
48
48
  #
49
- # @param data [Hash]
50
- def message_stop(_)
49
+ # @param _data [Hash]
50
+ def message_stop(_data)
51
51
  @id = nil
52
52
  @model = nil
53
53
  @role = nil
@@ -62,8 +62,8 @@ module OmniAI
62
62
 
63
63
  # Handler for Type::CONTENT_BLOCK_STOP
64
64
  #
65
- # @param data [Hash]
66
- def content_block_stop(_)
65
+ # @param _data [Hash]
66
+ def content_block_stop(_data)
67
67
  @index = nil
68
68
  end
69
69
 
@@ -2,15 +2,15 @@
2
2
 
3
3
  module OmniAI
4
4
  module Anthropic
5
- # A Anthropic chat implementation.
5
+ # An Anthropic chat implementation.
6
6
  #
7
7
  # Usage:
8
8
  #
9
- # chat = OmniAI::Anthropic::Chat.new(client: client)
10
- # chat.completion('Tell me a joke.')
11
- # chat.completion(['Tell me a joke.'])
12
- # chat.completion({ role: 'user', content: 'Tell me a joke.' })
13
- # chat.completion([{ role: 'system', content: 'Tell me a joke.' }])
9
+ # completion = OmniAI::Anthropic::Chat.process!(client: client) do |prompt|
10
+ # prompt.system('You are an expert in the field of AI.')
11
+ # prompt.user('What are the biggest risks of AI?')
12
+ # end
13
+ # completion.choice.message.content # '...'
14
14
  class Chat < OmniAI::Chat
15
15
  module Model
16
16
  CLAUDE_INSTANT_1_0 = 'claude-instant-1.2'
@@ -25,13 +25,33 @@ module OmniAI
25
25
  CLAUDE_SONET = CLAUDE_3_5_SONET_20240620
26
26
  end
27
27
 
28
- protected
28
+ # @param [Media]
29
+ # @return [Hash]
30
+ # @example
31
+ # media = Media.new(...)
32
+ # MEDIA_SERIALIZER.call(media)
33
+ MEDIA_SERIALIZER = lambda do |media, *|
34
+ {
35
+ type: media.kind, # i.e. 'image' / 'video' / 'audio' / ...
36
+ source: {
37
+ type: 'base64',
38
+ media_type: media.type, # i.e. 'image/jpeg' / 'video/ogg' / 'audio/mpeg' / ...
39
+ data: media.data,
40
+ },
41
+ }
42
+ end
43
+
44
+ # @return [Context]
45
+ CONTEXT = Context.build do |context|
46
+ context.serializers[:file] = MEDIA_SERIALIZER
47
+ context.serializers[:url] = MEDIA_SERIALIZER
48
+ end
29
49
 
30
50
  # @return [Hash]
31
51
  def payload
32
52
  OmniAI::Anthropic.config.chat_options.merge({
33
53
  model: @model,
34
- messages: messages.filter { |message| !message[:role].eql?(Role::SYSTEM) },
54
+ messages:,
35
55
  system:,
36
56
  stream: @stream.nil? ? nil : !@stream.nil?,
37
57
  temperature: @temperature,
@@ -39,12 +59,16 @@ module OmniAI
39
59
  }).compact
40
60
  end
41
61
 
62
+ # @return [Array<Hash>]
63
+ def messages
64
+ messages = @prompt.messages.filter(&:user?)
65
+ messages.map { |message| message.serialize(context: CONTEXT) }
66
+ end
67
+
42
68
  # @return [String, nil]
43
69
  def system
44
- messages = self.messages.filter { |message| message[:role].eql?(Role::SYSTEM) }
45
- messages << { role: Role::SYSTEM, content: JSON_PROMPT } if @format.eql?(:json)
46
-
47
- messages.map { |message| message[:content] }.join("\n\n") if messages.any?
70
+ messages = @prompt.messages.filter(&:system?)
71
+ messages.map(&:content).join("\n\n") if messages.any?
48
72
  end
49
73
 
50
74
  # @return [String]
@@ -2,6 +2,6 @@
2
2
 
3
3
  module OmniAI
4
4
  module Anthropic
5
- VERSION = '1.5.0'
5
+ VERSION = '1.6.0'
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.5.0
4
+ version: 1.6.0
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-07-12 00:00:00.000000000 Z
11
+ date: 2024-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: event_stream_parser