Python Forum
Making a question answering chatbot based on the files I upload into python.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making a question answering chatbot based on the files I upload into python.
#1
I am using the coding below to get answers from files I upload. The problem is when I run the code it says this "Ask a question based on the text files (type 'quit' to exit): ". When I type something as a questions and hit enter it just says "Ask a question based on the text files (type 'quit' to exit): " again. The only thing that will get it to do anything is when I type "quit" and hit enter. Was wondering if anyone knows why this is happening. I am very new to coding so not good at figuring out what the issue is. It will reference the files when I hit "quit" but it doesn't work well because it is just answering questions based on the word "quit". Also I have taken out the api key and my file path in the code but when I running it that stuff is in 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 = r"my file path"
    openai_api_key = "my key"

    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()
deanhystad write May-19-2023, 03:06 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Do you have an indentation error?
    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)  # This is not in the while loop.
    print(f"Answer: {answer}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy Paste excel files based on the first letters of the file name Viento 2 455 Feb-07-2024, 12:24 PM
Last Post: Viento
  Upload Files to Azure Storage Container phillyfa 6 712 Dec-22-2023, 06:11 AM
Last Post: Pedroski55
  Newbie question about switching between files - Python/Pycharm Busby222 3 623 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Move Files based on partial Match mohamedsalih12 2 832 Sep-20-2023, 07:38 PM
Last Post: snippsat
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,532 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Upload image to Instagram using python/selenium using image URL failed, need help greenpine 5 5,494 Feb-22-2022, 09:42 PM
Last Post: snippsat
Bug beginner attempting to make chatbot MonikaDreemur 1 1,112 Feb-02-2022, 10:24 PM
Last Post: BashBedlam
  Help with simple nltk Chatbot Extra 3 1,893 Jan-02-2022, 07:50 AM
Last Post: bepammoifoge
  making variables in my columns and rows in python kronhamilton 2 1,629 Oct-31-2021, 10:38 AM
Last Post: snippsat
  Rename Files based on XML file klturi421 3 2,211 Oct-22-2021, 07:37 PM
Last Post: klturi421

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020