@@ -171,6 +171,40 @@ def request_body(prompt, stream: false, tool_call: false)
171171 UploadCreator . new ( image100x100 , "image.jpg" ) . create_for ( Discourse . system_user . id )
172172 end
173173
174+ describe "max tokens for reasoning models" do
175+ it "uses max_completion_tokens for reasoning models" do
176+ model . update! ( name : "o3-mini" )
177+ llm = DiscourseAi ::Completions ::Llm . proxy ( "custom:#{ model . id } " )
178+ prompt =
179+ DiscourseAi ::Completions ::Prompt . new (
180+ "You are a bot" ,
181+ messages : [ type : :user , content : "hello" ] ,
182+ )
183+
184+ response_text = <<~RESPONSE
185+ data: {"id":"chatcmpl-B2VwlY6KzSDtHvg8pN1VAfRhhLFgn","object":"chat.completion.chunk","created":1739939159,"model":"o3-mini-2025-01-31","service_tier":"default","system_fingerprint":"fp_ef58bd3122","choices":[{"index":0,"delta":{"role":"assistant","content":"","refusal":null},"finish_reason":null}],"usage":null}
186+
187+ data: {"id":"chatcmpl-B2VwlY6KzSDtHvg8pN1VAfRhhLFgn","object":"chat.completion.chunk","created":1739939159,"model":"o3-mini-2025-01-31","service_tier":"default","system_fingerprint":"fp_ef58bd3122","choices":[{"index":0,"delta":{"content":"hello"},"finish_reason":null}],"usage":null}
188+
189+ data: {"id":"chatcmpl-B2VwlY6KzSDtHvg8pN1VAfRhhLFgn","object":"chat.completion.chunk","created":1739939159,"model":"o3-mini-2025-01-31","service_tier":"default","system_fingerprint":"fp_ef58bd3122","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":null}
190+
191+ data: {"id":"chatcmpl-B2VwlY6KzSDtHvg8pN1VAfRhhLFgn","object":"chat.completion.chunk","created":1739939159,"model":"o3-mini-2025-01-31","service_tier":"default","system_fingerprint":"fp_ef58bd3122","choices":[],"usage":{"prompt_tokens":22,"completion_tokens":203,"total_tokens":225,"prompt_tokens_details":{"cached_tokens":0,"audio_tokens":0},"completion_tokens_details":{"reasoning_tokens":192,"audio_tokens":0,"accepted_prediction_tokens":0,"rejected_prediction_tokens":0}}}
192+
193+ data: [DONE]
194+ RESPONSE
195+
196+ body_parsed = nil
197+ stub_request ( :post , "https://api.openai.com/v1/chat/completions" ) . with (
198+ body : -> ( body ) { body_parsed = JSON . parse ( body ) } ,
199+ ) . to_return ( body : response_text )
200+ result = +""
201+ llm . generate ( prompt , user : user , max_tokens : 1000 ) { |chunk | result << chunk }
202+
203+ expect ( result ) . to eq ( "hello" )
204+ expect ( body_parsed [ "max_completion_tokens" ] ) . to eq ( 1000 )
205+ end
206+ end
207+
174208 describe "repeat calls" do
175209 it "can properly reset context" do
176210 llm = DiscourseAi ::Completions ::Llm . proxy ( "custom:#{ model . id } " )
0 commit comments