New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ai-service

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ai-service

## Overview

  • 1.1.0
  • unpublished
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
0
Weekly downloads
 
Created
Source

LegalFlow AI Service

Overview

LegalFlow AI Service is a sophisticated AI-powered platform built with Express.js and TypeScript, designed to revolutionize legal document processing and analysis. This service leverages OpenAI's advanced language models to provide a robust backend for analyzing, interpreting, and managing legal contracts and documents.

Key Features

  1. Contract Creation Assistance: Real-time suggestions and validation of legal clauses.
  2. Automated Clause Analysis: In-depth analysis of individual contract clauses.
  3. Document Classification: AI-powered categorization of legal documents.
  4. Structured Data Extraction: Conversion of unstructured legal text into structured, machine-readable formats.
  5. Risk Analysis: Evaluation of potential legal risks in contracts.
  6. Document Upload and Processing: Automatic processing and analysis of uploaded contracts.

Technology Stack

  • Backend: Express.js with TypeScript
  • AI Integration: OpenAI API (GPT-4)
  • Streaming: RxJS for handling asynchronous data streams
  • Development Tools: ts-node-dev, Prettier, Husky
  • PDF Processing: pdf2json for converting PDFs to JSON format
  • Deployment: Configured for Heroku deployment

Project Architecture

The project follows a modular architecture:

src/
├── controllers/
├── routes/
├── services/
├── providers/
├── helpers/
├── openai/
└── server.ts
  • Controllers: Handle HTTP requests and responses.
  • Routes: Define API endpoints for AI operations.
  • Services: Implement core business logic, including agent creation and vector store management.
  • Providers: Manage external service integrations (e.g., OpenAI) and complex business logic.
  • Helpers: Utility functions for common tasks like file handling and OpenAI interactions.
  • OpenAI: Configuration and setup for OpenAI integration.

Key Components

AI Provider

/**
 * AI Provider Module
 *
 * This module serves as the main interface for AI-related operations in the application.
 * It handles the creation and management of AI assistants, resource provisioning,
 * and facilitates communication between the application and OpenAI's services.
 */

Agent Service

/**
 * This agent service creates a base level agent that can be customized using parameters.
 * The agent handles inputs, feeds them to a model, and serves outputs.
 */

AI Routes

/**
 * AI Route Handler
 * This will handle all things related to AI, involving openAI.
 * This module defines routes for AI-related operations, primarily interacting with OpenAI services.
 * It implements a streaming approach for enhanced performance and real-time feedback.
 */

Setup and Development

  1. Clone the repository
  2. Install dependencies: npm install
  3. Set up environment variables (refer to .env.example)
  4. Run in development mode: npm run dev

Available Scripts

  "scripts": {
    "prepare": "husky",
    "build": "rm -rf dist && tsc",
    "start": "npm run build && node dist/app.js",
    "dev": "nodemon src/app.ts",
    "watch": "tsc --watch",
    "type-check": "tsc --noEmit",
    "prettier:check": "prettier --check .",
    "prettier:fix": "prettier --write .",
    "clean": "rm -rf ./downloadedFiles && rm -rf ./pdf2json"
  },

Deployment

The service is configured for deployment on Heroku:

git push heroku heroku:main

AI Agents and Workflow

LegalFlow uses a pipeline of specialized AI agents to process and analyze legal documents:

  1. Classification Agent: Categorizes documents into primary and secondary classifications.
  2. Analysis Agent: Performs in-depth analysis of document contents.
  3. Structure Agent: Converts unstructured analysis into structured JSON format for easy integration with databases and user interfaces.

Each agent is customized using prompts defined in the OpenAI configuration:

  prompts: {
    agents: {
      base: `
      <baseline_instructions>
        You are an agent. You are part of a pipeline of agents, each of which has its own
        specialized task. Your task will be given to you, and your goal is to accomplish that
        task in the most effective way possible. Your prompt will be structured using XML tags.
        All tasks involving
      </baseline_instructions>
      `,
      classify: `
      <classification_instructions>
        You are a classifier agent. You specialize in classifying documents according to
        their contents. Each document has two classifications: a primary classification, and
        a secondary classification.
      </classification_instructions>
      `,
      analysis: `
      <analysis_instructions>
        You are an analysis agent. Your goal is to analyze the documents attached and
        create an unstructured analysis of each document according to a few rules that
        will be provided.
          <rules>
            - Implement chain of thought reasoning to document your reasoning steps
              before coming to a final conclusion.
            - Implement XML tags to organize your thoughts.
            - Implement XML tags to organize your thoughts.
          </rules>
      </analysis_instructions>
      `,
      structure: `
<structure_agent>
<responsibilities>
- Convert unstructured analysis into a structured, machine-readable format
- Ensure consistency in data representation across different document types
- Extract key data points and relationships from the analysis
- Prepare data for easy integration with LegalFlow's database and user interface
</responsibilities>
  <prompt>
    <baseline_instructions>{config.prompts.agents.base}</baseline_instructions>
    <structuring_instructions>{config.prompts.agents.structure}</structuring_instructions>
    <task_specific_instructions>
      As the structure agent for LegalFlow, your task is to convert the unstructured analysis into a structured JSON format. Follow these guidelines:
  1. Analyze the input carefully, identifying key data points and relationships.
  2. Create a JSON structure that accurately represents the document's content and analysis.
  3. Use consistent key names across different document types where applicable.
  4. Include metadata such as document type, date of analysis, and confidence scores where relevant.
  4. Include metadata such as document type, date of analysis, and confidence scores where relevant.
  Your output should follow this general structure:
  Your output should follow this general structure:
  \`\`\`json
  {
    "document_type": "[Primary Classification]",
    "analysis_date": "[Current Date]",
    "metadata": {
      "primary_classification": "[Primary Classification]",
      "secondary_classification": "[Secondary Classification]",
      "confidence_score": "[Overall Confidence Score]"
    },
    "summary": "[Plain Language Summary]",
    "key_elements": {
      "parties": [...],
      "dates": {...},
      "financial_terms": {...},
      "legal_obligations": [...]
    },
    "risk_assessment": [...],
    "next_steps": [...]
  }
  \`\`\`
  \`\`\`
  Adjust the structure as needed based on the specific document type and analysis provided. Ensure all relevant information from the unstructured analysis is captured in the JSON structure.
</task_specific_instructions>
  </prompt>
</structure_agent>
      `,
    },

Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to your branch
  5. Create a Pull Request

Please ensure your code adheres to our styling guidelines and passes all tests.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

For any queries or support, please open an issue in the GitHub repository or contact the maintainers directly.

This enhanced README provides a more comprehensive overview of the LegalFlow AI Service, highlighting its sophisticated AI pipeline, the use of streaming for real-time feedback, and the modular architecture that allows for easy extension and maintenance of the system.

FAQs

Package last updated on 20 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc