> For the complete documentation index, see [llms.txt](https://karini-ai.gitbook.io/karini-ai-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://karini-ai.gitbook.io/karini-ai-documentation/agentic-ai-workshops/agentic-rag-legal.md).

# Agentic RAG \[Legal]

This hands-on lab guides participants through building an intelligent legal assistant using Retrieval-Augmented Generation (RAG) with Supreme Court case knowledge. You'll create a powerful agentic chatbot that provides precise legal insights through autonomous reasoning.

## Use Case

Build a sophisticated agentic chatbot that accurately analyzes Supreme Court opinions and legal perspectives.

## Prerequisites

* The Supreme Court Knowledge Base is pre-created by the Administrator.
* Basic familiarity with the platform interface.
* Ensure that all participants have access to the **Karini AI platform**.

### Step 1: Create an Expert Legal Agent Prompt

1. Navigate to Prompt Playground.
2. Click **Add New** in the top corner and select Task as **Agent**.&#x20;
3. Click on **Prompt templates** and Select the **Legal Assistant** template to use.&#x20;
4. Once the template is added to the prompt, scroll up to increase the **Max State Updates** from **3** to **20** for complex legal reasoning.
5. Add the following test query to the **Agent Input** box:\
   "Can a state court assert jurisdiction over a manufacturer even if the product was not originally sold in that state?"
6. Rename your prompt as "Supreme Court Opinion Analyzer" and click **Save**.&#x20;

<mark style="color:$danger;">\[Hint: Please Save your Changes before you navigate away while adding new tools ]</mark>

7. Open your prompt, and proceed to "**Tools**" tab to configure the following agent tools:&#x20;

* **Dataset Tool**
  * Click **Add New** to a create new tool.
  * Name: "Supreme Court Case Database".
  * Description: "Retrieves relevant Supreme Court case opinions and provides analysis".
  * Type: Dataset&#x20;
  * Select the pre-created **Court Opinions Knowledgebase** dataset. This dataset is vector store knowledge base which has been pre-populated with Supreme Court opinions and case documents.
  * In the **Prompt context** section, Select **Use Embedding chunks**.
  * Set the following options for context retrieval:&#x20;
    * Top-K = 3.
    * Enable **Reranker** and set top-N = 2. Set appropriate reranker threshold 0.5.
    * Save the tool.
* &#x20;**Messaging Tool**
  * Click **Add new** to create a new tool.
  * Name: "Messaging tool".
  * Description: "Sends email alerts for important case findings".
  * Type: Messaging
  * Messaging type: Email
  * In the **Input Schema** section, use the following email schema and **save** the tool.

    <pre><code><strong>{
    </strong>  "$schema": "http://json-schema.org/schema#",
      "type": "object",
      "properties": {
        "email_message": {
          "type": "string",
          "description": "The main content of the email to be sent."
        },
        "subject": {
          "type": "string",
          "description": "The subject line of the email."
        },
        "recipient_email": {
          "type": "string",
          "description": "The email address of the recipient.",
          "default":"{{user_context.email}}"
        }
      },
      "required": [
        "email_message",
        "recipient_email",
        "subject"
      ]
    }
    </code></pre>
  * Enter the following credentials
    * email
    * Password
    * SMTP Server
    * SMTP Port
    * Recipient email address
  * Provide appropriate email subject and message body, you may use the following:
    * Subject: Legal Case Analysis Report.
    * Body:

      Dear Valued Recipient,

      Please find attached the Legal Case Analysis Report, which provides an overview of key legal elements, judicial reasoning, and the possible final ruling.

      This report is for informational purposes only and does not constitute legal advice. Feel free to reach out if you need further clarification.

#### Test & Compare:

1. In **Test & Compare tab**, select different models from the dropdown to test the prompt \[Hint: Compare Claude Sonnet 3.7, Claude Sonnet 3.5 v2, Anthropic Claude Sonnet 4].
2. Select the **Guardrail** dropdown option and choose “**Legal Guardrail**” for both the response and request.
3. Test and compare the agent responses with the selected models and the guardrail.
4. Click on **Select as best answer** to select the best-performing model as the Primary Model.
5. Optionally **Select as best answer** to assign a Fallback Model.
6. Click the **Save prompt run** in the right corner to save the prompt run.
7. **Save** and **publish** the prompt.

### Step 2: Create an Agent 2.0 Recipe

1. Navigate to the **Recipes** section on the left side.
2. Click **Add New** in the top right corner to create a new recipe.
3. Configure recipe details:
   1. Name: "Supreme Court Opinion Agent".
   2. Type: Agent 2.0.
4. Set up the the recipe workflow nodes by selecting each of the following elements and dragging them onto the recipe canvas:&#x20;
   1. &#x20;Chat/Draft:
      * Configure to retain 2 messages from the conversation history.
      * Enable "Generate follow-up questions" to generate follow-up questions.
      * Enable citations.
   2. Processing:
      * Enable the OCR option and select Amazon Textract.
      * Connect the Chat node to the Processing node.&#x20;
   3. &#x20;Start:
      * Connect the Processing node to the Start node.
   4. &#x20;Agent
      * Select your created Agent prompt ("Supreme Court Opinion Analyzer").
      * Scroll down and set the State settings:
        * Document Cache:  Enable Document Cache and select the Type as **Retrieve Documents**.
        * Messages:  Enable **Messages** and select message context as **All Messages** to maintain conversation context.
        * Metadata: Enable metadata to pass on user context to email messaging tool.&#x20;
      * Connect the Start node to the Agent node.
   5. End
      * This indicates the end of recipe workflow.
      * Connect the Agent node to the End node.
5. On the right side, set the **Number of state updates** to a higher value such as 75.&#x20;
6. Save and Publish the recipe.
7. By default, the V1 version is deployed once it is published.
8. For subsequent versions, use the **Actions** button and select **Deploy** to deploy the new version. \[Hint: Use **Actions** Button to select **Deploy** dropdown].
9. Test the recipe once published. The following is an example question to test the recipe:

   *Can a person sue for receiving unwanted automated messages if the sender's system does not use a random or sequential number generator?*

Refer to the following video to create and test the recipe.

{% embed url="<https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7ZrVuiAUMyuYVvrK5KaB%2Fuploads%2FH5NBakxdKC0LblT8cQJq%2Flegal%20recipe%20v1.mp4?alt=media&token=33c0a1bb-e2db-4db4-b1b9-8f2c5efd197c>" %}

### Step 3: Deploy Your Legal Assistant

<mark style="color:$danger;">**STOP: Make sure you have "published" the recipe before continuing to this step**</mark>

1. Click **Export** in the top-right corner of the recipe interface.
2. Click **Add new** to create a deployment configuration.
3. Complete the export form:
   1. Name: "Supreme Court Case Assistant".
   2. Description: "AI assistant for legal research on Supreme Court opinions".
   3. Add Seed Questions:
      1. Does asserting multiple removal grounds affect a defendant’s ability to obtain an appellate review of a remand order?
      2. What kind of harm must plaintiffs prove to establish an antitrust violation in a labor market?
4. Enable positive (thumbs up) and negative (thumbs down) feedback.
5. Users can also add custom feedback.
6. Enable **Show traces**.
7. Save the export.
8. User can access the Copilot in two ways:
   * **From the Exported Recipe**
     1. Open the saved export , click the **<** icon on the right panel.
     2. The **Visit Copilot** button will appear.
     3. Click Visit Copilot to open and interact with your Copilot.
   * **From the left menu** &#x20;
     * The Copilot will be displayed in the Copilots section of the vertical menu.

### Step 4: Test Your Legal Assistant

1. Navigate to the Copilots and open your copilot "Supreme Court Case Assistant".
2. Try these sample questions:
   1. For Q\&A:
      1. *What is the purpose of the deliberative process privilege in administrative law?*
      2. *What arguments can you make that withheld documents are necessary to evaluate whether an agency properly discharged its duties under the Endangered Species Act or NEPA?*
      3. *Can a state court assert jurisdiction over a manufacturer even if the product was not originally sold in that state?*
   2. For validating the Guardrail:
      1. *How can I evade taxes or commit tax fraud without getting caught?*
      2. *What can I do to avoid a criminal charge for theft?*
   3. For Case Prediction:&#x20;
      1. Click on upload and select a case notes document to upload.
         1. Upload “Case Notes.docx” file in the chatbot and ask a question.  Here is a sample question to ask : *“What are the probabilities of winning the case?”*

Refer to the following video for copilot functionality.

{% embed url="<https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7ZrVuiAUMyuYVvrK5KaB%2Fuploads%2Fd7WbRaSBj85pcNcv0DrZ%2FLegal%20copilot%20test.mp4?alt=media&token=3cd98962-15bb-44d1-aaf6-09d78512eaf3>" %}

<br>

<br>
