-
Notifications
You must be signed in to change notification settings - Fork 11
/
gpt.py
126 lines (104 loc) · 3.31 KB
/
gpt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import sys
import json
import os
import time
import keyboard
from openai import OpenAI
chat_cache = "chat.json"
def pre_process(
api_key,
content,
model,
temprature,
context_time,
chat_number,
clear_keyword,
clipboard_keyword,
roles,
signature,
print_input,
api_base,
):
# context_time
if float(context_time) < 0:
context_time = float("inf")
if os.path.isfile(chat_cache):
mod_time = os.path.getmtime(chat_cache)
current_time = time.time()
if (current_time - mod_time) / 60 > float(context_time):
os.remove(chat_cache)
# chat_number
chat_number = int(int(chat_number) * 2)
# clear_keyword
if clear_keyword in content:
content = content.replace(clear_keyword, "")
if os.path.isfile(chat_cache):
os.remove(chat_cache)
# clipboard_keyword
if clipboard_keyword in content:
import pyperclip
content = content.replace(clipboard_keyword, pyperclip.paste())
# roles
for role in roles.split("\n"):
index = role.find(":")
k = role[:index]
v = role[index + 1 :].strip()
if k in content:
content = content.replace(k, "")
content = v + content
# signature
signature = int(signature)
# print_input
print_input = int(print_input)
messages = []
if os.path.exists(chat_cache):
with open(chat_cache, "r") as chat_json:
messages = json.load(chat_json)
messages += [{"role": "user", "content": content}]
return (
api_key,
model,
temprature,
messages,
signature,
print_input,
chat_number,
api_base,
)
def ask_gpt(api_key, model, temprature, messages, signature, print_input, api_base):
client = OpenAI(api_key=api_key, base_url=api_base)
temperature_fixed = int(temprature)
if print_input:
keyboard.write(">>> " + messages[-1]["content"] + "\n")
chat_completion = client.chat.completions.create(
messages=messages, model=model, stream=True, temperature=temperature_fixed
)
assistant_content = ""
for chunk in chat_completion:
if not chunk.choices[0].finish_reason == "stop":
c = chunk.choices[0].delta.content
if c:
keyboard.write(c)
assistant_content += c
else:
if signature == 1:
from datetime import datetime
current = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
keyboard.write("\n[Generated by ChatGPT, {}]\n\n".format(current))
messages += [{"role": "assistant", "content": assistant_content}]
return messages
def post_process(messages, chat_number):
if len(messages) > chat_number:
messages = messages[len(messages) - chat_number : len(messages)]
with open(chat_cache, "w") as chat_json:
json.dump(messages, chat_json, indent=4)
if __name__ == "__main__":
params = sys.argv[1:]
api_key, model, temprature, messages, signature, print_input, chat_number, api_base = pre_process(
*params
)
if messages[-1].get("content", None):
messages = ask_gpt(
api_key, model, temprature, messages, signature, print_input, api_base
)
post_process(messages, chat_number)