generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce29cad
commit 6e35293
Showing
4 changed files
with
213 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
module DiscourseAi | ||
module Automation | ||
module LlmToolTriage | ||
def self.handle(post:, tool_id:, automation: nil) | ||
tool = AiTool.find_by(id: tool_id) | ||
return if !tool | ||
return if !tool.parameters.blank? | ||
|
||
context = { | ||
post_id: post.id, | ||
automation_id: automation&.id, | ||
automation_name: automation&.name, | ||
} | ||
|
||
runner = tool.runner({}, llm: nil, bot_user: Discourse.system_user, context: context) | ||
runner.invoke | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe DiscourseAi::Automation::LlmToolTriage do | ||
fab!(:solver) { Fabricate(:user) } | ||
fab!(:new_user) { Fabricate(:user, trust_level: TrustLevel[0], created_at: 1.day.ago) } | ||
fab!(:topic) { Fabricate(:topic, user: new_user) } | ||
fab!(:post) { Fabricate(:post, topic: topic, user: new_user, raw: "How do I reset my password?") } | ||
fab!(:llm_model) | ||
fab!(:ai_persona) do | ||
persona = Fabricate(:ai_persona, default_llm: llm_model) | ||
persona.create_user | ||
persona | ||
end | ||
|
||
fab!(:tool) do | ||
tool_script = <<~JS | ||
function invoke(params) { | ||
const postId = context.post_id; | ||
const post = discourse.getPost(postId); | ||
const user = discourse.getUser(post.user_id); | ||
if (user.trust_level > 0) { | ||
return { | ||
processed: false, | ||
reason: "User is not new" | ||
}; | ||
} | ||
const helper = discourse.getPersona("#{ai_persona.name}"); | ||
const answer = helper.respondTo({ post_id: post.id }); | ||
return { | ||
answer: answer, | ||
processed: true, | ||
reason: "answered question" | ||
}; | ||
} | ||
JS | ||
|
||
AiTool.create!( | ||
name: "New User Question Answerer", | ||
tool_name: "new_user_question_answerer", | ||
description: "Automatically answers questions from new users when possible", | ||
parameters: [], # No parameters as required by llm_tool_triage | ||
script: tool_script, | ||
created_by_id: Discourse.system_user.id, | ||
summary: "Answers new user questions", | ||
enabled: true, | ||
) | ||
end | ||
|
||
before do | ||
SiteSetting.discourse_ai_enabled = true | ||
SiteSetting.ai_bot_enabled = true | ||
end | ||
|
||
it "It is able to answer new user questions" do | ||
result = nil | ||
DiscourseAi::Completions::Llm.with_prepared_responses( | ||
["this is how you reset your password"], | ||
) { result = described_class.handle(post: post, tool_id: tool.id) } | ||
p result | ||
end | ||
end |