-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
3
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.
+189
−8
Open
Function tool callback #16637
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,162 @@ | ||
{ | ||
"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": 7, | ||
"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": 8, | ||
"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": 17, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def callback(message):\n", | ||
"\n", | ||
" confirmation = input(f\"{message[1]}\\nDo you approve of sending this greeting?\\nInput(Y/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 \"Greeting has not been approved, talk a bit about how to improve\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Simple function that only requires a recipient and a greeting message." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 18, | ||
"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", | ||
"hello_tool = FunctionTool.from_defaults(fn=send_hello, callback=callback)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 19, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"llm = OpenAI()\n", | ||
"agent = OpenAIAgent.from_tools([hello_tool])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 20, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"I attempted to send a hello message to Karen, but it seems the greeting has not been approved. Let's try to come up with a different greeting that might be more suitable. How about \"Hello Karen, your smile shines like the sun\"? Let's send this message instead.\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"response = agent.chat(\"Send hello to Karen\")\n", | ||
"print(str(response))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 21, | ||
"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", | ||
"version": "3.12.2" | ||
} | ||
}, | ||
"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.
Probably the callback should be async? Otherwise this will block the event loop (probably not ideal)
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.
In my view, yes. We developed this feature so that when a FunctionTool is called, it can request user input that will influence the result or execution of that function.
That said, it makes sense for it not to be asynchronous. However, in our case, we use synchronous calls. If you believe it should be handled asynchronously, I can change it without any problems.
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 think in a lot of use cases, people are using something like fastapi to serve APIs, and you wouldn't want this callback to halt the entire server.
It probably makes sense to let the user provide either a sync or async callback, and llama-index handles converting it either way (if a sync function is provided, we can make it a "fake" async function with a wrapper. If an async function is provided, we can make it sync using
from llama_index.core.utils import asyncio_run
and usingcallback_output = asyncio.run(async_fn(tool_output))
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.
Stopping to evaluate this point, the person who uses an asynchronous call in this case really doesn't make sense conceptually to stop the application.
So whoever uses the asynchronous callback in the asynchronous call will be in a different situation.
I will make the adjustment