Skip to content

Commit 0effb12

Browse files
committed
fix: roles parser (#5)
1 parent f5fb667 commit 0effb12

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

AskGPT.alfredworkflow

477 Bytes
Binary file not shown.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ Let AskGPT check grammar errors (from clipboard) for you:
4141

4242
## Changelog
4343

44+
[v0.6.1](https://github.com/phguo/AskGPT/releases/tag/v0.6.1) - Apr. 4, 2023
45+
- Fix #5 that are related to the roles parser.
46+
- Add a configuration for printing user-inputted content.
47+
4448
[v0.6](https://github.com/phguo/AskGPT/releases/tag/v0.6) - Apr. 2, 2023
4549

4650
<img src="https://raw.githubusercontent.com/phguo/AskGPT/main/video/v0.6_User_Configuration.png" width="70%" height="70%">

gpt.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
chat_cache = "chat.json"
1010

1111

12-
def pre_process(api_key, content, model, temprature, context_time, chat_number, clear_keyword, clipboard_keyword, roles, signature):
12+
def pre_process(api_key, content, model, temprature, context_time, chat_number, clear_keyword, clipboard_keyword, roles, signature, print_input):
1313
# context_time
14+
if float(context_time) < 0:
15+
context_time = float("inf")
1416
if os.path.isfile(chat_cache):
1517
mod_time = os.path.getmtime(chat_cache)
1618
current_time = time.time()
@@ -33,26 +35,34 @@ def pre_process(api_key, content, model, temprature, context_time, chat_number,
3335

3436
# roles
3537
for role in roles.split('\n'):
36-
k, v = role.split(':')
38+
index = role.find(":")
39+
k = role[:index]
40+
v = role[index + 1:].strip()
3741
if k in content:
3842
content = content.replace(k, '')
3943
content = v + content
4044

4145
# signature
4246
signature = int(signature)
4347

48+
# print_input
49+
print_input = int(print_input)
50+
4451
messages = []
4552
if os.path.exists(chat_cache):
4653
with open(chat_cache, "r") as chat_json:
4754
messages = json.load(chat_json)
4855
messages += [{'role': 'user', 'content': content}]
4956

50-
return api_key, model, temprature, messages, signature, chat_number
57+
return api_key, model, temprature, messages, signature, print_input, chat_number
5158

5259

53-
def ask_gpt(api_key, model, temprature, messages, signature):
60+
def ask_gpt(api_key, model, temprature, messages, signature, print_input):
5461
openai.api_key = api_key
5562

63+
if print_input:
64+
keyboard.write('>>> ' + messages[-1]["content"] + '\n')
65+
5666
response = openai.ChatCompletion.create(
5767
model=model,
5868
messages=messages,
@@ -71,7 +81,7 @@ def ask_gpt(api_key, model, temprature, messages, signature):
7181
if signature == 1:
7282
from datetime import datetime
7383
current = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
74-
keyboard.write("\n[Generated by ChatGPT, {}]".format(current))
84+
keyboard.write("\n[Generated by ChatGPT, {}]\n\n".format(current))
7585

7686
messages += [{'role': 'assistant', 'content': assistant_content}]
7787
return messages
@@ -86,7 +96,7 @@ def post_process(messages, chat_number):
8696

8797
if __name__ == '__main__':
8898
params = sys.argv[1:]
89-
api_key, model, temprature, messages, signature, chat_number = pre_process(*params)
99+
api_key, model, temprature, messages, signature, print_input, chat_number = pre_process(*params)
90100
if messages[-1].get("content", None):
91-
messages = ask_gpt(api_key, model, temprature, messages, signature)
101+
messages = ask_gpt(api_key, model, temprature, messages, signature, print_input)
92102
post_process(messages, chat_number)

0 commit comments

Comments
 (0)