Replies: 1 comment 2 replies
-
|
In Python, context manager convention is to clean up resource. The confusion here is that within the AzureAIAgentClient context, the agent gets cleaned up -- this is indeed confusing. I think it makes sense if we use context manager for the agent, it cleans up the agent. But with context manager created with the client, then it shouldn't clean up the agent. Specifically, I think this pattern should automatically clean up the agent: # Since no Agent ID is provided, the agent will be automatically created
# and deleted after getting a response
# For authentication, run `az login` command in terminal or replace AzureCliCredential with preferred
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
name="WeatherAgent",
instructions="You are a helpful weather agent.",
tools=get_weather,
) as agent,
):
query = "What's the weather like in Seattle?"
print(f"User: {query}")
result = await agent.run(query)
print(f"Agent: {result}\n")However, this pattern should not clean up the agent automatically: async with AzureAIAgentClient(async_credential=credential) as chat_client:
agent = chat_client.create_agent(
name="CodingAgent",
instructions=(
"You are a helpful assistant that can write and execute Python code to solve problems."
),
tools=HostedCodeInterpreterTool(),
)
await run_agent_task(agent) |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Why does the following code not persist my Agent and thread in foundry? I consider this an anit-pattern as it's destroying the agent and threads automatically. In earlier versions the behavior was not to auto-delete the agent. This seems destructive in nature and I would expect the developer would have to clean-up. Is there a config setting that I can toggle to force the agent and all artifacts to be persisted? Why does it persist in one example and not the other? Why do I have to use ChatAgent to persist the Agent and it's artifacts? I see no way to do this when using .create_agent().
I managed to do some more testing and found that I can only persist agents if I use ChatAgent. If I use chat_client.create_agent() the agent will not be persisted. Why is this? Here is some code that works, but why do I have to use ChatAgent to get my agent to persist? The code below has a toggle that allows me to persist or not to persist.
Beta Was this translation helpful? Give feedback.
All reactions