@@ -82,7 +82,7 @@ def attach_user_context(context, user = nil, force_default_locale: false)
8282 context . user_language = "#{ locale_hash [ "name" ] } "
8383
8484 if user
85- timezone = user . user_option . timezone || "UTC"
85+ timezone = user & .user_option & .timezone || "UTC"
8686 current_time = Time . now . in_time_zone ( timezone )
8787
8888 temporal_context = {
@@ -126,21 +126,34 @@ def generate_prompt(
126126 )
127127 context = attach_user_context ( context , user , force_default_locale : force_default_locale )
128128
129- helper_response = +""
129+ bad_json = false
130+ json_summary_schema_key = bot . persona . response_format &.first . to_h
131+
132+ schema_key = json_summary_schema_key [ "key" ] &.to_sym
133+ schema_type = json_summary_schema_key [ "type" ]
134+
135+ if schema_type == "array"
136+ helper_response = [ ]
137+ else
138+ helper_response = +""
139+ end
130140
131141 buffer_blk =
132142 Proc . new do |partial , _ , type |
133- json_summary_schema_key = bot . persona . response_format &.first . to_h
134- helper_response = [ ] if json_summary_schema_key [ "type" ] == "array"
135- if type == :structured_output
136- helper_chunk = partial . read_buffered_property ( json_summary_schema_key [ "key" ] &.to_sym )
143+ if type == :structured_output && schema_type
144+ bad_json ||= partial . broken?
145+ helper_chunk = partial . read_buffered_property ( schema_key )
137146 if !helper_chunk . nil? && !helper_chunk . empty?
138- if json_summary_schema_key [ "type" ] != "array"
139- helper_response = helper_chunk
140- else
147+ if !bad_json
141148 helper_response << helper_chunk
149+ else
150+ if schema_type == "string" || schema_type == "array"
151+ helper_response << helper_chunk
152+ else
153+ helper_response = helper_chunk
154+ end
155+ block . call ( helper_chunk ) if block
142156 end
143- block . call ( helper_chunk ) if block
144157 end
145158 elsif type . blank?
146159 # Assume response is a regular completion.
@@ -151,6 +164,17 @@ def generate_prompt(
151164
152165 bot . reply ( context , &buffer_blk )
153166
167+ # handle edge cases where structured output is all over the place
168+ if bad_json
169+ helper_response = helper_response . join if helper_response . is_a? ( Array )
170+ helper_response =
171+ DiscourseAi ::Utils ::BestEffortJsonParser . extract_key (
172+ helper_response ,
173+ schema_type ,
174+ schema_key ,
175+ )
176+ block . call ( helper_response ) if block
177+ end
154178 helper_response
155179 end
156180
@@ -255,7 +279,7 @@ def generate_image_caption(upload, user)
255279 Proc . new do |partial , _ , type |
256280 if type == :structured_output
257281 structured_output = partial
258- json_summary_schema_key = bot . persona . response_format &.first . to_h
282+ _json_summary_schema_key = bot . persona . response_format &.first . to_h
259283 end
260284 end
261285
@@ -287,6 +311,11 @@ def build_bot(helper_mode, user)
287311 end
288312
289313 def find_ai_helper_model ( helper_mode , persona_klass )
314+ if helper_mode == IMAGE_CAPTION && @image_caption_llm . is_a? ( LlmModel )
315+ return @image_caption_llm
316+ end
317+
318+ return @helper_llm if helper_mode != IMAGE_CAPTION && @helper_llm . is_a? ( LlmModel )
290319 self . class . find_ai_helper_model ( helper_mode , persona_klass )
291320 end
292321
@@ -299,9 +328,9 @@ def self.find_ai_helper_model(helper_mode, persona_klass)
299328
300329 if !model_id
301330 if helper_mode == IMAGE_CAPTION
302- model_id = @helper_llm || SiteSetting . ai_helper_image_caption_model &.split ( ":" ) &.last
331+ model_id = SiteSetting . ai_helper_image_caption_model &.split ( ":" ) &.last
303332 else
304- model_id = @image_caption_llm || SiteSetting . ai_helper_model &.split ( ":" ) &.last
333+ model_id = SiteSetting . ai_helper_model &.split ( ":" ) &.last
305334 end
306335 end
307336
0 commit comments