You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tools/function-tools.md
+81-31Lines changed: 81 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -58,62 +58,111 @@ While you have considerable flexibility in defining your function, remember that
58
58
59
59
Designed for tasks that require a significant amount of processing time without blocking the agent's execution. This tool is a subclass of `FunctionTool`.
60
60
61
-
When using a `LongRunningFunctionTool`, your Python function can initiate the long-running operation and optionally return an **intermediate result** to keep the model and user informed about the progress. The agent can then continue with other tasks. An example is the human-in-the-loop scenario where the agent needs human approval before proceeding with a task.
61
+
When using a `LongRunningFunctionTool`, your Python function can initiate the long-running operation and optionally return an **initial result**** (e.g. the long-running operation id). Once a long running function tool is invoked the agent runner will pause the agent run and let the agent client to decide whether to continue or wait until the long-running operation finishes. The agent client can query the progress of the long-running operation and send back an intermediate or final response. The agent can then continue with other tasks. An example is the human-in-the-loop scenario where the agent needs human approval before proceeding with a task.
62
62
63
63
### How it Works
64
64
65
-
You wrap a Python *generator*function (a function using `yield`) with `LongRunningFunctionTool`.
65
+
You wrap a Python function with LongRunningFunctionTool.
66
66
67
-
1.**Initiation:** When the LLM calls the tool, your generator function starts executing.
67
+
1.**Initiation:** When the LLM calls the tool, your python function starts the long-running operation.
68
68
69
-
2.**Intermediate Updates (`yield`):** Your function should yield intermediate Python objects (typically dictionaries) periodically to report progress. The ADK framework takes each yielded value and sends it back to the LLM packaged within a `FunctionResponse`. This allows the LLM to inform the user (e.g., status, percentage complete, messages).
69
+
2.**Initial Updates:** Your function should optionally return an initial result (e.g. the long-running operaiton id). The ADK framework takes the result and sends it back to the LLM packaged within a `FunctionResponse`. This allows the LLM to inform the user (e.g., status, percentage complete, messages). And then the agent run is ended / paused.
70
70
71
-
3.**Completion (`return`):**When the task is finished, the generator function uses `return`to provide the final Python object result.
71
+
3.**Continue or Wait:**After each agent run is completed. Agent client can query the progress of the long-running operation and decide whether to continue the agent run with an intermediate response (to update the progress) or wait until a final response is retrieved. Agent client should send the intermediate or final response back to the agent for the next run.
72
72
73
-
4.**Framework Handling:** The ADK framework manages the execution. It sends each yielded value back as an intermediate `FunctionResponse`. When the generator completes, the framework sends the returned value as the content of the final `FunctionResponse`, signaling the end of the long-running operation to the LLM.
73
+
4.**Framework Handling:** The ADK framework manages the execution. It sends the intermediate or final `FunctionResponse` sent by agent client to the LLM to generate a user friendly message.
74
74
75
75
### Creating the Tool
76
76
77
-
Define your generator function and wrap it using the `LongRunningFunctionTool` class:
77
+
Define your tool function and wrap it using the `LongRunningFunctionTool` class:
78
78
79
79
```py
80
80
from google.adk.tools import LongRunningFunctionTool
81
81
82
-
# Define your generator function (see example below)
83
-
defmy_long_task_generator(*args, **kwargs):
84
-
# ... setup ...
85
-
yield {"status": "pending", "message": "Starting task..."} # Framework sends this as FunctionResponse
86
-
# ... perform work incrementally ...
87
-
yield {"status": "pending", "progress": 50} # Framework sends this as FunctionResponse
88
-
# ... finish work ...
89
-
return {"status": "completed", "result": "Final outcome"} # Framework sends this as final FunctionResponse
82
+
# Define your long running function (see example below)
Yielding structured Python objects (like dictionaries) is crucial for providing meaningful updates. Include keys like:
97
+
Agent client received an event with long running function calls and check the status of the ticket. Then Agent client can send the intermediate or final response back to update the progress. The framework packages this value (even if it's None) into the content of the `FunctionResponse` sent back to the LLM.
if text :=''.join(part.text or''for part in event.content.parts):
158
+
print(f'[{event.author}]: {text}')
159
+
```
110
160
111
-
The Python object your generator function returns is considered the final result of the tool execution. The framework packages this value (even if it's None) into the content of the final `FunctionResponse` sent back to the LLM, indicating the tool execution is complete.
0 commit comments