mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-18 22:44:21 +01:00
'Refactored by Sourcery'
This commit is contained in:
@@ -36,11 +36,8 @@ class AgentManager(metaclass=Singleton):
|
|||||||
for plugin in self.cfg.plugins:
|
for plugin in self.cfg.plugins:
|
||||||
if not plugin.can_handle_pre_instruction():
|
if not plugin.can_handle_pre_instruction():
|
||||||
continue
|
continue
|
||||||
plugin_messages = plugin.pre_instruction(messages)
|
if plugin_messages := plugin.pre_instruction(messages):
|
||||||
if plugin_messages:
|
messages.extend(iter(plugin_messages))
|
||||||
for plugin_message in plugin_messages:
|
|
||||||
messages.append(plugin_message)
|
|
||||||
|
|
||||||
# Start GPT instance
|
# Start GPT instance
|
||||||
agent_reply = create_chat_completion(
|
agent_reply = create_chat_completion(
|
||||||
model=model,
|
model=model,
|
||||||
@@ -53,9 +50,8 @@ class AgentManager(metaclass=Singleton):
|
|||||||
for i, plugin in enumerate(self.cfg.plugins):
|
for i, plugin in enumerate(self.cfg.plugins):
|
||||||
if not plugin.can_handle_on_instruction():
|
if not plugin.can_handle_on_instruction():
|
||||||
continue
|
continue
|
||||||
plugin_result = plugin.on_instruction(messages)
|
if plugin_result := plugin.on_instruction(messages):
|
||||||
if plugin_result:
|
sep = "\n" if i else ""
|
||||||
sep = "" if not i else "\n"
|
|
||||||
plugins_reply = f"{plugins_reply}{sep}{plugin_result}"
|
plugins_reply = f"{plugins_reply}{sep}{plugin_result}"
|
||||||
|
|
||||||
if plugins_reply and plugins_reply != "":
|
if plugins_reply and plugins_reply != "":
|
||||||
@@ -92,8 +88,7 @@ class AgentManager(metaclass=Singleton):
|
|||||||
for plugin in self.cfg.plugins:
|
for plugin in self.cfg.plugins:
|
||||||
if not plugin.can_handle_pre_instruction():
|
if not plugin.can_handle_pre_instruction():
|
||||||
continue
|
continue
|
||||||
plugin_messages = plugin.pre_instruction(messages)
|
if plugin_messages := plugin.pre_instruction(messages):
|
||||||
if plugin_messages:
|
|
||||||
for plugin_message in plugin_messages:
|
for plugin_message in plugin_messages:
|
||||||
messages.append(plugin_message)
|
messages.append(plugin_message)
|
||||||
|
|
||||||
@@ -109,9 +104,8 @@ class AgentManager(metaclass=Singleton):
|
|||||||
for i, plugin in enumerate(self.cfg.plugins):
|
for i, plugin in enumerate(self.cfg.plugins):
|
||||||
if not plugin.can_handle_on_instruction():
|
if not plugin.can_handle_on_instruction():
|
||||||
continue
|
continue
|
||||||
plugin_result = plugin.on_instruction(messages)
|
if plugin_result := plugin.on_instruction(messages):
|
||||||
if plugin_result:
|
sep = "\n" if i else ""
|
||||||
sep = "" if not i else "\n"
|
|
||||||
plugins_reply = f"{plugins_reply}{sep}{plugin_result}"
|
plugins_reply = f"{plugins_reply}{sep}{plugin_result}"
|
||||||
# Update full message history
|
# Update full message history
|
||||||
if plugins_reply and plugins_reply != "":
|
if plugins_reply and plugins_reply != "":
|
||||||
|
|||||||
@@ -69,13 +69,10 @@ def create_chat_completion(
|
|||||||
Returns:
|
Returns:
|
||||||
str: The response from the chat completion
|
str: The response from the chat completion
|
||||||
"""
|
"""
|
||||||
response = None
|
|
||||||
num_retries = 10
|
num_retries = 10
|
||||||
if CFG.debug_mode:
|
if CFG.debug_mode:
|
||||||
print(
|
print(
|
||||||
Fore.GREEN
|
f"{Fore.GREEN}Creating chat completion with model {model}, temperature {temperature}, max_tokens {max_tokens}{Fore.RESET}"
|
||||||
+ f"Creating chat completion with model {model}, temperature {temperature},"
|
|
||||||
f" max_tokens {max_tokens}" + Fore.RESET
|
|
||||||
)
|
)
|
||||||
for plugin in CFG.plugins:
|
for plugin in CFG.plugins:
|
||||||
if plugin.can_handle_chat_completion(
|
if plugin.can_handle_chat_completion(
|
||||||
@@ -84,13 +81,13 @@ def create_chat_completion(
|
|||||||
temperature=temperature,
|
temperature=temperature,
|
||||||
max_tokens=max_tokens,
|
max_tokens=max_tokens,
|
||||||
):
|
):
|
||||||
response = plugin.handle_chat_completion(
|
return plugin.handle_chat_completion(
|
||||||
messages=messages,
|
messages=messages,
|
||||||
model=model,
|
model=model,
|
||||||
temperature=temperature,
|
temperature=temperature,
|
||||||
max_tokens=max_tokens,
|
max_tokens=max_tokens,
|
||||||
)
|
)
|
||||||
return response
|
response = None
|
||||||
for attempt in range(num_retries):
|
for attempt in range(num_retries):
|
||||||
backoff = 2 ** (attempt + 2)
|
backoff = 2 ** (attempt + 2)
|
||||||
try:
|
try:
|
||||||
@@ -112,21 +109,16 @@ def create_chat_completion(
|
|||||||
break
|
break
|
||||||
except RateLimitError:
|
except RateLimitError:
|
||||||
if CFG.debug_mode:
|
if CFG.debug_mode:
|
||||||
print(
|
print(f"{Fore.RED}Error: ", f"Reached rate limit, passing...{Fore.RESET}")
|
||||||
Fore.RED + "Error: ",
|
|
||||||
"Reached rate limit, passing..." + Fore.RESET,
|
|
||||||
)
|
|
||||||
except APIError as e:
|
except APIError as e:
|
||||||
if e.http_status == 502:
|
if e.http_status != 502:
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise
|
raise
|
||||||
if attempt == num_retries - 1:
|
if attempt == num_retries - 1:
|
||||||
raise
|
raise
|
||||||
if CFG.debug_mode:
|
if CFG.debug_mode:
|
||||||
print(
|
print(
|
||||||
Fore.RED + "Error: ",
|
f"{Fore.RED}Error: ",
|
||||||
f"API Bad gateway. Waiting {backoff} seconds..." + Fore.RESET,
|
f"API Bad gateway. Waiting {backoff} seconds...{Fore.RESET}",
|
||||||
)
|
)
|
||||||
time.sleep(backoff)
|
time.sleep(backoff)
|
||||||
if response is None:
|
if response is None:
|
||||||
@@ -159,15 +151,13 @@ def create_embedding_with_ada(text) -> list:
|
|||||||
except RateLimitError:
|
except RateLimitError:
|
||||||
pass
|
pass
|
||||||
except APIError as e:
|
except APIError as e:
|
||||||
if e.http_status == 502:
|
if e.http_status != 502:
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise
|
raise
|
||||||
if attempt == num_retries - 1:
|
if attempt == num_retries - 1:
|
||||||
raise
|
raise
|
||||||
if CFG.debug_mode:
|
if CFG.debug_mode:
|
||||||
print(
|
print(
|
||||||
Fore.RED + "Error: ",
|
f"{Fore.RED}Error: ",
|
||||||
f"API Bad gateway. Waiting {backoff} seconds..." + Fore.RESET,
|
f"API Bad gateway. Waiting {backoff} seconds...{Fore.RESET}",
|
||||||
)
|
)
|
||||||
time.sleep(backoff)
|
time.sleep(backoff)
|
||||||
|
|||||||
Reference in New Issue
Block a user