diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/filesearch.py b/filesearch.py index 9ea02ce..ef71aa5 100644 --- a/filesearch.py +++ b/filesearch.py @@ -1,9 +1,7 @@ import os -import openai +from openai import OpenAI from tkinter import simpledialog, Tk, Label, Entry, Button, Text, filedialog - -# Set your OpenAI API key here -openai.api_key = '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' +from dotenv import load_dotenv def get_user_input(): root = None # No parent window for simplicity @@ -21,18 +19,20 @@ def list_files(directory): return files def compare_with_openai(content, search_query): - response = openai.Completion.create( - engine="text-davinci-003", - prompt=f"Vergleiche den folgenden Text mit der Suchanfrage '{search_query}':\n\n{content[:1000]}...\n\nIst der Text relevant für die Suchanfrage?", - max_tokens=50, - n=1, - stop=None, - temperature=0.5, - ) - return "ja" in response.choices[0].text.lower() - + client = OpenAI( + api_key=os.environ.get("OPENAI_API_KEY") + ) + chat_completion = client.chat.completions.create( + messages=[ + { + "role": "user", + "content": f"Vergleiche den folgenden Text mit der Suchanfrage '{search_query}':\n\n{content[:1000]}...\n\nIst der Text relevant für die Suchanfrage?"}, + ], + model="gpt-4o-mini", + ) + return "ja" in chat_completion.choices[0].message.content.lower() def search_files(): - search_query = get_user_input() + search_query = query_entry.get() # Get the search query from the Entry widget if not search_query: print("No search query provided.") return @@ -50,13 +50,13 @@ def search_files(): found_files = [] for file_path in files: - try: - with open(file_path, 'r', encoding='utf-8', errors='ignore') as file: - file_content = file.read() - if compare_with_openai(file_content, search_query): - found_files.append(file_path) - except Exception as e: - print(f"Error reading {file_path}: {e}") + try: + with open(file_path, 'r', encoding='utf-8', errors='ignore') as file: + file_content = file.read() + if compare_with_openai(file_content, search_query): + found_files.append(file_path) + except Exception as e: + print(f"Error reading {file_path}: {e}") result_text.delete(1.0, 'end') if found_files: