Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.
Ruby agplai-assistantdockerdsgvogdprhotwireissue-trackerjira-alternativeknowledge-basellmmcp-servermeeting-notesopen-sourceproject-managementredmine-alternativeruby-on-railsself-hostedticketingtime-tracking
Give Gemini 2.5 a thinking budget on top of the answer budget
Guido Boyke8f53f54e4b78
diff --git a/app/services/lalabase/llm/google_vertex_provider.rb b/app/services/lalabase/llm/google_vertex_provider.rbindex fc28a68..3e08648 100644--- a/app/services/lalabase/llm/google_vertex_provider.rb+++ b/app/services/lalabase/llm/google_vertex_provider.rb@@ -58,6 +58,20 @@ module Lalabase# comment there) — unify in the base class at the third occurrence.CHARS_PER_TOKEN = 4.0+ # max_tokens is the budget of the ANSWER (Provider#chat contract). Gemini+ # 2.5 Flash and Pro think before they answer, dynamically up to 8,192+ # tokens, and the thought tokens count against maxOutputTokens: with the+ # bare limit a short answer could be left with almost nothing, and a+ # longer one stopped half way. So these two families get a+ # fixed thinking budget and the same reserve on top of the answer budget.+ # 2,048 lies inside both accepted ranges (Pro 128..32,768, it cannot turn+ # thinking off; Flash 1..24,576).+ # Not matched on purpose: Flash-Lite (a budget would switch thinking ON),+ # 2.0 Flash (no thinking) and Gemini 3 (thinkingLevel instead, and+ # thinkingBudget is deprecated there and can answer 400).+ THINKING_BUDGET = 2_048+ THINKING_MODEL_PATTERN = /\Agemini-2\.5-(?:pro|flash(?!-lite))/+# Gemini finishReason → canonical vocabulary (see Provider#chat). The# whole safety family collapses onto content_filter; unknown values pass# through so the Runner can log them. NB: Gemini reports STOP even for@@ -159,7 +173,7 @@ module Lalabasedef chat(messages:, tools: nil, model: nil, max_tokens: 1500, temperature: 0.7, provider_options: {})model ||= default_chat_model- payload = build_generate_payload(messages: messages, tools: tools, max_tokens: max_tokens,+ payload = build_generate_payload(messages: messages, tools: tools, model: model, max_tokens: max_tokens,temperature: temperature, provider_options: provider_options)response = post_json(model_path(model, :generateContent), payload)@@ -195,7 +209,7 @@ module Lalabaseraise ArgumentError, 'stream_chat requires a block' unless block_given?model ||= default_chat_model- payload = build_generate_payload(messages: messages, tools: nil, max_tokens: max_tokens,+ payload = build_generate_payload(messages: messages, tools: nil, model: model, max_tokens: max_tokens,temperature: temperature, provider_options: provider_options)start_time = Time.currentcaptured_content = +''@@ -387,13 +401,13 @@ module Lalabase# ---- request translation (canonical → Gemini) ------------------------- def build_generate_payload(messages:, tools:, max_tokens:, temperature:, provider_options: {})+ def build_generate_payload(messages:, tools:, model:, max_tokens:, temperature:, provider_options: {})system_prompt, conversation = split_system(messages)# provider_options are Gemini generationConfig extras (topP, topK,# stopSequences, …) — merged LAST so a raw override wins, the same# contract as the other providers' param merge.- generation_config = { maxOutputTokens: max_tokens, temperature: temperature }- .merge(provider_options.to_h.symbolize_keys)+ generation_config = answer_budget_config(model, max_tokens).merge(temperature: temperature)+ .merge(provider_options.to_h.symbolize_keys)payload = {contents: translate_messages_for_gemini(conversation),generationConfig: generation_config@@ -403,6 +417,14 @@ module Lalabasepayloadend+ # See THINKING_BUDGET: a thinking family gets the budget and the reserve,+ # every other model the bare limit, as before.+ def answer_budget_config(model, max_tokens)+ return { maxOutputTokens: max_tokens } unless THINKING_MODEL_PATTERN.match?(model.to_s)++ { maxOutputTokens: max_tokens + THINKING_BUDGET, thinkingConfig: { thinkingBudget: THINKING_BUDGET } }+ end+# A tool without arguments goes out without parameters. Gemini's schema subset refuses an# OBJECT with empty properties, and every tool schema goes with every answer: one tool# without arguments (help_contents) would fail every request on this provider.diff --git a/spec/services/lalabase/llm/google_vertex_provider_spec.rb b/spec/services/lalabase/llm/google_vertex_provider_spec.rbindex 797e196..185614c 100644--- a/spec/services/lalabase/llm/google_vertex_provider_spec.rb+++ b/spec/services/lalabase/llm/google_vertex_provider_spec.rb@@ -336,9 +336,57 @@ RSpec.describe Lalabase::Llm::GoogleVertexProvider doexpect(last_payload['contents'].map { |c| c['role'] }).to eq(%w[user model user])end- it 'sends maxOutputTokens and temperature in generationConfig (locked defaults 1500/0.7)' do- provider.chat(messages: messages)- expect(last_payload['generationConfig']).to eq('maxOutputTokens' => 1500, 'temperature' => 0.7)+ # max_tokens is the budget of the ANSWER. Gemini 2.5 Flash and Pro think+ # first, and their thought tokens count against maxOutputTokens: with the+ # bare limit a short answer could be left with almost nothing. So these+ # two families get a fixed thinking budget and the same reserve on top.+ describe 'answer budget and thinking reserve' do+ let(:budget) { described_class::THINKING_BUDGET }++ it 'gives the default model (gemini-2.5-flash) a thinking budget and adds it to maxOutputTokens' do+ provider.chat(messages: messages)+ expect(last_payload['generationConfig']).to eq(+ 'maxOutputTokens' => 1500 + budget, 'temperature' => 0.7,+ 'thinkingConfig' => { 'thinkingBudget' => budget }+ )+ end++ it 'does the same for gemini-2.5-pro, keeping the caller max_tokens as the answer part' do+ provider.chat(messages: messages, model: 'gemini-2.5-pro', max_tokens: 500)+ expect(last_payload['generationConfig']).to eq(+ 'maxOutputTokens' => 500 + budget, 'temperature' => 0.7,+ 'thinkingConfig' => { 'thinkingBudget' => budget }+ )+ end++ it 'matches a versioned name of a thinking family' do+ provider.chat(messages: messages, model: 'gemini-2.5-flash-002')+ expect(last_payload['generationConfig']).to include('thinkingConfig' => { 'thinkingBudget' => budget })+ end++ it 'keeps the budget inside the range both 2.5 families accept (Pro 128..32768, Flash 1..24576)' do+ expect(budget).to be_between(128, 24_576)+ end++ # Flash-Lite and 2.0 Flash are left exactly as before: a budget would switch+ # thinking ON for Flash-Lite and cost tokens nobody asked for. Gemini 3 takes+ # thinkingLevel instead (thinkingBudget is deprecated there and can 400), and+ # whether its thoughts count against maxOutputTokens is not documented yet.+ ['gemini-2.5-flash-lite', 'gemini-2.0-flash', 'gemini-3-pro-preview', 'gemini-3-flash', 'some-tuned-model']+ .each do |model|+ it "sends #{model} the bare limit, byte for byte as before" do+ provider.chat(messages: messages, model: model)+ expect(last_payload['generationConfig']).to eq('maxOutputTokens' => 1500, 'temperature' => 0.7)+ end+ end++ it 'lets provider_options override the reserve and the thinking config (raw override wins)' do+ provider.chat(messages: messages,+ provider_options: { maxOutputTokens: 900, thinkingConfig: { thinkingBudget: 0 } })+ expect(last_payload['generationConfig']).to eq(+ 'maxOutputTokens' => 900, 'temperature' => 0.7, 'thinkingConfig' => { 'thinkingBudget' => 0 }+ )+ endendit 'merges provider_options into generationConfig LAST (raw override wins)' do@@ -662,6 +710,21 @@ RSpec.describe Lalabase::Llm::GoogleVertexProvider doexpect(captured.last[:req].params['alt']).to eq('sse')end+ it 'adds the thinking reserve exactly like #chat (the assistant streams)' do+ stub_stream_post(chunks: [sse(final_event)])+ provider.stream_chat(messages: messages, model: 'gemini-2.5-pro', max_tokens: 1500) { |_| }+ budget = described_class::THINKING_BUDGET+ expect(last_payload['generationConfig']).to eq(+ 'maxOutputTokens' => 1500 + budget, 'temperature' => 0.7, 'thinkingConfig' => { 'thinkingBudget' => budget }+ )+ end++ it 'sends a non-thinking model the bare limit when streaming' do+ stub_stream_post(chunks: [sse(final_event)])+ provider.stream_chat(messages: messages, model: 'gemini-2.0-flash') { |_| }+ expect(last_payload['generationConfig']).to eq('maxOutputTokens' => 1500, 'temperature' => 0.7)+ end+it 'folds a system-only request into a single user content (parity with #chat)' dostub_stream_post(chunks: [sse(final_event)])provider.stream_chat(messages: [{ role: 'system', content: 'Fasse zusammen.' }]) { |_| }