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

Commit 6729147

Browse files
committed
REFACTOR: Move personas into it's own module.
1 parent 30242a2 commit 6729147

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

lib/ai_bot/playground.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,44 @@ def title_playground(post, user)
283283
title: new_title.sub(/\A"/, "").sub(/"\Z/, ""),
284284
)
285285

286+
system_insts = <<~TEXT.strip
287+
You are titlebot. Given a conversation, you will suggest a title.
288+
289+
- You will never respond with anything but the suggested title.
290+
- You will always match the conversation language in your title suggestion.
291+
- Title will capture the essence of the conversation.
292+
TEXT
293+
294+
instruction = <<~TEXT.strip
295+
Given the following conversation:
296+
297+
{{{
298+
#{conversation}
299+
}}}
300+
301+
Reply only with a title that is 7 words or less.
302+
TEXT
303+
304+
title_prompt =
305+
DiscourseAi::Completions::Prompt.new(
306+
system_insts,
307+
messages: [type: :user, content: instruction],
308+
topic_id: post.topic_id,
309+
)
310+
311+
title =
312+
bot
313+
.llm
314+
.generate(title_prompt, user: user, feature_name: "bot_title")
315+
.strip
316+
.split("\n")
317+
.last
318+
319+
PostRevisor.new(post.topic.first_post, post.topic).revise!(
320+
bot.bot_user,
321+
title: title.sub(/\A"/, "").sub(/"\Z/, ""),
322+
)
323+
286324
allowed_users = post.topic.topic_allowed_users.pluck(:user_id)
287325
MessageBus.publish(
288326
"/discourse-ai/ai-bot/topic/#{post.topic.id}",

lib/personas/bot.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def reply(context, &update_blk)
5555
unless context.is_a?(BotContext)
5656
raise ArgumentError, "context must be an instance of BotContext"
5757
end
58-
llm = DiscourseAi::Completions::Llm.proxy(model)
59-
prompt = persona.craft_prompt(context, llm: llm)
58+
current_llm = llm
59+
prompt = persona.craft_prompt(context, llm: current_llm)
6060

6161
total_completions = 0
6262
ongoing_chain = true
@@ -82,7 +82,7 @@ def reply(context, &update_blk)
8282
current_thinking = []
8383

8484
result =
85-
llm.generate(
85+
current_llm.generate(
8686
prompt,
8787
feature_name: "bot",
8888
partial_tool_calls: allow_partial_tool_calls,
@@ -93,7 +93,7 @@ def reply(context, &update_blk)
9393
persona.find_tool(
9494
partial,
9595
bot_user: user,
96-
llm: llm,
96+
llm: current_llm,
9797
context: context,
9898
existing_tools: existing_tools,
9999
)
@@ -120,7 +120,7 @@ def reply(context, &update_blk)
120120
process_tool(
121121
tool: tool,
122122
raw_context: raw_context,
123-
llm: llm,
123+
current_llm: current_llm,
124124
cancel: cancel,
125125
update_blk: update_blk,
126126
prompt: prompt,
@@ -204,15 +204,15 @@ def embed_thinking(raw_context)
204204
def process_tool(
205205
tool:,
206206
raw_context:,
207-
llm:,
207+
current_llm:,
208208
cancel:,
209209
update_blk:,
210210
prompt:,
211211
context:,
212212
current_thinking:
213213
)
214214
tool_call_id = tool.tool_call_id
215-
invocation_result_json = invoke_tool(tool, llm, cancel, context, &update_blk).to_json
215+
invocation_result_json = invoke_tool(tool, cancel, context, &update_blk).to_json
216216

217217
tool_call_message = {
218218
type: :tool_call,
@@ -246,8 +246,13 @@ def process_tool(
246246
raw_context << [invocation_result_json, tool_call_id, "tool", tool.name]
247247
end
248248

249+
<<<<<<< HEAD
249250
def invoke_tool(tool, llm, cancel, context, &update_blk)
250251
show_placeholder = !context.skip_tool_details && !tool.class.allow_partial_tool_calls?
252+
=======
253+
def invoke_tool(tool, cancel, context, &update_blk)
254+
show_placeholder = !context[:skip_tool_details] && !tool.class.allow_partial_tool_calls?
255+
>>>>>>> 5bfe2314 (REFACTOR: Move personas into it's own module.)
251256

252257
update_blk.call("", cancel, build_placeholder(tool.summary, "")) if show_placeholder
253258

spec/lib/personas/bot_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
RSpec.describe DiscourseAi::Personas::Bot do
4-
subject(:bot) { described_class.as(bot_user) }
4+
subject(:bot) { described_class.as(bot_user, persona: DiscourseAi::Personas::General.new) }
55

66
fab!(:admin)
77
fab!(:gpt_4) { Fabricate(:llm_model, name: "gpt-4") }

0 commit comments

Comments
 (0)