Asking questions via chat to the BRPTO’s Basic Manual for Patent Protection PDF, using LangChain, Pinecone, and Open AI

Have you ever wanted to search through your PDF files and find the most relevant information quickly and easily? If you have a lot of PDF documents, such as books, articles, reports, or manuals, you might find it hard to locate the information you need without opening each file and scanning through the pages. Wouldn’t it be nice if you could type in a query and get the best matches from your PDF collection?

In this blog post, I will show you how to build a simple but powerful PDF search engine using LangChain, Pinecone, and Open AI. By combining these tools, we can create a system that can:

  1. Extract text and metadata from PDF files.
  2. Embed the text into vector representations using a language model.
  3. Index and query the vectors using a vector database.
  4. Generate natural language responses using the “text-embedding-ada-002” model from Open AI.

What is LangChain?

LangChain is a framework for developing applications powered by language models. It provides modular abstractions for the components necessary to work with language models, such as data loaders, prompters, generators, and evaluators. It also has collections of implementations for these components and use-case-specific chains that assemble these components in particular ways to accomplish a specific task.

Prompts: This part allows you to create adaptable instructions using templates. It can adjust to different language learning models based on the size of the conversation window and input factors like conversation history, search results, previous answers, and more.

Models: This part serves as a bridge to connect with most third-party language learning models. It has connections to roughly 40 public language learning models, chat, and text representation models.

Memory: This allows the language learning models to remember the conversation history.

Indexes: Indexes are methods to arrange documents so that language learning models can interact with them effectively. This part includes helpful functions for dealing with documents and connections to different database systems for storing vectors (numeric representations of text).

Agents: Some applications don’t just need a set sequence of calls to language learning models or other tools, but possibly an unpredictable sequence based on the user’s input. In these sequences, there’s an agent that has access to a collection of tools. Depending on the user’s input, the agent can decide which tool – if any – to use.

Chains: Using a language learning model on its own is fine for some simple applications, but more complex ones need to link multiple language learning models, either with each other or with other experts. LangChain offers a standard interface for these chains, as well as some common chain setups for easy use.

With LangChain, you can build applications that can:

  • Connect a language model to other sources of data, such as documents, databases, or APIs
  • Allow a language model to interact with its environments, such as chatbots, agents, or generators
  • Optimize the performance and quality of a language model using feedback and reinforcement learning

Some examples of applications that you can build with LangChain are:

  • Question answering over specific documents
  • Chatbots that can access external knowledge or services
  • Agents that can perform tasks or solve problems using language models
  • Generators that can create content or code using language models

You can learn more about LangChain from their documentation or their GitHub repository. You can also find tutorials and demos in different languages, such as Chinese, Japanese, or English.

What is Pinecone?

Pinecone is a vector database for vector search. It makes it easy to build high-performance vector search applications by managing and searching through vector embeddings in a scalable and efficient way. Vector embeddings are numerical representations of data that capture their semantic meaning and similarity. For example, you can embed text into vectors using a language model, such that similar texts have similar vectors.

With Pinecone, you can create indexes that store your vector embeddings and metadata, such as document titles or authors. You can then query these indexes using vectors or keywords, and get the most relevant results in milliseconds. Pinecone also handles all the infrastructure and algorithmic complexities behind the scenes, ensuring you get the best performance and results without any hassle.

Some examples of applications that you can build with Pinecone are:

  • Semantic search: Find documents or products that match the user’s intent or query
  • Recommendations: Suggest items or content that are similar or complementary to the user’s preferences or behavior
  • Anomaly detection: Identify outliers or suspicious patterns in data
  • Generation: Create new content or code that is similar or related to the input

You can learn more about Pinecone from their website or their blog. You can also find pricing details and sign up for a free account here.

Presenting the Python code and explaining its functionality

This code is divided into two parts:

This stage involves preparing the PDF document for querying
This stage pertains to executing queries on the PDF

Below is the Python script that I’ve developed which can be also executed in Google Colab at this link.

PowerShell
# Install the dependencies
pip install langChain
pip install OpenAI
pip install pinecone-client
pip install tiktoken
pip install pypdf
Python
# Provide your OpenAI API key and define the embedding model
OPENAI_API_KEY = "INSERT HERE YOUR OPENAI API KEY"
embed_model = "text-embedding-ada-002"

# Provide your Pinecone API key and specify the environment
PINECONE_API_KEY = "INSERT HERE YOUR PINECONE API KEY"
PINECONE_ENV = "INSERT HERE YOUR PINECONE ENVIRONMENT"

# Import the required modules
import openai, langchain, pinecone
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Pinecone
from langchain.llms import OpenAI
from langchain.chains.question_answering import load_qa_chain
from langchain.document_loaders import UnstructuredPDFLoader, OnlinePDFLoader, PyPDFLoader

# Define a text splitter to handle the 4096 token limit of OpenAI
text_splitter = RecursiveCharacterTextSplitter(
    # We set a small chunk size for demonstration
    chunk_size = 2000,
    chunk_overlap  = 0,
    length_function = len,
)

