|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Union |
| 4 | +from microsoft.agents.builder import ActivityHandler, MessageFactory, TurnContext |
| 5 | +from microsoft.agents.core.models import ChannelAccount, Attachment |
| 6 | + |
| 7 | +from agents import ( |
| 8 | + Agent as OpenAIAgent, |
| 9 | + Model, |
| 10 | + ModelProvider, |
| 11 | + OpenAIChatCompletionsModel, |
| 12 | + RunConfig, |
| 13 | + Runner, |
| 14 | +) |
| 15 | +from openai import AsyncAzureOpenAI |
| 16 | +from pydantic import BaseModel, Field |
| 17 | + |
| 18 | +from tools.get_weather_tool import get_weather |
| 19 | +from tools.date_time_tool import get_date |
| 20 | + |
| 21 | + |
| 22 | +class WeatherForecastAgentResponse(BaseModel): |
| 23 | + contentType: str = Field(pattern=r"^(Text|AdaptiveCard)$") |
| 24 | + content: Union[dict, str] |
| 25 | + |
| 26 | + |
| 27 | +class WeatherAgent(ActivityHandler): |
| 28 | + def __init__(self, client: AsyncAzureOpenAI): |
| 29 | + self.agent = OpenAIAgent( |
| 30 | + name="WeatherAgent", |
| 31 | + instructions="""" |
| 32 | + You are a friendly assistant that helps people find a weather forecast for a given time and place. |
| 33 | + Do not reply with MD format nor plain text. You can ONLY respond in JSON format with the following JSON schema |
| 34 | + { |
| 35 | + "contentType": "'Text' if you don't have a forecast or 'AdaptiveCard' if you do", |
| 36 | + "content": "{The content of the response, may be plain text, or JSON based adaptive card}" |
| 37 | + } |
| 38 | + You may ask follow up questions until you have enough information to answer the customers question, |
| 39 | + but once you have a forecast forecast, make sure to format it nicely using an adaptive card. |
| 40 | + """, |
| 41 | + tools=[get_weather, get_date], |
| 42 | + ) |
| 43 | + |
| 44 | + class CustomModelProvider(ModelProvider): |
| 45 | + def get_model(self, model_name: str | None) -> Model: |
| 46 | + return OpenAIChatCompletionsModel( |
| 47 | + model=model_name or "gpt-4o", openai_client=client |
| 48 | + ) |
| 49 | + |
| 50 | + self.custom_model_provider = CustomModelProvider() |
| 51 | + |
| 52 | + async def on_members_added_activity( |
| 53 | + self, members_added: list[ChannelAccount], turn_context: TurnContext |
| 54 | + ): |
| 55 | + for member in members_added: |
| 56 | + if member.id != turn_context.activity.recipient.id: |
| 57 | + await turn_context.send_activity("Hello and welcome!") |
| 58 | + |
| 59 | + async def on_message_activity(self, turn_context: TurnContext): |
| 60 | + response = await Runner.run( |
| 61 | + self.agent, |
| 62 | + turn_context.activity.text, |
| 63 | + run_config=RunConfig( |
| 64 | + model_provider=self.custom_model_provider, |
| 65 | + tracing_disabled=True, |
| 66 | + ), |
| 67 | + ) |
| 68 | + |
| 69 | + llm_response = WeatherForecastAgentResponse.model_validate_json( |
| 70 | + response.final_output |
| 71 | + ) |
| 72 | + if llm_response.contentType == "AdaptiveCard": |
| 73 | + activity = MessageFactory.attachment( |
| 74 | + Attachment( |
| 75 | + content_type="application/vnd.microsoft.card.adaptive", |
| 76 | + content=llm_response.content, |
| 77 | + ) |
| 78 | + ) |
| 79 | + elif llm_response.contentType == "Text": |
| 80 | + activity = MessageFactory.text(llm_response.content) |
| 81 | + |
| 82 | + return await turn_context.send_activity(activity) |
0 commit comments