intelligence 0.6.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +576 -0
- data/intelligence.gemspec +2 -1
- data/lib/intelligence/adapter/base.rb +13 -6
- data/lib/intelligence/adapter/class_methods.rb +15 -0
- data/lib/intelligence/adapter/module_methods.rb +41 -0
- data/lib/intelligence/adapter.rb +2 -2
- data/lib/intelligence/adapters/anthropic/adapter.rb +21 -19
- data/lib/intelligence/adapters/anthropic/chat_request_methods.rb +189 -0
- data/lib/intelligence/adapters/anthropic/{chat_methods.rb → chat_response_methods.rb} +13 -137
- data/lib/intelligence/adapters/cerebras.rb +19 -19
- data/lib/intelligence/adapters/generic/adapter.rb +4 -2
- data/lib/intelligence/adapters/generic/chat_request_methods.rb +221 -0
- data/lib/intelligence/adapters/generic/chat_response_methods.rb +234 -0
- data/lib/intelligence/adapters/generic.rb +1 -1
- data/lib/intelligence/adapters/google/adapter.rb +33 -22
- data/lib/intelligence/adapters/google/chat_request_methods.rb +234 -0
- data/lib/intelligence/adapters/google/chat_response_methods.rb +236 -0
- data/lib/intelligence/adapters/groq.rb +29 -49
- data/lib/intelligence/adapters/hyperbolic.rb +13 -39
- data/lib/intelligence/adapters/mistral.rb +21 -42
- data/lib/intelligence/adapters/open_ai/adapter.rb +39 -32
- data/lib/intelligence/adapters/open_ai/chat_request_methods.rb +186 -0
- data/lib/intelligence/adapters/open_ai/chat_response_methods.rb +239 -0
- data/lib/intelligence/adapters/open_ai.rb +1 -1
- data/lib/intelligence/adapters/open_router.rb +18 -18
- data/lib/intelligence/adapters/samba_nova.rb +16 -18
- data/lib/intelligence/adapters/together_ai.rb +25 -23
- data/lib/intelligence/conversation.rb +11 -10
- data/lib/intelligence/message.rb +45 -29
- data/lib/intelligence/message_content/base.rb +2 -9
- data/lib/intelligence/message_content/binary.rb +3 -3
- data/lib/intelligence/message_content/file.rb +3 -3
- data/lib/intelligence/message_content/text.rb +10 -2
- data/lib/intelligence/message_content/tool_call.rb +61 -5
- data/lib/intelligence/message_content/tool_result.rb +11 -6
- data/lib/intelligence/tool.rb +139 -0
- data/lib/intelligence/version.rb +1 -1
- data/lib/intelligence.rb +3 -1
- metadata +31 -13
- data/lib/intelligence/adapter/class_methods/construction.rb +0 -17
- data/lib/intelligence/adapter/module_methods/construction.rb +0 -43
- data/lib/intelligence/adapters/generic/chat_methods.rb +0 -355
- data/lib/intelligence/adapters/google/chat_methods.rb +0 -393
- data/lib/intelligence/adapters/legacy/adapter.rb +0 -11
- data/lib/intelligence/adapters/legacy/chat_methods.rb +0 -54
- data/lib/intelligence/adapters/open_ai/chat_methods.rb +0 -345
@@ -1,33 +1,33 @@
|
|
1
|
-
require_relative '
|
1
|
+
require_relative 'generic/adapter'
|
2
2
|
|
3
3
|
module Intelligence
|
4
4
|
module SambaNova
|
5
5
|
|
6
|
-
class Adapter <
|
6
|
+
class Adapter < Generic::Adapter
|
7
7
|
|
8
8
|
chat_request_uri "https://api.sambanova.ai/v1/chat/completions"
|
9
9
|
|
10
|
-
|
10
|
+
schema do
|
11
11
|
|
12
12
|
# normalized properties for all endpoints
|
13
|
-
|
13
|
+
key String
|
14
14
|
|
15
15
|
# properties for generative text endpoints
|
16
|
-
|
16
|
+
chat_options do
|
17
17
|
|
18
18
|
# normalized properties for samba nova generative text endpoint
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
19
|
+
model String
|
20
|
+
max_tokens Integer
|
21
|
+
temperature Float
|
22
|
+
top_p Float
|
23
|
+
top_k Float
|
24
|
+
stop String, array: true
|
25
|
+
stream [ TrueClass, FalseClass ]
|
26
26
|
|
27
27
|
# samba nova properties for samba nova generative text endpoint
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
repetition_penalty Float
|
29
|
+
stream_options do
|
30
|
+
include_usage [ TrueClass, FalseClass ]
|
31
31
|
end
|
32
32
|
|
33
33
|
end
|
@@ -35,8 +35,7 @@ module Intelligence
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def chat_result_error_attributes( response )
|
38
|
-
|
39
|
-
error_type, error_description = translate_error_response_status( response.status )
|
38
|
+
error_type, error_description = to_error_response( response.status )
|
40
39
|
result = {
|
41
40
|
error_type: error_type.to_s,
|
42
41
|
error_description: error_description
|
@@ -55,7 +54,6 @@ module Intelligence
|
|
55
54
|
end
|
56
55
|
|
57
56
|
result
|
58
|
-
|
59
57
|
end
|
60
58
|
|
61
59
|
end
|
@@ -1,39 +1,41 @@
|
|
1
|
-
require_relative '
|
1
|
+
require_relative 'generic/adapter'
|
2
2
|
|
3
3
|
module Intelligence
|
4
4
|
module TogetherAi
|
5
5
|
|
6
|
-
class Adapter <
|
6
|
+
class Adapter < Generic::Adapter
|
7
7
|
|
8
8
|
chat_request_uri "https://api.together.xyz/v1/chat/completions"
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
10
|
+
schema do
|
11
|
+
key String
|
12
|
+
chat_options do
|
13
|
+
model String
|
14
|
+
temperature Float
|
15
|
+
top_p Float
|
16
|
+
top_k Integer
|
17
|
+
n Integer
|
18
|
+
max_tokens Float
|
19
|
+
stop String, array: true
|
20
|
+
stream [ TrueClass, FalseClass ]
|
21
|
+
frequency_penalty Float
|
22
|
+
presence_penalty Float
|
23
|
+
repetition_penalty Float
|
24
|
+
user String
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
def
|
28
|
+
def to_end_reason( end_result )
|
29
29
|
case end_result
|
30
|
-
when 'eos'
|
30
|
+
when 'eos', 'stop'
|
31
31
|
:ended
|
32
|
-
|
32
|
+
# unfortunatelly eos seems to only work with certain models while others always return
|
33
|
+
# stop so for now tomorrow ai will not support :end_sequence_encountered
|
34
|
+
# when 'stop'
|
35
|
+
# :end_sequence_encountered
|
36
|
+
when 'length'
|
33
37
|
:token_limit_exceeded
|
34
|
-
when '
|
35
|
-
:end_sequence_encountered
|
36
|
-
when 'function_call'
|
38
|
+
when 'tool_calls'
|
37
39
|
:tool_called
|
38
40
|
else
|
39
41
|
nil
|
@@ -1,16 +1,12 @@
|
|
1
1
|
module Intelligence
|
2
2
|
class Conversation
|
3
3
|
|
4
|
-
|
4
|
+
include DynamicSchema::Definable
|
5
|
+
include DynamicSchema::Buildable
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.build( attributes = nil, &block )
|
12
|
-
configuration = self.configure( attributes, &block )
|
13
|
-
self.new( configuration.to_h )
|
7
|
+
schema do
|
8
|
+
system_message default: { role: :system }, &Message.schema
|
9
|
+
message as: :messages, array: true, &Message.schema
|
14
10
|
end
|
15
11
|
|
16
12
|
attr_reader :system_message
|
@@ -22,7 +18,7 @@ module Intelligence
|
|
22
18
|
@messages = []
|
23
19
|
@tools = []
|
24
20
|
if attributes
|
25
|
-
if attributes[ :system_message ]
|
21
|
+
if attributes[ :system_message ]&.any?
|
26
22
|
system_message = Message.new(
|
27
23
|
attributes[ :system_message ][ :role ],
|
28
24
|
attributes[ :system_message ]
|
@@ -64,6 +60,11 @@ module Intelligence
|
|
64
60
|
|
65
61
|
alias :<< :append_message
|
66
62
|
|
63
|
+
def append_tool( *tools )
|
64
|
+
@tools.concat( tools.flatten )
|
65
|
+
self
|
66
|
+
end
|
67
|
+
|
67
68
|
def to_h
|
68
69
|
result = {}
|
69
70
|
result[ :system_message ] = @system_message.to_h if @system_message
|
data/lib/intelligence/message.rb
CHANGED
@@ -1,33 +1,47 @@
|
|
1
1
|
module Intelligence
|
2
2
|
class Message
|
3
3
|
|
4
|
-
|
4
|
+
include DynamicSchema::Definable
|
5
5
|
|
6
6
|
ROLES = [ :system, :user, :assistant ]
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
7
|
+
schema do
|
8
|
+
role Symbol, required: true
|
9
|
+
content array: true, as: :contents do
|
10
|
+
type Symbol, default: :text
|
11
|
+
|
12
|
+
# note: we replicate these schema elements of the individual content types here to
|
13
|
+
# provide more semantic flexibility when building a message; we don't delegate to the
|
14
|
+
# individual content type schemas because, unlike for a specific content type, not all
|
15
|
+
# attributes are required
|
16
|
+
|
17
|
+
# text
|
18
|
+
text String
|
19
|
+
# binary and file
|
20
|
+
content_type String
|
21
|
+
bytes String
|
22
|
+
uri URI
|
23
|
+
# tool call and tool result
|
24
|
+
tool_call_id String
|
25
|
+
tool_name String
|
26
|
+
tool_parameters [ Hash, String ]
|
27
|
+
tool_result [ Hash, String ]
|
15
28
|
end
|
16
29
|
end
|
17
30
|
|
18
|
-
configuration( &CONFIGURATION )
|
19
|
-
|
20
31
|
attr_reader :role
|
21
32
|
attr_reader :contents
|
22
33
|
|
23
34
|
##
|
24
|
-
# The build
|
25
|
-
# accepts message +attributes+ and a block, which may be combined when constructing
|
26
|
-
# The +role+ is required, either as
|
27
|
-
# an exception will be raised.
|
35
|
+
# The +build!+ class method constructs and returns a new +Message+ instance. The +build!+
|
36
|
+
# method accepts message +attributes+ and a block, which may be combined when constructing
|
37
|
+
# a +Message+. The +role+ is required, either as an attribute or in the block. If the +role+
|
38
|
+
# is not present, an exception will be raised.
|
28
39
|
#
|
29
40
|
# The block offers the +role+ method, as well as a +content+ method permiting the caller to
|
30
41
|
# set the role and add content respectivelly.
|
42
|
+
#
|
43
|
+
# Note that there is no corresponding +build+ method because a +Message+ strictly requires a
|
44
|
+
# +role+. It cannot be constructed without one.
|
31
45
|
#
|
32
46
|
# === examples
|
33
47
|
#
|
@@ -50,19 +64,21 @@ module Intelligence
|
|
50
64
|
# content type: :binary do
|
51
65
|
# content_type 'image/png'
|
52
66
|
# bytes File.binread( '99_red_balloons.png' )
|
67
|
+
# end
|
68
|
+
# end
|
53
69
|
#
|
54
70
|
def self.build!( attributes = nil, &block )
|
55
|
-
|
56
|
-
self.new(
|
71
|
+
attributes = self.builder.build!( attributes, &block )
|
72
|
+
self.new( attributes[ :role ], attributes )
|
57
73
|
end
|
58
74
|
|
59
75
|
def initialize( role, attributes = nil )
|
60
|
-
|
61
|
-
unless ROLES.include?( role&.to_sym )
|
62
|
-
|
63
|
-
@role = role.to_sym
|
76
|
+
@role = role&.to_sym
|
64
77
|
@contents = []
|
65
78
|
|
79
|
+
raise ArgumentError.new( "The role is invalid. It must be one of #{ROLES.join( ', ' )}." ) \
|
80
|
+
unless ROLES.include?( @role )
|
81
|
+
|
66
82
|
if attributes && attributes[ :contents ]
|
67
83
|
attributes[ :contents ].each do | content |
|
68
84
|
@contents << MessageContent.build!( content[ :type ], content )
|
@@ -93,18 +109,11 @@ module Intelligence
|
|
93
109
|
def text
|
94
110
|
result = []
|
95
111
|
each_content do | content |
|
96
|
-
result << content.text if content.is_a?( MessageContent::Text )
|
112
|
+
result << content.text if content.is_a?( MessageContent::Text ) && content.text
|
97
113
|
end
|
98
114
|
result.join( "\n" )
|
99
115
|
end
|
100
116
|
|
101
|
-
def to_h
|
102
|
-
{
|
103
|
-
role: @role,
|
104
|
-
contents: @contents.map { | c | c.to_h }
|
105
|
-
}
|
106
|
-
end
|
107
|
-
|
108
117
|
def each_content( &block )
|
109
118
|
@contents.each( &block )
|
110
119
|
end
|
@@ -116,5 +125,12 @@ module Intelligence
|
|
116
125
|
|
117
126
|
alias :<< :append_content
|
118
127
|
|
128
|
+
def to_h
|
129
|
+
{
|
130
|
+
role: @role,
|
131
|
+
contents: @contents.map { | c | c.to_h }
|
132
|
+
}
|
133
|
+
end
|
134
|
+
|
119
135
|
end
|
120
136
|
end
|
@@ -2,16 +2,9 @@ module Intelligence
|
|
2
2
|
module MessageContent
|
3
3
|
|
4
4
|
class Base
|
5
|
-
|
5
|
+
include DynamicSchema::Definable
|
6
|
+
include DynamicSchema::Buildable
|
6
7
|
|
7
|
-
def self.build( attributes = nil, &block )
|
8
|
-
self.new( self.configure( attributes, &block ) )
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.build!( attributes = nil, &block )
|
12
|
-
self.new( self.configure!( attributes, &block ) )
|
13
|
-
end
|
14
|
-
|
15
8
|
def initialize( attributes = {} )
|
16
9
|
attributes.each do | key, value |
|
17
10
|
instance_variable_set( "@#{key}", value.freeze ) if self.respond_to?( "#{key}" )
|
@@ -3,9 +3,9 @@ module Intelligence
|
|
3
3
|
|
4
4
|
class Binary < Base
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
schema do
|
7
|
+
content_type String, required: true
|
8
|
+
bytes String, required: true
|
9
9
|
end
|
10
10
|
|
11
11
|
attr_reader :content_type
|
@@ -3,8 +3,8 @@ module Intelligence
|
|
3
3
|
|
4
4
|
class Text < Base
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
schema do
|
7
|
+
text String, required: true
|
8
8
|
end
|
9
9
|
|
10
10
|
attr_reader :text
|
@@ -13,6 +13,14 @@ module Intelligence
|
|
13
13
|
( text || false ) && text.respond_to?( :empty? ) && !text.empty?
|
14
14
|
end
|
15
15
|
|
16
|
+
def merge( other )
|
17
|
+
other_text = other.text
|
18
|
+
text = @text
|
19
|
+
text = ( @text || '' ) + other_text if other_text
|
20
|
+
|
21
|
+
self.class.new( text: text )
|
22
|
+
end
|
23
|
+
|
16
24
|
def to_h
|
17
25
|
{ type: :text, text: text }
|
18
26
|
end
|
@@ -3,15 +3,71 @@ module Intelligence
|
|
3
3
|
|
4
4
|
class ToolCall < Base
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
schema do
|
7
|
+
tool_call_id String
|
8
|
+
tool_name String, required: true
|
9
|
+
tool_parameters [ Hash, String ]
|
10
10
|
end
|
11
11
|
|
12
12
|
attr_reader :tool_call_id
|
13
13
|
attr_reader :tool_name
|
14
|
-
|
14
|
+
|
15
|
+
def tool_parameters( options = nil )
|
16
|
+
return @tool_parameters if @tool_parameters.nil? || @tool_parameters.is_a?( Hash )
|
17
|
+
|
18
|
+
options = options || {}
|
19
|
+
parse = options.key?( :parse ) ? options[ :parse ] : true
|
20
|
+
|
21
|
+
return @parsed_tool_parameters if @parsed_tool_parameters && parse
|
22
|
+
|
23
|
+
tool_parameters = ( options.key?( :repair ) ? options[ :repair ] : true ) ?
|
24
|
+
JSON.repair( @tool_parameters ) : @tool_parameters
|
25
|
+
|
26
|
+
if parse
|
27
|
+
@parsed_tool_parameters = tool_parameters =
|
28
|
+
JSON.parse(
|
29
|
+
tool_parameters,
|
30
|
+
parse.is_a?( Hash ) ? parse : { symbolize_names: true }
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
tool_parameters
|
35
|
+
end
|
36
|
+
|
37
|
+
def valid?
|
38
|
+
tool_name && !tool_name.empty?
|
39
|
+
end
|
40
|
+
|
41
|
+
def merge( other_tool_call )
|
42
|
+
other_tool_call_id = other_tool_call.tool_call_id
|
43
|
+
other_tool_name = other_tool_call.tool_name
|
44
|
+
other_tool_parameters = other_tool_call.tool_parameters( repair: false, parse: false )
|
45
|
+
|
46
|
+
raise ArgumentError,
|
47
|
+
"The given tool call parameters are incompative with this tool's parameters." \
|
48
|
+
unless @tool_parameters.nil? || other_tool_parameters.nil? ||
|
49
|
+
@tool_parameters.is_a?( other_tool_parameters.class )
|
50
|
+
|
51
|
+
tool_call_id = other_tool_call_id.nil? ?
|
52
|
+
@tool_call_id : @tool_call_id || '' + other_tool_call_id
|
53
|
+
tool_name = other_tool_name.nil? ?
|
54
|
+
@tool_name : @tool_name || '' + other_tool_name
|
55
|
+
tool_parameters = @tool_parameters
|
56
|
+
|
57
|
+
unless other_tool_parameters.nil?
|
58
|
+
if other_tool_parameters.is_a?( Hash )
|
59
|
+
tool_parameters = ( tool_parameters || {} ).merge( other_tool_parameters )
|
60
|
+
else
|
61
|
+
tool_parameters = ( tool_parameters || '' ) + other_tool_parameters
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
self.class.new(
|
66
|
+
tool_call_id: tool_call_id,
|
67
|
+
tool_name: tool_name,
|
68
|
+
tool_parameters: tool_parameters
|
69
|
+
)
|
70
|
+
end
|
15
71
|
|
16
72
|
def to_h
|
17
73
|
{
|
@@ -3,22 +3,27 @@ module Intelligence
|
|
3
3
|
|
4
4
|
class ToolResult < Base
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
schema do
|
7
|
+
tool_call_id
|
8
|
+
tool_name String, required: true
|
9
|
+
tool_result [ Hash, String ]
|
10
10
|
end
|
11
11
|
|
12
12
|
attr_reader :tool_call_id
|
13
13
|
attr_reader :tool_name
|
14
14
|
attr_reader :tool_result
|
15
15
|
|
16
|
+
def valid?
|
17
|
+
tool_call_id && !tool_call_id.empty? &&
|
18
|
+
tool_name && !tool_name.empty?
|
19
|
+
end
|
20
|
+
|
16
21
|
def to_h
|
17
22
|
{
|
18
|
-
type: :
|
23
|
+
type: :tool_result,
|
19
24
|
tool_call_id: tool_call_id,
|
20
25
|
tool_name: tool_name,
|
21
|
-
|
26
|
+
tool_result: tool_result
|
22
27
|
}.compact
|
23
28
|
end
|
24
29
|
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
module Intelligence
|
2
|
+
##
|
3
|
+
# The +Tool+ class instance encpasulates a definition of a tool that may be executed by a
|
4
|
+
# model. The properies of a tool include a unique name, a comprehensive description of what
|
5
|
+
# the tool does and a set of properies that describe the arguments the tool accepts, each
|
6
|
+
# with its own type and validation rules.
|
7
|
+
#
|
8
|
+
# A +Tool+ instance does not implement the tool, only the defintion of the tool, which is
|
9
|
+
# presented to the model to allow it to make a tool call.
|
10
|
+
#
|
11
|
+
# == Configuration
|
12
|
+
#
|
13
|
+
# A tool MUST include the +name+ and +description+ and may include any number of
|
14
|
+
# +arguments+ as well as a +required+ attribute with a list of the required arguments.
|
15
|
+
# Each argument must include a +name+ and may include +description+, +type+, a list of
|
16
|
+
# acceptable values through +enum+ and a +minimum+ and +maximum+ for numerical
|
17
|
+
# arguments.
|
18
|
+
#
|
19
|
+
# === Tool Attributes
|
20
|
+
#
|
21
|
+
# - +name+: A +String+ representing the unique name of the tool. *required*
|
22
|
+
# - +description+: A +String+ describing it's purpose and functionality. *required*
|
23
|
+
#
|
24
|
+
# === Argument Attributes
|
25
|
+
#
|
26
|
+
# An argument is defined through the +argument+ block. The block MUST include the +name+,
|
27
|
+
# while all other attributes, including the +description+ and +type+ are all optional.
|
28
|
+
#
|
29
|
+
# - +type+:
|
30
|
+
# A +String+ indicating the data type of the property. Accepted values include +string+,
|
31
|
+
# +number+, +integer+, +array+, +boolean+, +object+.
|
32
|
+
# - +name+: A +String+ representing the name of the property. *required*
|
33
|
+
# - +description+: A +String+ that describes the property and its purpose. *required*
|
34
|
+
# - +minimum+: An +Integer+ or +Float+ specifying the minimum value for numerical properties.
|
35
|
+
# - +maximum+: An +Integer+ or +Float+ specifying the maximum value for numerical properties.
|
36
|
+
# - +enum+:
|
37
|
+
# An +Array+ of acceptable values for the property. Note that ( as per the JSON schema
|
38
|
+
# specification ) an enum could be composed of mixed types but this is discouraged.
|
39
|
+
# - +required:
|
40
|
+
# A boolean ( +TrueClass+, +FalseClass+ ) that specifying if the argument is required.
|
41
|
+
#
|
42
|
+
# === Examples
|
43
|
+
#
|
44
|
+
# weather_tool = Tool.build! do
|
45
|
+
# name :get_weather_by_locality
|
46
|
+
# description \
|
47
|
+
# "The get_weather_by_locality tool will return the current weather in a given locality " \
|
48
|
+
# "( city, state or province, and country )."
|
49
|
+
# argument name: 'city', type: 'string', required: true do
|
50
|
+
# description "The city or town for which the current weather should be returned."
|
51
|
+
# end
|
52
|
+
# argument name: 'state', type: 'string' do
|
53
|
+
# description \
|
54
|
+
# "The state or province for which the current weather should be returned. If this is " \
|
55
|
+
# "not provided the largest or most prominent city with the given name, in the given " \
|
56
|
+
# "country, will be assumed."
|
57
|
+
# end
|
58
|
+
# argument name: 'country', type: 'string', required: true do
|
59
|
+
# description "The city or town for which the current weather should be returned."
|
60
|
+
# end
|
61
|
+
# end
|
62
|
+
#
|
63
|
+
# web_browser_tool = Tool.build!
|
64
|
+
# name :get_web_page
|
65
|
+
# description "The get_web_page tool will return the content of a web page, given a page url."
|
66
|
+
# argument name: :url, type: 'string', required: true do
|
67
|
+
# description \
|
68
|
+
# "The url of the page including the scheme.\n"
|
69
|
+
# "Examples:\n"
|
70
|
+
# " https://example.com\n"
|
71
|
+
# " https://www.iana.org/help/example-domains\n"
|
72
|
+
# end
|
73
|
+
# argument name: :format do
|
74
|
+
# description \
|
75
|
+
# "The format of the returned content. By default you will receive 'markdown'. You " \
|
76
|
+
# "should specify 'html' only if it is specifically required to fullfill the user " \
|
77
|
+
# "request."
|
78
|
+
# enum [ 'html', 'markdown' ]
|
79
|
+
# end
|
80
|
+
# argument name: :content do
|
81
|
+
# description \
|
82
|
+
# "The content of the page to be returned. By default you will receive only the 'main' " \
|
83
|
+
# "content, excluding header, footer, menu, advertising and other miscelanous elements. " \
|
84
|
+
# "You should request 'full' only if absolutely neccessry to fullfill the user request. " \
|
85
|
+
# enum [ 'main', 'full' ]
|
86
|
+
# end
|
87
|
+
# argument name: :include_tags do
|
88
|
+
# description "If content is set to html some tags will."
|
89
|
+
# end
|
90
|
+
# end
|
91
|
+
#
|
92
|
+
class Tool
|
93
|
+
include DynamicSchema::Definable
|
94
|
+
include DynamicSchema::Buildable
|
95
|
+
|
96
|
+
ARGUMENT_TYPES = [ 'string', 'number', 'integer', 'array', 'boolean', 'object' ]
|
97
|
+
|
98
|
+
ARGUMENT_SCHEMA = proc do
|
99
|
+
type String, in: ARGUMENT_TYPES
|
100
|
+
name String, required: true
|
101
|
+
description String, required: true
|
102
|
+
required [ TrueClass, FalseClass ]
|
103
|
+
# note that an enum does not require a type as it implicitly restricts the argument
|
104
|
+
# to specific values
|
105
|
+
enum array: true
|
106
|
+
# for arguments of type number and integer
|
107
|
+
minimum [ Integer, Float ]
|
108
|
+
maximum [ Integer, Float ]
|
109
|
+
# for arguments of type array
|
110
|
+
maximum_items Integer, as: :maxItems
|
111
|
+
minimum_items Integer, as: :minItems
|
112
|
+
unique_items [ TrueClass, FalseClass ]
|
113
|
+
items do
|
114
|
+
type in: ARGUMENT_TYPES
|
115
|
+
property array: true, as: :properties, &ARGUMENT_SCHEMA
|
116
|
+
end
|
117
|
+
# for arguments of type object
|
118
|
+
property array: true, as: :properties, &ARGUMENT_SCHEMA
|
119
|
+
end
|
120
|
+
|
121
|
+
schema do
|
122
|
+
name String, required: true
|
123
|
+
description String, required: true
|
124
|
+
argument array: true, as: :properties do
|
125
|
+
self.instance_eval( &ARGUMENT_SCHEMA )
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def initialize( attributes = nil )
|
130
|
+
@properies = attributes
|
131
|
+
end
|
132
|
+
|
133
|
+
def to_h
|
134
|
+
@properies.to_h
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
data/lib/intelligence/version.rb
CHANGED
data/lib/intelligence.rb
CHANGED
@@ -2,8 +2,9 @@ require 'json'
|
|
2
2
|
require 'base64'
|
3
3
|
|
4
4
|
require 'faraday'
|
5
|
-
require '
|
5
|
+
require 'dynamic_schema'
|
6
6
|
require 'mime-types'
|
7
|
+
require 'json/repair'
|
7
8
|
|
8
9
|
require 'intelligence/version'
|
9
10
|
|
@@ -14,6 +15,7 @@ require 'intelligence/unsupported_content_error'
|
|
14
15
|
require 'intelligence/error_result'
|
15
16
|
require 'intelligence/chat_error_result'
|
16
17
|
|
18
|
+
require 'intelligence/tool'
|
17
19
|
require 'intelligence/message_content'
|
18
20
|
require 'intelligence/message'
|
19
21
|
require 'intelligence/conversation'
|