diff --git a/chat_command.py b/chat_command.py index 1ac24ce..c817ca3 100644 --- a/chat_command.py +++ b/chat_command.py @@ -1 +1,48 @@ -import openai \ No newline at end of file +import openai +import os +from time import sleep +import sys + +ml = os.getcwd() +file_path = os.path.join(ml, "api_key.txt") + +# 设置openai库的API认证密钥 +def api_key(): + if os.path.exists(os.path.join(ml, f"api_key.txt")): + with open(file_path, 'r') as f: + openai.api_key = f.read().strip() + else: + print('文件丢失,请重新输入api_key') + sleep(1) + if os.path.exists(os.path.join(ml, "chat_command_GUI.exe")): + os.startfile("chat_command_GUI.exe") + else: + print('错误,文件不存在!?') + sleep(1) + sys.exit() + pass + + +# 设置GPT-3.5模型要使用的API_key +api_key() + +# 设置GPT-3.5模型的引擎ID +model_engine = 'text-davinci-003' + +# 循环读入用户输入并输出聊天结果 +while True: + # 获取用户输入 + prompt = input("你好,请问有什么需要帮助的吗?\n") + + # 调用openai.ChatCompletion.create()方法来获取聊天结果 + response = openai.Completion.create( + engine='text-davinci-003', + prompt=prompt, + temperature=0.7, + max_tokens=210, + top_p=1, + frequency_penalty=0, + presence_penalty=0 + ) + result = response['choices'][0]['text'].strip() + print(f"chatGPT:{result}") diff --git a/chat_command_client.pyw b/chat_command_client.pyw new file mode 100644 index 0000000..2ed06b4 --- /dev/null +++ b/chat_command_client.pyw @@ -0,0 +1,50 @@ +import sys +from tkinter import * +from tkinter import messagebox +import os +import requests + +root = Tk() +ml = os.getcwd() +file_path = os.path.join(ml, "api_key.txt") + + +def open_exe(exe_name): + if os.path.exists(os.path.join(ml ,f"{exe_name}.exe")): + os.system(f"start {exe_name}.exe") + else: + messagebox.showerror('chatGPT','错误:命令行文件不存在!') + pass +def chat_command(): + API_key = entry_APIkey.get() + if API_key != None: + with open(file_path,'w') as f: + f.write(API_key) + pass + open_exe('chat_command') + pass + else: + messagebox.showerror('错误','API_key不存在!无法使用!') + +def fh(): + sys.exit() + + +# Button +run_command = Button(root, text='chatGPT启动', command=chat_command) +fh_bt = Button(root, text='返回', command=fh) + +# entry +entry_APIkey = Entry(root) + +# grid +Label(root, text='api_key:').grid(row=0,column=0) +entry_APIkey.grid(row=0, column=1) +run_command.grid(row=2,column=0) +fh_bt.grid(row=2,column=1) + + +# mainloop +root.title('chatGPT') +root.geometry('200x250+100+100') +root.mainloop() \ No newline at end of file