llama_index 在尝试使用StructuredPlannerAgent与FunctionCallingAgentWorker时,出现了错误"KeyError: 'multi_tool_use.parallel'",

oo7oh9g9  于 6个月前  发布在  其他
关注(0)|答案(7)|浏览(62)

问题验证

  • 我已经在文档和Discord上寻找答案。

问题

我尝试使用StructuredPlannerAgent与FunctionCallingAgentWorker。代码如下。然而,每次运行时,我都会遇到一个错误,提示:
KeyError: 'multi_tool_use.parallel
代码:

from llama_index.core.tools import FunctionTool

get_client_comparison_tool = FunctionTool.from_defaults(fn=get_client_comparison)
get_compared_to_peer_group_tool = FunctionTool.from_defaults(fn=get_compared_to_peer_group)

from llama_index.core.agent import (
    StructuredPlannerAgent,
    FunctionCallingAgentWorker,
    ReActAgentWorker,
)

# create the function calling worker for reasoning
worker = FunctionCallingAgentWorker.from_tools(
    tools=[get_client_comparison_tool, get_compared_to_peer_group_tool], 
    verbose=True
)

# wrap the worker in the top-level planner
agent = StructuredPlannerAgent(
    worker, tools=[get_client_comparison_tool, get_compared_to_peer_group_tool], verbose=True
)

import nest_asyncio

nest_asyncio.apply()

response = agent.chat(
    f"You are now AI assistant for RBC Mobile Banking app for a given client to understand comparitive spending & budgeting suggestions. "
    "Based on the insights below can you come up with a 'Budget Suggestion follow-up action' that helps the client to decide on how should he budget based on his peer group & his own transactions"
)

错误:

File ~/Documents/amplify/hermes/utilities/research_rag/venv/lib/python3.9/site-packages/llama_index/core/tools/calling.py:74, in call_tool_with_selection(tool_call, tools, verbose)
     72     print("=== Calling Function ===")
     73     print(f"Calling function: {name} with args: {arguments_str}")
---> 74 tool = tools_by_name[name]
     75 output = call_tool(tool, tool_call.tool_kwargs)
     77 if verbose:

KeyError: 'multi_tool_use.parallel'

# 代码示例

from llama_index.core.tools import FunctionTool

get_client_comparison_tool = FunctionTool.from_defaults(fn=get_client_comparison)
get_compared_to_peer_group_tool = FunctionTool.from_defaults(fn=get_compared_to_peer_group)

from llama_index.core.agent import (
StructuredPlannerAgent,
FunctionCallingAgentWorker,
ReActAgentWorker,
)

create the function calling worker for reasoning

worker = FunctionCallingAgentWorker.from_tools(
tools=[get_client_comparison_tool, get_compared_to_peer_group_tool],
verbose=True
)

wrap the worker in the top-level planner

agent = StructuredPlannerAgent(
worker, tools=[get_client_comparison_tool, get_compared_to_peer_group_tool], verbose=True
)

import nest_asyncio

nest_asyncio.apply()

response = agent.chat(
f"You are now AI assistant for RBC Mobile Banking app for a given client to understand comparitive spending & budgeting suggestions. "
"Based on the insights below can you come up with a 'Budget Suggestion follow-up action' that helps the client to decide on how should he budget based on his peer group & his own transactions"
)

xa9qqrwz

xa9qqrwz1#

要解决在使用 StructuredPlannerAgentFunctionCallingAgentWorker 时出现的 KeyError: 'multi_tool_use.parallel' 问题,请确保正确设置 allow_parallel_tool_calls 参数。FunctionCallingAgentWorker 类具有一个默认值为 Trueallow_parallel_tool_calls 属性。以下是如何使用 allow_parallel_tool_calls 参数初始化 FunctionCallingAgentWorker 的示例:

class `FunctionCallingAgentWorker`:
    `allow_parallel_tool_calls` = "default_value"

# 初始化 `FunctionCallingAgentWorker`

`FunctionCallingAgentWorker` = `FunctionCallingAgentWorker`()

请根据需要设置 allow_parallel_tool_calls 参数,以避免出现 KeyError。此外,确保工具和 LLM 已正确初始化并传递给 FunctionCallingAgentWorkerStructuredPlannerAgent

  • 要继续与 Dosu 进行对话,请提及 @dosu。*
