Skip to content

DEV: Use structured responses for summaries #1252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/controllers/discourse_ai/admin/ai_personas_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ def ai_persona_params
permitted[:tools] = permit_tools(tools)
end

if response_format = params.dig(:ai_persona, :response_format)
permitted[:response_format] = permit_response_format(response_format)
end

permitted
end

Expand All @@ -235,6 +239,18 @@ def permit_tools(tools)
[tool, options, !!force_tool]
end
end

def permit_response_format(response_format)
return [] if !response_format.is_a?(Array)

response_format.map do |element|
if element && element.is_a?(ActionController::Parameters)
element.permit!
else
false
end
end
end
end
end
end
14 changes: 10 additions & 4 deletions app/models/ai_persona.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,19 +325,24 @@ def chat_preconditions
end

def system_persona_unchangeable
error_msg = I18n.t("discourse_ai.ai_bot.personas.cannot_edit_system_persona")

if top_p_changed? || temperature_changed? || system_prompt_changed? || name_changed? ||
description_changed?
errors.add(:base, I18n.t("discourse_ai.ai_bot.personas.cannot_edit_system_persona"))
errors.add(:base, error_msg)
elsif tools_changed?
old_tools = tools_change[0]
new_tools = tools_change[1]

old_tool_names = old_tools.map { |t| t.is_a?(Array) ? t[0] : t }.to_set
new_tool_names = new_tools.map { |t| t.is_a?(Array) ? t[0] : t }.to_set

if old_tool_names != new_tool_names
errors.add(:base, I18n.t("discourse_ai.ai_bot.personas.cannot_edit_system_persona"))
end
errors.add(:base, error_msg) if old_tool_names != new_tool_names
elsif response_format_changed?
old_format = response_format_change[0].map { |f| f["key"] }.to_set
new_format = response_format_change[1].map { |f| f["key"] }.to_set

errors.add(:base, error_msg) if old_format != new_format
end
end

Expand Down Expand Up @@ -395,6 +400,7 @@ def allowed_seeded_model
# rag_llm_model_id :bigint
# default_llm_id :bigint
# question_consolidator_llm_id :bigint
# response_format :jsonb
#
# Indexes
#
Expand Down
3 changes: 2 additions & 1 deletion app/serializers/localized_ai_persona_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class LocalizedAiPersonaSerializer < ApplicationSerializer
:allow_chat_direct_messages,
:allow_topic_mentions,
:allow_personal_messages,
:force_default_llm
:force_default_llm,
:response_format

has_one :user, serializer: BasicUserSerializer, embed: :object
has_many :rag_uploads, serializer: UploadSerializer, embed: :object
Expand Down
3 changes: 3 additions & 0 deletions assets/javascripts/discourse/admin/models/ai-persona.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const CREATE_ATTRIBUTES = [
"allow_topic_mentions",
"allow_chat_channel_mentions",
"allow_chat_direct_messages",
"response_format",
];

