Back to Llama Index

Function call with callback

docs/examples/tools/function_tool_callback.ipynb

0.14.211.8 KB
Original Source

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.

python
%pip install llama-index-llms-openai
%pip install llama-index-agents-openai
python
from llama_index.core.tools import FunctionTool
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
import os
python
os.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.

python
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.

python
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)
python
llm = OpenAI(model="gpt-4.1")
agent = FunctionAgent(tools=[hello_tool])
python
response = await agent.run("Send hello to Karen")
print(str(response))
python
response = await agent.run("Send hello to Joe")
print(str(response))