intelli_agent 0.2.8 → 0.2.10
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/lib/intelli_agent/openai.rb +33 -22
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2e54e7e019c57ad7e17a50264d734c159db622dffb08061b5a272641ab7e5a99
|
|
4
|
+
data.tar.gz: 0e97fcfd1a8feffbdfbbf072b463616d24071ee248a507a706e4d67876597104
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '09e61137807a651493053efe64017962fd262595b66230627147f8cb394c6f2dd9dfc6a6e992067e65662f584e6c109f46374d2f7d677fcf10b24a21914b4c41'
|
|
7
|
+
data.tar.gz: 3de2b502fa1cd60094e465d3cef74f58ce1ea31f704293628afbfbf5ce8350c45bfe3132eebec498fd1f674a320cc950901465e1881dea472ea5367e3e70722c
|
data/lib/intelli_agent/openai.rb
CHANGED
|
@@ -8,6 +8,8 @@ module IntelliAgent::OpenAI
|
|
|
8
8
|
MAX_TOKENS = ENV.fetch('OPENAI_MAX_TOKENS', 16_383).to_i
|
|
9
9
|
|
|
10
10
|
module ResponseExtender
|
|
11
|
+
def chat_params = self[:chat_params]
|
|
12
|
+
|
|
11
13
|
def message = dig('choices', 0, 'message')
|
|
12
14
|
|
|
13
15
|
def content = dig('choices', 0, 'message', 'content')
|
|
@@ -24,13 +26,29 @@ module IntelliAgent::OpenAI
|
|
|
24
26
|
|
|
25
27
|
functions_list = []
|
|
26
28
|
functions.map.with_index do |function, function_index|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
function_info = tool_calls.dig(function_index, 'function')
|
|
30
|
+
function_def = { id: function['id'], name: function_info['name'], arguments: Oj.load(function_info['arguments'], symbol_keys: true) }
|
|
31
|
+
|
|
32
|
+
def function_def.run(context:)
|
|
33
|
+
{
|
|
34
|
+
tool_call_id: self[:id],
|
|
35
|
+
role: :tool,
|
|
36
|
+
name: self[:name],
|
|
37
|
+
content: context.send(self[:name], **self[:arguments])
|
|
38
|
+
}
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
functions_list << function_def
|
|
29
42
|
end
|
|
30
43
|
|
|
31
44
|
functions_list
|
|
32
45
|
end
|
|
33
46
|
|
|
47
|
+
def functions_run_all(context:)
|
|
48
|
+
raise 'No functions to run' if functions.nil?
|
|
49
|
+
functions.map { |function| function.run(context:) }
|
|
50
|
+
end
|
|
51
|
+
|
|
34
52
|
def functions? = !functions.nil?
|
|
35
53
|
end
|
|
36
54
|
|
|
@@ -55,15 +73,15 @@ module IntelliAgent::OpenAI
|
|
|
55
73
|
response
|
|
56
74
|
end
|
|
57
75
|
|
|
58
|
-
def self.single_prompt(prompt:, model: :gpt_basic, response_format: nil, max_tokens: MAX_TOKENS, tools: nil, auto_run_functions: false, function_context: nil)
|
|
59
|
-
chat(messages: [{ user: prompt }], model:, response_format:, max_tokens:, tools:, auto_run_functions:, function_context:)
|
|
76
|
+
def self.single_prompt(prompt:, model: :gpt_basic, response_format: nil, max_tokens: MAX_TOKENS, store: true, metadata: nil, tools: nil, auto_run_functions: false, function_context: nil)
|
|
77
|
+
chat(messages: [{ user: prompt }], model:, response_format:, max_tokens:, store:, tools:, auto_run_functions:, function_context:)
|
|
60
78
|
end
|
|
61
79
|
|
|
62
|
-
def self.single_chat(system:, user:, model: :gpt_basic, response_format: nil, max_tokens: MAX_TOKENS, tools: nil, auto_run_functions: false, function_context: nil)
|
|
63
|
-
chat(messages: [{ system: }, { user: }], model:, response_format:, max_tokens:, tools:, auto_run_functions:, function_context:)
|
|
80
|
+
def self.single_chat(system:, user:, model: :gpt_basic, response_format: nil, max_tokens: MAX_TOKENS, store: true, metadata: nil, tools: nil, auto_run_functions: false, function_context: nil)
|
|
81
|
+
chat(messages: [{ system: }, { user: }], model:, response_format:, max_tokens:, store:, tools:, auto_run_functions:, function_context:)
|
|
64
82
|
end
|
|
65
83
|
|
|
66
|
-
def self.chat(messages:, model: :gpt_basic, response_format: nil, max_tokens: MAX_TOKENS, tools: nil, auto_run_functions: false, function_context: nil)
|
|
84
|
+
def self.chat(messages:, model: :gpt_basic, response_format: nil, max_tokens: MAX_TOKENS, store: true, metadata: nil, tools: nil, auto_run_functions: false, function_context: nil)
|
|
67
85
|
model = select_model(model)
|
|
68
86
|
|
|
69
87
|
# o1 models doesn't support max_tokens, instead max_completion_tokens
|
|
@@ -72,8 +90,9 @@ module IntelliAgent::OpenAI
|
|
|
72
90
|
|
|
73
91
|
messages = parse_messages(messages)
|
|
74
92
|
|
|
75
|
-
parameters = { model:, messages:, store:
|
|
76
|
-
|
|
93
|
+
parameters = { model:, messages:, store: }
|
|
94
|
+
parameters[:metadata] = metadata if metadata
|
|
95
|
+
|
|
77
96
|
parameters[:max_completion_tokens] = max_completion_tokens if is_o1_model
|
|
78
97
|
parameters[:max_tokens] = max_completion_tokens unless is_o1_model
|
|
79
98
|
|
|
@@ -81,26 +100,18 @@ module IntelliAgent::OpenAI
|
|
|
81
100
|
parameters[:tools] = tools if tools
|
|
82
101
|
|
|
83
102
|
response = OpenAI::Client.new.chat(parameters:)
|
|
103
|
+
|
|
104
|
+
response[:chat_params] = parameters
|
|
84
105
|
response.extend(ResponseExtender)
|
|
85
106
|
|
|
86
107
|
if response.functions? && auto_run_functions
|
|
87
108
|
raise 'Function context not provided for auto-running functions' if function_context.nil?
|
|
88
|
-
|
|
89
109
|
parameters[:messages] << response.message
|
|
110
|
+
parameters[:messages] += response.functions_run_all(context: function_context)
|
|
90
111
|
|
|
91
|
-
response
|
|
92
|
-
parameters[:messages] << {
|
|
93
|
-
tool_call_id: function[:id],
|
|
94
|
-
role: :tool,
|
|
95
|
-
name: function[:name],
|
|
96
|
-
content: function_context.send(function[:name], **function[:arguments])
|
|
97
|
-
}
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
response = OpenAI::Client.new.chat(parameters:)
|
|
101
|
-
response.extend(ResponseExtender)
|
|
112
|
+
response = chat(**parameters.except(:chat_params))
|
|
102
113
|
end
|
|
103
|
-
|
|
114
|
+
|
|
104
115
|
response
|
|
105
116
|
end
|
|
106
117
|
|