Skip to content
Snippets Groups Projects

Add ability to stream chat responses

Merged Nicolas Dular requested to merge nd/streaming-connect-fe-be into master
All threads resolved!
Files
5
@@ -24,18 +24,21 @@ class Executor
# @param [String] user_input - a question from a user
# @param [Array<Tool>] tools - an array of Tools defined in the tools module.
# @param [GitlabContext] context - Gitlab context containing useful context information
def initialize(user_input:, tools:, context:, response_handler:)
# @param [ResponseService] response_handler - Handles returning the response to the client
# @param [ResponseService] stream_response_handler - Handles streaming chunks to the client
def initialize(user_input:, tools:, context:, response_handler:, stream_response_handler: nil)
@user_input = user_input
@tools = tools
@context = context
@iterations = 0
@logger = Gitlab::Llm::Logger.build
@response_handler = response_handler
@stream_response_handler = stream_response_handler
end
def execute
MAX_ITERATIONS.times do
thought = if Feature.enabled?(:stream_gitlab_duo, context.current_user)
thought = if stream_response_handler && Feature.enabled?(:stream_gitlab_duo, context.current_user)
execute_streamed_request
else
request
@@ -85,7 +88,7 @@ def execute_streamed_request
chunk = streamed_answer.next_chunk(content)
if chunk
response_handler.execute(
stream_response_handler.execute(
response: Gitlab::Llm::Chain::PlainResponseModifier.new(content),
options: {
cache_response: false,
@@ -101,7 +104,7 @@ def tools_cycle?
context.tools_used.size != context.tools_used.uniq.size
end
attr_reader :logger
attr_reader :logger, :stream_response_handler
# This method should not be memoized because the input variables change over time
def base_prompt
Loading