vql8enpb

vql8enpb2#

我修改了我的代码如下:

from llama_index.llms.openai import OpenAI

Settings.llm = OpenAI(
    model=C.LLM_MODEL, temperature=0, max_tokens=4000, api_base=os.environ.get('GW_BASE_URL'), api_key=get_api_key()
)

from llama_index.core.tools import FunctionTool

get_client_comparison_tool = FunctionTool.from_defaults(fn=get_client_comparison)
get_compared_to_peer_group_tool = FunctionTool.from_defaults(fn=get_compared_to_peer_group)

from llama_index.core.tools import FunctionTool
from llama_index.core.agent import (
    StructuredPlannerAgent,
    FunctionCallingAgentWorker,
)

# create the function calling worker for reasoning
worker = FunctionCallingAgentWorker.from_tools(
    tools=[get_client_comparison_tool, get_compared_to_peer_group_tool], 
    verbose=True,
    allow_parallel_tool_calls=True
)

# wrap the worker in the top-level planner
agent = StructuredPlannerAgent(
    worker, tools=[get_client_comparison_tool, get_compared_to_peer_group_tool], verbose=True
)

import nest_asyncio

nest_asyncio.apply()

response = agent.chat(
    f"You are now AI assistant for RBC Mobile Banking app for a given client to understand comparitive spending & budgeting suggestions. "
    "Based on the insights below can you come up with a 'Budget Suggestion follow-up action' that helps the client to decide on how should he budget based on his peer group & his own transactions"
)

再次出现错误。我也在这里添加了回溯信息。

--> 341 result = program(**prompt_args)
    342 dispatcher.event(LLMStructuredPredictEndEvent(output=result))
    343 return result

File ~/Documents/amplify/hermes/utilities/research_rag/venv/lib/python3.9/site-packages/llama_index/core/instrumentation/dispatcher.py:230, in Dispatcher.span.<locals>.wrapper(func, instance, args, kwargs)
    226 self.span_enter(
    227     id_=id_, bound_args=bound_args, instance=instance, parent_id=parent_id
    228 )
    229 try:
--> 230     result = func(*args, **kwargs)
    231 except BaseException as e:
    232     self.event(SpanDropEvent(span_id=id_, err_str=str(e)))

File ~/Documents/amplify/hermes/utilities/research_rag/venv/lib/python3.9/site-packages/llama_index/core/program/function_program.py:138, in FunctionCallingProgram.__call__(self, llm_kwargs, *args, **kwargs)
    135 messages = self._prompt.format_messages(llm=self._llm, **kwargs)
    136 messages = self._llm._extend_messages(messages)
--> 138 agent_response = self._llm.predict_and_call(
    139     [tool],
    140     chat_history=messages,
    141     verbose=self._verbose,
    142     allow_parallel_tool_calls=self._allow_parallel_tool_calls,
    143     **llm_kwargs,
    144 )
    145 return _parse_tool_outputs(
    146     agent_response,
    147     allow_parallel_tool_calls=self._allow_parallel_tool_calls,
    148 )

File ~/Documents/amplify/hermes/utilities/research_rag/venv/lib/python3.9/site-packages/llama_index/core/instrumentation/dispatcher.py:230, in Dispatcher.span.<locals>.wrapper(func, instance, args, kwargs)
    226 self.span_enter(
    227     id_=id_, bound_args=bound_args, instance=instance, parent_id=parent_id
    228 )
    229 try:
--> 230     result = func(*args, **kwargs)
    231 except BaseException as e:
    232     self.event(SpanDropEvent(span_id=id_, err_str=str(e)))

File ~/Documents/amplify/hermes/utilities/research_rag/venv/lib/python3.9/site-packages/llama_index/core/llms/function_calling.py:101, in FunctionCallingLLM.predict_and_call(self, tools, user_msg, chat_history, verbose, allow_parallel_tool_calls, **kwargs)
     92 response = self.chat_with_tools(
     93     tools,
     94     user_msg,
   (...)
     98     **kwargs,
     99 )
    100 tool_calls = self.get_tool_calls_from_response(response)
