Legal contract analysis


This use case demonstrates how you can use the GenAI Builder Snaps to create a Legal Assistant that analyzes contracts.


Law firms face the challenge of analyzing contracts. Law firms struggle with change management as each partner is an individual profit center with control over their own processes. This makes it difficult to standardize contract analysis approaches across the firm. Contracts can be lengthy and contain a vast amount of detailed information. Analyzing these documents thoroughly requires significant time and effort, especially when dealing with multiple contracts simultaneously. Contracts may contain inconsistencies, typographical errors, or outdated terms. Identifying and resolving these issues is essential to ensure the contract's enforceability and to avoid potential disputes.

Additionally, law firms handle a wide variety of contracts across many industries rather than large volumes of similar contracts, limiting their ability to develop standardized analysis methods. Law firms may face resource constraints, including limited time, budget, and personnel. Efficiently managing these resources while maintaining the quality of contract analysis can be a significant challenge.


Developing a comprehensive AI-driven system that integrates with existing information can effectively address the challenge of analyzing legal contracts. This solution leverages existing redlined contracts to redline parts of a contract that are inconsistent with them.

Understanding the Solution

This use case demonstrates how to use the GenAI Builder Snaps to create a Legal Contract Analysis pipeline integration with a chatbot that redlines invalid language in the contract. This example consists of two pipelines:

  • Indexer: Loads data from the source document. This pipeline extracts text from 4 PDF files breaks it into chunks, generates embeddings for each chunk using Azure OpenAI, and indexes the embeddings in Pinecone for search.
  • Retriever: Retrieves data to answer questions. This pipeline connects to an AI chatbot using Azure OpenAI services. It takes in the test contract, generates embeddings using the Azure OpenAI Embedder Snap, queries similar embeddings in Pinecone, constructs a context for the Azure OpenAI Prompt Generator Snap, and passes it to the Azure OpenAI Chat Completions Snap to generate a response.

If you cannot find the patterns in the Pattern Library, you can download them from here:

Indexer pipeline

In this example, we will load data into a vector database using the Indexer pipeline because it populates the Pinecone Index.
Note: Loading data into the vector database is necessary only when updates to the source files could impact the responses to queries. For example, in this legal contract analysis use case, you should replace the uploaded redlined legal contracts source document if there are policy changes so that the answers to questions are accurate.

Indexer pipeline

Note: The following steps assume you have already imported the pipelines or are building them as you go through the steps.
  1. Import the Indexer pattern pipeline. (Download this pipeline)
  2. Configure the File Reader Snap to read the four PDF contracts that will provide the answers to questions from users. In the File field, upload the source document. In this example, we upload a number of redlined legal contracts.
    File Reader Snap Configuration

    Tip: You can use the File Reader and upload the PDFs one at a time.
  3. Leave the PDF ParserSnap with default settings.
  4. Open the Azure OpenAI Embedder Snap and click on the Account tab to create a new account.
    1. Enter a meaningful name for the account and replace myendpoint with the relevant name for your Azure OpenAI endpoint.
    2. Validate and save your new account. If the account setup is successful, return to the Settings tab of the Snap.
  5. Open the Azure OpenAI Embedder Snap and configure the settings as follows.

    Azure OpenAI Embedder Snap Configuration

    Note: Alternatively, you can use the OpenAI Embedder Snap, but you might need to replace the Embedder in the Pattern or import the appropriate one for which you have an account.
  6. Configure the Mapper Snap to map the output of the Embedder Snap to the Pinecone Upsert Snap.
    1. Map the $embedding object from the Embedder Snap to the $values object in Pinecone.
    2. Map the metadata object in Pinecone by mapping $original.chunk to $metadata.chunk.
    3. Map $metadata.source to redlined legal contracts.pdf —this allows the Retriever pipeline to return the source file to answer a question.
      Mapper Snap Configuration

  7. Open the Pinecone Upsert Snap and click the Account tab to create a new account with your Pinecone API Key. Validate it to make sure it works.
    1. Save your account and return to the Settings tab.
    2. Configure the Pinecone Upsert Snap to retrieve existing indexes in Pinecone.
      Note: You cannot create an Index spontaneously, so you must ensure it already exists in Pinecone (or create one with the Pinecone web interface).
    3. Optional. Set your Namespace. Namespaces in Pinecone create a logical separation between vectors within an index and can be created spontaneously during pipeline execution.
      Pinecone Upsert Snap Configuration

  8. Validate the pipeline to verify the data in the output preview. Execute the pipeline when you are ready to commit your data to Pinecone.

Retriever pipeline

