Skip to content
代码片段 群组 项目
未验证 提交 3f66cf31 编辑于 作者: Alejandro Rodríguez's avatar Alejandro Rodríguez
浏览文件

Stream Duo Chat slash commands responses

上级 64199015
No related branches found
No related tags found
无相关合并请求
显示
72 个添加37 个删除
......@@ -80,22 +80,7 @@ def execute
private
def execute_streamed_request
streamed_answer = StreamedZeroShotAnswer.new
request do |content|
next unless stream_response_handler
chunk = streamed_answer.next_chunk(content)
if chunk
stream_response_handler.execute(
response: Gitlab::Llm::Chain::PlainResponseModifier.new(content),
options: {
chunk_id: chunk[:id]
}
)
end
end
request(&streamed_request_handler(StreamedZeroShotAnswer.new))
end
attr_reader :logger, :stream_response_handler
......
......@@ -19,6 +19,21 @@ def request(&block)
ai_request.request(prompt_str, &block)
end
def streamed_request_handler(streamed_answer)
proc do |content|
next unless stream_response_handler
chunk = streamed_answer.next_chunk(content)
if chunk
stream_response_handler.execute(
response: Gitlab::Llm::Chain::PlainResponseModifier.new(content),
options: { chunk_id: chunk[:id] }
)
end
end
end
private
def ai_request
......
......@@ -57,12 +57,6 @@ def self.slash_commands
SLASH_COMMANDS
end
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
......
......@@ -64,12 +64,6 @@ def self.slash_commands
SLASH_COMMANDS
end
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 selected_text_options
......
......@@ -7,6 +7,14 @@ module Tools
class SlashCommandTool < Tool
extend ::Gitlab::Utils::Override
def perform
content = request(&streamed_request_handler(StreamedAnswer.new))
Answer.new(status: :ok, context: context, content: content, tool: nil)
rescue StandardError
Answer.error_answer(context: context, content: _("Unexpected error"))
end
private
attr_reader :command
......
......@@ -63,12 +63,6 @@ def self.slash_commands
SLASH_COMMANDS
end
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
......
......@@ -8,6 +8,7 @@
let(:ai_request_double) { instance_double(Gitlab::Llm::Chain::Requests::Anthropic) }
let(:input) { 'input' }
let(:options) { { input: input } }
let(:stream_response_handler) { nil }
let(:command) { nil }
let(:context) do
......@@ -22,7 +23,11 @@
)
end
subject(:tool) { described_class.new(context: context, options: options, command: command) }
subject(:tool) do
described_class.new(
context: context, options: options, stream_response_handler: stream_response_handler, command: command
)
end
describe '#name' do
it 'returns tool name' do
......
......@@ -8,6 +8,7 @@
let(:ai_request_double) { instance_double(Gitlab::Llm::Chain::Requests::Anthropic) }
let(:input) { 'input' }
let(:options) { { input: input } }
let(:stream_response_handler) { nil }
let(:command) { nil }
let(:context) do
......@@ -22,7 +23,11 @@
)
end
subject(:tool) { described_class.new(context: context, options: options, command: command) }
subject(:tool) do
described_class.new(
context: context, options: options, stream_response_handler: stream_response_handler, command: command
)
end
describe '#name' do
it 'returns tool name' do
......
......@@ -8,6 +8,7 @@
let(:ai_request_double) { instance_double(Gitlab::Llm::Chain::Requests::Anthropic) }
let(:input) { 'input' }
let(:options) { { input: input } }
let(:stream_response_handler) { nil }
let(:command) { nil }
let(:context) do
......@@ -17,7 +18,11 @@
)
end
subject(:tool) { described_class.new(context: context, options: options, command: command) }
subject(:tool) do
described_class.new(
context: context, options: options, stream_response_handler: stream_response_handler, command: command
)
end
describe '#name' do
it 'returns tool name' do
......
......@@ -84,4 +84,34 @@
tool.execute
end
end
context 'when stream_response_service is set' do
let(:stream_response_handler) { instance_double(::Gitlab::Llm::ResponseService) }
before do
allow(ai_request_double).to receive(:request).and_yield("Hello").and_yield(" World")
end
it 'streams the final answer' do
first_response_double = double
second_response_double = double
allow(Gitlab::Llm::Chain::PlainResponseModifier).to receive(:new).with("Hello")
.and_return(first_response_double)
allow(Gitlab::Llm::Chain::PlainResponseModifier).to receive(:new).with(" World")
.and_return(second_response_double)
expect(stream_response_handler).to receive(:execute).with(
response: first_response_double,
options: { chunk_id: 1 }
)
expect(stream_response_handler).to receive(:execute).with(
response: second_response_double,
options: { chunk_id: 2 }
)
tool.execute
end
end
end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册