|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | +import asyncio |
| 3 | +from typing import Annotated |
| 4 | + |
| 5 | +from semantic_kernel.agents.open_ai import AzureAssistantAgent, OpenAIAssistantAgent |
| 6 | +from semantic_kernel.contents.chat_message_content import ChatMessageContent |
| 7 | +from semantic_kernel.contents.utils.author_role import AuthorRole |
| 8 | +from semantic_kernel.functions.kernel_function_decorator import kernel_function |
| 9 | +from semantic_kernel.kernel import Kernel |
| 10 | + |
| 11 | +##################################################################### |
| 12 | +# The following sample demonstrates how to create an OpenAI # |
| 13 | +# assistant using either Azure OpenAI or OpenAI. OpenAI Assistants # |
| 14 | +# allow for function calling, the use of file search and a # |
| 15 | +# code interpreter. Assistant Threads are used to manage the # |
| 16 | +# conversation state, similar to a Semantic Kernel Chat History. # |
| 17 | +# This sample also demonstrates the Assistants Streaming # |
| 18 | +# capability and how to manage an Assistants chat history. # |
| 19 | +##################################################################### |
| 20 | + |
| 21 | +HOST_NAME = "Host" |
| 22 | +HOST_INSTRUCTIONS = "Answer questions about the menu." |
| 23 | + |
| 24 | +# Note: you may toggle this to switch between AzureOpenAI and OpenAI |
| 25 | +use_azure_openai = True |
| 26 | + |
| 27 | + |
| 28 | +# Define a sample plugin for the sample |
| 29 | +class MenuPlugin: |
| 30 | + """A sample Menu Plugin used for the concept sample.""" |
| 31 | + |
| 32 | + @kernel_function(description="Provides a list of specials from the menu.") |
| 33 | + def get_specials(self) -> Annotated[str, "Returns the specials from the menu."]: |
| 34 | + return """ |
| 35 | + Special Soup: Clam Chowder |
| 36 | + Special Salad: Cobb Salad |
| 37 | + Special Drink: Chai Tea |
| 38 | + """ |
| 39 | + |
| 40 | + @kernel_function(description="Provides the price of the requested menu item.") |
| 41 | + def get_item_price( |
| 42 | + self, menu_item: Annotated[str, "The name of the menu item."] |
| 43 | + ) -> Annotated[str, "Returns the price of the menu item."]: |
| 44 | + return "$9.99" |
| 45 | + |
| 46 | + |
| 47 | +# A helper method to invoke the agent with the user input |
| 48 | +async def invoke_agent( |
| 49 | + agent: OpenAIAssistantAgent, thread_id: str, input: str, history: list[ChatMessageContent] |
| 50 | +) -> None: |
| 51 | + """Invoke the agent with the user input.""" |
| 52 | + message = ChatMessageContent(role=AuthorRole.USER, content=input) |
| 53 | + await agent.add_chat_message(thread_id=thread_id, message=message) |
| 54 | + |
| 55 | + # Add the user message to the history |
| 56 | + history.append(message) |
| 57 | + |
| 58 | + print(f"# {AuthorRole.USER}: '{input}'") |
| 59 | + |
| 60 | + first_chunk = True |
| 61 | + async for content in agent.invoke_stream(thread_id=thread_id, messages=history): |
| 62 | + if content.role != AuthorRole.TOOL: |
| 63 | + if first_chunk: |
| 64 | + print(f"# {content.role}: ", end="", flush=True) |
| 65 | + first_chunk = False |
| 66 | + print(content.content, end="", flush=True) |
| 67 | + print() |
| 68 | + |
| 69 | + |
| 70 | +async def main(): |
| 71 | + # Create the instance of the Kernel |
| 72 | + kernel = Kernel() |
| 73 | + |
| 74 | + # Add the sample plugin to the kernel |
| 75 | + kernel.add_plugin(plugin=MenuPlugin(), plugin_name="menu") |
| 76 | + |
| 77 | + # Create the OpenAI Assistant Agent |
| 78 | + service_id = "agent" |
| 79 | + if use_azure_openai: |
| 80 | + agent = await AzureAssistantAgent.create( |
| 81 | + kernel=kernel, service_id=service_id, name=HOST_NAME, instructions=HOST_INSTRUCTIONS |
| 82 | + ) |
| 83 | + else: |
| 84 | + agent = await OpenAIAssistantAgent.create( |
| 85 | + kernel=kernel, service_id=service_id, name=HOST_NAME, instructions=HOST_INSTRUCTIONS |
| 86 | + ) |
| 87 | + |
| 88 | + thread_id = await agent.create_thread() |
| 89 | + |
| 90 | + history: list[ChatMessageContent] = [] |
| 91 | + |
| 92 | + try: |
| 93 | + await invoke_agent(agent, thread_id=thread_id, input="Hello", history=history) |
| 94 | + await invoke_agent(agent, thread_id=thread_id, input="What is the special soup?", history=history) |
| 95 | + await invoke_agent(agent, thread_id=thread_id, input="What is the special drink?", history=history) |
| 96 | + await invoke_agent(agent, thread_id=thread_id, input="Thank you", history=history) |
| 97 | + finally: |
| 98 | + await agent.delete_thread(thread_id) |
| 99 | + await agent.delete() |
| 100 | + |
| 101 | + # You may then view the conversation history |
| 102 | + print("========= Conversation History =========") |
| 103 | + for content in history: |
| 104 | + if content.role != AuthorRole.TOOL: |
| 105 | + print(f"# {content.role}: {content.content}") |
| 106 | + print("========= End of Conversation History =========") |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + asyncio.run(main()) |
0 commit comments