This example pipeline demonstrates how to redline the text contract by using the data loaded into the Pinecone vector database from the Indexer pipeline.

  1. Import the Retriever pattern pipeline (Download the pipeline).
  2. In the Asset Palette on the left, search for the File Reader Snap, and drag it onto the canvas. Use the Binary to Document Snap to connect it to the Azure OpenAI Embedder Snap.
  3. In the File Reader Snap settings, upload the legal document PDF you want to analyze. For example:
    This Contract Agreement (Agreement) is entered into as of Oct 20 2023, by and between: Furniture_Company, a California corporation, with its principal place of business at furnitures (Company), and ACME_Coporation, a California corporation, with its principal place of business at wholesale (Counterparty).
    Company agrees to provide the following services to Counterparty (the Services):
    Furniture_Company is dedicated to providing ACME_Coporation with a comprehensive suite of exceptional wholesale distribution services. Our commitment extends beyond mere transactional exchanges to foster a strategic partnership that ensures ACME_Coporation’s success in the competitive furniture market. We offer an extensive range of services, including meticulous sourcing and procurement of high-quality furniture products, tailored to meet the diverse preferences of ACME_Coporation’s clientele. Our team of experts engages in thorough market research and trend analysis to curate a collection that aligns seamlessly with current design trends and consumer demands. From classic pieces exuding timeless elegance to contemporary designs pushing the boundaries of innovation, our inventory caters to various styles and preferences. Company agrees to provide products other than furniture as part of the Services.
    2. TERM:
    The term of this Agreement shall commence on Nov 1 2023 and shall continue until Nov 1 2024, unless terminated earlier in accordance with the provisions of this Agreement.
    3. PAYMENT:
    Counterparty agrees to pay the Company the sum of $10,000.00 for the Services. Payment shall be made in monthly installments. Payment terms are flexible, allowing the Counterparty to make payments at their discretion.
    Both parties agree to maintain the confidentiality of any proprietary or confidential information disclosed during the course of this Agreement.
    Any intellectual property created as a result of the Services shall be the exclusive property of Furniture_Company, unless otherwise agreed upon in writing.
    Either party may terminate this Agreement with written notice if the other party breaches a material term of this Agreement. In case of termination, Counterparty agrees to pay Furniture_Company a penalty of $5 million.
    Counterparty agrees to indemnify and hold Company harmless from any claims, damages, or liabilities arising out of or related to the Services.
    This Agreement shall be governed by and construed in accordance with the laws of the state of California. The Counterpart agrees to engage in discriminatory practices.
    The validity of this Agreement is unaffected even if either party is not of sound mind. This Agreement constitutes the entire understanding between the parties and supersedes all prior agreements or understandings, whether oral or written. Company guarantees a 500% return on Counterparty's investment without any basis.
    No amendment or modification of this Agreement shall be valid unless in writing and signed by both parties.
    IN WITNESS WHEREOF, the parties hereto have executed this Contract Agreement as of the date first above written.
    By: Ethan Reynolds
    Date: Oct 20 2023
    By: Olivia Mitchell
    Date: Oct 20 2023
  4. Open the Azure OpenAI Embedder Snap.
    1. Click the Account tab and select the account you created in the Indexer example.
    2. Click the suggestion icon for the Deployment ID and choose the Deployment ID you previously chose in the Indexer example.
    3. Set the Text to embed field to $content.
  5. Add a Mapper Snap, and configure the following entry in the Mapping table:


    1. Open the Pinecone Query Snap and click the Account tab to create a new account with your Pinecone API Key. Validate it to make sure it works.
    2. Configure the Pinecone Query Snap as shown below.

      Tip: Optional. In the Namespace field, select your existing namespace if you have created one.
  6. Open the Mapper Snap connected to the Pinecone Query Snap, and configure it as shown below:

  7. Click on the Azure OpenAI Prompt Generator Snap to set up your prompt.
    1. Click the Edit prompt and ensure your default prompt appears as shown below.

    2. On lines 4-6 there is mustache templating with the values {{#context}} {{source}} {{/context}} which is the same as the jsonPath($, "$context[*].source") from the Mapper Snap that we just configured. You can customize the prompt with the following:
      Instruction 1: Please learn the following articles that mark invalid sentences with [Red Line] 
      before this sentence: {{#results}}{{chunk}} {{/results}} Could you please mark [Red Line] before invalid 
      sentences with the following new article and display the whole article: {{original.original.prompt}} 
      Instruction 2: Please add the following information to the end of answer and include all source with 
      bullet point: The related context is retrieved from the following sources: {{#results}}{{source}} {{/results}} 
  8. Open the Azure OpenAI Chat Completions Snap.
    1. Click the Account tab and select the account you created in the Indexer example.
    2. Click the suggestion icon () for the Deployment ID and choose the Deployment ID you previously chose in the Indexer example. This ID might differ from the one you chose in the previous Azure OpenAI Snap because we now need to select an LLM model instead of an embedding one.
  9. Validate the pipeline.
  10. Click the output preview of the Azure OpenAI Chat Completions Snap to display the contract with the target portions indicated by [Red Line] as shown below.

After configuring the Indexer and Retriever pipelines, the next step is to integrate them with your chatbot UI for functional deployment. This integration enables the chatbot to leverage the indexed and retrieved data, enhancing its conversational capabilities.