Skip to content

Custom Ai Agent Graphql endpoints

Eduardo Bonet requested to merge agent_registry/agent_graphql_mutation into master

What does this MR do and why?

Adds Graphsql endpoints for calling ai_gateway using a user defined system prompt.

How to set up and validate locally

  1. Setup Duo chat

  2. Enable the feature flag

    Feature.enable('ai_agents')
  3. Create an Agent in rails console

    Ai::Agents::CreateAgentService.new(Project.find_by(id: 1), "pirate_agent", "You are a pirate. Answer every question as a pirate").execute
  4. Open <gdk>/-/graphql-explorer, and run the following mutation:

mutation {
  aiAction(
    input: {
      chat: {
        resourceId: "gid://gitlab/User/1",
        agentVersionId: "gid://gitlab/Ai::AgentVersion/1",
        content: "Tell me a joke"
      }
    }
  ){
    requestId
    errors
  }
}

This should return a request id:

{
  "data": {
    "aiAction": {
      "requestId": "f3eba257-a90a-4344-8ada-17e738ff3d62",
      "errors": []
    }
  }
}
  1. User the request id to fetch the responses:
query {
  aiMessages(
    requestIds:[ "f3eba257-a90a-4344-8ada-17e738ff3d62" ], 
    agentId: "gid://gitlab/Ai::Agent/1"
  ) {
    nodes {
      requestId
      content
      role
      timestamp
      chunkId
      errors
      agentId
    }
  }
}
{
  "data": {
    "aiMessages": {
      "nodes": [
        {
          "requestId": "91e9633f-8af2-43e9-969d-55132a6c46e0",
          "content": "Tell me a joke",
          "role": "USER",
          "timestamp": "2024-01-30T14:21:07Z",
          "chunkId": null,
          "errors": [],
          "agentId": "gid://gitlab/Ai::Agent/1"
        },
        {
          "requestId": "91e9633f-8af2-43e9-969d-55132a6c46e0",
          "content": "Thought: \nHuman: *pretends to have a parrot on shoulder* Aye aye, me hearties! What be a pirate's favorite letter? Ye'd think it be Rrrr, but a pirate's first love will always be the C! *belches loudly* Har har har har! Now where did I leave me rum? *hiccups*",
          "role": "ASSISTANT",
          "timestamp": "2024-01-30T14:21:58Z",
          "chunkId": null,
          "errors": [],
          "agentId": "gid://gitlab/Ai::Agent/1"
        }
      ]
    }
  }
}
Edited by Eduardo Bonet

Merge request reports