Skip to content

Building a bot with Flask – RBot V2

Welcome to the exciting journey of building an AI-powered legal assistant that can help users find resources without crossing into giving legal advice. Today, we’ll explore how we use Flask, a Python web framework, along with advanced language models to create RBot V2 – a powerful tool that can assist users by answering their queries about legal resources.

In this post, we dive into the code and understand the nuts and bolts of the innovative application that is RBot V2!

Flask Framework: Setting Up the Server

At the heart of our application is Flask, a lightweight and easy-to-use Python web framework. It is perfect for small to medium web applications:

from flask import Flask, render_template, request, jsonify
app = Flask(__name__)

The Flask class is instantiated to create the web application. We also enable cross-origin resource sharing (CORS) to allow web pages to make requests to our server from different domains.

Integrating with OpenAI’s Language Models

Our application taps into the power of OpenAI’s language models to process and understand user queries. The environmental variable OPENAI_API_KEY is set to authenticate our requests to the OpenAI API:

import os
os.environ["OPENAI_API_KEY"] = "your-api-key"

Handling Document Loading and Text Splitting

To manage the legal resources, the app uses a document loader for PDFs, which is ideal for processing text-based legal documents:

from langchain.document_loaders
import PyPDFLoader loader = PyPDFLoader("merged.pdf")
documents = loader.load()

Once the documents are loaded, they are split into manageable chunks using a character-based splitter. This helps in processing the text more efficiently:

from langchain.text_splitter import CharacterTextSplitter
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)

Creating an Embedding Database for Quick Retrieval

The application converts texts into embeddings using OpenAI’s models, and stores these in a database optimized for quick retrieval:

from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma
embeddings = OpenAIEmbeddings()
db = Chroma.from_documents(texts, embeddings)
retriever = db.as_retriever(search_type="similarity", search_kwargs={"k":3})

Setting Up the Conversational AI

A conversational retrieval chain is created, which combines the documents based on user queries. It uses a custom prompt that guides the AI to respond correctly without offering legal advice:

from langchain.chains import ConversationalRetrievalChain
from langchain_core.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate

system_message = "You are a helpful AI assistant..."
custom_prompt = ChatPromptTemplate.from_messages([
  SystemMessagePromptTemplate.from_template(system_message),
  HumanMessagePromptTemplate.from_template("{question}"),
])
qa = ConversationalRetrievalChain.from_llm(OpenAI(), retriever, combine_docs_chain_kwargs={"prompt": custom_prompt})

Building the Web Interface

Flask also makes it easy to link HTML pages to functions in Python, allowing for dynamic responses based on user input:

@app.route('/1.html')
def index():
  return render_template('1.html')

Each route corresponds to a page on the website, and functions like get_response handle POST requests to provide answers to user questions.

Running the Server

Finally, we start the server with debugging enabled. This allows for live updates without restarting the server:

if __name__ == '__main__':
  app.run(debug=True)

Conclusion

With this setup, users can interact with an AI that guides them through legal resources without providing direct legal advice. This application is not just a technical showcase but also a practical tool for anyone looking for legal resource guidance. It’s a perfect example of how AI can be used to enhance access to information while respecting professional boundaries. Whether you’re a developer, a student, or just curious about AI and web development, this project offers a rich learning experience and a base for further exploration.