--> 101 tool_outputs = [
    102     call_tool_with_selection(tool_call, tools, verbose=verbose)
    103     for tool_call in tool_calls
    104 ]
    105 if allow_parallel_tool_calls:
    106     output_text = "\n\n".join(
    107         [tool_output.content for tool_output in tool_outputs]
    108     )

File ~/Documents/amplify/hermes/utilities/research_rag/venv/lib/python3.9/site-packages/llama_index/core/llms/function_calling.py:102, in <listcomp>(.0)
     92 response = self.chat_with_tools(
     93     tools,
     94     user_msg,
   (...)
     98     **kwargs,
     99 )
    100 tool_calls = self.get_tool_calls_from_response(response)
    101 tool_outputs = [
--> 102     call_tool_with_selection(tool_call, tools, verbose=verbose)
    103     for tool_call in tool_calls
    104 ]
    105 if allow_parallel_tool_calls:
    106     output_text = "\n\n".join(
    107         [tool_output.content for tool_output in tool_outputs]
    108     )

File ~/Documents/amplify/hermes/utilities/research_rag/venv/lib/python3.9/site-packages/llama_index/core/tools/calling.py:74, in call_tool_with_selection(tool_call, tools, verbose)
     72     print("=== Calling Function ===")
     73     print(f"Calling function: {name} with args: {arguments_str}")
---> 74 tool = tools_by_name[name]
     75 output = call_tool(tool, tool_call.tool_kwargs)
     77 if verbose:

KeyError: 'multi_tool_use.parallel'
qeeaahzv

qeeaahzv3#

KeyError: 'multi_tool_use.parallel' 表示在 tools_by_name 字典中找不到工具名 'multi_tool_use.parallel'。此字典通过将工具名Map到其对应的工具对象来创建。
要解决此问题,请确保您正在使用的工具已正确初始化,并且它们的名称与 tools_by_name 字典中的预期名称相匹配。以下是您的代码的更新版本,其中包含额外的检查和初始化:

from llama_index.llms.openai import OpenAI
from llama_index.core.tools import FunctionTool
from llama_index.core.agent import StructuredPlannerAgent, FunctionCallingAgentWorker
from llama_index.core.llms.function_calling import FunctionCallingLLM
from llama_index.core.base.llms.types import ChatMessage
import nest_asyncio
import os

# Initialize the LLM
llm = OpenAI(
    model="your_model_name",  # Replace with your model name
    temperature=0,
    max_tokens=4000,
    api_base=os.environ.get('GW_BASE_URL'),
    api_key="your_api_key"  # Replace with your API key
)

# Define your functions
def get_client_comparison():
    # Your function implementation
    pass

def get_compared_to_peer_group():
    # Your function implementation
    pass

# Initialize the tools
get_client_comparison_tool = FunctionTool.from_defaults(fn=get_client_comparison)
get_compared_to_peer_group_tool = FunctionTool.from_defaults(fn=get_compared_to_peer_group)

# Initialize the FunctionCallingAgentWorker with allow_parallel_tool_calls set to True
worker = FunctionCallingAgentWorker(
    tools=[get_client_comparison_tool, get_compared_to_peer_group_tool],
    llm=llm,
    prefix_messages=[ChatMessage(content="System message", role="system")],
    verbose=True,
    allow_parallel_tool_calls=True
)

# Wrap the worker in the top-level planner
agent = StructuredPlannerAgent(
    agent_worker=worker,
    tools=[get_client_comparison_tool, get_compared_to_peer_group_tool],
    verbose=True
)

# Apply nest_asyncio
nest_asyncio.apply()

# Run the agent
response = agent.chat(
    "You are now AI assistant for RBC Mobile Banking app for a given client to understand comparative spending & budgeting suggestions. "
    "Based on the insights below can you come up with a 'Budget Suggestion follow-up action' that helps the client to decide on how should he budget based on his peer group & his own transactions"
)

print(response)

确保:

  1. 工具已正确初始化,且名称唯一。
  2. FunctionCallingAgentWorker 已正确配置 allow_parallel_tool_calls 参数。
  3. LLM 和工具已正确传递给 FunctionCallingAgentWorkerStructuredPlannerAgent
    如果问题仍然存在,请验证您在函数调用中使用的工具名称是否与 tools_by_name 字典中的名称相匹配(1(https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/tools/calling.py))。
  • 要继续与 Dosu 交谈,请提及 @dosu。*
