Skip to content
Snippets Groups Projects
Commit 9fee27f4 authored by George Koltsov's avatar George Koltsov :two:
Browse files

Add ExplainTool to GitLab Chat & add authorization check

Changelog: added
EE: true
parent 42b9f5d9
No related branches found
No related tags found
2 merge requests!123370Add ExplainTool to GitLab Chat & add authorization check,!119439Draft: Prevent file variable content expansion in downstream pipeline
......@@ -10,6 +10,7 @@ class Executor < Tool
NAME = 'ExplainCode'
DESCRIPTION = 'Useful tool to explain code snippets.'
RESOURCE_NAME = 'explain code answer'
PROVIDER_PROMPT_CLASSES = {
anthropic: ::Gitlab::Llm::Chain::Tools::ExplainCode::Prompts::Anthropic,
vertex_ai: ::Gitlab::Llm::Chain::Tools::ExplainCode::Prompts::VertexAi
......@@ -27,11 +28,21 @@ class Executor < Tool
Utils::Prompt.as_user("%<input>s")
].freeze
def execute
def perform
Answer.new(status: :ok, context: context, content: request, tool: nil)
rescue StandardError
Answer.error_answer(context: context, content: _("Unexpected error"))
end
private
def authorize
Utils::Authorizer.context_authorized?(context: context)
end
def resource_name
RESOURCE_NAME
end
end
end
end
......
......@@ -5,10 +5,10 @@ module Llm
module Completions
class Chat < Base
TOOLS = [
Gitlab::Llm::Chain::Tools::IssueIdentifier,
Gitlab::Llm::Chain::Tools::SummarizeComments,
Gitlab::Llm::Chain::Tools::JsonReader,
Gitlab::Llm::Chain::Tools::ExplainCode
::Gitlab::Llm::Chain::Tools::ExplainCode,
::Gitlab::Llm::Chain::Tools::IssueIdentifier,
::Gitlab::Llm::Chain::Tools::JsonReader,
::Gitlab::Llm::Chain::Tools::SummarizeComments
].freeze
def execute(user, resource, options)
......
......@@ -20,19 +20,38 @@
end
describe '#execute' do
context 'when response is successful' do
it 'returns success answer' do
allow(tool).to receive(:request).and_return('response')
context 'when context is authorized' do
before do
allow(Gitlab::Llm::Chain::Utils::Authorizer).to receive(:context_authorized?).and_return(true)
end
context 'when response is successful' do
it 'returns success answer' do
allow(tool).to receive(:request).and_return('response')
expect(tool.execute.content).to eq('response')
expect(tool.execute.content).to eq('response')
end
end
context 'when error is raised during a request' do
it 'returns error answer' do
allow(tool).to receive(:request).and_raise(StandardError)
expect(tool.execute.content).to eq('Unexpected error')
end
end
end
context 'when error is raised during a request' do
context 'when context is not authorized' do
before do
allow(Gitlab::Llm::Chain::Utils::Authorizer).to receive(:context_authorized?).and_return(false)
end
it 'returns error answer' do
allow(tool).to receive(:request).and_raise(StandardError)
allow(tool).to receive(:authorize).and_return(false)
expect(tool.execute.content).to eq('Unexpected error')
expect(tool.execute.content)
.to eq('I am sorry, I am unable to find the explain code answer you are looking for.')
end
end
end
......
......@@ -24,10 +24,10 @@
shared_examples 'success' do
it 'calls the ZeroShot Agent with the right parameters' do
tools = [
::Gitlab::Llm::Chain::Tools::ExplainCode,
::Gitlab::Llm::Chain::Tools::IssueIdentifier,
::Gitlab::Llm::Chain::Tools::SummarizeComments,
Gitlab::Llm::Chain::Tools::JsonReader,
Gitlab::Llm::Chain::Tools::ExplainCode
::Gitlab::Llm::Chain::Tools::JsonReader,
::Gitlab::Llm::Chain::Tools::SummarizeComments
]
expected_params = [
user_input: content,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment