import tkinter as tk import openai import pandas as pd import random import os from openpyxl import load_workbook # Your OpenAI API Key openai.api_key = '' # Initialize the questions dataframe if os.path.isfile('C:/Users/User/Desktop/Venus/customer_questions.xlsx'): df_questions = pd.read_excel('C:/Users/User/Desktop/Venus/customer_questions.xlsx') else: df_questions = pd.DataFrame(columns=['customer_id', 'question']) # Define your product database db_products = pd.DataFrame({ 'product_id': ['001', '002', '003'], 'name': ['Jeans', 'Shirt', 'Jacket'], 'size': ['M', 'S', 'L'], 'color': ['Blue', 'Red', 'Black'], 'material': ['Denim', 'Cotton', 'Leather'], 'shipping': ['Free shipping', '$3 shipping', '$5 shipping'], }) # Function to convert your product database to text def db_to_text(): text_db = "" for i, row in db_products.iterrows(): text_db += f"Product ID: {row['product_id']}\n" text_db += f"Name: {row['name']}\n" text_db += f"Size: {row['size']}\n" text_db += f"Color: {row['color']}\n" text_db += f"Material: {row['material']}\n" text_db += f"Shipping: {row['shipping']}\n\n" return text_db # Function to get an answer from GPT-3.5 # Function to get an answer from GPT-3.5 def get_answer(question, db_text, previous_questions): # Start with a system message messages = [ {"role": "system", "content": "You are a knowledgeable assistant with access to the following product database:\n" + db_text}, {"role": "system", "content": "The assistant should provide useful information based on the user's most recent question, using the previous questions for context."}, ] # Add previous user questions for prev_question in previous_questions: messages.append({"role": "user", "content": prev_question}) # Add current question last messages.append({"role": "user", "content": question}) response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages ) return response['choices'][0]['message']['content'] # Function to ask a question def ask_question(): global customer_id global previous_questions global df_questions question = question_entry.get() question_entry.delete(0, 'end') df_questions = pd.concat([df_questions, pd.DataFrame({'customer_id': [customer_id], 'question': [question]})], ignore_index=True) df_questions.to_excel('C:/Users/User/Desktop/Venus/customer_questions.xlsx', index=False) db_text = db_to_text() answer = get_answer(question, db_text, previous_questions) answer_label['text'] = answer previous_questions.append(question) # Function to create a new customer ID def new_customer(): global customer_id global previous_questions customer_id = random.randint(1, 1000) previous_questions = [] # Create a root window root = tk.Tk() # Create the question entry field question_entry = tk.Entry(root, width=100) question_entry.pack() # Create the ask question button ask_button = tk.Button(root, text='Ask Question', command=ask_question) ask_button.pack() # Create the answer label answer_label = tk.Label(root, text='', wraplength=500) # wraplength is in pixels answer_label.pack() # Create the new customer button new_customer_button = tk.Button(root, text='New Customer', command=new_customer) new_customer_button.pack() # Initialize a new customer new_customer() # Run the tkinter event loop root.mainloop()