# Initialize Pinecone with your API key and environment
pinecone.init(
        api_key = PINECONE_API_KEY,
        environment = PINECONE_ENV
)

# Define the index name for Pinecone
index_name = 'pine-search'

# Create an OpenAI embedding object with your API key
embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)

# Set up an OpenAI LLM model
llm = OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY)

# Define a PDF loader and load the file
loader = PyPDFLoader("https://lawrence.eti.br/wp-content/uploads/2023/07/ManualdePatentes20210706.pdf")

# Use the text splitter to split the loaded file content into manageable chunks
book_texts = text_splitter.split_documents(file_content)

# Check if the index exists in Pinecone
if index_name not in pinecone.list_indexes():
    print("Index does not exist: ", index_name)

# Create a Pinecone vector search object from the text chunks
book_docsearch = Pinecone.from_texts([t.page_content for t in book_texts], embeddings, index_name = index_name)

# Define your query
query = "Como eu faço para depositar uma patente no Brasil?"

# Use the Pinecone vector search to find documents similar to the query
docs = book_docsearch.similarity_search(query)

# Set up a QA chain with the LLM model and the selected chain type
chain = load_qa_chain(llm, chain_type="stuff")

# Run the QA chain with the found documents and your query to get the answer
chain.run(input_documents=docs, question=query)

Below is the application I developed for real-time evaluation of the PDF Search Engine

You can examine the web application that I’ve designed, enabling you to carry out real-time tests of the PDF search engine. This app provides you with the facility to pose questions about the data contained within BRPTO’S Basic Manual for Patent Protection. Click here to launch the application.

Conclusion

In this blog post, I have shown you how to build a simple but powerful PDF search engine using LangChain, Pinecone, and Open AI. This system can help you find the most relevant information from your PDF files in a fast and easy way. You can also extend this system to handle other types of documents, such as images, audio, or video, by using different data loaders and language models.

I hope you enjoyed this tutorial and learned something new. If you have any questions or feedback, please feel free to leave a comment below or contact me here. Thank you for reading!

That’s it for today!

Sources:

GoodAITechnology/LangChain-Tutorials (github.com)

INPI – Instituto Nacional da Propriedade Industrial — Instituto Nacional da Propriedade Industrial (www.gov.br)

AutoGPT: The Game Changer in Artificial Intelligence and Autonomous Agents

Auto-GPT is a revolutionary technology that unleashes new abilities for ChatGPT, enabling it to complete tasks all by itself, creating its own prompts to get the job done. AutoGPT, a groundbreaking artificial intelligence (AI) model, has taken the world by storm with its ability to provide large language models with “arms and hands” for task execution based on specific goals. This state-of-the-art technology has captured the attention of open-source developers and has the potential to revolutionize the AI landscape. For those who may not be familiar with AutoGPT, this article will provide an in-depth overview of this innovative AI model, its key features, and its impact on industries and applications.

The buzz around Auto-GPT has recently surpassed ChatGPT itself, trending as number one on Twitter for several days in a row.

How AutoGPT works?

AutoGPT works by utilizing the GPT-4 language model as its core intelligence to automate tasks and perform web searches. To use AutoGPT, you need to provide three inputs:

  1. AI Name: A name for the AI instance.
  2. AI Role: A description of the AI’s purpose.
  3. Up to 5 goals: Specific tasks you want the AI to accomplish.

Once these inputs are provided, AutoGPT starts working on the assigned goals. It may search the internet, extract information, or perform other necessary actions to complete the tasks.

AutoGPT also features long and short-term memory management, allowing it to learn from past experiences and make better decisions based on context. This is achieved through its integration with vector databases for memory storage. Additionally, unlike ChatGPT, AutoGPT has internet access, which enables it to fetch relevant information from the web as needed. Furthermore, it can manipulate files, access, and extract data from them, and summarize the information if required.

Follow 3 examples of how AutoGPT works:

1 – Market Research on Headphones: AI Name: ResearchGPT AI Role: An AI designed to conduct market research on tech products.

Goal 1: Do market research for different headphones on the market today. Goal 2: Get the top 5 headphones and list their pros and cons. Goal 3: Include the price of each one and save the analysis. Goal 4: Once you are done, terminate.

Auto-GPT will search the internet, find information on various headphones, list the top 5 headphones with their pros, cons, and prices, save the analysis, and terminate once the task is complete.

2 – Create FAQs for a Product: AI Name: FAQGPT AI Role: An AI designed to create FAQs for products.

Goal 1: Research a new smartphone model and its features. Goal 2: Create a list of 10 frequently asked questions about the smartphone. Goal 3: Provide clear and concise answers to the FAQs. Goal 4: Save the FAQs in a text file. Goal 5: Once you are done, terminate.

In this case, AutoGPT will research the specified smartphone model, create a list of FAQs, answer them, save the information in a text file, and terminate after completing the task.

3 – Writing a Python Program: AI Name: CodeGPT AI Role: An AI designed to write simple Python programs.

