Claude 3.7 Sonnet无法多次调用工具提取多对象的问题求助
Claude 3.7 Sonnet无法多次调用工具提取多对象的问题求助
我最近在测试Claude 3.7 Sonnet的函数调用能力,遇到了一个头疼的问题:当需要从文本中提取多个符合要求的对象时,它只能调用一次工具就返回结果,没法批量处理所有符合条件的对象。
我的需求很明确:从给定文本里提取出匹配指定类型(比如PERSON和CITY NAME)的所有对象,每个提取到的对象都要通过调用工具来格式化输出。但目前Claude只会提取第一个对象,完全忽略了第二个符合条件的对象。
下面是我的完整测试代码(补充了Pydantic的必要导入,原文里漏写了):
from pydantic import BaseModel, Field from llama_index.core.program.function_program import FunctionCallingProgram from llama_index.llms.anthropic import Anthropic from llama_index.core import PromptTemplate class ObjectInstance(BaseModel): type: str = Field( ..., description="The category or class of the object extracted from the text. Must exactly match one of the predefined object types provided.", ) name: str = Field( ..., description="The exact name of the object as it appears in the text in upper case.", ) description: str = Field( ..., description="Information about the object from the text it is extracted from", ) test_prompt = PromptTemplate( """ Task Description: From a given text, extract one or more objects that match the object type provided. For each object, call the provided tool to format the output. OBject Types and Descriptions: Entity Type 1: PERSON Description: A name of a person Entity Type 2: CITY NAME Description: A name of a city Text: {content} """ ) llm = Anthropic(model = "claude-3-7-sonnet-20250219") program = FunctionCallingProgram.from_defaults( output_cls=ObjectInstance, prompt=test_prompt, llm=llm, allow_parallel_tool_calls=True, ) content = "John Doe is a person who lives in San Francisco." output = program(content=content) # 实际输出只提取到第一个对象: # Name: JOHN DOE # Type: PERSON # 预期应该同时提取两个对象: # Name: JOHN DOE # Type: PERSON # ------------------- # Name: SAN FRANCISCO # Type: CITY NAME
为了解决这个问题,我还尝试了另外两种调用方式,但结果还是一样,只能提取一个对象:
尝试2:使用structured_predict方法
response = llm.structured_predict( ObjectInstance, test_prompt, content=content, allow_parallel_tool_calls=True )
尝试3:使用get_function_tool结合chat_with_tools
from llama_index.core.program.function_program import get_function_tool tool = get_function_tool(ObjectInstance) resp = llm.chat_with_tools( [tool], user_msg=test_prompt.format(content=content), allow_parallel_tool_calls=True, ) tool_calls = llm.get_tool_calls_from_response( resp, error_on_no_tool_calls=False )
更奇怪的是,同样的代码和提示词,GPT-4o和Mistral Large都能正常触发多次工具调用,把两个对象都完整提取出来。只有Claude 3.7 Sonnet做不到这一点,我是不是哪里配置错了,或者遗漏了什么关键参数?
备注:内容来源于stack exchange,提问作者Makoto Miyazaki
相关产品推荐
相关产品推荐

