May-19-2023, 02:24 PM
(This post was last modified: May-19-2023, 03:06 PM by deanhystad.)
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.
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.