Refactor Anthropic bot for small/large model switching and update developer prompt for autonomy
This commit is contained in:
@@ -10,13 +10,19 @@ class AnthropicTelegramInferenceBot(BaseTelegramInferenceBot):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
self.anthropic_client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
|
self.anthropic_client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
|
||||||
|
|
||||||
|
# Initialize with the small model by default
|
||||||
|
self.small_model_name = os.environ.get("ANTHROPIC_SMALL_MODEL", "claude-3-haiku-20240307")
|
||||||
|
self.small_model_max_tokens = os.environ.get("ANTHROPIC_SMALL_MODEL_MAX_TOKENS", "2048")
|
||||||
|
self.large_model_name = os.environ.get("ANTHROPIC_LARGE_MODEL", "claude-3-opus-20240229")
|
||||||
|
self.large_model_max_tokens = os.environ.get("ANTHROPIC_LARGE_MODEL_MAX_TOKENS", "4096")
|
||||||
|
|
||||||
self._configure_model_and_tokens(
|
self._configure_model_and_tokens(
|
||||||
os.environ.get("ANTHROPIC_MODEL", "claude-3-5-sonnet-20240620"),
|
self.small_model_name,
|
||||||
os.environ.get("ANTHROPIC_MAX_TOKENS", "4096")
|
self.small_model_max_tokens
|
||||||
)
|
)
|
||||||
|
|
||||||
def _configure_model_and_tokens(self, model_name, max_tokens_str, default_max_tokens=4096):
|
def _configure_model_and_tokens(self, model_name, max_tokens_str, default_max_tokens=2048): # Default max_tokens adjusted for typical "small"
|
||||||
self.model = model_name if model_name else "claude-3-5-sonnet-20240620"
|
self.model = model_name
|
||||||
try:
|
try:
|
||||||
self.max_tokens = int(max_tokens_str) if max_tokens_str is not None else default_max_tokens
|
self.max_tokens = int(max_tokens_str) if max_tokens_str is not None else default_max_tokens
|
||||||
except ValueError:
|
except ValueError:
|
||||||
@@ -173,26 +179,35 @@ class AnthropicTelegramInferenceBot(BaseTelegramInferenceBot):
|
|||||||
return "No active processing found to abort. Conversation cleared."
|
return "No active processing found to abort. Conversation cleared."
|
||||||
|
|
||||||
async def switch_model(self):
|
async def switch_model(self):
|
||||||
primary_model = os.environ.get("ANTHROPIC_MODEL", "claude-3-5-sonnet-20240620")
|
# Ensure ANTHROPIC_SMALL_MODEL and ANTHROPIC_LARGE_MODEL related env vars are loaded in __init__
|
||||||
primary_max_tokens = os.environ.get("ANTHROPIC_MAX_TOKENS", "4096")
|
# or ensure they are freshly checked here if they can change during runtime (less common for model names).
|
||||||
|
# For this implementation, we rely on the values stored during __init__.
|
||||||
secondary_model_env = os.environ.get("ANTHROPIC_SECONDARY_MODEL")
|
|
||||||
secondary_max_tokens_env = os.environ.get("ANTHROPIC_SECONDARY_MAX_TOKENS")
|
|
||||||
|
|
||||||
if not secondary_model_env:
|
if not self.small_model_name or not self.large_model_name:
|
||||||
logging.warning("ANTHROPIC_SECONDARY_MODEL not defined. Cannot switch model.")
|
logging.warning("Small or Large model names for Anthropic are not defined. Cannot switch model.")
|
||||||
return f"Model switching not configured. Currently using {self.model}."
|
return f"Model switching not fully configured. Currently using {self.model}."
|
||||||
|
|
||||||
if self.model == primary_model:
|
if self.model == self.small_model_name:
|
||||||
target_model = secondary_model_env
|
target_model = self.large_model_name
|
||||||
target_max_tokens = secondary_max_tokens_env if secondary_max_tokens_env else "2048"
|
target_max_tokens = self.large_model_max_tokens
|
||||||
|
# Use default large max_tokens if specific one isn't set or invalid
|
||||||
|
default_max_tokens_for_large = "4096"
|
||||||
|
elif self.model == self.large_model_name:
|
||||||
|
target_model = self.small_model_name
|
||||||
|
target_max_tokens = self.small_model_max_tokens
|
||||||
|
# Use default small max_tokens if specific one isn't set or invalid
|
||||||
|
default_max_tokens_for_large = "2048"
|
||||||
else:
|
else:
|
||||||
target_model = primary_model
|
# Current model is neither the designated small nor large, switch to small as a reset
|
||||||
target_max_tokens = primary_max_tokens
|
logging.warning(f"Current model {self.model} is neither the configured small nor large model. Switching to small model.")
|
||||||
|
target_model = self.small_model_name
|
||||||
self._configure_model_and_tokens(target_model, target_max_tokens)
|
target_max_tokens = self.small_model_max_tokens
|
||||||
|
default_max_tokens_for_large = "2048"
|
||||||
|
|
||||||
|
|
||||||
|
self._configure_model_and_tokens(target_model, target_max_tokens, default_max_tokens=int(default_max_tokens_for_large)) # Pass appropriate default
|
||||||
logging.info(f"Switched Anthropic model to: {self.model}")
|
logging.info(f"Switched Anthropic model to: {self.model}")
|
||||||
return f"Switched to Anthropic model: {self.model}"
|
return f"Switched to Anthropic model: {self.model} (Max Tokens: {self.max_tokens})"#Provide token info
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if not os.environ.get("ANTHROPIC_API_KEY"):
|
if not os.environ.get("ANTHROPIC_API_KEY"):
|
||||||
|
|||||||
Reference in New Issue
Block a user