MediumCrush 1L Year With AI-Powered Tools: Lesson 2. | by legaltextai | Medium
Sitemap
legaltextai

legaltextai

Gentle introduction of AI tools into the legal education and practice.

Crush 1L Year With AI-Powered Tools: Lesson 2.

2 min readJun 13, 2023

In Lesson 1, we learned how to add a PDF file of a legal case and query it using the Langchain framework. In this lesson, we will add several cases to our database and learn how to query them all at once.

Press enter or click to view image in full size

First, let’s make sure we have all the necessary packages and libraries installed:

from langchain import OpenAI
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.document_loaders import PyPDFDirectoryLoader
from langchain.indexes import VectorstoreIndexCreator

We’ll explore these packages in more depth later. At a high level, they are needed to:

  1. Load the files.
  2. Create an index.
  3. Retrieve the relevant texts.

For now, our goal is to demonstrate the abilities of large language models without going into too much detail.

Next, we set our OpenAI API key. You can obtain it at platform.openai.com:

os.environ["OPENAI_API_KEY"] = "your_api_key"

Let’s upload several PDF files into a directory called ‘supreme_court’:

Get legaltextai’s stories in your inbox

Join Medium for free to get updates from this writer.

Marbury v. Madison (1803)

Brown v. Board of Education (1954)

Gideon v. Wainwright (1963)

Miranda v. Arizona (1966)

Roe v. Wade (1973)

Next, we will ingest the uploaded documents using Langchain’s PDF directory loader and generate an index. For question answering across multiple documents, building an index over the data enables us to intelligently access the most pertinent documents for a given question, without the need to pass all the documents to the language model (which saves us time and money).

loader = PyPDFDirectoryLoader('supreme_court/')
index = VectorstoreIndexCreator().from_loaders([loader])

Don’t forget, use of the OpenAI API is not free (even though OpenAI offers some credits for new subscribers). Please monitor your usage and budget accordingly.

We are now ready to query our cases, all at once:

query = "What was the issue in Miranda?"
result = index.query_with_sources(query)
print(result)

{'question': 'What was the issue in Miranda?',
'answer': ' The issue in Miranda was the admissibility of a confession
obtained by police without informing the defendant of their rights.\n',
'sources': 'supreme_court/service-ll-usrep-usrep384-usrep384436-usrep384436.pdf'}

Note that “query_with_sources” allows you to see the name of the document where the answer was found.

You can now experiment with various questions by changing the input in the “query” variable. For example:

query = "which case talks about segregation in public schools?"
result = qa.run(query)
print(result)

{'question': 'which case talks about segregation in public schools?',
'answer': ' The case of Brown v. Board of Education talks about segregation in public schools.\n',
'sources': 'supreme_court/service-ll-usrep-usrep347-usrep347483-usrep347483.pdf'}

That concludes our lesson for today. We have learned how to construct a database containing multiple cases and execute natural language searches over that database. As your legal studies advance, your database will expand exponentially, and the ability to rapidly locate answers across precedents will prove invaluable.

--

--

legaltextai
legaltextai

Written by legaltextai

Gentle introduction of AI tools into the legal education and practice.