whoosh 1.8.0 → 1.9.1
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/whoosh/ai/llm.rb +38 -8
- data/lib/whoosh/ai.rb +2 -1
- data/lib/whoosh/app.rb +2 -23
- data/lib/whoosh/endpoint.rb +2 -0
- data/lib/whoosh/streaming/helpers.rb +29 -0
- data/lib/whoosh/version.rb +1 -1
- data/lib/whoosh.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 035c0a8c4cfc32ee16cba0eba7e84e23ec1c510f41bd541a8409fa59dc5dbbd2
|
|
4
|
+
data.tar.gz: aca768fe55956b76f30b9296fe9ee156b3547ce2089a49a5edcf18ab7ce33d07
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f3f877e2e128bd4b16ffece2ebf7a41b87678185906688cac6d514e70c28d0bb4c0bcebd1e466368a2fc591af83f8e89b6b15d5c6b59d493a8e5a2bc78c3da76
|
|
7
|
+
data.tar.gz: aca365000fb9776754d29038d60061dcf92f71ade132e0ea602f80f0defe47a2fe83d5c3ef435e6e241b9bbd2cbdcb9fd1f4c34d38f99f160befd1b0f322930c
|
data/lib/whoosh/ai/llm.rb
CHANGED
|
@@ -2,15 +2,48 @@
|
|
|
2
2
|
|
|
3
3
|
module Whoosh
|
|
4
4
|
module AI
|
|
5
|
+
# Bounded LRU cache. Ruby's Hash preserves insertion order, so we reorder
|
|
6
|
+
# on read (delete+reinsert) and evict the oldest entry when over capacity.
|
|
7
|
+
class LRUCache
|
|
8
|
+
def initialize(max_size)
|
|
9
|
+
@max_size = max_size
|
|
10
|
+
@store = {}
|
|
11
|
+
@mutex = Mutex.new
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def [](key)
|
|
15
|
+
@mutex.synchronize do
|
|
16
|
+
return nil unless @store.key?(key)
|
|
17
|
+
value = @store.delete(key)
|
|
18
|
+
@store[key] = value
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def []=(key, value)
|
|
23
|
+
@mutex.synchronize do
|
|
24
|
+
@store.delete(key) if @store.key?(key)
|
|
25
|
+
@store[key] = value
|
|
26
|
+
@store.shift while @store.size > @max_size
|
|
27
|
+
value
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def size
|
|
32
|
+
@store.size
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
DEFAULT_MODEL = "claude-sonnet-4-6"
|
|
37
|
+
DEFAULT_CACHE_MAX = 1000
|
|
38
|
+
|
|
5
39
|
class LLM
|
|
6
40
|
attr_reader :provider, :model
|
|
7
41
|
|
|
8
|
-
def initialize(provider: "auto", model: nil, cache_enabled: true)
|
|
42
|
+
def initialize(provider: "auto", model: nil, cache_enabled: true, cache_size: DEFAULT_CACHE_MAX)
|
|
9
43
|
@provider = provider
|
|
10
44
|
@model = model
|
|
11
45
|
@cache_enabled = cache_enabled
|
|
12
|
-
@cache = cache_enabled ?
|
|
13
|
-
@mutex = Mutex.new
|
|
46
|
+
@cache = cache_enabled ? LRUCache.new(cache_size) : nil
|
|
14
47
|
@ruby_llm = nil
|
|
15
48
|
end
|
|
16
49
|
|
|
@@ -32,10 +65,7 @@ module Whoosh
|
|
|
32
65
|
temperature: temperature
|
|
33
66
|
)
|
|
34
67
|
|
|
35
|
-
|
|
36
|
-
if use_cache && @cache
|
|
37
|
-
@mutex.synchronize { @cache[cache_key] = result }
|
|
38
|
-
end
|
|
68
|
+
@cache[cache_key] = result if use_cache && @cache
|
|
39
69
|
|
|
40
70
|
result
|
|
41
71
|
end
|
|
@@ -87,7 +117,7 @@ module Whoosh
|
|
|
87
117
|
|
|
88
118
|
if @ruby_llm
|
|
89
119
|
# Use ruby_llm gem
|
|
90
|
-
chat = RubyLLM.chat(model: model ||
|
|
120
|
+
chat = RubyLLM.chat(model: model || DEFAULT_MODEL)
|
|
91
121
|
chat.with_instructions(system) if system
|
|
92
122
|
response = chat.ask(messages.last[:content])
|
|
93
123
|
response.content
|
data/lib/whoosh/ai.rb
CHANGED
|
@@ -11,7 +11,8 @@ module Whoosh
|
|
|
11
11
|
LLM.new(
|
|
12
12
|
provider: ai_config["provider"] || "auto",
|
|
13
13
|
model: ai_config["model"],
|
|
14
|
-
cache_enabled: ai_config["cache"] != false
|
|
14
|
+
cache_enabled: ai_config["cache"] != false,
|
|
15
|
+
cache_size: ai_config["cache_size"] || DEFAULT_CACHE_MAX
|
|
15
16
|
)
|
|
16
17
|
end
|
|
17
18
|
end
|
data/lib/whoosh/app.rb
CHANGED
|
@@ -211,29 +211,8 @@ module Whoosh
|
|
|
211
211
|
end
|
|
212
212
|
end
|
|
213
213
|
|
|
214
|
-
# --- Streaming helpers ---
|
|
215
|
-
|
|
216
|
-
def stream(type, &block)
|
|
217
|
-
case type
|
|
218
|
-
when :sse
|
|
219
|
-
body = Streaming::StreamBody.new do |out|
|
|
220
|
-
sse = Streaming::SSE.new(out)
|
|
221
|
-
block.call(sse)
|
|
222
|
-
end
|
|
223
|
-
[200, Streaming::SSE.headers, body]
|
|
224
|
-
else
|
|
225
|
-
raise ArgumentError, "Unknown stream type: #{type}"
|
|
226
|
-
end
|
|
227
|
-
end
|
|
228
|
-
|
|
229
|
-
def stream_llm(&block)
|
|
230
|
-
body = Streaming::StreamBody.new do |out|
|
|
231
|
-
llm_stream = Streaming::LlmStream.new(out)
|
|
232
|
-
block.call(llm_stream)
|
|
233
|
-
llm_stream.finish
|
|
234
|
-
end
|
|
235
|
-
[200, Streaming::LlmStream.headers, body]
|
|
236
|
-
end
|
|
214
|
+
# --- Streaming helpers (stream, stream_llm) ---
|
|
215
|
+
include Streaming::Helpers
|
|
237
216
|
|
|
238
217
|
# WebSocket endpoint helper — use in handle_request, returns hijack response
|
|
239
218
|
def websocket(env, &block)
|
data/lib/whoosh/endpoint.rb
CHANGED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Whoosh
|
|
4
|
+
module Streaming
|
|
5
|
+
module Helpers
|
|
6
|
+
def stream(type, &block)
|
|
7
|
+
case type
|
|
8
|
+
when :sse
|
|
9
|
+
body = StreamBody.new do |out|
|
|
10
|
+
sse = SSE.new(out)
|
|
11
|
+
block.call(sse)
|
|
12
|
+
end
|
|
13
|
+
[200, SSE.headers, body]
|
|
14
|
+
else
|
|
15
|
+
raise ArgumentError, "Unknown stream type: #{type}"
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def stream_llm(&block)
|
|
20
|
+
body = StreamBody.new do |out|
|
|
21
|
+
llm_stream = LlmStream.new(out)
|
|
22
|
+
block.call(llm_stream)
|
|
23
|
+
llm_stream.finish
|
|
24
|
+
end
|
|
25
|
+
[200, LlmStream.headers, body]
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/whoosh/version.rb
CHANGED
data/lib/whoosh.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: whoosh
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.9.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Johannes Dwi Cahyo
|
|
@@ -264,6 +264,7 @@ files:
|
|
|
264
264
|
- lib/whoosh/storage.rb
|
|
265
265
|
- lib/whoosh/storage/local.rb
|
|
266
266
|
- lib/whoosh/storage/s3.rb
|
|
267
|
+
- lib/whoosh/streaming/helpers.rb
|
|
267
268
|
- lib/whoosh/streaming/llm_stream.rb
|
|
268
269
|
- lib/whoosh/streaming/sse.rb
|
|
269
270
|
- lib/whoosh/streaming/stream_body.rb
|