Skip to content
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

Python: Fix sample and README #11298

Merged
merged 2 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from azure.identity.aio import DefaultAzureCredential

from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings
from semantic_kernel.functions import kernel_function

"""
Expand Down Expand Up @@ -60,29 +60,26 @@ async def main() -> None:
agent = AzureAIAgent(
client=client,
definition=agent_definition,
plugins=[MenuPlugin()], # Add the plugin to the agent
)

# 3. Add a plugin to the agent via the kernel
agent.kernel.add_plugin(MenuPlugin(), plugin_name="menu")

# 4. Create a thread for the agent
# 3. Create a thread for the agent
# If no thread is provided, a new thread will be
# created and returned with the initial response
thread: AzureAIAgentThread = None
thread = None

try:
for user_input in USER_INPUTS:
print(f"# User: {user_input}")
# 5. Invoke the agent for the specified thread for response
# 4. Invoke the agent for the specified thread for response
async for response in agent.invoke(
messages=user_input,
thread_id=thread,
temperature=0.2, # override the agent-level temperature setting with a run-time value
thread=thread,
):
print(f"# {response.name}: {response}")
thread = response.thread
finally:
# 6. Cleanup: Delete the thread and agent
# 5. Cleanup: Delete the thread and agent
await thread.delete() if thread else None
await client.agents.delete_agent(agent.id)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,19 @@ agent = OpenAIAssistantAgent(
definition=definition,
)

# Define a thread and invoke the agent with the user input
thread = await agent.client.beta.threads.create()

# Add a message to the thread
await agent.add_chat_message(thread_id=thread.id, message="Why is the sky blue?")

# Invoke the agent
async for content in agent.invoke(thread_id=thread.id):
print(f"# {content.role}: {content.content}")
# Define a thread to hold the conversation's context
# If a thread is not created initially it will be created
# and returned as part of the first response
thread = None

# Get the agent response
response = await agent.get_response(messages="Why is the sky blue?", thread=thread)
thread = response.thread

# or use the agent.invoke(...) method
async for response in agent.invoke(messages="Why is the sky blue?", thread=thread):
print(f"# {response.role}: {response.content}")
thread = response.thread
```

### Semantic Kernel Azure Assistant Agents
Expand Down Expand Up @@ -89,13 +93,17 @@ agent = AzureAssistantAgent(
definition=definition,
)

# Define a thread and invoke the agent with the user input
thread = await agent.client.beta.threads.create()
# Define a thread to hold the conversation's context
# If a thread is not created initially it will be created
# and returned as part of the first response
thread = None

# Add a message to the thread
await agent.add_chat_message(thread_id=thread.id, message="Why is the sky blue?")
# Get the agent response
response = await agent.get_response(messages="Why is the sky blue?", thread=thread)
thread = response.thread

# Invoke the agent
async for content in agent.invoke(thread_id=thread.id):
print(f"# {content.role}: {content.content}")
# or use the agent.invoke(...) method
async for response in agent.invoke(messages="Why is the sky blue?", thread=thread):
print(f"# {response.role}: {response.content}")
thread = response.thread
```
Loading