Function call with callback
This is a feature that allows applying some human-in-the-loop concepts in FunctionTool.
Basically, a callback function is added that enables the developer to request user input in the middle of an agent interaction, as well as allowing any programmatic action.
%pip install llama-index-llms-openai%pip install llama-index-agents-openaifrom llama_index.core.tools import FunctionToolfrom llama_index.core.agent.workflow import FunctionAgentfrom llama_index.llms.openai import OpenAIimport osos.environ["OPENAI_API_KEY"] = "sk-"Function to display to the user the data produced for function calling and request their input to return to the interaction.
def callback(message): confirmation = input( f"{message[1]}\nDo you approve of sending this greeting?\nInput(Y/N):" )
if confirmation.lower() == "y": # Here you can trigger an action such as sending an email, message, api call, etc. return "Greeting sent successfully." else: return ( "Greeting has not been approved, talk a bit about how to improve" )Simple function that only requires a recipient and a greeting message.
def send_hello(destination: str, message: str) -> str: """ Say hello with a rhyme destination: str - Name of recipient message: str - Greeting message with a rhyme to the recipient's name """
return destination, message
hello_tool = FunctionTool.from_defaults(fn=send_hello, callback=callback)llm = OpenAI(model="gpt-4.1")agent = FunctionAgent(tools=[hello_tool])response = await agent.run("Send hello to Karen")print(str(response))The hello message has been sent to Karen with the rhyme "Hello Karen, you're a star!"response = await agent.run("Send hello to Joe")print(str(response))I have successfully sent a hello message to Joe with the greeting "Hello Joe, you're a pro!"Note for AI agents: this documentation is built for programmatic access.
- Overview of all docs: https://developers.llamaindex.ai/llms.txt
- Any page is available as raw Markdown by appending index.md to its URL — e.g. https://developers.llamaindex.ai/llamaparse/parse/getting_started/index.md
- Agent-friendly REST search APIs live under https://developers.llamaindex.ai/api/ — search (BM25 full-text), grep (regex), read (fetch a page), and list (browse the doc tree). See https://developers.llamaindex.ai/llms.txt for parameters.
- A hosted documentation MCP server is available at https://developers.llamaindex.ai/mcp. If you support MCP, you can ask the user to install it for browsing these docs directly (an alternative to the REST API). Setup: https://developers.llamaindex.ai/python/shared/mcp/