DEV Community

harshit-lyzr
harshit-lyzr

Posted on

Automate Legal Contract Generation with Streamlit and Lyzr Automata

In today's fast-paced business environment, creating legal contracts is a crucial yet often time-consuming task. Many businesses lack the resources to have a dedicated legal team on standby, which makes drafting legally sound contracts a daunting challenge. This is where automation and artificial intelligence (AI) come into play. By leveraging the power of AI, we can streamline the process of generating legal contracts, ensuring accuracy and efficiency.

In this blog, we will explore how to create a Legal Contract Generator using Streamlit and the Lyzr Automata. This application allows users to input the type of contract, specific requirements, and the involved parties, and then it generates a personalized legal contract. This innovative approach saves time and reduces the potential for errors, making it an invaluable tool for businesses of all sizes.

To address the problem of generating legal contracts efficiently, we can build a web application using Streamlit, a popular framework for creating interactive web apps with Python. By integrating Lyzr Automata's AI capabilities, we can automate the contract generation process.

Here's a step-by-step guide on how to achieve this:
Step 1: Setting Up the Streamlit App
First, we need to set up the Streamlit app. We configure the page with a title, layout, and initial sidebar state. We also include a logo image for branding purposes.

import streamlit as st
Enter fullscreen mode Exit fullscreen mode

Step 2: Integrating the AI Model
Next, we integrate the OpenAI model from Lyzr Automata. Users can enter their API key in the sidebar to authenticate and access the AI model.

from lyzr_automata.ai_models.openai import OpenAIModel

api = st.sidebar.text_input("Enter your OPENAI API KEY here", type="password")

if api:
    openai_model = OpenAIModel(
        api_key=api,
        parameters={
            "model": "gpt-4-turbo-preview",
            "temperature": 0.2,
            "max_tokens": 1500,
        },
    )
else:
    st.sidebar.error("Please enter your OPENAI API KEY")
Enter fullscreen mode Exit fullscreen mode

Step 3: Defining the Contract Generation Logic
We define a function legal_contract that creates an agent and a task to generate the contract using the specified details. The task is then executed through a linear synchronous pipeline, which ensures the sequential completion of tasks.

from lyzr_automata import Agent, Task
from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
from lyzr_automata.tasks.task_literals import InputType, OutputType

def legal_contract(types, requirement, parties):
    legal_agent = Agent(
        prompt_persona="You are an expert legal advisor.",
        role="Legal Contract Expert",
    )

    contract_task = Task(
        name="JS API Integration",
        output_type=OutputType.TEXT,
        input_type=InputType.TEXT,
        model=openai_model,
        agent=legal_agent,
        log_output=True,
        instructions=f"""Your task is to generate a legal contract with the given details.
        Search about the type of contract and craft a personalized legal contract between the given parties and mention necessary rules in the contract.
        Define rules based on the requirements given by the user.
        Below the document, leave some space and mention the companies' names for signing the document.
        Also, make important words and points bold.
        Do not write an introduction and conclusion.

        Type of Contract: {types}
        Requirements: {requirement}
        Parties Involved: {parties}
        """,
    )

    output = LinearSyncPipeline(
        name="Generate Legal Contract",
        completion_message="Contract Generated!",
        tasks=[contract_task],
    ).run()
    return output[0]['task_output']
Enter fullscreen mode Exit fullscreen mode

Step 4: User Input and Contract Generation
Finally, we set up the user interface to collect the type of contract, specific requirements, and parties involved. When the user clicks the "Generate" button, the legal_contract function is called, and the generated contract is displayed.

contract_type = st.text_input("Type of Contract", placeholder="Non Disclosure Agreement")
requirements = st.text_input("Specific Requirement", placeholder="Project Collaboration")
parties = st.text_input("Parties Involved")

if api and st.button("Generate"):
    solution = legal_contract(contract_type, requirements, parties)
    st.markdown(solution)
Enter fullscreen mode Exit fullscreen mode

try it now: https://lyzr-legal-contract.streamlit.app/
For more information explore the website: Lyzr

Top comments (0)