Skip to content

Commit 42e5b2d

Browse files
committed
Add multiple model compatibility and ModelNotRecognizedException
1 parent 579b51e commit 42e5b2d

File tree

1 file changed

+56
-16
lines changed

1 file changed

+56
-16
lines changed

operate/main.py

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@
104104
Now share the results!
105105
"""
106106

107+
108+
class ModelNotRecognizedException(Exception):
109+
"""Exception raised for unrecognized models."""
110+
111+
def __init__(self, model, message="Model not recognized"):
112+
self.model = model
113+
self.message = message
114+
super().__init__(self.message)
115+
116+
def __str__(self):
117+
return f"{self.model} -> {self.message}"
118+
119+
107120
# Define style
108121
style = PromptStyle.from_dict(
109122
{
@@ -125,14 +138,17 @@
125138
# Standard yellow text
126139
ANSI_YELLOW = "\033[33m"
127140

141+
ANSI_RED = "\033[31m"
142+
128143
# Bright magenta text
129144
ANSI_BRIGHT_MAGENTA = "\033[95m"
130145

131146

132-
def main(model="gpt-4-vision-preview"):
147+
def main(model):
133148
"""
134149
Main function for the Self-Operating Computer
135150
"""
151+
136152
message_dialog(
137153
title="Self-Operating Computer",
138154
text="Ask a computer to do anything.",
@@ -161,7 +177,21 @@ def main(model="gpt-4-vision-preview"):
161177
while looping:
162178
if DEBUG:
163179
print("[loop] messages before next action:\n\n\n", messages[1:])
164-
response = get_next_action(messages, objective)
180+
try:
181+
response = get_next_action(model, messages, objective)
182+
183+
except ModelNotRecognizedException as e:
184+
print(
185+
f"{ANSI_GREEN}[Self-Operating Computer]{ANSI_RED} error: {e} {ANSI_RESET}"
186+
)
187+
looping = False
188+
break
189+
except Exception as e:
190+
print(
191+
f"{ANSI_GREEN}[Self-Operating Computer]{ANSI_RED} error: {e} {ANSI_RESET}"
192+
)
193+
looping = False
194+
break
165195

166196
action = parse_oai_response(response)
167197
action_type = action.get("type")
@@ -227,7 +257,17 @@ def format_vision_prompt(objective):
227257
return prompt
228258

229259

230-
def get_next_action(messages, objective):
260+
def get_next_action(model, messages, objective):
261+
if model == "gpt-4-vision-preview":
262+
content = get_next_action_from_oai(messages, objective)
263+
return content
264+
elif model == "agent-1":
265+
return "coming soon"
266+
267+
raise ModelNotRecognizedException(model)
268+
269+
270+
def get_next_action_from_oai(messages, objective):
231271
"""
232272
Get the next action for Self-Operating Computer
233273
"""
@@ -515,16 +555,16 @@ def convert_percent_to_decimal(percent_str):
515555

516556

517557
if __name__ == "__main__":
518-
# parser = argparse.ArgumentParser(
519-
# description="Run the self-operating-computer with a specified model."
520-
# )
521-
# parser.add_argument(
522-
# "-m",
523-
# "--model",
524-
# help="Specify the model to use",
525-
# required=False,
526-
# default="default-model",
527-
# )
528-
529-
# args = parser.parse_args()
530-
main()
558+
parser = argparse.ArgumentParser(
559+
description="Run the self-operating-computer with a specified model."
560+
)
561+
parser.add_argument(
562+
"-m",
563+
"--model",
564+
help="Specify the model to use",
565+
required=False,
566+
default="gpt-4-vision-preview",
567+
)
568+
569+
args = parser.parse_args()
570+
main(args.model)

0 commit comments

Comments
 (0)