format code and remove max_input_tokens for ToolCallAgent
This commit is contained in:
parent
86399b97d6
commit
65a3898592
@ -33,7 +33,6 @@ class ToolCallAgent(ReActAgent):
|
||||
|
||||
max_steps: int = 30
|
||||
max_observe: Optional[Union[int, bool]] = None
|
||||
max_input_tokens: Optional[int] = None
|
||||
|
||||
async def think(self) -> bool:
|
||||
"""Process current state and decide next actions using tools"""
|
||||
@ -51,13 +50,15 @@ class ToolCallAgent(ReActAgent):
|
||||
tools=self.available_tools.to_params(),
|
||||
tool_choice=self.tool_choices,
|
||||
)
|
||||
except ValueError as e:
|
||||
except ValueError:
|
||||
raise
|
||||
except Exception as e:
|
||||
# Check if this is a RetryError containing TokenLimitExceeded
|
||||
if hasattr(e, "__cause__") and isinstance(e.__cause__, TokenLimitExceeded):
|
||||
token_limit_error = e.__cause__
|
||||
logger.error(f"🚨 Token limit error (from RetryError): {token_limit_error}")
|
||||
logger.error(
|
||||
f"🚨 Token limit error (from RetryError): {token_limit_error}"
|
||||
)
|
||||
self.memory.add_message(
|
||||
Message.assistant_message(
|
||||
f"Maximum token limit reached, cannot continue execution: {str(token_limit_error)}"
|
||||
|
@ -20,7 +20,10 @@ class LLMSettings(BaseModel):
|
||||
base_url: str = Field(..., description="API base URL")
|
||||
api_key: str = Field(..., description="API key")
|
||||
max_tokens: int = Field(4096, description="Maximum number of tokens per request")
|
||||
max_input_tokens: Optional[int] = Field(None, description="Maximum input tokens to use across all requests (None for unlimited)")
|
||||
max_input_tokens: Optional[int] = Field(
|
||||
None,
|
||||
description="Maximum input tokens to use across all requests (None for unlimited)",
|
||||
)
|
||||
temperature: float = Field(1.0, description="Sampling temperature")
|
||||
api_type: str = Field(..., description="AzureOpenai or Openai")
|
||||
api_version: str = Field(..., description="Azure Openai version if AzureOpenai")
|
||||
|
@ -4,10 +4,10 @@ class ToolError(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
|
||||
class OpenManusError(Exception):
|
||||
"""Base exception for all OpenManus errors"""
|
||||
pass
|
||||
|
||||
|
||||
class TokenLimitExceeded(OpenManusError):
|
||||
"""Exception raised when the token limit is exceeded"""
|
||||
pass
|
||||
|
40
app/llm.py
40
app/llm.py
@ -1,5 +1,6 @@
|
||||
from typing import Dict, List, Optional, Union
|
||||
|
||||
import tiktoken
|
||||
from openai import (
|
||||
APIError,
|
||||
AsyncAzureOpenAI,
|
||||
@ -8,8 +9,12 @@ from openai import (
|
||||
OpenAIError,
|
||||
RateLimitError,
|
||||
)
|
||||
import tiktoken
|
||||
from tenacity import retry, stop_after_attempt, wait_random_exponential, retry_if_exception_type
|
||||
from tenacity import (
|
||||
retry,
|
||||
retry_if_exception_type,
|
||||
stop_after_attempt,
|
||||
wait_random_exponential,
|
||||
)
|
||||
|
||||
from app.config import LLMSettings, config
|
||||
from app.exceptions import TokenLimitExceeded
|
||||
@ -54,7 +59,11 @@ class LLM:
|
||||
|
||||
# Add token counting related attributes
|
||||
self.total_input_tokens = 0
|
||||
self.max_input_tokens = llm_config.max_input_tokens if hasattr(llm_config, "max_input_tokens") else None
|
||||
self.max_input_tokens = (
|
||||
llm_config.max_input_tokens
|
||||
if hasattr(llm_config, "max_input_tokens")
|
||||
else None
|
||||
)
|
||||
|
||||
# Initialize tokenizer
|
||||
try:
|
||||
@ -99,10 +108,14 @@ class LLM:
|
||||
if "function" in tool_call:
|
||||
# Function name
|
||||
if "name" in tool_call["function"]:
|
||||
token_count += self.count_tokens(tool_call["function"]["name"])
|
||||
token_count += self.count_tokens(
|
||||
tool_call["function"]["name"]
|
||||
)
|
||||
# Function arguments
|
||||
if "arguments" in tool_call["function"]:
|
||||
token_count += self.count_tokens(tool_call["function"]["arguments"])
|
||||
token_count += self.count_tokens(
|
||||
tool_call["function"]["arguments"]
|
||||
)
|
||||
|
||||
# Calculate tokens for tool responses
|
||||
if "name" in message and message["name"]:
|
||||
@ -120,7 +133,9 @@ class LLM:
|
||||
"""Update token counts"""
|
||||
# Only track tokens if max_input_tokens is set
|
||||
self.total_input_tokens += input_tokens
|
||||
logger.info(f"Token usage: Input={input_tokens}, Cumulative Input={self.total_input_tokens}")
|
||||
logger.info(
|
||||
f"Token usage: Input={input_tokens}, Cumulative Input={self.total_input_tokens}"
|
||||
)
|
||||
|
||||
def check_token_limit(self, input_tokens: int) -> bool:
|
||||
"""Check if token limits are exceeded"""
|
||||
@ -131,7 +146,10 @@ class LLM:
|
||||
|
||||
def get_limit_error_message(self, input_tokens: int) -> str:
|
||||
"""Generate error message for token limit exceeded"""
|
||||
if self.max_input_tokens is not None and (self.total_input_tokens + input_tokens) > self.max_input_tokens:
|
||||
if (
|
||||
self.max_input_tokens is not None
|
||||
and (self.total_input_tokens + input_tokens) > self.max_input_tokens
|
||||
):
|
||||
return f"Request may exceed input token limit (Current: {self.total_input_tokens}, Needed: {input_tokens}, Max: {self.max_input_tokens})"
|
||||
|
||||
return "Token limit exceeded"
|
||||
@ -187,7 +205,9 @@ class LLM:
|
||||
@retry(
|
||||
wait=wait_random_exponential(min=1, max=60),
|
||||
stop=stop_after_attempt(6),
|
||||
retry=retry_if_exception_type((OpenAIError, Exception, ValueError)), # Don't retry TokenLimitExceeded
|
||||
retry=retry_if_exception_type(
|
||||
(OpenAIError, Exception, ValueError)
|
||||
), # Don't retry TokenLimitExceeded
|
||||
)
|
||||
async def ask(
|
||||
self,
|
||||
@ -299,7 +319,9 @@ class LLM:
|
||||
@retry(
|
||||
wait=wait_random_exponential(min=1, max=60),
|
||||
stop=stop_after_attempt(6),
|
||||
retry=retry_if_exception_type((OpenAIError, Exception, ValueError)), # Don't retry TokenLimitExceeded
|
||||
retry=retry_if_exception_type(
|
||||
(OpenAIError, Exception, ValueError)
|
||||
), # Don't retry TokenLimitExceeded
|
||||
)
|
||||
async def ask_tool(
|
||||
self,
|
||||
|
Loading…
x
Reference in New Issue
Block a user