parent
562554a978
commit
0f3fac76a6
@ -1,120 +0,0 @@
|
||||
import http.client
|
||||
import json
|
||||
import tkinter as tk
|
||||
import time
|
||||
import chat2
|
||||
|
||||
# 创建HTTPS连接和请求头
|
||||
conn = http.client.HTTPSConnection("api.chatanywhere.com.cn")
|
||||
headers = {
|
||||
'Authorization': 'Bearer sk-T2uMUUuvXHFTpVo0M96rOMkLCygv6BODpwBozgXjmzU9c2yi',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
# 初始化暂停标志
|
||||
paused = False
|
||||
|
||||
# 对话历史列表
|
||||
dialogue_history = []
|
||||
|
||||
# 发送消息函数
|
||||
def send_message(event=None):
|
||||
global paused
|
||||
if paused:
|
||||
return
|
||||
|
||||
# 获取用户输入的消息
|
||||
user_message = user_input.get()
|
||||
user_input.delete(0, tk.END)
|
||||
|
||||
# 构建请求payload
|
||||
payload = json.dumps({
|
||||
"model": "gpt-3.5-turbo",
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": user_message
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
# 发送POST请求获取助手回复
|
||||
conn.request("POST", "/v1/chat/completions", payload, headers)
|
||||
res = conn.getresponse()
|
||||
data = res.read()
|
||||
|
||||
# 解析助手回复数据
|
||||
response_data = json.loads(data.decode("utf-8"))
|
||||
assistant_response = response_data["choices"][0]["message"]["content"]
|
||||
|
||||
# 将助手回复按行拆分
|
||||
assistant_lines = assistant_response.split("\n")
|
||||
|
||||
# 保存对话时间标识
|
||||
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
||||
|
||||
# 更新对话历史列表
|
||||
dialogue_history.append((current_time, "我", user_message))
|
||||
dialogue_history.extend([(current_time, "下等外国马", line) for line in assistant_lines])
|
||||
|
||||
# 保存对话到指定的txt文件
|
||||
with open("dialogue.txt", "a") as file:
|
||||
for time_stamp, role, message in dialogue_history:
|
||||
file.write(f"[{time_stamp}] {role}: {message}\n")
|
||||
|
||||
# 更新对话历史文本框并显示助手回复
|
||||
message_history.config(state=tk.NORMAL)
|
||||
message_history.insert(tk.END, "我: " + user_message + "\n", "user_message")
|
||||
for line in assistant_lines:
|
||||
if paused:
|
||||
break
|
||||
message_history.insert(tk.END, "下等外国马: " + line + "\n", "assistant_message")
|
||||
root.update()
|
||||
time.sleep(1)
|
||||
message_history.config(state=tk.DISABLED)
|
||||
message_history.yview(tk.END)
|
||||
|
||||
# 控制暂停和继续回答的函数
|
||||
def pause_response():
|
||||
global paused
|
||||
paused = not paused
|
||||
|
||||
# 打开新的tk界面
|
||||
def open_new_window():
|
||||
chat2.run()
|
||||
|
||||
# 创建主窗口
|
||||
root = tk.Tk()
|
||||
root.title("Chat_fk")
|
||||
root.geometry("660x480")
|
||||
|
||||
# 创建对话历史文本框
|
||||
message_history = tk.Text(root, width=60, height=20, wrap="word")
|
||||
message_history.pack(pady=10)
|
||||
message_history.tag_config("user_message", justify="right", foreground="blue")
|
||||
message_history.tag_config("assistant_message", justify="left", foreground="green")
|
||||
message_history.config(state=tk.DISABLED)
|
||||
|
||||
# 创建用户输入框和发送按钮
|
||||
input_frame = tk.Frame(root)
|
||||
input_frame.pack()
|
||||
|
||||
user_input = tk.Entry(input_frame, width=50)
|
||||
user_input.pack(side=tk.LEFT, padx=5)
|
||||
|
||||
send_button = tk.Button(input_frame, text="发送", command=send_message)
|
||||
send_button.pack(side=tk.LEFT, padx=5)
|
||||
|
||||
# 创建暂停按钮
|
||||
pause_button = tk.Button(input_frame, text="暂停", command=pause_response)
|
||||
pause_button.pack(side=tk.LEFT, padx=5)
|
||||
|
||||
# 创建打开新界面的按钮
|
||||
open_button = tk.Button(input_frame, text="打开历史对话", command=open_new_window)
|
||||
open_button.pack(side=tk.LEFT, padx=5)
|
||||
|
||||
|
||||
|
||||
root.bind('<Return>', send_message)
|
||||
|
||||
root.mainloop()
|
Loading…
Reference in new issue