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