ChatContext
can be used to manage the flow of conversations in a more structured and secure way.
By using ChatContext
, we can track and control the sequence of function calls during a conversation. This is particularly useful in situations where one task must be completed before another — for example, ensuring that a user logs in before they can check their account balance. This approach helps to prevent errors and enhances the security of the system.
Benefits of Using ChatContext
:
AG2
, simply run the following command:
autogen
or ag2
, all you need to do is upgrade it using:autogen
and ag2
are aliases for the same PyPI package.Account
class and helper functions are adapted from
the Tools with Dependency Injection post. They define the structure for securely handling
account data and operations like login and balance retrieval.
llm_config
defines the LLM configurations, including the model
and API key.UserProxyAgent
simulates user inputs without requiring actual
human interaction (set to NEVER
).AssistantAgent
represents the AI agent, configured with the LLM
settings.ChatContext
parameter. This enhancement allows us to enforce
proper execution order in the workflow, ensuring that users log in
before accessing sensitive data like account balances.
The following functions will be registered:
login
: Verifies the user’s credentials and ensures they are logged
in.get_balance
: Retrieves the account balance but only if the user
has successfully logged in first.user_proxy.initiate_chat(agent, message="Get users balance", max_turns=4)
is called, the following happens:
"Get users balance"
, prompting the agent to suggest calling get_balance
.get_balance
, but since the user isn’t logged in, it raises an error: "Please login first"
. The agent then suggests calling login
to authenticate the user.login
function is executed, confirming the user’s login with "You are logged in"
.get_balance
and retrieves the balance."Your balance is 200 USD"
, confirming the correct execution of the workflow.ChatContext
as an extension of dependency injection, showing how it simplifies and secures workflows in conversational AI by ensuring tasks like login happen before accessing sensitive data, improving both functionality and security.
Highlights:
ChatContext
can ensure that functions are executed in the correct sequence, reducing the risk of logical errors.ChatContext
, developers can trace conversations, making it easier to identify and resolve issues.ChatContext
and employing dependency injection principles, you can build robust, secure, and user-friendly conversational workflows.