|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +import asyncio |
| 4 | + |
| 5 | +from semantic_kernel.agents import AgentGroupChat, ChatCompletionAgent |
| 6 | +from semantic_kernel.agents.open_ai import OpenAIAssistantAgent |
| 7 | +from semantic_kernel.agents.open_ai.azure_assistant_agent import AzureAssistantAgent |
| 8 | +from semantic_kernel.connectors.ai.open_ai.services.azure_chat_completion import AzureChatCompletion |
| 9 | +from semantic_kernel.contents.annotation_content import AnnotationContent |
| 10 | +from semantic_kernel.contents.chat_message_content import ChatMessageContent |
| 11 | +from semantic_kernel.contents.utils.author_role import AuthorRole |
| 12 | +from semantic_kernel.kernel import Kernel |
| 13 | + |
| 14 | +##################################################################### |
| 15 | +# The following sample demonstrates how to create an OpenAI # |
| 16 | +# assistant using either Azure OpenAI or OpenAI, a chat completion # |
| 17 | +# agent and have them participate in a group chat working with # |
| 18 | +# image content. # |
| 19 | +##################################################################### |
| 20 | + |
| 21 | + |
| 22 | +def _create_kernel_with_chat_completion(service_id: str) -> Kernel: |
| 23 | + kernel = Kernel() |
| 24 | + kernel.add_service(AzureChatCompletion(service_id=service_id)) |
| 25 | + return kernel |
| 26 | + |
| 27 | + |
| 28 | +async def invoke_agent( |
| 29 | + chat: AgentGroupChat, agent: ChatCompletionAgent | OpenAIAssistantAgent, input: str | None = None |
| 30 | +) -> None: |
| 31 | + """Invoke the agent with the user input.""" |
| 32 | + if input: |
| 33 | + await chat.add_chat_message(message=ChatMessageContent(role=AuthorRole.USER, content=input)) |
| 34 | + print(f"# {AuthorRole.USER}: '{input}'") |
| 35 | + |
| 36 | + async for content in chat.invoke(agent=agent): |
| 37 | + print(f"# {content.role} - {content.name or '*'}: '{content.content}'") |
| 38 | + if len(content.items) > 0: |
| 39 | + for item in content.items: |
| 40 | + if isinstance(item, AnnotationContent): |
| 41 | + print(f"\n`{item.quote}` => {item.file_id}") |
| 42 | + response_content = await agent.client.files.content(item.file_id) |
| 43 | + print(response_content.text) |
| 44 | + |
| 45 | + |
| 46 | +async def main(): |
| 47 | + try: |
| 48 | + ANALYST_NAME = "Analyst" |
| 49 | + ANALYST_INSTRUCTIONS = "Create charts as requested without explanation." |
| 50 | + analyst_agent = await AzureAssistantAgent.create( |
| 51 | + kernel=Kernel(), |
| 52 | + enable_code_interpreter=True, |
| 53 | + name=ANALYST_NAME, |
| 54 | + instructions=ANALYST_INSTRUCTIONS, |
| 55 | + ) |
| 56 | + |
| 57 | + SUMMARIZER_NAME = "Summarizer" |
| 58 | + SUMMARIZER_INSTRUCTIONS = "Summarize the entire conversation for the user in natural language." |
| 59 | + service_id = "summary" |
| 60 | + summary_agent = ChatCompletionAgent( |
| 61 | + service_id=service_id, |
| 62 | + kernel=_create_kernel_with_chat_completion(service_id=service_id), |
| 63 | + instructions=SUMMARIZER_INSTRUCTIONS, |
| 64 | + name=SUMMARIZER_NAME, |
| 65 | + ) |
| 66 | + |
| 67 | + chat = AgentGroupChat() |
| 68 | + |
| 69 | + await invoke_agent( |
| 70 | + chat=chat, |
| 71 | + agent=analyst_agent, |
| 72 | + input=""" |
| 73 | + Graph the percentage of storm events by state using a pie chart: |
| 74 | +
|
| 75 | + State, StormCount |
| 76 | + TEXAS, 4701 |
| 77 | + KANSAS, 3166 |
| 78 | + IOWA, 2337 |
| 79 | + ILLINOIS, 2022 |
| 80 | + MISSOURI, 2016 |
| 81 | + GEORGIA, 1983 |
| 82 | + MINNESOTA, 1881 |
| 83 | + WISCONSIN, 1850 |
| 84 | + NEBRASKA, 1766 |
| 85 | + NEW YORK, 1750 |
| 86 | + """, |
| 87 | + ) |
| 88 | + await invoke_agent(chat=chat, agent=summary_agent) |
| 89 | + finally: |
| 90 | + if analyst_agent is not None: |
| 91 | + [await analyst_agent.delete_file(file_id=file_id) for file_id in analyst_agent.code_interpreter_file_ids] |
| 92 | + await analyst_agent.delete() |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + asyncio.run(main()) |
0 commit comments