The open blogging platform. Say no to algorithms and paywalls.

Using GPT to Answer Questions from Multiple Text Files

Let’s face it! We’ve all thought about how cool it would be if I could upload all my notes and chatgpt would read them, figure out what I need, and answer me. But there is no way to upload files in…

Unlocking the Power of GPT to Tackle Complex Questions with Ease

Photo by Levart_Photographer on Unsplash

Let’s face it! We’ve all thought about how cool it would be if I could upload all my notes and chatgpt would read them, figure out what I need, and answer me. But there is no way to upload files in ChatGPT. So, what should we do? Today, I’d like to tell you about a great tool that will change the way you get information from text files. Think about having an AI assistant that can answer questions quickly and correctly based on a set of text files. Doesn’t that sound pretty cool? Well, we’ll take care of it! It’s a piece of code written in Python. For the purpose of this post, let’s call it AI-powered Q&A Helper. It’s a simple Python script that uses OpenAI’s GPT-4 language model to answer questions about the text files you give it. This tool is great for students, researchers, and professionals who want to save time and quickly understand their documents.So, let’s get started and find out how to use this great tool.

Let’s see how it works before we jump in!

So, I need to give a short summary of three academic papers.

Image: Author

So I just ran my Python code, and the code will allow me to ask a question, and it does the rest of it!

Image: Author

Setting up the environment

First, make sure you have Python installed on your machine. You’ll also need the openai Python library, which you can install by running pip install openai.

The Code

import os
import openai


def read_files(folder_path: str) -> str:
    all_text = ""
    for filename in os.listdir(folder_path):
        if filename.endswith(".txt"):
            with open(os.path.join(folder_path, filename), "r") as file:
                all_text += file.read() + "\n"
    return all_text


def ask_question(openai_api_key: str, text: str, question: str) -> str:
    openai.api_key = openai_api_key

    response = openai.Completion.create(
        engine="text-davinci-002",
        prompt=f"Answer the following question based on these texts:\n\n{text}\n\nQuestion: {question} \n ",
        temperature=0.5,
        max_tokens=100,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0,
    )

    answer = response.choices[0].text.strip()
    return answer


def main():
    folder_path = "./paper"
    openai_api_key = "put your api here"

    all_text = read_files(folder_path)

    while True:
        question = input("Ask a question based on the text files (type 'quit' to exit): ")
        if question.lower() == "quit":
            break

        answer = ask_question(openai_api_key, all_text, question)
        print(f"Answer: {answer}")


if __name__ == "__main__":
    main()

How to use the AI-powered Q&A Helper

  • Place all the text files you want the AI to consider in a folder. For instance, you can create a folder named “paper” and put all the relevant .txt files inside.
  • Update the openai_api_key variable in the main() function with your OpenAI API key. If you don't have one, head over to the OpenAI website to sign up and get your API key.
  • Run the script using python script_name.py (replace "script_name" with the name you gave your script). The AI will read all the text files in the specified folder and be ready to answer your questions.
  • Ask a question based on the text files. The AI will generate an answer using the OpenAI API, and you’ll see the answer printed on the screen. Feel free to ask more questions; the program will keep running until you type ‘quit’ and hit enter.

When to use the AI-powered Q&A Helper

Here are some scenarios when this tool can be extremely useful:

  • You’re a student with a collection of lecture notes, and you want to quickly find answers to your questions without going through all the files.
  • You’re a researcher who has gathered a bunch of articles or papers, and you need a quick way to query information without reading everything from scratch.
  • You’re a professional who wants to find specific information from a set of reports or documents without spending hours manually searching.

Limitation

One thing that Python code can’t do when using OpenAI’s GPT models is process more than the maximum number of tokens that the model can handle. This limit can be anywhere from a few hundred to a few thousand tokens, depending on the GPT model that is being used. This means that longer documents or input text may need to be split up into smaller pieces for the model to be able to process them. To solve this problem, you can look at my other post where I explain how to break up long texts, summarise them, and get a shorter version of a long post. check out this post. Also, some Python code implementations may not have the option for recursive processing, which can be limiting when dealing with complex questions or tasks that require multiple levels of processing. Recursive processing is the ability to use the same function or algorithm over and over again on smaller parts of a larger problem. This can help break up difficult tasks into smaller, easier-to-handle parts. Without this option, it might be harder or take more time to do some things with Python and OpenAI’s GPT models.

Closing

So, there you have it! An AI-powered Q&A Helper to make your life easier and save you time. Give it a try, and let the power of AI assist you in finding the answers you need. Happy questioning!




Continue Learning