soka 0.0.1 → 0.0.3
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/CHANGELOG.md +38 -0
- data/CLAUDE.md +347 -188
- data/README.md +137 -9
- data/examples/10_think_in_languages.rb +113 -0
- data/examples/2_event_handling.rb +5 -4
- data/examples/3_memory.rb +4 -2
- data/examples/4_hooks.rb +0 -1
- data/examples/9_custom_instructions.rb +190 -0
- data/lib/soka/agent.rb +22 -4
- data/lib/soka/agent_tool.rb +1 -1
- data/lib/soka/agents/dsl_methods.rb +14 -1
- data/lib/soka/engines/base.rb +6 -4
- data/lib/soka/engines/concerns/prompt_template.rb +44 -53
- data/lib/soka/engines/concerns/response_parser.rb +60 -0
- data/lib/soka/engines/concerns/response_processor.rb +5 -6
- data/lib/soka/engines/concerns/result_builder.rb +25 -0
- data/lib/soka/engines/react.rb +4 -26
- data/lib/soka/engines/reasoning_context.rb +4 -2
- data/lib/soka/result.rb +1 -5
- data/lib/soka/thoughts_memory.rb +0 -12
- data/lib/soka/version.rb +1 -1
- data/lib/soka.rb +0 -3
- metadata +7 -46
- data/lib/soka/test_helpers.rb +0 -162
data/lib/soka/test_helpers.rb
DELETED
@@ -1,162 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Soka
|
4
|
-
# RSpec test helpers for Soka framework
|
5
|
-
module TestHelpers
|
6
|
-
def self.included(base)
|
7
|
-
base.class_eval do
|
8
|
-
let(:mock_llm_response) { nil }
|
9
|
-
let(:mock_tool_responses) { {} }
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def mock_ai_response(response_data)
|
14
|
-
@mock_llm_response = response_data
|
15
|
-
|
16
|
-
# Create a mock LLM that returns the specified response
|
17
|
-
mock_llm = instance_double(Soka::LLM::Base)
|
18
|
-
|
19
|
-
# Format the response content based on the response data
|
20
|
-
content = build_react_content(response_data)
|
21
|
-
|
22
|
-
result = create_mock_llm_result(content)
|
23
|
-
|
24
|
-
allow(mock_llm).to receive(:chat).and_return(result)
|
25
|
-
allow(Soka::LLM).to receive(:new).and_return(mock_llm)
|
26
|
-
|
27
|
-
mock_llm
|
28
|
-
end
|
29
|
-
|
30
|
-
def create_mock_llm_result(content)
|
31
|
-
Soka::LLM::Result.new(
|
32
|
-
model: 'mock-model',
|
33
|
-
content: content,
|
34
|
-
input_tokens: 100,
|
35
|
-
output_tokens: 200,
|
36
|
-
finish_reason: 'stop',
|
37
|
-
raw_response: { mock: true }
|
38
|
-
)
|
39
|
-
end
|
40
|
-
|
41
|
-
def mock_tool_response(tool_class, response)
|
42
|
-
@mock_tool_responses[tool_class] = response
|
43
|
-
|
44
|
-
# Create a mock instance of the tool
|
45
|
-
mock_tool = instance_double(tool_class)
|
46
|
-
allow(mock_tool).to receive(:class).and_return(tool_class)
|
47
|
-
allow(mock_tool).to receive(:execute).and_return(response)
|
48
|
-
|
49
|
-
# Allow the tool class to be instantiated with the mock
|
50
|
-
allow(tool_class).to receive(:new).and_return(mock_tool)
|
51
|
-
|
52
|
-
mock_tool
|
53
|
-
end
|
54
|
-
|
55
|
-
def allow_tool_to_fail(tool_class, error)
|
56
|
-
mock_tool = instance_double(tool_class)
|
57
|
-
allow(mock_tool).to receive(:class).and_return(tool_class)
|
58
|
-
allow(mock_tool).to receive(:execute).and_raise(error)
|
59
|
-
|
60
|
-
allow(tool_class).to receive(:new).and_return(mock_tool)
|
61
|
-
|
62
|
-
mock_tool
|
63
|
-
end
|
64
|
-
|
65
|
-
def create_test_agent(options = {})
|
66
|
-
Class.new(Soka::Agent) do
|
67
|
-
provider :gemini
|
68
|
-
model 'gemini-2.5-flash'
|
69
|
-
max_iterations 5
|
70
|
-
timeout 10
|
71
|
-
end.new(**options)
|
72
|
-
end
|
73
|
-
|
74
|
-
def create_test_tool(name: 'test_tool', description: 'Test tool', &block)
|
75
|
-
Class.new(Soka::AgentTool) do
|
76
|
-
desc description
|
77
|
-
|
78
|
-
define_singleton_method :tool_name do
|
79
|
-
name
|
80
|
-
end
|
81
|
-
|
82
|
-
define_method :call, &block || -> { 'Test response' }
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def stub_env(env_vars)
|
87
|
-
env_vars.each do |key, value|
|
88
|
-
allow(ENV).to receive(:fetch).with(key).and_return(value)
|
89
|
-
allow(ENV).to receive(:[]).with(key).and_return(value)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
def with_configuration
|
94
|
-
original_config = Soka.configuration
|
95
|
-
Soka.reset!
|
96
|
-
yield
|
97
|
-
ensure
|
98
|
-
Soka.configuration = original_config
|
99
|
-
end
|
100
|
-
|
101
|
-
private
|
102
|
-
|
103
|
-
def build_react_content(response_data)
|
104
|
-
content = []
|
105
|
-
|
106
|
-
# Build thoughts and actions
|
107
|
-
response_data[:thoughts].each_with_index do |thought_data, _index|
|
108
|
-
content << "<Thought>#{thought_data[:thought]}</Thought>"
|
109
|
-
|
110
|
-
next unless thought_data[:action]
|
111
|
-
|
112
|
-
action = thought_data[:action]
|
113
|
-
params_json = action[:params].to_json
|
114
|
-
|
115
|
-
content << build_action_content(action[:tool], params_json)
|
116
|
-
|
117
|
-
# The observation will be added by the engine
|
118
|
-
end
|
119
|
-
|
120
|
-
# Add final answer if present
|
121
|
-
content << "<Final_Answer>#{response_data[:final_answer]}</Final_Answer>" if response_data[:final_answer]
|
122
|
-
|
123
|
-
content.join("\n")
|
124
|
-
end
|
125
|
-
|
126
|
-
def build_action_content(tool, params_json)
|
127
|
-
<<~ACTION
|
128
|
-
<Action>
|
129
|
-
Tool: #{tool}
|
130
|
-
Parameters: #{params_json}
|
131
|
-
</Action>
|
132
|
-
ACTION
|
133
|
-
end
|
134
|
-
|
135
|
-
# Matcher helpers for RSpec
|
136
|
-
module Matchers
|
137
|
-
def be_successful
|
138
|
-
satisfy(&:successful?)
|
139
|
-
end
|
140
|
-
|
141
|
-
def be_failed
|
142
|
-
satisfy(&:failed?)
|
143
|
-
end
|
144
|
-
|
145
|
-
def final_answer?(expected = nil)
|
146
|
-
if expected
|
147
|
-
satisfy { |result| result.final_answer == expected }
|
148
|
-
else
|
149
|
-
satisfy { |result| !result.final_answer.nil? }
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
def thoughts_count?(count)
|
154
|
-
satisfy { |result| result.thoughts.length == count }
|
155
|
-
end
|
156
|
-
|
157
|
-
def confidence_score_above?(threshold)
|
158
|
-
satisfy { |result| result.confidence_score > threshold }
|
159
|
-
end
|
160
|
-
end
|
161
|
-
end
|
162
|
-
end
|