Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function normalizeRounds(rounds: DiscussionRound[]): DiscussionRound[] {
return rounds.map((round, index) => ({
...round,
index: index + 1,
parallel: true,
parallel: round.parallel ?? true,
}));
}

Expand Down Expand Up @@ -217,6 +217,18 @@ export function AdminDiscussionsPage() {
}));
};

const updateRoundParallel = (roundIndex: number, parallel: boolean) => {
setDraft((current) => ({
...current,
config: {
...current.config,
rounds: current.config.rounds.map((round, index) =>
index === roundIndex ? { ...round, parallel } : round,
),
},
}));
};

const toggleRoundParticipant = (roundIndex: number, participant: string) => {
setDraft((current) => ({
...current,
Expand Down Expand Up @@ -608,8 +620,29 @@ export function AdminDiscussionsPage() {
<GripVertical className="size-4 text-muted-foreground" />
</span>
{t("round", { round: roundIndex + 1 })}
<span className="rounded-full bg-muted px-1.5 text-[10px] font-normal text-muted-foreground">
{t("parallel")}
<span className="inline-flex rounded-full bg-muted p-0.5 text-[10px] font-normal">
<button
type="button"
aria-pressed={round.parallel}
className={cn(
"rounded-full px-1.5 py-0.5",
round.parallel ? "bg-background text-primary" : "text-muted-foreground",
)}
onClick={() => updateRoundParallel(roundIndex, true)}
>
{t("parallel")}
</button>
<button
type="button"
aria-pressed={!round.parallel}
className={cn(
"rounded-full px-1.5 py-0.5",
!round.parallel ? "bg-background text-primary" : "text-muted-foreground",
)}
onClick={() => updateRoundParallel(roundIndex, false)}
>
{t("serial")}
</button>
</span>
<button
type="button"
Expand All @@ -621,7 +654,11 @@ export function AdminDiscussionsPage() {
</button>
</div>
<p className="mb-2 text-[11px] leading-4 text-muted-foreground">
{roundIndex === 0 ? t("roundIndependent") : t("roundSeesPrevious")}
{round.parallel
? roundIndex === 0
? t("roundIndependent")
: t("roundSeesPrevious")
: t("roundSerial")}
</p>

<textarea
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function DiscussionsWorkflowPreview({
<div className="flex items-center justify-between gap-2 text-xs font-medium text-foreground">
<span>{t("round", { round: roundIndex + 1 })}</span>
<span className="rounded-full bg-muted px-1.5 text-[10px] font-normal text-muted-foreground">
{t("parallel")}
{round.parallel ? t("parallel") : t("serial")}
</span>
</div>
<div className="mt-1 space-y-0.5">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ export function MessageDiscussion({ discussion }: { discussion?: DiscussionState
? t("done")
: t("pendingTurn")}
</span>
<span className="rounded-full bg-muted px-1.5 py-0.5 text-[9px] text-muted-foreground">
{round.parallel === false ? t("serial") : t("parallel")}
</span>
</button>
<div className="ml-auto flex flex-wrap items-center justify-end gap-1">
{steps.map((step) => {
Expand Down
8 changes: 5 additions & 3 deletions apps/web/i18n/messages/en-US/admin-discussions.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "Multi-round discussion",
"description": "Define multi-model discussions as a workflow: rounds run serially and steps inside a round run in parallel. Placeholders bind to real models at chat time.",
"description": "Define multi-model discussions as a workflow: rounds run serially, and each round can run in parallel or serial. Placeholders bind to real models at chat time.",
"newTemplate": "New template",
"loading": "Loading…",
"empty": "No discussion templates yet.",
Expand Down Expand Up @@ -29,16 +29,18 @@
"addRound": "Add round",
"round": "Round {round}",
"parallel": "Parallel",
"serial": "Serial",
"roundIndependent": "Every model answers independently in this round.",
"roundSeesPrevious": "Every model sees the previous round and all earlier discussion in this round.",
"roundSeesPrevious": "In a parallel round, every model sees the previous round and all earlier discussion.",
"roundSerial": "A serial round runs models 1, 2, 3…N in order; later models see and respond to earlier answers from the same round.",
"addStep": "Add step",
"promptPlaceholder": "Prompt for this round",
"summary": "Summary model (final round)",
"summaryHint": "After the rounds, the summary model turns the successful turns into the final answer for the user.",
"workflow": "Workflow",
"workflowQuestion": "User question",
"workflowSummary": "Summary",
"workflowHint": "Rounds run serially and steps inside a round run in parallel. From round 2 on, every model sees the previous round and all earlier discussion; the summary model then writes the final answer. Placeholders bind to real models at chat time; this page only defines the structure.",
"workflowHint": "Rounds run serially, and each round can be parallel or serial. Parallel models answer independently; serial models read and respond to earlier answers in model order. The summary model then writes the final answer. Placeholders bind to real models at chat time.",
"delete": "Delete",
"save": "Save",
"saving": "Saving…",
Expand Down
2 changes: 2 additions & 0 deletions apps/web/i18n/messages/en-US/chat.json
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,8 @@
"collapse": "Collapse",
"all": "All",
"round": "Round {round}",
"parallel": "Parallel",
"serial": "Serial",
"thinking": "Thinking",
"done": "Done",
"failed": "Failed",
Expand Down
8 changes: 5 additions & 3 deletions apps/web/i18n/messages/zh-CN/admin-discussions.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "多轮讨论",
"description": "用工作流定义多模型讨论:轮次串行、轮内并行,占位符在对话时绑定真实模型。",
"description": "用工作流定义多模型讨论:轮次串行,每轮可选并行或串行,占位符在对话时绑定真实模型。",
"newTemplate": "新建模板",
"loading": "加载中…",
"empty": "还没有多轮讨论模板。",
Expand Down Expand Up @@ -29,16 +29,18 @@
"addRound": "添加轮次",
"round": "第 {round} 轮",
"parallel": "并行",
"serial": "串行",
"roundIndependent": "本轮各模型并行独立回答。",
"roundSeesPrevious": "本轮每个模型都会看到上一轮及更早的讨论记录。",
"roundSeesPrevious": "并行轮次中,每个模型都会看到上一轮及更早的讨论记录。",
"roundSerial": "串行轮次按模型 1、2、3…N 执行,后续模型能看到同轮前序回答并回应分歧。",
"addStep": "添加步骤",
"promptPlaceholder": "这一轮的提示词",
"summary": "总结模型(最后一轮)",
"summaryHint": "讨论结束后由总结模型汇总成功发言,作为面向用户的最终答案。",
"workflow": "工作流",
"workflowQuestion": "用户问题",
"workflowSummary": "总结",
"workflowHint": "轮次串行执行,轮内并行;第 2 轮起每个模型都会看到上一轮及更早的讨论记录;最后由总结模型输出最终答案。占位符在对话时绑定真实模型,这里只决定结构。",
"workflowHint": "轮次之间串行,每轮可选并行或串行;并行模型独立回答,串行模型按编号顺序阅读并回应同轮前序回答;最后由总结模型输出最终答案。占位符在对话时绑定真实模型。",
"delete": "删除",
"save": "保存",
"saving": "保存中…",
Expand Down
2 changes: 2 additions & 0 deletions apps/web/i18n/messages/zh-CN/chat.json
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,8 @@
"collapse": "收起",
"all": "全部",
"round": "第 {round} 轮",
"parallel": "并行",
"serial": "串行",
"thinking": "思考中",
"done": "已完成",
"failed": "失败",
Expand Down
1 change: 1 addition & 0 deletions apps/web/shared/api/discussion-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export type DiscussionTurn = {

export type DiscussionRuntimeRound = {
index: number;
parallel?: boolean;
steps: Array<{
id: string;
participant: string;
Expand Down
71 changes: 52 additions & 19 deletions backend/internal/application/conversation/service_discussion.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const (
discussionModelPrefix = "discussion:"
discussionDefaultStepPrompt = "你是多模型讨论中的一员。请独立、具体地回答用户问题,指出关键假设与风险。"
discussionFollowupPrompt = "你是多模型讨论中的一员。请阅读已有讨论,补充遗漏、纠正错误、挑战薄弱推理,不要重复已经充分的内容。"
discussionParallelPrompt = "本轮为并行独立发言。你看不到同轮其他模型的回答,请独立判断,不要假设存在共识。"
discussionSerialPrompt = "本轮为串行讨论。你可以看到同轮排在你之前的模型发言。先独立判断,再针对具体分歧进行回应;指出对方依赖的假设、逻辑跳跃或缺失证据,并给出反例或替代解释。不要重复已经充分的内容,也不要为了反对而反对。"
discussionSummaryPrompt = "你是多模型讨论的主持人。请只依据成功的讨论发言,给出面向用户的最终答案;失败的发言不是观点或证据。"
discussionMaxTurnRunes = 12000
discussionMaxTranscriptRunes = 60000
Expand Down Expand Up @@ -146,21 +148,8 @@ func (s *Service) StreamDiscussionMessage(ctx context.Context, input DiscussionS
if err = ctx.Err(); err != nil {
break
}
transcript := discussionTranscript(state.Turns)
results := make([]domaindiscussion.Turn, len(round.Steps))
var wg sync.WaitGroup
for index, step := range round.Steps {
wg.Add(1)
go func(index int, step domaindiscussion.RuntimeStep) {
defer wg.Done()
turn := s.runDiscussionTurn(ctx, turnInput, conversation, round.Index, step, transcript, history)
results[index] = turn
emit("discussion_turn_done", map[string]any{"turn": discussionTurnPayloadOf(turn)})
}(index, step)
}
wg.Wait()
results := s.runDiscussionRound(ctx, turnInput, conversation, round, &state, history, emit)
for _, turn := range results {
state.Turns = append(state.Turns, turn)
totalInput += turn.InputTokens
totalOutput += turn.OutputTokens
totalCacheRead += turn.CacheReadTokens
Expand Down Expand Up @@ -266,6 +255,41 @@ func (s *Service) StreamDiscussionMessage(ctx context.Context, input DiscussionS
}, nil
}

func (s *Service) runDiscussionRound(
ctx context.Context,
input DiscussionSendInput,
conversation *model.Conversation,
round domaindiscussion.RuntimeRound,
state *domaindiscussion.State,
history []llm.Message,
emit func(string, map[string]any),
) []domaindiscussion.Turn {
results := make([]domaindiscussion.Turn, len(round.Steps))
if round.IsParallel() {
transcript := discussionTranscript(state.Turns)
var wg sync.WaitGroup
for index, step := range round.Steps {
wg.Add(1)
go func(index int, step domaindiscussion.RuntimeStep) {
defer wg.Done()
turn := s.runDiscussionTurn(ctx, input, conversation, round.Index, step, transcript, history, true)
results[index] = turn
emit("discussion_turn_done", map[string]any{"turn": discussionTurnPayloadOf(turn)})
}(index, step)
}
wg.Wait()
state.Turns = append(state.Turns, results...)
return results
}
for index, step := range round.Steps {
turn := s.runDiscussionTurn(ctx, input, conversation, round.Index, step, discussionTranscript(state.Turns), history, false)
results[index] = turn
state.Turns = append(state.Turns, turn)
emit("discussion_turn_done", map[string]any{"turn": discussionTurnPayloadOf(turn)})
}
return results
}

func (s *Service) runDiscussionTurn(
ctx context.Context,
input DiscussionSendInput,
Expand All @@ -274,6 +298,7 @@ func (s *Service) runDiscussionTurn(
step domaindiscussion.RuntimeStep,
transcript string,
history []llm.Message,
parallel bool,
) domaindiscussion.Turn {
turn := domaindiscussion.Turn{
Round: roundIndex,
Expand All @@ -291,8 +316,13 @@ func (s *Service) runDiscussionTurn(
prompt = discussionFollowupPrompt
}
}
if parallel {
prompt += "\n\n" + discussionParallelPrompt
} else {
prompt += "\n\n" + discussionSerialPrompt
}
messages := append([]llm.Message{{Role: "system", Content: prompt}}, history...)
messages = append(messages, llm.Message{Role: "user", Content: discussionTurnUserContent(input.Content, transcript)})
messages = append(messages, llm.Message{Role: "user", Content: discussionTurnUserContent(input.Content, transcript, parallel)})
output, promptText, promptMessages, err := s.generateDiscussion(ctx, input, conversation, step.Model, messages, func(promptText string, promptMessages []domaindiscussion.PromptMessage) {
if input.OnEvent != nil {
_ = input.OnEvent("discussion_turn_start", map[string]any{
Expand Down Expand Up @@ -453,14 +483,17 @@ func discussionPromptMessagesOf(messages []llm.Message) []domaindiscussion.Promp
return result
}

// discussionTurnUserContent 组装一次轮次发言的当前用户消息:本次问题 + 本轮之前的讨论记录。
// discussionTurnUserContent 组装一次轮次发言的当前用户消息:本次问题 + 当前模型可见的讨论记录。
// 历史对话由 discussionHistoryMessages 以真实 user/assistant 消息的形式携带,不再拼进这一段。
func discussionTurnUserContent(question string, transcript string) string {
func discussionTurnUserContent(question string, transcript string, parallel bool) string {
var builder strings.Builder
builder.WriteString(strings.TrimSpace(question))
if transcript != "" {
// From the second round on, every model sees the previous round and all earlier rounds.
builder.WriteString("\n\n上一轮及更早的讨论记录:\n")
if parallel {
builder.WriteString("\n\n此前轮次的讨论记录(不包含本轮其他模型):\n")
} else {
builder.WriteString("\n\n此前轮次及本轮在你之前的讨论记录:\n")
}
builder.WriteString(transcript)
}
return builder.String()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ type discussionStepPayload struct {
}

type discussionRoundPayload struct {
Index int `json:"index"`
Steps []discussionStepPayload `json:"steps"`
Index int `json:"index"`
Parallel bool `json:"parallel"`
Steps []discussionStepPayload `json:"steps"`
}

type discussionSummaryPayload struct {
Expand Down Expand Up @@ -92,8 +93,9 @@ func discussionRoundsPayloadOf(rounds []domaindiscussion.RuntimeRound) []discuss
payload := make([]discussionRoundPayload, 0, len(rounds))
for _, round := range rounds {
roundPayload := discussionRoundPayload{
Index: round.Index,
Steps: make([]discussionStepPayload, 0, len(round.Steps)),
Index: round.Index,
Parallel: round.IsParallel(),
Steps: make([]discussionStepPayload, 0, len(round.Steps)),
}
for _, step := range round.Steps {
roundPayload.Steps = append(roundPayload.Steps, discussionStepPayload{
Expand Down
Loading
Loading