Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 20612fd

Browse files
authored
FEATURE: add the ability to disable streaming on an Open AI LLM
Disabling streaming is required for models such o1 that do not have streaming enabled yet It is good to carry this feature around in case various apis decide not to support streaming endpoints and Discourse AI can continue to work just as it did before. Also: fixes issue where sharing artifacts would miss viewport leading to tiny artifacts on mobile
1 parent 7e9c0dc commit 20612fd

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed

app/controllers/discourse_ai/ai_bot/artifacts_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def show
5757
<head>
5858
<meta charset="UTF-8">
5959
<title>#{ERB::Util.html_escape(name)}</title>
60+
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, user-scalable=yes, viewport-fit=cover, interactive-widget=resizes-content">
6061
<style>
6162
html, body, iframe {
6263
margin: 0;

app/models/llm_model.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def self.provider_params
3232
open_ai: {
3333
organization: :text,
3434
disable_native_tools: :checkbox,
35+
disable_streaming: :checkbox,
3536
},
3637
mistral: {
3738
disable_native_tools: :checkbox,
@@ -51,11 +52,13 @@ def self.provider_params
5152
ollama: {
5253
disable_system_prompt: :checkbox,
5354
enable_native_tool: :checkbox,
55+
disable_streaming: :checkbox,
5456
},
5557
open_router: {
5658
disable_native_tools: :checkbox,
5759
provider_order: :text,
5860
provider_quantizations: :text,
61+
disable_streaming: :checkbox,
5962
},
6063
}
6164
end

config/locales/client.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ en:
420420
disable_native_tools: "Disable native tool support (use XML based tools)"
421421
provider_order: "Provider order (comma delimited list)"
422422
provider_quantizations: "Order of provider quantizations (comma delimited list eg: fp16,fp8)"
423+
disable_streaming: "Disable streaming completions (convert streaming to non streaming requests)"
423424

424425
related_topics:
425426
title: "Related topics"

lib/completions/endpoints/base.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,27 @@ def perform_completion!(
6969
model_params = normalize_model_params(model_params)
7070
orig_blk = blk
7171

72+
if block_given? && disable_streaming?
73+
result =
74+
perform_completion!(
75+
dialect,
76+
user,
77+
model_params,
78+
feature_name: feature_name,
79+
feature_context: feature_context,
80+
partial_tool_calls: partial_tool_calls,
81+
)
82+
83+
result = [result] if !result.is_a?(Array)
84+
cancelled_by_caller = false
85+
cancel_proc = -> { cancelled_by_caller = true }
86+
result.each do |partial|
87+
blk.call(partial, cancel_proc)
88+
break if cancelled_by_caller
89+
end
90+
return result
91+
end
92+
7293
@streaming_mode = block_given?
7394

7495
prompt = dialect.translate
@@ -261,6 +282,10 @@ def xml_tools_enabled?
261282
raise NotImplementedError
262283
end
263284

285+
def disable_streaming?
286+
@disable_streaming = !!llm_model.lookup_custom_param("disable_streaming")
287+
end
288+
264289
private
265290

266291
def start_log(

lib/completions/endpoints/open_ai.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ def perform_completion!(
4242

4343
private
4444

45+
def disable_streaming?
46+
@disable_streaming = llm_model.lookup_custom_param("disable_streaming")
47+
end
48+
4549
def model_uri
4650
if llm_model.url.to_s.starts_with?("srv://")
4751
service = DiscourseAi::Utils::DnsSrv.lookup(llm_model.url.sub("srv://", ""))

spec/lib/completions/endpoints/open_ai_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,43 @@ def request_body(prompt, stream: false, tool_call: false)
457457
end
458458
end
459459

460+
it "falls back to non-streaming mode when streaming is disabled" do
461+
model.update!(provider_params: { disable_streaming: true })
462+
463+
response = {
464+
id: "chatcmpl-123",
465+
object: "chat.completion",
466+
created: 1_677_652_288,
467+
choices: [
468+
{
469+
message: {
470+
role: "assistant",
471+
content: "Hello there",
472+
},
473+
index: 0,
474+
finish_reason: "stop",
475+
},
476+
],
477+
}
478+
479+
parsed_body = nil
480+
stub_request(:post, "https://api.openai.com/v1/chat/completions").with(
481+
body:
482+
proc do |req_body|
483+
parsed_body = JSON.parse(req_body, symbolize_names: true)
484+
true
485+
end,
486+
).to_return(status: 200, body: response.to_json)
487+
488+
chunks = []
489+
dialect = compliance.dialect(prompt: compliance.generic_prompt)
490+
endpoint.perform_completion!(dialect, user) { |chunk| chunks << chunk }
491+
492+
expect(parsed_body).not_to have_key(:stream)
493+
494+
expect(chunks).to eq(["Hello there"])
495+
end
496+
460497
describe "when using streaming mode" do
461498
context "with simple prompts" do
462499
it "completes a trivial prompt and logs the response" do

0 commit comments

Comments
 (0)