Store additional context into chat history
What does this MR do and why?
Store additional context into Duo Chat history
Related to #480702 (closed)
MR acceptance checklist
Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Screenshots or screen recordings
Screenshots are required for UI changes, and strongly recommended for all other merge requests.
Before | After |
---|---|
How to set up and validate locally
REST endpoint
- Run
bundle exec rails c
- Call chat completion:
user = User.find_by_username('<YOUR-USERNAME>') safe_params = { content: 'please explain the following code', additional_context: [{ category: 'snippet', id: 'print', content: 'puts "Hello world"', metadata: "{\"freeform\":\"json\"}" }] } ::Gitlab::Duo::Chat::Completions.new(user).execute(safe_params: safe_params)
- Read stored messages
Gitlab::Llm::ChatStorage.new(user).messages
You should receive recent messages for the user, one of them should contain additional_context
. For example:
#<Gitlab::Llm::ChatMessage:0x000000017ccfece8
@additional_context=[{"id"=>"print", "category"=>"snippet", "content"=>"puts \"Hello world\"", "metadata"=>"{\"freeform\":\"json\"}"}],
@agent_version_id=nil,
@ai_action="chat",
@content="please explain the following code",
@errors=[],
@extras={"has_feedback"=>false},
@id="fae26fd5-6ca2-427f-b995-1273854f2428",
@request_id="df88b042-dc46-4822-82db-b86bdfce98d7",
@role="user",
@timestamp=Thu, 05 Sep 2024 08:52:51.000000000 UTC +00:00,
@user=#<User id:45 @ck3g>>,
GraphQL mutation
- Run
bundle exec rails c
- Find a required user
user = User.find_by_username('<YOUR-USERNAME>')
- Make sure feature flag is enabled
Feature.enable(:duo_additional_context, user)
- In your browser make sure you're logged in at
http://gdk.test:3000
as a user from previous steps - Navigate to
http://gdk.test:3000/-/graphql-explorer
- Run the following mutation
mutation chatAction { aiAction(input: {chat: {content: "Explain this code", additionalContext: [ { category: SNIPPET, id: "hello world", content: "puts 'Hello, world'", metadata: "{\"freeform\":\"json\"}" } ]}}) { clientMutationId errors } }
- Back in Rails console, call the following method
Gitlab::Llm::ChatStorage.new(user).messages
You should receive recent messages for the user, one of them should contain additional_context
. For example:
#<Gitlab::Llm::ChatMessage:0x000000017ca32aa0
@additional_context=[{"id"=>"hello world", "category"=>"snippet", "content"=>"puts 'Hello, world'", "metadata"=>"{\"freeform\":\"json\"}"}],
@agent_version_id=nil,
@ai_action="chat",
@content="Explain this code",
@errors=[],
@extras={"has_feedback"=>false},
@id="7cc9ae62-d69a-4e41-80b5-ce1922df8798",
@referer_url="http://gdk.test:3000/-/graphql-explorer",
@request_id="c60ec3e1-39e0-407a-92da-cb75dcbbed7a",
@role="user",
@timestamp=Thu, 05 Sep 2024 08:49:23.000000000 UTC +00:00,
@user=#<User id:45 @ck3g>>,
Edited by Vitali Tatarintev