drnojrws

drnojrws4#

我仍然偶尔会遇到这个错误。问题是:
我正在尝试使用StructuredPlannerAgent与FunctionCallingAgentWorker。有时我会收到一个错误,提示KeyError: 'multi_tool_use.parallel'。这是我的代码:

from llama_index.core.tools import FunctionTool

financial_summary_tool = FunctionTool.from_defaults(fn=get_financial_summary)
daily_balances_tool = FunctionTool.from_defaults(fn=get_daily_balances)
spending_conditions_tool = FunctionTool.from_defaults(fn=get_spending_conditions)
client_spending_tool = FunctionTool.from_defaults(fn=get_client_spending_for_current_month)

from llama_index.core.tools import FunctionTool
from llama_index.core.base.llms.types import ChatMessage
from llama_index.core.agent import (
    StructuredPlannerAgent,
    FunctionCallingAgentWorker,
)

# Initialize the FunctionCallingAgentWorker with allow_parallel_tool_calls set to True
worker = FunctionCallingAgentWorker(
    tools=[financial_summary_tool, daily_balances_tool, spending_conditions_tool, client_spending_tool],
    llm=llm,
    prefix_messages=[ChatMessage(content="You are now AI assistant for a Banking app for a given client to understand financial overview. ", role="system")],
    verbose=True,
    allow_parallel_tool_calls=True
)

# Wrap the worker in the top-level planner
agent = StructuredPlannerAgent(
    agent_worker=worker,
    tools=[financial_summary_tool, daily_balances_tool, spending_conditions_tool, client_spending_tool],
    verbose=True
)


initiator_prompt = """
You are an AI assistant for a banking application. Your purpose is to help clients understand their financial overview and provide personalized advice for financial well-being.
Based on your analysis, create 'What If' scenarios that could improve the client's financial situation. Consider both short-term actions and their long-term impacts.
Develop personalized ideas for financial well-being. These should be tailored to the client's specific financial situation and goals.
For each suggestion, demonstrate how it would affect the client's finances in the upcoming month. Use the tools to calculate and show the potential impact.
Additionally, showcase the longer-term impact of these actions over a 6-month and 1-year period.
"""

whatif_insight = agent.chat(initiator_prompt)

错误:

File ~/Documents/amplify/hermes/utilities/research_rag/venv/lib/python3.9/site-packages/llama_index/core/tools/calling.py:74, in call_tool_with_selection(tool_call, tools, verbose)
     72     print("=== Calling Function ===")
     73     print(f"Calling function: {name} with args: {arguments_str}")
---> 74 tool = tools_by_name[name]
     75 output = call_tool(tool, tool_call.tool_kwargs)
     77 if verbose:

KeyError: 'multi_tool_use.parallel'
2skhul33

2skhul335#

KeyError: 'multi_tool_use.parallel' 发生是因为工具名 'multi_tool_use.parallel'tools_by_name 字典中找不到。如果函数调用期间生成的工具名与 tools 列表中的任何工具名都不匹配,就会出现这个问题。
要解决这个问题,可以在尝试访问它之前,确保 tool_call.tool_name 存在于 tools_by_name 字典中。以下是带有此检查的 call_tool_with_selection 函数的更新版本:

def call_tool_with_selection(
    tool_call: ToolSelection,
    tools: List["BaseTool"],
    verbose: bool = False,
) -> ToolOutput:
    from llama_index.core.tools.calling import call_tool

    tools_by_name = {tool.metadata.name: tool for tool in tools}
    name = tool_call.tool_name
    if verbose:
        arguments_str = json.dumps(tool_call.tool_kwargs)
        print("=== Calling Function ===")
        print(f"Calling function: {name} with args: {arguments_str}")
    
    if name not in tools_by_name:
        return ToolOutput(
            content=f"Error: Tool '{name}' not found.",
            tool_name=name,
            raw_input=tool_call.tool_kwargs,
            raw_output="",
            is_error=True,
        )
    
    tool = tools_by_name[name]
    output = call_tool(tool, tool_call.tool_kwargs)

    if verbose:
        print("=== Function Output ===")
        print(output.content)

    return output

