from tkinter import Tk, Label, Entry, Button, Text, Scrollbar, messagebox
import openai

# 设置OpenAI API凭证
openai.api_key = 'YOUR_API_KEY'
def generate_response(message):
    model_prompt = "对话开始:\nBoss: " + message + "\n你: "
    response = openai.Completion.create(
        engine="text-davinci-003",
        prompt=model_prompt,
        temperature=0.7,
        max_tokens=100,
        n=1,
        stop=None
    )
    reply = response.choices[0].text.strip().split('你: ')[1]
    return reply
def send_message():
    message = message_entry.get("1.0", "end-1c")

    if message:
        reply = generate_response(message)

        messagebox.showinfo("发送成功", f"您的消息已成功发送:\n\n{message}\n\n回复:{reply}")

        message_history.insert("end", f"你: {message}\n\n")
        message_history.insert("end", f"Boss: {reply}\n\n")

        message_entry.delete("1.0", "end")
    else:
        messagebox.showwarning("错误", "消息不能为空!")
root = Tk()
root.title("与老板对话")
root.geometry("400x400")

message_history = Text(root, height=15, width=50)
message_history.pack()

message_scrollbar = Scrollbar(root)
message_scrollbar.pack(side="right", fill="y")

message_history.config(yscrollcommand=message_scrollbar.set)
message_scrollbar.config(command=message_history.yview)

message_label = Label(root, text="消息:")
message_label.pack()

message_entry = Text(root, height=5, width=50)
message_entry.pack()

send_button = Button(root, text="发送", command=send_message)
send_button.pack()

root.mainloop()
from tkinter import Tk, Label, Entry, Button, Text, Scrollbar, messagebox
import openai

# 设置OpenAI API凭证
openai.api_key = 'YOUR_API_KEY'


def generate_response(message):
    model_prompt = "对话开始:\nBoss: " + message + "\n你: "
    response = openai.Completion.create(
        engine="text-davinci-003",
        prompt=model_prompt,
        temperature=0.7,
        max_tokens=100,
        n=1,
        stop=None
    )
    reply = response.choices[0].text.strip().split('你: ')[1]
    return reply


def send_message():
    message = message_entry.get("1.0", "end-1c")

    if message:
        reply = generate_response(message)

        messagebox.showinfo("发送成功", f"您的消息已成功发送:\n\n{message}\n\n回复:{reply}")

        message_history.insert("end", f"你: {message}\n\n")
        message_history.insert("end", f"Boss: {reply}\n\n")

        message_entry.delete("1.0", "end")
    else:
        messagebox.showwarning("错误", "消息不能为空!")


# 程序其他部分保持不变

root = Tk()
root.title("与老板对话")
root.geometry("400x400")

message_history = Text(root, height=15, width=50)
message_history.pack()

message_scrollbar = Scrollbar(root)
message_scrollbar.pack(side="right", fill="y")

message_history.config(yscrollcommand=message_scrollbar.set)
message_scrollbar.config(command=message_history.yview)

message_label = Label(root, text="消息:")
message_label.pack()

message_entry = Text(root, height=5, width=50)
message_entry.pack()

send_button = Button(root, text="发送", command=send_message)
send_button.pack()

root.mainloop()

使用Python和OpenAI构建与老板对话的弹窗软件-LMLPHP

07-08 18:42