omniai-anthropic 1.5.0 → 1.6.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: 2b6103ed1e3c87ab2d1f2767dc4dce991756eefb558886ab8436632083058aba
4
- data.tar.gz: aa75e2707afdb2bdffb7f74716e1afa31c027141dbfd114c45a77c00abe52045
3
+ metadata.gz: e9b758783278cd58cfeac0e8a80765d796e63efb811671eaa44b4e4653496c45
4
+ data.tar.gz: 1cdcff27b130211ff661bf14d219716373eb7bc03d568a50d1c5a453215977f9
5
5
  SHA512:
6
- metadata.gz: b06128d930cd70b61d2cea0ee2bb0318342d9114f7c9fe46ddaf8a1778f3570cb6e49748efd3a626d95daac9df1ecaf5680bb05180c500a4933e95fd682c942e
7
- data.tar.gz: a18cce1b18ca45810dc9677856ef96e8af5d5faa71bf6160a981a5584d1f76157f45679481fc69e946d2837681cb9a40d8860fa6f52e879206c0af44612f5095
6
+ metadata.gz: 3ff6a720b6a032067d2e1c217f1cda76e552fe06e3c61e82b3eec36f307e9ab55da840b850718c38f7ed074c719597bbcd22d2c36bcdf56822782253661f74b2
7
+ data.tar.gz: 76cd69ef19bc717166576ea4ba7cb11579508a182da624fa18764804ee46466958c651e7be79874ac1b51ae2791839e74cc08613e1fb07ce3e4cae594958c820
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]
@@ -59,9 +59,12 @@ module OmniAI
59
59
  # @param stream [Proc, nil] optional
60
60
  # @param tools [Array<OmniAI::Tool>, nil] optional
61
61
  #
62
+ # @yield [prompt]
63
+ # @yieldparam prompt [OmniAI::Chat::Prompt]
64
+ #
62
65
  # @return [OmniAI::Chat::Completion]
63
- def chat(messages, model: Chat::Model::CLAUDE_HAIKU, temperature: nil, format: nil, stream: nil, tools: nil)
64
- Chat.process!(messages, model:, temperature:, format:, stream:, tools:, client: self)
66
+ def chat(messages, model: Chat::Model::CLAUDE_HAIKU, temperature: nil, format: nil, stream: nil, tools: nil, &)
67
+ Chat.process!(messages, model:, temperature:, format:, stream:, tools:, client: self, &)
65
68
  end
66
69
  end
67
70
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module OmniAI
4
4
  module Anthropic
5
- VERSION = '1.5.0'
5
+ VERSION = '1.6.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.5.0
4
+ version: 1.6.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-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