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
  Need Assistance with Creating an AI Chatbot for Client's Website LauraB 4 2,237 Aug-08-2024, 06:00 AM
Last Post: Siska
  How can I upload a .gz file to the Swift Object Storage using Python swift client? Hanginium65 0 1,196 Jul-24-2024, 03:24 PM
Last Post: Hanginium65
  Copy Paste excel files based on the first letters of the file name Viento 2 2,339 Feb-07-2024, 12:24 PM
Last Post: Viento
  Upload Files to Azure Storage Container phillyfa 6 5,690 Dec-22-2023, 06:11 AM
Last Post: Pedroski55
  Newbie question about switching between files - Python/Pycharm Busby222 3 2,055 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Move Files based on partial Match mohamedsalih12 2 4,579 Sep-20-2023, 07:38 PM
Last Post: snippsat
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 2,733 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Upload image to Instagram using python/selenium using image URL failed, need help greenpine 5 9,429 Feb-22-2022, 09:42 PM
Last Post: snippsat
Bug beginner attempting to make chatbot MonikaDreemur 1 2,124 Feb-02-2022, 10:24 PM
Last Post: BashBedlam
  Help with simple nltk Chatbot Extra 3 4,697 Jan-02-2022, 07:50 AM
Last Post: bepammoifoge

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.