这个检查确保,如果在字典中找不到工具名,就会返回一个适当的错误消息,而不是引发一个 KeyError1(https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/tools/calling.py)。
此外,确保 allow_parallel_tool_calls 参数正确设置,工具已正确初始化并传递给 FunctionCallingAgentWorkerStructuredPlannerAgent。以下是考虑了这些因素的更新代码:

from llama_index.core.tools import FunctionTool
from llama_index.core.base.llms.types import ChatMessage
from llama_index.core.agent import StructuredPlannerAgent, FunctionCallingAgentWorker
from llama_index.llms.openai import OpenAI
import nest_asyncio
import os

# Initialize the LLM
llm = OpenAI(
    model="your_model_name",  # Replace with your model name
    temperature=0,
    max_tokens=4000,
    api_base=os.environ.get('GW_BASE_URL'),
    api_key="your_api_key"  # Replace with your API key
)

# Define your functions
def get_financial_summary():
    # Your function implementation
    pass

def get_daily_balances():
    # Your function implementation
    pass

def get_spending_conditions():
    # Your function implementation
    pass

def get_client_spending_for_current_month():
    # Your function implementation
    pass

# Initialize the tools
financial_summary_tool = FunctionTool.from_defaults(fn=get_financial_summary)
daily_balances_tool = FunctionTool.from_defaults(fn=get_daily_balances)
spending_conditions_tool = FunctionTool.from_defaults(fn=get_spending_conditions)
client_spending_tool = FunctionTool.from_defaults(fn=get_client_spending_for_current_month)

# Initialize the FunctionCallingAgentWorker with allow_parallel_tool_calls set to True
worker = FunctionCallingAgentWorker(
    tools=[financial_summary_tool, daily_balances_tool, spending_conditions_tool, client_spending_tool],
    llm=llm,
    prefix_messages=[ChatMessage(content="You are now AI assistant for a Banking app for a given client to understand financial overview.", role="system")],
    verbose=True,
    allow_parallel_tool_calls=True
)

# Wrap the worker in the top-level planner
agent = StructuredPlannerAgent(
    agent_worker=worker,
    tools=[financial_summary_tool, daily_balances_tool, spending_conditions_tool, client_spending_tool],
    verbose=True
)

# Apply nest_asyncio
nest_asyncio.apply()

# Run the agent
initiator_prompt = """
You are an AI assistant for a banking application. Your purpose is to help clients understand their financial overview and provide personalized advice for financial well-being.
Based on your analysis, create 'What If' scenarios that could improve the client's financial situation. Consider both short-term actions and their long-term impacts.
Develop personalized ideas for financial well-being. These should be tailored to the client's specific financial situation and goals.
For each suggestion, demonstrate how it would affect the client's finances in the upcoming month. Use the tools to calculate and show the potential impact.
Additionally, showcase the longer-term impact of these actions over a 6-month and 1-year period.
"""

whatif_insight = agent.chat(initiator_prompt)
print(whatif_insight)

这应该有助于通过确保正确检查和处理工具名来减轻 KeyError 的影响。

  • 要继续与 Dosu 交谈,请提及 @dosu。*
zsbz8rwp

zsbz8rwp6#

这给我带来了新的错误:

File ~/Documents/amplify/hermes/utilities/research_rag/venv/lib/python3.9/site-packages/llama_index/core/agent/runner/planner.py:258, in StructuredPlannerAgent.create_plan(self, input, **kwargs)
    256 if self.verbose:
    257     print(f"=== Initial plan ===")
--> 258     for sub_task in plan.sub_tasks:
    259         print(
    260             f"{sub_task.name}:\n{sub_task.input} -> {sub_task.expected_output}\ndeps: {sub_task.dependencies}\n\n"
    261         )
    263 plan_id = str(uuid.uuid4())

AttributeError: 'str' object has no attribute 'sub_tasks'
3gtaxfhh

3gtaxfhh7#

要解决在使用 StructuredPlannerAgent 时出现的 AttributeError: 'str' object has no attribute 'sub_tasks' 错误,请确保如果 initial_plan_promptplan_refine_prompt 作为字符串提供,它们被正确初始化为 PromptTemplate 对象。这个错误很可能是因为在一个期望是 Plan 对象的地方使用了字符串。
以下是处理此错误的 StructuredPlannerAgent 初始化相关部分:

