ai_sentinel 0.3.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 57b7b717300a40c7538e4a7da84bd574677c9bd38fa55c094ad44725354e07d8
4
- data.tar.gz: 1897eb521d5a9b2c71a1b1d54e7aa015172466accb65ef6c0bfa6a3b9ac5879a
3
+ metadata.gz: 54f5dcacdba120c16ed3a4c09384f8b981aef569b9fb66a350400ec8a3f13b4b
4
+ data.tar.gz: 0b15c9519a2085dcc7b3a3b920ff4b91e89f282da8346429f6152b886334225f
5
5
  SHA512:
6
- metadata.gz: 46a5467ba869e34a6b2e4f942c23cdcb719a8b2d8d6583ba29237873bb0145bed9ef736d6949bdf0434bf0f05fce8529b790b89f691594f40545667be12b574d
7
- data.tar.gz: 8d05fe5f7477260054a4d9083fa2cbdcecb0effc5c2c701b1768fae3882604e9a8840e0cb5ceba0fb11b61a2fc5a488f1e3eef61d298f3d1e06c6d6e60327e64
6
+ metadata.gz: 02f23261d5e52c11c03b9c15c7633f5eb12b54daacb49062d3cc8eb5be7168b72c1069e0c1607d3c02d13867af4545455dbd995eb49cc01dfc25007a8ed5d4ff
7
+ data.tar.gz: 8a9a3159203a5e5e580dfe2801237ff68067a3ef9b30b23389e4e05204851d5135ad6d38093d568bbfd6b328186180d8df56d706363cfc5d5059fb8a110ff067
@@ -32,7 +32,8 @@ module AiSentinel
32
32
  prompt_template: step.params[:prompt],
33
33
  system_template: step.params[:system],
34
34
  tool_executor: tool_executor,
35
- max_tool_rounds: step.params.fetch(:max_tool_rounds, configuration.max_tool_rounds)
35
+ max_tool_rounds: step.params.fetch(:max_tool_rounds, configuration.max_tool_rounds),
36
+ compaction_prompt: step.params[:compaction_prompt]
36
37
  )
37
38
  end
38
39
 
@@ -10,11 +10,12 @@ module AiSentinel
10
10
  Respond with ONLY the summary, no preamble or explanation.
11
11
  PROMPT
12
12
 
13
- attr_reader :context_key, :configuration
13
+ attr_reader :context_key, :configuration, :custom_compaction_prompt
14
14
 
15
- def initialize(context_key:, configuration:)
15
+ def initialize(context_key:, configuration:, compaction_prompt: nil)
16
16
  @context_key = context_key
17
17
  @configuration = configuration
18
+ @custom_compaction_prompt = compaction_prompt
18
19
  end
19
20
 
20
21
  def compact_if_needed
@@ -74,7 +75,7 @@ module AiSentinel
74
75
 
75
76
  result = provider.chat(
76
77
  prompt: prompt,
77
- system: SUMMARIZATION_PROMPT,
78
+ system: custom_compaction_prompt || SUMMARIZATION_PROMPT,
78
79
  workflow_name: nil,
79
80
  step_name: nil,
80
81
  remember: false
@@ -9,7 +9,8 @@ module AiSentinel
9
9
  API_VERSION = '2023-06-01'
10
10
 
11
11
  def chat(prompt:, system: nil, model: nil, workflow_name: nil, step_name: nil, remember: true,
12
- prompt_template: nil, system_template: nil, tool_executor: nil, max_tool_rounds: 10)
12
+ prompt_template: nil, system_template: nil, tool_executor: nil, max_tool_rounds: 10,
13
+ compaction_prompt: nil)
13
14
  model ||= configuration.model
14
15
  context_key = "#{workflow_name}:#{step_name}"
15
16
 
@@ -26,7 +27,8 @@ module AiSentinel
26
27
  assistant_text = extract_text(response_data)
27
28
  if remember
28
29
  save_context(context_key, prompt, assistant_text,
29
- prompt_template: prompt_template, system_template: system_template)
30
+ prompt_template: prompt_template, system_template: system_template,
31
+ compaction_prompt: compaction_prompt)
30
32
  end
31
33
 
32
34
  Actions::AiPrompt::Result.new(
@@ -66,7 +66,8 @@ module AiSentinel
66
66
  messages
67
67
  end
68
68
 
69
- def save_context(context_key, user_message, assistant_message, prompt_template: nil, system_template: nil)
69
+ def save_context(context_key, user_message, assistant_message, prompt_template: nil, system_template: nil,
70
+ compaction_prompt: nil)
70
71
  return unless Persistence::Database.connected?
71
72
 
72
73
  ctx = Persistence::Database.find_or_create_context(context_key)
@@ -81,7 +82,7 @@ module AiSentinel
81
82
 
82
83
  save_prompt_hash(context_key, prompt_template, system_template) if prompt_template
83
84
  prune_old_messages(ctx[:id])
84
- compact_context(context_key)
85
+ compact_context(context_key, compaction_prompt: compaction_prompt)
85
86
  end
86
87
 
87
88
  def save_prompt_hash(context_key, prompt_template, system_template)
@@ -91,8 +92,9 @@ module AiSentinel
91
92
  )
92
93
  end
93
94
 
94
- def compact_context(context_key)
95
- ContextCompactor.new(context_key: context_key, configuration: configuration).compact_if_needed
95
+ def compact_context(context_key, compaction_prompt: nil)
96
+ ContextCompactor.new(context_key: context_key, configuration: configuration,
97
+ compaction_prompt: compaction_prompt).compact_if_needed
96
98
  rescue StandardError => e
97
99
  AiSentinel.log_error(e, context: "Context compaction failed for '#{context_key}'")
98
100
  end
@@ -7,7 +7,8 @@ module AiSentinel
7
7
  module Providers
8
8
  class Openai < Base
9
9
  def chat(prompt:, system: nil, model: nil, workflow_name: nil, step_name: nil, remember: true,
10
- prompt_template: nil, system_template: nil, tool_executor: nil, max_tool_rounds: 10)
10
+ prompt_template: nil, system_template: nil, tool_executor: nil, max_tool_rounds: 10,
11
+ compaction_prompt: nil)
11
12
  model ||= configuration.model
12
13
  context_key = "#{workflow_name}:#{step_name}"
13
14
 
@@ -24,7 +25,8 @@ module AiSentinel
24
25
  assistant_text = extract_text(response_data)
25
26
  if remember
26
27
  save_context(context_key, prompt, assistant_text,
27
- prompt_template: prompt_template, system_template: system_template)
28
+ prompt_template: prompt_template, system_template: system_template,
29
+ compaction_prompt: compaction_prompt)
28
30
  end
29
31
 
30
32
  Actions::AiPrompt::Result.new(
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AiSentinel
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ai_sentinel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Celi