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 +4 -4
- data/README.md +7 -18
- data/lib/omniai/anthropic/chat/response/stream.rb +4 -4
- data/lib/omniai/anthropic/chat.rb +36 -12
- data/lib/omniai/anthropic/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9ae15992c71cef2c9e50527a2d46ec71232177c5360aa2e37dff56317245a59
|
4
|
+
data.tar.gz: a2425bfd1981cf37d12792ce39a9318bd9c8a68d25459970904b027d2c5bdf36
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
49
|
-
|
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
|
-
|
107
|
-
|
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
|
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
|
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
|
-
#
|
5
|
+
# An Anthropic chat implementation.
|
6
6
|
#
|
7
7
|
# Usage:
|
8
8
|
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
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
|
-
|
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
|
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 =
|
45
|
-
messages
|
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]
|
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.
|
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-
|
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
|