-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Function tool callback #16637
Open
JoseLuckmann
wants to merge
12
commits into
run-llama:main
Choose a base branch
from
JoseLuckmann:function_tool_callback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Function tool callback #16637
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5a99127
Add callback suport to Function Tool
a420154
Adjustment to remove callback from langchain function tool
81116ea
Merge branch 'run-llama:main' into function_tool_callback
JoseLuckmann 8443a5b
Refactor langchain tool kwargs processing in BaseTool
5c5eea6
Merge branch 'main' into function_tool_callback
JoseLuckmann 2cf1a05
Merge branch 'function_tool_callback' of https://github.com/JoseLuckm…
6488fb6
Merge branch 'main' into function_tool_callback
logan-markewich 48c8ac1
Merge branch 'run-llama:main' into function_tool_callback
JoseLuckmann 56d4e3b
Linting and Typing
4854be3
Typing adjustment
fca0752
Merge branch 'main' into function_tool_callback
JoseLuckmann 2edf981
Merge branch 'main' into function_tool_callback
JoseLuckmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Function call with callback\n", | ||
"\n", | ||
"This is a feature that allows applying some human-in-the-loop concepts in FunctionTool.\n", | ||
"\n", | ||
"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." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%pip install llama-index-llms-openai\n", | ||
"%pip install llama-index-agents-openai" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from llama_index.core.tools import FunctionTool\n", | ||
"from llama_index.agent.openai import OpenAIAgent\n", | ||
"from llama_index.llms.openai import OpenAI\n", | ||
"import os" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"os.environ[\"OPENAI_API_KEY\"] = \"sk-\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Function to display to the user the data produced for function calling and request their input to return to the interaction." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def callback(message):\n", | ||
" confirmation = input(\n", | ||
" f\"{message[1]}\\nDo you approve of sending this greeting?\\nInput(Y/N):\"\n", | ||
" )\n", | ||
"\n", | ||
" if confirmation.lower() == \"y\":\n", | ||
" # Here you can trigger an action such as sending an email, message, api call, etc.\n", | ||
" return \"Greeting sent successfully.\"\n", | ||
" else:\n", | ||
" return (\n", | ||
" \"Greeting has not been approved, talk a bit about how to improve\"\n", | ||
" )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Simple function that only requires a recipient and a greeting message." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def send_hello(destination: str, message: str) -> str:\n", | ||
" \"\"\"\n", | ||
" Say hello with a rhyme\n", | ||
" destination: str - Name of recipient\n", | ||
" message: str - Greeting message with a rhyme to the recipient's name\n", | ||
" \"\"\"\n", | ||
"\n", | ||
" return destination, message\n", | ||
"\n", | ||
"\n", | ||
"hello_tool = FunctionTool.from_defaults(fn=send_hello, callback=callback)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<bound method FunctionTool.to_langchain_tool of <llama_index.core.tools.function_tool.FunctionTool object at 0x7f7da9fa5670>>" | ||
] | ||
}, | ||
"execution_count": null, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"hello_tool.to_langchain_tool" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"llm = OpenAI()\n", | ||
"agent = OpenAIAgent.from_tools([hello_tool])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"The hello message has been sent to Karen with the rhyme \"Hello Karen, you're a star!\"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"response = agent.chat(\"Send hello to Karen\")\n", | ||
"print(str(response))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"I have successfully sent a hello message to Joe with the greeting \"Hello Joe, you're a pro!\"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"response = agent.chat(\"Send hello to Joe\")\n", | ||
"print(str(response))" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "base", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, looking at this again, this seems rather opinionated 😅 What's the intended use case for this?
I kind of expected the callback to just either be logging something, or outright modifying the tool output in place.
Appending a string like this seems kind of strange
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll explain the use case we applied here to see if it becomes a bit clearer:
I have a tool that sends information to the external system of my company.
The agent is responsible for setting all parameters for the API call, but I can't blindly trust the information constructed by the agent.
So, I use this callback strategy to request user confirmation.
This confirmation returns to the agent's interaction, allowing the agent to decide the next steps.
Here, we apply this rule with interactions between agents and the front end for confirmation, but if you check the example notebook I attached, you can see this flow abstracted in a certain way that's easier to understand.
Basically, I need the callback return to influence the remainder of the agent's flow. I couldn't think of an easier way to adapt the classes without violating any principles. If the user doesn't want the callback to influence the flow, they just don't return anything in the function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like to me a better design might be
Basically the callback either returns a new tool output to override the existing, or returns None and the original tool_output is used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then its up to the user how the callback changes the tool output