使用Hugging Face smolagents时输入Token量异常偏高的原因排查求助
使用Hugging Face smolagents时输入Token量异常偏高的原因排查求助
我最近在试用Hugging Face推出的新智能体框架smolagents,在运行工具调用智能体和代码智能体的时候,发现了一个很费解的问题:哪怕我给智能体的系统提示里完全没填实质内容,输入Token的计数还是高得离谱。
我运行的完整代码如下:
from smolagents import ToolCallingAgent, HfApiModel, tool from dotenv import load_dotenv from smolagents.prompts import TOOL_CALLING_SYSTEM_PROMPT import os load_dotenv() # 选择模型 model_id = "meta-llama/Llama-3.3-70B-Instruct" model = HfApiModel(model_id=model_id) # 创建几个工具 @tool def add_numbers(a: float, b: float) -> float: """ Add two floating point numbers together. Args: a: first number b: second number """ return a + b @tool def subtract_numbers(a: float, b: float) -> float: """ Subtract second number from the first number. Args: a: number to subtract from b: number to subtract """ return a - b @tool def multiply_numbers(a: float, b: float) -> float: """ Multiply two floating point numbers. Args: a: first number b: second number """ return a * b @tool def divide_numbers(a: float, b: float) -> float: """ Divide first number by the second number. Args: a: dividend (number to be divided) b: divisor (number to divide by) """ if b == 0: raise ValueError("Cannot divide by zero") return a / b # 自定义提示词 custom_prompt = """You are a math expert. You will only use the tools available to you. Here are the tools available to you: {{tool_descriptions}} {{managed_agents_descriptions}} IMPORTANT NOTE: You will ALWAYS evaluate the user's query and perfom query classification and print three things: answer, tool_used, reasoning like this: Answer: answer Tool Used: tool_name Reasoning: reasoning for using the tool An example: Answer: 21.0 Tool Used: multiply Reasoning: The tool was used to calculate the product of two numbers. Solve the queries STEP by STEP and feel free to use the tools available to you and do not hallucinate or make assumptions.""" new_prompt = """{{managed_agents_descriptions}}""" # 基于模型和工具创建智能体 agent = ToolCallingAgent(tools=[add_numbers, subtract_numbers, multiply_numbers, divide_numbers], model=model, add_base_tools=True, system_prompt=new_prompt) # print(agent.initialize_system_prompt()) # agent.run("What's 2 + 8 - 3?") if __name__ == "__main__": print("new prompt is: ", agent.system_prompt) print(agent.run("What's 2 + 8 - 3?"))
我特意用了new_prompt作为系统提示,这里面只有{{managed_agents_descriptions}}这个占位符——但我根本没用到托管智能体,所以理论上这个提示几乎是空的。可运行后终端显示输入Token量居然有大约5000个,这完全超出预期了。

有没有大佬能帮我分析下,这超高的输入Token量到底是哪里来的?是不是我在配置的时候漏了什么细节?
备注:内容来源于stack exchange,提问作者Ketan Kunkalikar
相关产品推荐
相关产品推荐

