Find answers from the community

Updated 2 months ago

when I create an instance of FunctionCallingAgentWorker with some tools, is there a way to

when I create an instance of FunctionCallingAgentWorker with some tools, is there a way to add more tools after creating this instance? Can i manipulate tool_retriever somehow?
k
t
11 comments
Please stand by @tomi as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
Based on the provided knowledge sources, there is no direct method to add more tools to an instance of FunctionCallingAgentWorker after it has been created. The FunctionCallingAgentWorker is initialized with a set of tools or a tool_retriever, and these are not designed to be modified after the instance has been created.

However, there is a way to work around this by using a tool_retriever. The tool_retriever is an instance of ObjectRetriever that can retrieve tools based on some criteria. If you initialize the FunctionCallingAgentWorker with a tool_retriever, you can potentially modify the underlying data of the tool_retriever to change the tools that are available.

Here is an example of creating a FunctionCallingAgentWorker with a tool_retriever:

Plain Text
from llama_index.core import VectorStoreIndex
from llama_index.core.objects import ObjectIndex

# define an "object" index and retriever over these tools
obj_index = ObjectIndex.from_objects(
    all_tools,
    index_cls=VectorStoreIndex,
)

obj_retriever = obj_index.as_retriever(similarity_top_k=3)

agent_worker = FunctionCallingAgentWorker.from_tools(
    tool_retriever=obj_retriever,
    llm=vertex_gemini,
    system_prompt="""   
    You are an agent designed to answer queries over a set of given papers.
    Please use the tools provided to answer a question as possible. Do not rely on prior knowledge. Summarize your answer\
    """,
    verbose=True,
)
agent = AgentRunner(agent_worker)

In this example, all_tools is a list of all the tools you want to use. If you want to add more tools, you can modify all_tools and recreate obj_index, obj_retriever, and agent_worker.

Please note that this is a workaround and may not be the best practice. It's always recommended to initialize the FunctionCallingAgentWorker with all the tools you plan to use.
For more details, you can refer to the LlamaIndex documentation ().
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai could i make all_tools dynamic by "passing by reference"?
@kapa.ai what are your thoughts
Let me check the knowledge base for you. One moment! :books:
----------------------
As an AI, I don't have personal thoughts or opinions. However, I'm here to provide information and answer questions to the best of my ability based on the information provided to me. If you have any more questions about LlamaIndex or any other topic, feel free to ask!
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai could i make all_tools dynamic by "passing by reference"?
Let me check the knowledge base for you. One moment! :books:
----------------------
In Python, all variables are passed by reference. However, the way they are treated can make it seem like they are passed by value. For mutable objects like lists, changes you make inside a function will affect the original object. However, if you reassign the variable inside the function, it won't affect the original object.

In the context of LlamaIndex, the all_tools list is used to create an ObjectIndex and then a tool_retriever. If you modify the all_tools list after this (e.g., by appending more tools), it won't affect the ObjectIndex or the tool_retriever because they have already been created with the original list.

If you want to make all_tools dynamic, you would need to create a new ObjectIndex and tool_retriever every time you modify all_tools. Here's an example:

Plain Text
all_tools.append(new_tool)

obj_index = ObjectIndex.from_objects(
    all_tools,
    index_cls=VectorStoreIndex,
)

obj_retriever = obj_index.as_retriever(similarity_top_k=3)

agent_worker = FunctionCallingAgentWorker.from_tools(
    tool_retriever=obj_retriever,
    llm=vertex_gemini,
    system_prompt="""   
    You are an agent designed to answer queries over a set of given papers.
    Please use the tools provided to answer a question as possible. Do not rely on prior knowledge. Summarize your answer\
    """,
    verbose=True,
)
agent = AgentRunner(agent_worker)


In this example, new_tool is the tool you want to add. You would need to run this code every time you want to add a new tool.

Please note that this could be inefficient if you are frequently adding new tools. It might be better to gather all the tools you want to use before creating the ObjectIndex and tool_retriever.
For more details, you can refer to the LlamaIndex documentation ().
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord