Python - Plugins to be async #12772
Closed
akozhikkot
started this conversation in
General
Replies: 1 comment
-
Hi @akozhikkot, definitely - all of SK is async focused, so yes, plugins that can contain async kernel functions work well. Most of our plugin samples are so simple, we've just made them sync, but you can see the following works: # Copyright (c) Microsoft. All rights reserved.
import asyncio
from semantic_kernel.agents import ChatCompletionAgent, ChatHistoryAgentThread
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
from semantic_kernel.functions import kernel_function
class WeatherPlugin:
@kernel_function(description="Provides the weather for the requested city.")
async def get_weather(self, city: str) -> str:
await asyncio.sleep(1.0) # Simulate a delay for some underlying processing
return f"The weather in {city} is sunny!"
# Simulate a conversation with the agent
USER_INPUTS = ["What is the weather like today in Seattle?"]
async def main():
# 1. Create the agent
agent = ChatCompletionAgent(
service=AzureChatCompletion(), # This assumes you have the necessary vars set in the .env file
name="WeatherAgent",
instructions="Answer questions about the weather.",
plugins=[WeatherPlugin()],
)
# 2. Create a thread to hold the conversation
# If no thread is provided, a new thread will be
# created and returned with the initial response
thread: ChatHistoryAgentThread = None
for user_input in USER_INPUTS:
print(f"# User: {user_input}")
# 4. Invoke the agent for a response
response = await agent.get_response(messages=user_input, thread=thread)
print(f"# {response.name}: {response} ")
thread = response.thread
# 4. Cleanup: Clear the thread
await thread.delete() if thread else None
if __name__ == "__main__":
asyncio.run(main()) I see the following output:
|
Beta Was this translation helpful? Give feedback.
0 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.
-
Is it possible to have kernel functions as async ones ?
Beta Was this translation helpful? Give feedback.
All reactions