Skip to content

Commit 403a62c

Browse files
authored
Python: Fix sample and README (#11298)
### Motivation and Context The AzureAIAgent step1 sample is using a `thread_id` kwarg for `agent.invoke(...)`. Fix this to be `thread` instead, so we can use the created thread for later invokes. For the OpenAIAssistantAgent README, update the `thread` related items to use the new common agent invoke api constructs. <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description Update a sample and a README. <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone 😄
1 parent 5f29dcc commit 403a62c

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

python/samples/getting_started_with_agents/azure_ai_agent/step2_azure_ai_agent_plugin.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from azure.identity.aio import DefaultAzureCredential
77

8-
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread
8+
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings
99
from semantic_kernel.functions import kernel_function
1010

1111
"""
@@ -60,29 +60,26 @@ async def main() -> None:
6060
agent = AzureAIAgent(
6161
client=client,
6262
definition=agent_definition,
63+
plugins=[MenuPlugin()], # Add the plugin to the agent
6364
)
6465

65-
# 3. Add a plugin to the agent via the kernel
66-
agent.kernel.add_plugin(MenuPlugin(), plugin_name="menu")
67-
68-
# 4. Create a thread for the agent
66+
# 3. Create a thread for the agent
6967
# If no thread is provided, a new thread will be
7068
# created and returned with the initial response
71-
thread: AzureAIAgentThread = None
69+
thread = None
7270

7371
try:
7472
for user_input in USER_INPUTS:
7573
print(f"# User: {user_input}")
76-
# 5. Invoke the agent for the specified thread for response
74+
# 4. Invoke the agent for the specified thread for response
7775
async for response in agent.invoke(
7876
messages=user_input,
79-
thread_id=thread,
80-
temperature=0.2, # override the agent-level temperature setting with a run-time value
77+
thread=thread,
8178
):
8279
print(f"# {response.name}: {response}")
8380
thread = response.thread
8481
finally:
85-
# 6. Cleanup: Delete the thread and agent
82+
# 5. Cleanup: Delete the thread and agent
8683
await thread.delete() if thread else None
8784
await client.agents.delete_agent(agent.id)
8885

python/samples/getting_started_with_agents/openai_assistant/README.md

+24-16
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,19 @@ agent = OpenAIAssistantAgent(
4747
definition=definition,
4848
)
4949

50-
# Define a thread and invoke the agent with the user input
51-
thread = await agent.client.beta.threads.create()
52-
53-
# Add a message to the thread
54-
await agent.add_chat_message(thread_id=thread.id, message="Why is the sky blue?")
55-
56-
# Invoke the agent
57-
async for content in agent.invoke(thread_id=thread.id):
58-
print(f"# {content.role}: {content.content}")
50+
# Define a thread to hold the conversation's context
51+
# If a thread is not created initially it will be created
52+
# and returned as part of the first response
53+
thread = None
54+
55+
# Get the agent response
56+
response = await agent.get_response(messages="Why is the sky blue?", thread=thread)
57+
thread = response.thread
58+
59+
# or use the agent.invoke(...) method
60+
async for response in agent.invoke(messages="Why is the sky blue?", thread=thread):
61+
print(f"# {response.role}: {response.content}")
62+
thread = response.thread
5963
```
6064

6165
### Semantic Kernel Azure Assistant Agents
@@ -89,13 +93,17 @@ agent = AzureAssistantAgent(
8993
definition=definition,
9094
)
9195

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

95-
# Add a message to the thread
96-
await agent.add_chat_message(thread_id=thread.id, message="Why is the sky blue?")
101+
# Get the agent response
102+
response = await agent.get_response(messages="Why is the sky blue?", thread=thread)
103+
thread = response.thread
97104

98-
# Invoke the agent
99-
async for content in agent.invoke(thread_id=thread.id):
100-
print(f"# {content.role}: {content.content}")
105+
# or use the agent.invoke(...) method
106+
async for response in agent.invoke(messages="Why is the sky blue?", thread=thread):
107+
print(f"# {response.role}: {response.content}")
108+
thread = response.thread
101109
```

0 commit comments

Comments
 (0)