const SYSTEM_ATTRIBUTES = [
Expand Down Expand Up @@ -60,6 +61,7 @@ const SYSTEM_ATTRIBUTES = [
"allow_topic_mentions",
"allow_chat_channel_mentions",
"allow_chat_direct_messages",
"response_format",
];

export default class AiPersona extends RestModel {
Expand Down Expand Up @@ -151,6 +153,7 @@ export default class AiPersona extends RestModel {
const attrs = this.getProperties(CREATE_ATTRIBUTES);
this.populateTools(attrs);
attrs.forced_tool_count = this.forced_tool_count || -1;
attrs.response_format = attrs.response_format || [];

return attrs;
}
Expand Down
3 changes: 3 additions & 0 deletions assets/javascripts/discourse/components/ai-persona-editor.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Group from "discourse/models/group";
import { i18n } from "discourse-i18n";
import AdminUser from "admin/models/admin-user";
import GroupChooser from "select-kit/components/group-chooser";
import AiPersonaResponseFormatEditor from "../components/modal/ai-persona-response-format-editor";
import AiLlmSelector from "./ai-llm-selector";
import AiPersonaToolOptions from "./ai-persona-tool-options";
import AiToolSelector from "./ai-tool-selector";
Expand Down Expand Up @@ -325,6 +326,8 @@ export default class PersonaEditor extends Component {
<field.Textarea />
</form.Field>

<AiPersonaResponseFormatEditor @form={{form}} @data={{data}} />

<form.Field
@name="default_llm_id"
@title={{i18n "discourse_ai.ai_persona.default_llm"}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { fn, hash } from "@ember/helper";
import { action } from "@ember/object";
import { gt } from "truth-helpers";
import ModalJsonSchemaEditor from "discourse/components/modal/json-schema-editor";
import { prettyJSON } from "discourse/lib/formatter";
import { i18n } from "discourse-i18n";

export default class AiPersonaResponseFormatEditor extends Component {
@tracked showJsonEditorModal = false;

jsonSchema = {
type: "array",
uniqueItems: true,
title: i18n("discourse_ai.ai_persona.response_format.modal.root_title"),
items: {
type: "object",
title: i18n("discourse_ai.ai_persona.response_format.modal.key_title"),
properties: {
key: {
type: "string",
},
type: {
type: "string",
enum: ["string", "integer", "boolean"],
},
},
},
};

get editorTitle() {
return i18n("discourse_ai.ai_persona.response_format.title");
}

get responseFormatAsJSON() {
return JSON.stringify(this.args.data.response_format);
}

get displayJSON() {
const toDisplay = {};

this.args.data.response_format.forEach((keyDesc) => {
toDisplay[keyDesc.key] = keyDesc.type;
});

return prettyJSON(toDisplay);
}

@action
openModal() {
this.showJsonEditorModal = true;
}

@action
closeModal() {
this.showJsonEditorModal = false;
}

@action
updateResponseFormat(form, value) {
form.set("response_format", JSON.parse(value));
}

<template>
<@form.Container @title={{this.editorTitle}} @format="large">
<div class="ai-persona-editor__response-format">
{{#if (gt @data.response_format.length 0)}}
<pre class="ai-persona-editor__response-format-pre">
<code
>{{this.displayJSON}}</code>
</pre>
{{else}}
<div class="ai-persona-editor__response-format-none">
{{i18n "discourse_ai.ai_persona.response_format.no_format"}}
</div>
{{/if}}

<@form.Button
@action={{this.openModal}}
@label="discourse_ai.ai_persona.response_format.open_modal"
@disabled={{@data.system}}
/>
</div>
</@form.Container>

{{#if this.showJsonEditorModal}}
<ModalJsonSchemaEditor
@model={{hash
value=this.responseFormatAsJSON
updateValue=(fn this.updateResponseFormat @form)
settingName=this.editorTitle
jsonSchema=this.jsonSchema
}}
@closeModal={{this.closeModal}}
/>
{{/if}}
</template>
}
15 changes: 15 additions & 0 deletions assets/stylesheets/modules/ai-bot/common/ai-persona.scss
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@
margin-bottom: 10px;
font-size: var(--font-down-1);
}

&__response-format {
width: 100%;
display: block;
}

&__response-format-pre {
margin-bottom: 0;
white-space: pre-line;
}

&__response-format-none {
margin-bottom: 1em;
margin-top: 0.5em;
}
}

.rag-options {
Expand Down
7 changes: 7 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ en:
rag_conversation_chunks: "Search conversation chunks"
rag_conversation_chunks_help: "The number of chunks to use for the RAG model searches. Increase to increase the amount of context the AI can use."
persona_description: "Personas are a powerful feature that allows you to customize the behavior of the AI engine in your Discourse forum. They act as a 'system message' that guides the AI's responses and interactions, helping to create a more personalized and engaging user experience."
response_format:
title: "JSON response format"
no_format: "No JSON format specified"
open_modal: "Edit"
modal:
root_title: "Response structure"
key_title: "Key"

list:
enabled: "AI Bot?"
Expand Down
2 changes: 2 additions & 0 deletions db/fixtures/personas/603_ai_personas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def from_setting(setting_name)

persona.tools = tools.map { |name, value| [name, value] }

persona.response_format = instance.response_format

persona.system_prompt = instance.system_prompt
persona.top_p = instance.top_p
persona.temperature = instance.temperature
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true
class AddResponseFormatJsonToPersonass < ActiveRecord::Migration[7.2]
def change
add_column :ai_personas, :response_format, :jsonb
end
end
2 changes: 1 addition & 1 deletion lib/completions/anthropic_message_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(name, id, partial_tool_calls: false)
@raw_json = +""
@tool_call = DiscourseAi::Completions::ToolCall.new(id: id, name: name, parameters: {})
@streaming_parser =
DiscourseAi::Completions::ToolCallProgressTracker.new(self) if partial_tool_calls
DiscourseAi::Completions::JsonStreamingTracker.new(self) if partial_tool_calls
end

def append(json)
Expand Down
1 change: 1 addition & 0 deletions lib/completions/dialects/nova.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def to_payload(options = nil)
result = { system: system, messages: messages }
result[:inferenceConfig] = inference_config if inference_config.present?
result[:toolConfig] = tool_config if tool_config.present?
result[:response_format] = { type: "json_object" } if options[:response_format].present?

result
end
Expand Down
24 changes: 19 additions & 5 deletions lib/completions/endpoints/anthropic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,16 @@ def xml_tools_enabled?
def prepare_payload(prompt, model_params, dialect)
@native_tool_support = dialect.native_tool_support?

payload = default_options(dialect).merge(model_params).merge(messages: prompt.messages)
payload =
default_options(dialect).merge(model_params.except(:response_format)).merge(
messages: prompt.messages,
)

payload[:system] = prompt.system_prompt if prompt.system_prompt.present?
payload[:stream] = true if @streaming_mode

prefilled_message = +""

if prompt.has_tools?
payload[:tools] = prompt.tools
if dialect.tool_choice.present?
Expand All @@ -100,16 +106,24 @@ def prepare_payload(prompt, model_params, dialect)

# prefill prompt to nudge LLM to generate a response that is useful.
# without this LLM (even 3.7) can get confused and start text preambles for a tool calls.
payload[:messages] << {
role: "assistant",
content: dialect.no_more_tool_calls_text,
}
prefilled_message << dialect.no_more_tool_calls_text
else
payload[:tool_choice] = { type: "tool", name: prompt.tool_choice }
end
end
end

# Prefill prompt to force JSON output.
if model_params[:response_format].present?
prefilled_message << " " if !prefilled_message.empty?
prefilled_message << "{"
@forced_json_through_prefill = true
end

if !prefilled_message.empty?
payload[:messages] << { role: "assistant", content: prefilled_message }
end

payload
end

Expand Down
23 changes: 18 additions & 5 deletions lib/completions/endpoints/aws_bedrock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,14 @@ def prepare_payload(prompt, model_params, dialect)
payload = nil

if dialect.is_a?(DiscourseAi::Completions::Dialects::Claude)
payload = default_options(dialect).merge(model_params).merge(messages: prompt.messages)
payload =
default_options(dialect).merge(model_params.except(:response_format)).merge(
messages: prompt.messages,
)
payload[:system] = prompt.system_prompt if prompt.system_prompt.present?

prefilled_message = +""

if prompt.has_tools?
payload[:tools] = prompt.tools
if dialect.tool_choice.present?
Expand All @@ -128,15 +133,23 @@ def prepare_payload(prompt, model_params, dialect)
# payload[:tool_choice] = { type: "none" }

# prefill prompt to nudge LLM to generate a response that is useful, instead of trying to call a tool
payload[:messages] << {
role: "assistant",
content: dialect.no_more_tool_calls_text,
}
prefilled_message << dialect.no_more_tool_calls_text
else
payload[:tool_choice] = { type: "tool", name: prompt.tool_choice }
end
end
end

# Prefill prompt to force JSON output.
if model_params[:response_format].present?
prefilled_message << " " if !prefilled_message.empty?
prefilled_message << "{"
@forced_json_through_prefill = true
end

if !prefilled_message.empty?
payload[:messages] << { role: "assistant", content: prefilled_message }
end
elsif dialect.is_a?(DiscourseAi::Completions::Dialects::Nova)
payload = prompt.to_payload(default_options(dialect).merge(model_params))
else
Expand Down
Loading
Loading