Created
July 14, 2025 18:06
-
-
Save filmo/14bda48ebf9e313197de1f22505be054 to your computer and use it in GitHub Desktop.
semantic IF-THEN-ELSE loops
This file contains hidden or 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
# set of actions for which a system would require additional confirmation from the user | |
ACTIONS_REQUIRING_CONFIRMATION = [ | |
"saving a file to disk", | |
"executing a file", | |
"stopping a computer process", | |
] | |
# This function takes the original user input and attempts to determine if any actions are being requested | |
# that require human confirmation. This is more flexible the searching for a string literal. | |
async def classify_intent(llm, user_msg: str, actions: list[str]) -> str|None: | |
""" | |
:param llm: which llm object to use for checking the intent. Doesn't necessarily have to be same as main process | |
:param user_msg: What the user is asking to do | |
:param actions: set of sensitive actions that require confirmation | |
:return: If the LLM determines that the text contains an action, it returns the action | |
""" | |
actions_str = "\n".join(f"- {a}" for a in actions) | |
prompt = ( | |
f"User_message: {user_msg}\n" | |
f"Which of the following actions is the user requesting (if any)?\n" | |
f"{actions_str}\n" | |
"If none, answer 'none'. Respond with the exact action string or 'none'." | |
) | |
response = llm.complete(prompt) | |
response = str(response).strip().lower() | |
for action in actions: | |
if action in response: | |
return action | |
if "none" in response: | |
return None | |
return None | |
# the function comments within the """ """ quotes are actually semantically important and directly | |
# read by the agent when checking if a tool performs an action is might want to complete. | |
async def intent_checked_tool(ctx: Context) -> str: | |
""" | |
This tool extracts the user's original request and then uses an llm to see if | |
the intent was one of three actions that require confirmation. | |
The following actions require confirmation: | |
saving a file to disk | |
executing a file | |
stopping a computer process | |
Args: | |
ctx (Context): The llamaIndex Context object containing the user's original message | |
Returns: | |
str: The results of the actions or an indication that no confirmation required. | |
""" | |
user_msg = await ctx.store.get('original_user_msg') | |
# print(f' intent_checked_tool: {user_msg}') | |
action = await classify_intent(llm, user_msg, ACTIONS_REQUIRING_CONFIRMATION) | |
# print(f' classify_intent result: {action}') | |
if action == "saving a file to disk": | |
return await save_file_tool(ctx) | |
elif action == "executing a file": | |
return await execute_file_tool(ctx) | |
elif action == "stopping a computer process": | |
return await stop_process_tool(ctx) | |
else: | |
return "No sensitive action requested." | |
# 2025-06-30 -- The detailed prompt instructing the LLM to use the tool output is necessary to override | |
# built in knowledge like 'I can't save files' or other similar tasks it won't normally perform. | |
workflow = FunctionAgent( | |
tools=[intent_checked_tool], | |
llm=llm, | |
system_prompt="You are a helpful assistant. " | |
"Always run the intent_checked_tool on any user input before proceeding. " | |
"If a tool output is present in the conversation history, " | |
"assume the action was performed and reference the tool output in your response. " | |
"Do not contradict tool outputs." | |
) | |
# more code here taking the users input, which is then checked to see if they're doing something on the "required" | |
# confirmation list. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment