intelligence 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/intelligence.gemspec +47 -0
- data/lib/intelligence/adapter/base.rb +7 -0
- data/lib/intelligence/adapter/construction_methods.rb +37 -0
- data/lib/intelligence/adapter.rb +8 -0
- data/lib/intelligence/adapter_error.rb +8 -0
- data/lib/intelligence/adapters/anthropic/adapter.rb +59 -0
- data/lib/intelligence/adapters/anthropic/chat_methods.rb +358 -0
- data/lib/intelligence/adapters/anthropic.rb +2 -0
- data/lib/intelligence/adapters/cerebras.rb +35 -0
- data/lib/intelligence/adapters/generic/adapter.rb +21 -0
- data/lib/intelligence/adapters/generic/chat_methods.rb +331 -0
- data/lib/intelligence/adapters/generic.rb +2 -0
- data/lib/intelligence/adapters/google/adapter.rb +60 -0
- data/lib/intelligence/adapters/google/chat_methods.rb +346 -0
- data/lib/intelligence/adapters/google.rb +2 -0
- data/lib/intelligence/adapters/groq.rb +51 -0
- data/lib/intelligence/adapters/hyperbolic.rb +55 -0
- data/lib/intelligence/adapters/legacy/adapter.rb +13 -0
- data/lib/intelligence/adapters/legacy/chat_methods.rb +37 -0
- data/lib/intelligence/adapters/open_ai/adapter.rb +75 -0
- data/lib/intelligence/adapters/open_ai/chat_methods.rb +314 -0
- data/lib/intelligence/adapters/open_ai.rb +2 -0
- data/lib/intelligence/adapters/samba_nova.rb +64 -0
- data/lib/intelligence/adapters/together_ai.rb +46 -0
- data/lib/intelligence/chat_error_result.rb +11 -0
- data/lib/intelligence/chat_metrics.rb +32 -0
- data/lib/intelligence/chat_request.rb +117 -0
- data/lib/intelligence/chat_result.rb +32 -0
- data/lib/intelligence/chat_result_choice.rb +26 -0
- data/lib/intelligence/conversation.rb +48 -0
- data/lib/intelligence/error.rb +3 -0
- data/lib/intelligence/error_result.rb +24 -0
- data/lib/intelligence/invalid_content_error.rb +3 -0
- data/lib/intelligence/message.rb +53 -0
- data/lib/intelligence/message_content/base.rb +18 -0
- data/lib/intelligence/message_content/binary.rb +24 -0
- data/lib/intelligence/message_content/text.rb +17 -0
- data/lib/intelligence/message_content/tool_call.rb +20 -0
- data/lib/intelligence/message_content/tool_result.rb +20 -0
- data/lib/intelligence/message_content.rb +16 -0
- data/lib/intelligence/unsupported_content_error.rb +3 -0
- data/lib/intelligence/version.rb +3 -0
- data/lib/intelligence.rb +24 -0
- metadata +181 -0
@@ -0,0 +1,24 @@
|
|
1
|
+
module Intelligence
|
2
|
+
|
3
|
+
class ErrorResult
|
4
|
+
# ---------------
|
5
|
+
|
6
|
+
attr_reader :error_type
|
7
|
+
attr_reader :error
|
8
|
+
attr_reader :error_description
|
9
|
+
|
10
|
+
def initialize( error_attributes )
|
11
|
+
# --------------------------------
|
12
|
+
error_attributes.each do | key, value |
|
13
|
+
instance_variable_set( "@#{key}", value ) if self.respond_to?( "#{key}" )
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def empty?
|
18
|
+
# --------
|
19
|
+
@error_type.nil? && @error.nil? && @error_description.nil?
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Intelligence
|
2
|
+
class Message
|
3
|
+
|
4
|
+
ROLES = [ :system, :user, :assistant ]
|
5
|
+
|
6
|
+
attr_reader :role
|
7
|
+
attr_reader :contents
|
8
|
+
|
9
|
+
def initialize( role, content_type = nil, content_attributes = nil )
|
10
|
+
raise ArgumentError.new( "The role is invalid. It must be one of #{ROLES.join( ', ' )}." ) \
|
11
|
+
unless ROLES.include?( role.to_sym )
|
12
|
+
|
13
|
+
@role = role.to_sym
|
14
|
+
@contents = []
|
15
|
+
|
16
|
+
@contents << MessageContent.build( content_type, content_attributes ) unless content_type.nil?
|
17
|
+
end
|
18
|
+
|
19
|
+
def append_content( content )
|
20
|
+
@contents.push( content ) unless content.nil?
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def build_and_append_content( content_type = nil, content_attributes = nil )
|
25
|
+
append_content(
|
26
|
+
MessageContent.build( content_type, content_attributes )
|
27
|
+
)
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
alias :<< :append_content
|
32
|
+
|
33
|
+
def each_content( &block )
|
34
|
+
@contents.each( &block )
|
35
|
+
end
|
36
|
+
|
37
|
+
def empty?
|
38
|
+
@contents.empty?
|
39
|
+
end
|
40
|
+
|
41
|
+
def valid?
|
42
|
+
!@role.nil? && @contents.all?{ | contents | contents.valid? }
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_h
|
46
|
+
{
|
47
|
+
role: @role,
|
48
|
+
contents: @contents.map { | c | c.to_h }
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Intelligence
|
2
|
+
module MessageContent
|
3
|
+
|
4
|
+
class Base
|
5
|
+
def initialize( attributes = {} )
|
6
|
+
attributes.each do | key, value |
|
7
|
+
instance_variable_set( "@#{key}", value.freeze ) if self.respond_to?( "#{key}" )
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
false
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Intelligence
|
2
|
+
module MessageContent
|
3
|
+
|
4
|
+
class Binary < Base
|
5
|
+
attr_reader :content_type
|
6
|
+
attr_reader :bytes
|
7
|
+
|
8
|
+
def valid?
|
9
|
+
( @content_type || false ) && !MIME::Types[ @content_type ].empty? &&
|
10
|
+
( @bytes || false ) && bytes.respond_to?( :empty? ) && !bytes.empty?
|
11
|
+
end
|
12
|
+
|
13
|
+
def image?
|
14
|
+
( @content_type || false ) &&
|
15
|
+
( MIME::Types[ @content_type ]&.first&.media_type == 'image' )
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_h
|
19
|
+
{ type: :binary, content_type: content_type, bytes: bytes }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Intelligence
|
2
|
+
module MessageContent
|
3
|
+
|
4
|
+
class Text < Base
|
5
|
+
attr_reader :text
|
6
|
+
|
7
|
+
def valid?
|
8
|
+
( text || false ) && text.respond_to?( :empty? ) && !text.empty?
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_h
|
12
|
+
{ type: :text, text: text }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Intelligence
|
2
|
+
module MessageContent
|
3
|
+
|
4
|
+
class ToolCall < Base
|
5
|
+
attr_reader :tool_call_id
|
6
|
+
attr_reader :tool_name
|
7
|
+
attr_reader :tool_parameters
|
8
|
+
|
9
|
+
def to_h
|
10
|
+
{
|
11
|
+
type: :tool_call,
|
12
|
+
tool_call_id: tool_call_id,
|
13
|
+
tool_name: tool_name,
|
14
|
+
tool_parameters: tool_parameters
|
15
|
+
}.compact
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Intelligence
|
2
|
+
module MessageContent
|
3
|
+
|
4
|
+
class ToolResult < Base
|
5
|
+
attr_reader :tool_call_id
|
6
|
+
attr_reader :tool_name
|
7
|
+
attr_reader :tool_result
|
8
|
+
|
9
|
+
def to_h
|
10
|
+
{
|
11
|
+
type: :tool_call,
|
12
|
+
tool_call_id: tool_call_id,
|
13
|
+
tool_name: tool_name,
|
14
|
+
tool_parameters: tool_result
|
15
|
+
}.compact
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
Dir[ File.join( __dir__, 'message_content', '*.rb' ) ].each do | file |
|
2
|
+
require_relative file
|
3
|
+
end
|
4
|
+
|
5
|
+
module Intelligence
|
6
|
+
module MessageContent
|
7
|
+
|
8
|
+
def self.build( type, attributes )
|
9
|
+
type_name = type.to_s.split( '_' ).map { | word | word.capitalize }.join
|
10
|
+
klass = Intelligence.const_get( "MessageContent::#{type_name}" ) rescue nil
|
11
|
+
klass.nil? ? nil : klass.new( attributes )
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
data/lib/intelligence.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
require 'adaptive_configuration'
|
5
|
+
require 'mime-types'
|
6
|
+
|
7
|
+
require 'intelligence/version'
|
8
|
+
|
9
|
+
require 'intelligence/error'
|
10
|
+
require 'intelligence/adapter_error'
|
11
|
+
require 'intelligence/unsupported_content_error'
|
12
|
+
|
13
|
+
require 'intelligence/error_result'
|
14
|
+
|
15
|
+
require 'intelligence/conversation'
|
16
|
+
require 'intelligence/message'
|
17
|
+
require 'intelligence/message_content'
|
18
|
+
require 'intelligence/adapter'
|
19
|
+
require 'intelligence/chat_request'
|
20
|
+
require 'intelligence/chat_result'
|
21
|
+
require 'intelligence/chat_result_choice'
|
22
|
+
require 'intelligence/chat_metrics'
|
23
|
+
require 'intelligence/chat_error_result'
|
24
|
+
|
metadata
ADDED
@@ -0,0 +1,181 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: intelligence
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kristoph Cichocki-Romanov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-10-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: adaptiveconfiguration
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.0.beta01
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.0.0.beta01
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mime-types
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.6'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.6'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.4'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: debug
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.9'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.9'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: vcr
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '6.3'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '6.3'
|
97
|
+
description: Intelligence is a lightweight yet powerful Ruby gem that allows you to
|
98
|
+
seamlessly and uniformly interact with large language and vision models (LLM) API's
|
99
|
+
of numerous vendors, including OpenAI, Anthropic, Google, Cerebras, Groq, Hyperbolic,
|
100
|
+
Samba Nova and Together AI. It can be trivially expanded to other OpenAI conformant
|
101
|
+
API providers as well as self hosted models. Intelligence supports text models
|
102
|
+
in streaming and non-streaming mode, vision models, and tool use. Intelligence
|
103
|
+
has minimal dependencies and does not require the vendors ( often bloated ) SDK's.
|
104
|
+
email:
|
105
|
+
- rubygems.org@kristoph.net
|
106
|
+
executables: []
|
107
|
+
extensions: []
|
108
|
+
extra_rdoc_files: []
|
109
|
+
files:
|
110
|
+
- LICENSE
|
111
|
+
- intelligence.gemspec
|
112
|
+
- lib/intelligence.rb
|
113
|
+
- lib/intelligence/adapter.rb
|
114
|
+
- lib/intelligence/adapter/base.rb
|
115
|
+
- lib/intelligence/adapter/construction_methods.rb
|
116
|
+
- lib/intelligence/adapter_error.rb
|
117
|
+
- lib/intelligence/adapters/anthropic.rb
|
118
|
+
- lib/intelligence/adapters/anthropic/adapter.rb
|
119
|
+
- lib/intelligence/adapters/anthropic/chat_methods.rb
|
120
|
+
- lib/intelligence/adapters/cerebras.rb
|
121
|
+
- lib/intelligence/adapters/generic.rb
|
122
|
+
- lib/intelligence/adapters/generic/adapter.rb
|
123
|
+
- lib/intelligence/adapters/generic/chat_methods.rb
|
124
|
+
- lib/intelligence/adapters/google.rb
|
125
|
+
- lib/intelligence/adapters/google/adapter.rb
|
126
|
+
- lib/intelligence/adapters/google/chat_methods.rb
|
127
|
+
- lib/intelligence/adapters/groq.rb
|
128
|
+
- lib/intelligence/adapters/hyperbolic.rb
|
129
|
+
- lib/intelligence/adapters/legacy/adapter.rb
|
130
|
+
- lib/intelligence/adapters/legacy/chat_methods.rb
|
131
|
+
- lib/intelligence/adapters/open_ai.rb
|
132
|
+
- lib/intelligence/adapters/open_ai/adapter.rb
|
133
|
+
- lib/intelligence/adapters/open_ai/chat_methods.rb
|
134
|
+
- lib/intelligence/adapters/samba_nova.rb
|
135
|
+
- lib/intelligence/adapters/together_ai.rb
|
136
|
+
- lib/intelligence/chat_error_result.rb
|
137
|
+
- lib/intelligence/chat_metrics.rb
|
138
|
+
- lib/intelligence/chat_request.rb
|
139
|
+
- lib/intelligence/chat_result.rb
|
140
|
+
- lib/intelligence/chat_result_choice.rb
|
141
|
+
- lib/intelligence/conversation.rb
|
142
|
+
- lib/intelligence/error.rb
|
143
|
+
- lib/intelligence/error_result.rb
|
144
|
+
- lib/intelligence/invalid_content_error.rb
|
145
|
+
- lib/intelligence/message.rb
|
146
|
+
- lib/intelligence/message_content.rb
|
147
|
+
- lib/intelligence/message_content/base.rb
|
148
|
+
- lib/intelligence/message_content/binary.rb
|
149
|
+
- lib/intelligence/message_content/text.rb
|
150
|
+
- lib/intelligence/message_content/tool_call.rb
|
151
|
+
- lib/intelligence/message_content/tool_result.rb
|
152
|
+
- lib/intelligence/unsupported_content_error.rb
|
153
|
+
- lib/intelligence/version.rb
|
154
|
+
homepage: https://github.com/EndlessInternational/intelligence
|
155
|
+
licenses:
|
156
|
+
- MIT
|
157
|
+
metadata:
|
158
|
+
source_code_uri: https://github.com/EndlessInternational/intelligence
|
159
|
+
bug_tracker_uri: https://github.com/EndlessInternational/intelligence/issues
|
160
|
+
post_install_message:
|
161
|
+
rdoc_options: []
|
162
|
+
require_paths:
|
163
|
+
- lib
|
164
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
165
|
+
requirements:
|
166
|
+
- - ">="
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
version: '3.0'
|
169
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
requirements: []
|
175
|
+
rubygems_version: 3.5.19
|
176
|
+
signing_key:
|
177
|
+
specification_version: 4
|
178
|
+
summary: A Ruby gem for seamlessly and uniformly interacting with large languge and
|
179
|
+
vision model (LLM) API's served by numerous services, including those of OpenAI,
|
180
|
+
Anthropic, Google and others.
|
181
|
+
test_files: []
|