Goal 1: Write a Python program that calculates the factorial of a given number. Goal 2: Test the program with sample inputs and ensure it works correctly. Goal 3: Save the Python code in a .py file. Goal 4: Once you are done, terminate.

AutoGPT will generate the Python code to calculate the factorial of a given number, test it with sample inputs, save the code in a .py file, and terminate upon completion.

Keep in mind that AutoGPT might not always be perfect in completing the assigned tasks, as its performance depends on the accuracy and limitations of the GPT-4 model it is built upon.

AutoGPT boasts several key features that set it apart from its predecessors

  1. Dynamic learning: AutoGPT is designed to adapt to new data, making it an ever-evolving conversational AI model that stays up-to-date with the latest information and trends.
  2. Enhanced context awareness: AutoGPT’s understanding of context and user intent has been fine-tuned to provide more accurate and relevant responses.
  3. Customization capabilities: AutoGPT can be tailored to specific industries and applications, making it a versatile tool for many use cases.

AutoGPT’s Impact on Industries and Applications

The innovative features of AutoGPT are transforming various sectors through a wide range of applications:

  1. Personalized marketing: AutoGPT creates targeted marketing campaigns by continuously learning from user data and preferences.
  2. Sentiment analysis: AutoGPT accurately gauges user sentiment, providing valuable insights for businesses to improve customer experiences.
  3. Real-time adaptation: AutoGPT adapts to changing market conditions and trends, ensuring AI-powered solutions remain relevant and practical.
  4. Automation of complex tasks: AutoGPT’s self-improvement capabilities make it suitable for automating intricate tasks and streamlining processes across industries.

Integration of AutoGPT with advanced conversational AI models like BabyAGI, AgentGPT, and Microsoft’s Jarvis unlocks the full potential of AI and revolutionizes human-technology interactions. These AI models are transforming the world by enabling innovative applications across industries, such as enhanced customer support, improved content generation, seamless language translation, virtual personal assistants, healthcare applications, education and training, and human resources management.

AutoGPT also has a number of limitations, such as:

  1. Imperfect accuracy: AutoGPT is built upon the GPT-4 language model, which, although a significant improvement over GPT-3.5, is still not 100% accurate. Errors in the generated output might require further steps to resolve or could lead to an inability to complete the assigned task.
  2. Looping issues: While working on a task, AutoGPT may get stuck in a loop trying to find solutions to errors or problems. This can cause delays and increase costs, as the GPT-4 API usage fees can become expensive.
  3. Cost: The GPT-4 API, which Auto-GPT relies on, is more expensive than the GPT-3.5 API. The costs can quickly add up, especially if the AI is stuck in a loop or takes multiple steps to accomplish a task.
  4. Not production-ready: AutoGPT is not yet considered a production-ready solution. Users have reported that it often does not complete projects or only partially solves tasks. It requires further refinement and development before it can be relied upon as a complete, dependable solution.
  5. Task-specific limitations: AutoGPT might perform well for relatively simple and straightforward tasks, but it could struggle with more complex tasks or tasks requiring specialized knowledge. Its capabilities are limited by the underlying GPT-4 model and its ability to understand and solve a given problem.

These limitations should be taken into consideration when using AutoGPT, as it may not be suitable for all use cases or provide flawless results.

There are two methods for utilizing and evaluating AutoGPT

The first one is to download and install in our computer the AutoGPT source code from GitHub. Follow the instruction above. This maybe requires technical knowledge.

https://github.com/Significant-Gravitas/Auto-GPT/releases/latest

The second one involves utilizing a version I have deployed for direct access in your browser via this link and having fun!

You must input the name and goal, then click on “Deploy Agent.” Entering your OpenAI key is required. If you don’t possess one, I provide five tasks per goal at no cost.

Following this, the agent will commence processing.

The interactions will be divided into separate tasks.

Upon completing the five tasks, AutoGPT will cease operation. To run more than five tasks, you must input your OpenAI Key.

To deploy it yourself, click on this link and adhere to the provided guidelines.

Conclusion

AutoGPT is a game changer in the field of artificial intelligence and autonomous agents. Its dynamic learning, enhanced context awareness, and customization capabilities make it a powerful tool poised to revolutionize industries and applications. As AutoGPT continues to evolve and integrate with other advanced conversational AI models, the potential for AI to enhance and streamline various aspects of our lives grows exponentially. The future of AI is undoubtedly bright, with AutoGPT leading the way.

Additionally, I’ve developed a section on my blog dedicated to my Generative AI projects. You can view the screenshot below.

picture capture from my blog

That’s it for today!

Follow below some interesting articles talking about AutoGPT:

https://www.linkedin.com/pulse/what-auto-gpt-next-level-ai-tool-surpassing-chatgpt-bernard-marr/

https://en.wikipedia.org/wiki/Auto-GPT

https://levelup.gitconnected.com/autogpt-is-taking-over-the-internet-here-are-the-incredible-use-cases-that-will-blow-your-mind-ac31ea94e06e

THE RISE OF AUTONOMOUS AGENTS: PREPARING FOR THE AI REVOLUTION