def __init__(
    self,
    agent_worker: BaseAgentWorker,
    tools: Optional[List[BaseTool]] = None,
    tool_retriever: Optional[ObjectRetriever[BaseTool]] = None,
    chat_history: Optional[List[ChatMessage]] = None,
    state: Optional[PlannerAgentState] = None,
    memory: Optional[BaseMemory] = None,
    llm: Optional[LLM] = None,
    initial_plan_prompt: Union[str, PromptTemplate] = DEFAULT_INITIAL_PLAN_PROMPT,
    plan_refine_prompt: Union[str, PromptTemplate] = DEFAULT_PLAN_REFINE_PROMPT,
    callback_manager: Optional[CallbackManager] = None,
    init_task_state_kwargs: Optional[dict] = None,
    delete_task_on_finish: bool = False,
    default_tool_choice: str = "auto",
    verbose: bool = False,
) -> None:
    """Initialize."""
    self.agent_worker = agent_worker
    self.state = state or PlannerAgentState()
    self.memory = memory or ChatMemoryBuffer.from_defaults(chat_history, llm=llm)
    self.tools = tools
    self.tool_retriever = tool_retriever
    self.llm = llm or Settings.llm

    if isinstance(initial_plan_prompt, str):
        initial_plan_prompt = PromptTemplate(initial_plan_prompt)
    self.initial_plan_prompt = initial_plan_prompt

    if isinstance(plan_refine_prompt, str):
        plan_refine_prompt = PromptTemplate(plan_refine_prompt)
    self.plan_refine_prompt = plan_refine_prompt

    # get and set callback manager
    if callback_manager is not None:
        self.agent_worker.set_callback_manager(callback_manager)
        self.callback_manager = callback_manager
    else:
        if hasattr(self.agent_worker, "callback_manager"):
            self.callback_manager = (
                self.agent_worker.callback_manager or CallbackManager()
            )
        else:
            self.callback_manager = Settings.callback_manager
    self.init_task_state_kwargs = init_task_state_kwargs or {}
    self.delete_task_on_finish = delete_task_on_finish
    self.default_tool_choice = default_tool_choice
    self.verbose = verbose

确保如果 initial_plan_promptplan_refine_prompt 作为字符串提供,它们被正确转换为 PromptTemplate 对象。这应该解决 AttributeError 问题。
此外,确保 StructuredPlannerAgent 能够正确地将初始输入分解为子任务。每个子任务应由一个输入、预期结果以及任何应首先完成的依赖子任务表示。以下是如何创建和使用 StructuredPlannerAgent 的示例:

from llama_index.core.agent import (
    StructuredPlannerAgent,
    FunctionCallingAgentWorker,
    ReActAgentWorker,
)

# create the function calling worker for reasoning
worker = FunctionCallingAgentWorker.from_tools(
    [financial_summary_tool, daily_balances_tool, spending_conditions_tool, client_spending_tool], verbose=True
)

# wrap the worker in the top-level planner
agent = StructuredPlannerAgent(
    worker, tools=[financial_summary_tool, daily_balances_tool, spending_conditions_tool, client_spending_tool], verbose=True
)

# Give the agent a complex task
response = agent.chat(
    "You are an AI assistant for a banking application. Your purpose is to help clients understand their financial overview and provide personalized advice for financial well-being. Based on your analysis, create 'What If' scenarios that could improve the client's financial situation. Consider both short-term actions and their long-term impacts. Develop personalized ideas for financial well-being. These should be tailored to the client's specific financial situation and goals. For each suggestion, demonstrate how it would affect the client's finances in the upcoming month. Use the tools to calculate and show the potential impact. Additionally, showcase the longer-term impact of these actions over a 6-month and 1-year period."
)

print(str(response))

通过确保提示正确初始化并代理正确分解任务,您应该能够避免 AttributeError1(https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/agent/runner/planner.py)2(https://github.com/run-llama/llama_index/blob/main/docs/docs/examples/agent/structured_planner.ipynb)3(https://github.com/run-llama/llama_index/blob/main/llama-index-core/tests/agent/runner/test_planner.py) 错误。

  • 要继续与 Dosu 对话,请提及 @dosu。*

相关问题