Making a question answering chatbot based on the files I upload into python. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Making a question answering chatbot based on the files I upload into python. (/thread-40015.html) |
Making a question answering chatbot based on the files I upload into python. - Joejones - May-19-2023 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() RE: Making a question answering chatbot based on the files I upload into python. - deanhystad - May-19-2023 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}") |