generated from stratosphereips/awesome-code-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLinuxSSHbot.py
109 lines (82 loc) · 4.16 KB
/
LinuxSSHbot.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
import openai
from dotenv import dotenv_values
import argparse
from datetime import datetime
import yaml
from time import sleep
import random
import os
config = dotenv_values(".env")
openai.api_key = config["OPENAI_API_KEY"]
today = datetime.now()
history = open("history.txt", "a+", encoding="utf-8")
if os.stat('history.txt').st_size == 0:
with open('personalitySSH.yml', 'r', encoding="utf-8") as file:
identity = yaml.safe_load(file)
identity = identity['personality']
prompt = identity['prompt']
else:
history.write("\nHere the session stopped. Now you will start it again from the beginning with the same user. You must respond just with starting message and nothing more. " +
"Make sure you use same file and folder names. Ignore date-time in <>. This is not your concern.\n")
history.seek(0)
prompt = history.read()
def main():
parser = argparse.ArgumentParser(description = "Simple command line with GPT-3.5-turbo")
parser.add_argument("--personality", type=str, help="A brief summary of chatbot's personality",
default= prompt +
f"\nBased on these examples make something of your own (different username and hostname) to be a starting message. Always start the communication in this way and make sure your output ends with '$'. For the last login date use {today}\n" +
"Ignore date-time in <> after user input. This is not your concern.\n")
args = parser.parse_args()
initial_prompt = f"You are Linux OS terminal. Your personality is: {args.personality}"
messages = [{"role": "system", "content": initial_prompt}]
if os.stat('history.txt').st_size == 0:
for msg in messages:
history.write(msg["content"])
else:
history.write("The session continues in following lines.\n\n")
history.close()
while True:
logs = open("history.txt", "a+", encoding="utf-8")
try:
res = openai.chat.completions.create(
model="gpt-3.5-turbo-16k",
messages = messages,
temperature = 0.0,
max_tokens = 800
)
msg = res.choices[0].message.content
message = {"content": msg, "role": 'assistant'}
if "$cd" in message["content"] or "$ cd" in message["content"]:
message["content"] = message["content"].split("\n")[1]
lines = []
messages.append(message)
logs.write(messages[len(messages) - 1]["content"])
logs.close()
logs = open("history.txt", "a+", encoding="utf-8")
if "will be reported" in messages[len(messages) - 1]["content"]:
print(messages[len(messages) - 1]["content"])
raise KeyboardInterrupt
if "PING" in message["content"]:
lines = message["content"].split("\n")
print(lines[0])
for i in range(1, len(lines)-5):
print(lines[i])
sleep(random.uniform(0.1, 0.5))
for i in range(len(lines)-4, len(lines)-1):
print(lines[i])
user_input = input(f'{lines[len(lines)-1]}'.strip() + " ")
messages.append({"role": "user", "content": user_input + f"\t<{datetime.now()}>\n" })
logs.write(" " + user_input + f"\t<{datetime.now()}>\n")
else:
#print("\n", messages[len(messages) - 1]["content"], " ")
user_input = input(f'\n{messages[len(messages) - 1]["content"]}'.strip() + " ")
messages.append({"role": "user", "content": " " + user_input + f"\t<{datetime.now()}>\n"})
logs.write(" " + user_input + f"\t<{datetime.now()}>\n")
except KeyboardInterrupt:
messages.append({"role": "user", "content": "\n"})
print("")
break
logs.close()
# print(res)
if __name__ == "__main__":
main()