Draft: Rough Draft of Conversational CI backend
What does this MR do and why?
This MR introduces a graphql mutation and query field.
The first mutation takes the message content from the user and sends it to the Open ai endpoint. It then persists the messages from the user and the assistant to the database.
The second query field allows the frontend to poll for the entire conversation as paginated via graphql.
Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/407739
Screenshots or screen recordings
How to set up and validate locally
- Enable the feature flag for this feature
ai_ci_config_creator
Feature.enable(:ai_ci_config_generator)
- Enable the
:openai_experimentation
feature flag that thellm_client
is behindFeature.enable(:openai_experimentation)
- Run
rails c
then setup the api key locally. You have to request one: https://gitlab.slack.com/archives/C0517A9PZ8S/p1681445186381439Gitlab::CurrentSettings.update(openai_api_key: "<your-key>")
- Then navigate to your graphql explorer. For instance localhost:3000/-/graphql-explorer
- Run the mutation
mutation { ciAiGenerateConfig(input: { projectPath: "root/cache-test", userContent: "How can I deploy a python application?" } ) { errors userMessage { id, role, content, } } }
- Query for conversation messages
query q1 { project(fullPath: "root/cache-test") { aiConversations { ciConfigMessages { nodes { id, content, role, errors, isFetching } } } } }
Note
Since clearing messages over the token limit is not yet implemented you can nuke them all like this:
rails c
- Destroy all messages
Ci::Editor::AiConversation::Message.destroy_all
@andrei.zubov, I wasn't sure if you wanted them with created_at asc of desc. If you want to return the results in the opposite order you can apply this patch:
diff --git a/ee/app/models/ai/project/conversations.rb b/ee/app/models/ai/project/conversations.rb
index 081aebd38f48..91ca830ef2c5 100644
--- a/ee/app/models/ai/project/conversations.rb
+++ b/ee/app/models/ai/project/conversations.rb
@@ -9,7 +9,7 @@ def initialize(project, user)
end
def ci_config_messages
- Ci::Editor::AiConversation::Message.where(project: @project, user: @user).order(:created_at)
+ Ci::Editor::AiConversation::Message.where(project: @project, user: @user).order(created_at: :desc)
end
end
end
TODOS
- We have deduplication but if the content is changed they could send another message while the other one is processing. We should make sure that the service which calls the worker locks the resource for editing in some way. We could just return if the last message for the user/project combo is
- Add feature checks for ultimate
- Add tests
- Take token limits into consideration (clear messages older than 90 days based on legal limitations)
- Write code to clear messages older than 90 days
Edited by Allison Browne