Commit 87d4082
Changed files (3)
lib
elelem
ollama
spec
elelem
ollama
lib/elelem/ollama/provider.rb
@@ -25,12 +25,12 @@ module Elelem
@http = http
end
- def fetch(messages, tools = [], &block)
+ def fetch(messages, tools = [], &)
tool_calls = []
body = build_request_body(messages, tools)
stream(body) do |event|
- handle_event(event, tool_calls, &block)
+ handle_event(event, tool_calls, &)
end
tool_calls
@@ -97,18 +97,18 @@ module Elelem
block.call(type: "thinking", text: message["thinking"]) if message["thinking"]
end
- if message["tool_calls"]
- parsed = parse_tool_calls(message["tool_calls"])
- parsed.each { |tc| block.call(tc.merge(type: "doing")) }
- tool_calls.concat(parsed)
- end
+ return unless message["tool_calls"]
+
+ parsed = parse_tool_calls(message["tool_calls"])
+ parsed.each { |tc| block.call(tc.merge(type: "doing")) }
+ tool_calls.concat(parsed)
end
- def stream(body)
+ def stream(body, &)
http.post(@uri, body: body) do |response|
raise "HTTP #{response.code}: #{response.body}" unless response.is_a?(Net::HTTPSuccess)
- read_ndjson_stream(response) { |event| yield event }
+ read_ndjson_stream(response, &)
end
end
@@ -147,7 +147,7 @@ module Elelem
@open_timeout = open_timeout
end
- def post(uri, body:)
+ def post(uri, body:, &)
request = Net::HTTP::Post.new(uri)
request["content-type"] = "application/json"
request.body = JSON.generate(body)
@@ -158,7 +158,7 @@ module Elelem
http.open_timeout = @open_timeout
http.start do |conn|
- conn.request(request) { |response| yield response }
+ conn.request(request, &)
end
end
end
lib/elelem/ollama.rb
@@ -13,8 +13,8 @@ Elelem.configure do |config|
Elelem::Ollama::Provider.new(
model: ENV.fetch("OLLAMA_MODEL", "gpt-oss:latest"),
host: ENV.fetch("OLLAMA_HOST", "localhost:11434"),
- think: ENV["OLLAMA_THINK"],
- keep_alive: ENV["OLLAMA_KEEP_ALIVE"]
+ think: ENV.fetch("OLLAMA_THINK", nil),
+ keep_alive: ENV.fetch("OLLAMA_KEEP_ALIVE", nil)
)
end
end
spec/elelem/ollama/provider_spec.rb
@@ -8,8 +8,8 @@ RSpec.describe Elelem::Ollama::Provider do
let(:body) { http.body }
let(:response) do
- ::Net::HTTPOK.new("1.1", "200", "OK").tap do |it|
- allow(it).to receive(:read_body).and_yield(%({"done":true,"message":{}}\n))
+ Net::HTTPOK.new("1.1", "200", "OK").tap do |res|
+ allow(res).to receive(:read_body).and_yield(%({"done":true,"message":{}}\n))
end
end
@@ -30,7 +30,7 @@ RSpec.describe Elelem::Ollama::Provider do
describe "#fetch" do
it "sends only model, messages and stream by default" do
- provider.fetch(messages) { }
+ provider.fetch(messages, nil)
expect(body).to eq(model: "gpt-oss:latest", messages:, stream: true)
end
@@ -38,7 +38,7 @@ RSpec.describe Elelem::Ollama::Provider do
it "sends tools when present" do
tools = [{ type: "function", function: { name: "read" } }]
- provider.fetch(messages, tools) { }
+ provider.fetch(messages, tools) {}
expect(body[:tools]).to eq(tools)
end
@@ -47,7 +47,7 @@ RSpec.describe Elelem::Ollama::Provider do
let(:params) { { think: "high", keep_alive: "30m", options: { num_ctx: 32_768 } } }
it "sends them in the request body" do
- provider.fetch(messages) { }
+ provider.fetch(messages, nil)
expect(body).to include(think: "high", keep_alive: "30m", options: { num_ctx: 32_768 })
end
@@ -57,7 +57,7 @@ RSpec.describe Elelem::Ollama::Provider do
let(:params) { { options: {} } }
it "omits options" do
- provider.fetch(messages) { }
+ provider.fetch(messages, nil)
expect(body).not_to have_key(:options)
end
@@ -67,7 +67,7 @@ RSpec.describe Elelem::Ollama::Provider do
let(:params) { { params: { format: "json", truncate: false, top_logprobs: 3 } } }
it "merges them into the request body" do
- provider.fetch(messages) { }
+ provider.fetch(messages, nil)
expect(body).to include(format: "json", truncate: false, top_logprobs: 3)
end
@@ -77,7 +77,7 @@ RSpec.describe Elelem::Ollama::Provider do
let(:params) { { think: "low", params: { think: "high" } } }
it "prefers the passthrough value" do
- provider.fetch(messages) { }
+ provider.fetch(messages, nil)
expect(body[:think]).to eq("high")
end