-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmagi.py
159 lines (109 loc) · 5.32 KB
/
magi.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'''
=====================================================================================
Name : MAGI
Author : Kenshiro
Version : 10.39
Copyright : GNU General Public License (GPLv3)
Description : Advanced Chatbot
=====================================================================================
'''
import core
import plugin
import agent
import re
SYSTEM_HINT_TEXT = "\n\nHint: to switch AI mode, type the letter 'm' and press enter. To exit MAGI, type 'exit'.\n"
PRIME_DIRECTIVES_TEXT = "\n\n----- Prime Directives -----\n\n"
MISSION_DATA_TEXT = "\n\n----- Mission Data -----\n\n"
DATA_TEXT = "DATA = "
MISSION_TEXT = "\n\nMISSION = "
TASK_LIST_SYSTEM_PROMPT = "Your task is to break down the user's prompt provided in the MISSION section into a list of specific and detailed tasks. Use the DATA section only if it provides useful information for the MISSION. Each task must be precise and long enough to convey its purpose fully, but it must fit on a single paragraph. Ensure each task is actionable, detailed, and written in a clear, self-contained manner. Only output the task list, no titles, headers, or additional text."
ACTION_HELPER_TEXT = "Do this: "
SUMMARY_TEXT = "\n\n----- Summary -----\n\n"
ACTIONS_TEXT = "\n\n----- Actions -----\n\n"
PROGRESS_REPORT_TEXT = "\n\n----- Progress Report -----\n\n"
ACTION_TAG = "\n[ACTION] "
NORMAL_MODE_TEXT = "\nNormal mode enabled"
MISSION_MODE_TEXT = "\nMission mode enabled"
NERV_MODE_TEXT = "\nNERV mode enabled"
SWITCH_AI_MODE_COMMAND = "M"
EXIT_COMMAND = "EXIT"
def sanitizeTask(task):
# Remove digits, dots, dashes, spaces and "Task:" prefixes at the beginning of the task
task = re.sub(r"^[0-9.\- ]*|^[Tt]ask[:]? *", '', task)
return task
def createTaskList(mission, summary, header, ai_mode):
context = []
taskListText = core.send_prompt(TASK_LIST_SYSTEM_PROMPT, DATA_TEXT + summary + MISSION_TEXT + mission, context)
plugin.printSystemText(header + taskListText + "\n", ai_mode)
# Remove blank lines and create the task list
taskList = [line for line in taskListText.splitlines() if line.strip()]
return taskList
def runNerv(primeDirectives, mission, context, ai_mode):
global nerv_data
if not nerv_data:
nerv_data = core.load_mission_data(mission)
plugin.printSystemText(MISSION_DATA_TEXT + nerv_data, ai_mode)
agent.displayNervSquad(ai_mode)
orders = agent.captain.issueOrders(DATA_TEXT + nerv_data + MISSION_TEXT + mission, ai_mode)
team_response = agent.runSquadOrders(orders, ai_mode)
nerv_data = core.update_summary(mission, context, nerv_data, team_response)
plugin.printSystemText(PROGRESS_REPORT_TEXT + nerv_data + "\n", ai_mode)
def runMission(primeDirectives, mission, context, ai_mode):
summary = ""
if ai_mode == core.AiMode.MISSION:
summary = core.load_mission_data(mission)
if summary:
plugin.printSystemText(MISSION_DATA_TEXT + summary, ai_mode)
actionList = createTaskList(mission, summary, ACTIONS_TEXT, ai_mode)
for action in actionList:
action = sanitizeTask(action)
plugin.printSystemText(ACTION_TAG + action, ai_mode)
action = ACTION_HELPER_TEXT + action
actionSummary = plugin.runAction(primeDirectives, action, context, ai_mode)
summary = core.update_summary(mission, context, summary, actionSummary)
plugin.printMagiText(SUMMARY_TEXT + summary, ai_mode)
return summary
def checkPrompt(primeDirectives, prompt, context, ai_mode):
if ai_mode == core.AiMode.MISSION:
runMission(primeDirectives, prompt, context, ai_mode)
elif ai_mode == core.AiMode.NERV:
runNerv(primeDirectives, prompt, context, ai_mode)
else:
plugin.runAction(primeDirectives, prompt, context, ai_mode)
def switchAiMode(ai_mode):
if ai_mode == core.AiMode.NORMAL:
ai_mode = core.AiMode.MISSION
plugin.printSystemText(MISSION_MODE_TEXT, ai_mode)
elif ai_mode == core.AiMode.MISSION:
ai_mode = core.AiMode.NERV
plugin.printSystemText(NERV_MODE_TEXT, ai_mode)
else:
ai_mode = core.AiMode.NORMAL
plugin.printSystemText(NORMAL_MODE_TEXT, ai_mode)
return ai_mode
# Main logic
if __name__ == "__main__":
context = []
nerv_data = ""
ai_mode = core.AiMode.NORMAL
primeDirectives = core.read_text_file(core.PRIME_DIRECTIVES_FILE_PATH)
if primeDirectives:
plugin.printSystemText(PRIME_DIRECTIVES_TEXT + primeDirectives, ai_mode)
plugin.printSystemText(SYSTEM_HINT_TEXT, ai_mode)
# Main loop
while True:
prompt = plugin.userInput(ai_mode)
if prompt == "" or prompt.isspace():
continue
prompt_tokens = core.get_number_of_tokens(prompt)
if prompt_tokens > core.MAX_INPUT_TOKENS:
plugin.printSystemText(core.MAX_INPUT_TOKENS_ERROR + str(prompt_tokens), ai_mode)
continue
command = prompt.split()[0]
if command.upper() == EXIT_COMMAND:
break
if command.upper() == SWITCH_AI_MODE_COMMAND:
ai_mode = switchAiMode(ai_mode)
else:
checkPrompt(primeDirectives, prompt, context, ai_mode)
plugin.printSystemText("\n", ai_mode)