Error when asking questions on the page with a code file

Problem

I am getting this error on gitlab.com even after running /clear for every request when I am on this file:

Screenshot_2024-07-30_at_10.32.29_AM

AI Gateway logs for this error show an interesting backtrace:

    raise KeyError(
KeyError: "Input to ChatPromptTemplate is missing variables {'current_file.file_path'}.  Expected: ['agent_scratchpad', 'chat_history', 'current_file.file_path', 'question'] Received: ['chat_history', 'question', 'agent_scratchpad', 'current_file']"

Steps to reproduce

on GitLab.com:

  1. Enable flag v2_chat_agent_integration for your account.
  2. Go to any page with code file, for example this one
  3. Ask Chat any questions, example "how to make rebase"
  4. Observe Chat response
I'm sorry, I couldn't respond in time. Please try a more specific request or enter /clear to start a new chat.
  1. Correct behaviour: Chat replies on the question.

on GDK

  1. AI Gateway and Rails server are running.
  2. Feature flag v2_chat_agent_integration is enabled
  3. Go to a project where Duo Chat is enabled
  4. Go to or create a code file in a repository. Example:
# quicksort.rb

# This function takes an array and returns a sorted array using the quicksort algorithm.
def quicksort(arr)
  return arr if arr.length <= 1

  pivot = arr.pop
  left = arr.select { |x| x < pivot }
  right = arr.select { |x| x >= pivot }

  quicksort(left) + [pivot] + quicksort(right)
end

# Example usage
unsorted_array = [5, 2, 8, 1, 9, 3]
sorted_array = quicksort(unsorted_array)
puts sorted_array.inspect # Output: [1, 2, 3, 5, 8, 9]
  1. On a page with the code file open ask Chat any question, example "how to make rebase"
  2. Observe Chat response
I'm sorry, I couldn't respond in time. Please try a more specific request or enter /clear to start a new chat.
  1. In AI Gateway logs observe an error similar to
File "/Users/tetianachupryna/Documents/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.10/site-packages/langchain_core/prompts/base.py", line 145, in _validate_input
2024-07-31_13:21:45.53387 gitlab-ai-gateway     :     |     raise KeyError(
2024-07-31_13:21:45.53388 gitlab-ai-gateway     :     | KeyError: "Input to ChatPromptTemplate is missing variables {' |x| x < pivot ', ' |x| x > pivot ', 'array', ' |x| x == pivot ', 'current_file.file_path', 'sorted_array'}.  Expected: [' |x| x < pivot ', ' |x| x == pivot ', ' |x| x > pivot ', 'agent_scratchpad', 'array', 'chat_history', 'current_file.file_path', 'question', 'sorted_array'] Received: ['chat_history', 'question', 'agent_scratchpad']"

Debug notes

Debug showed that Rails correctly pass current_file params and AIGW correctly receive it. To check that, go to ai_gateway/api/v2/chat/agent.py and inspect input:

print(f"FILE PATH: {agent_request.options.current_file.file_path}")
print(f"FILE DATA: {agent_request.options.current_file.data}")

This will print correct file path and file content.

The possible error happens somewhere later when we parse or process input params.

Edited by Tetiana Chupryna