Coverage for docs/docs_src/user_guide/runtimes/ag2/mesop/main.py: 50%

14 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-19 12:16 +0000

1import os 1dabc

2from typing import Any 1dabc

3 

4from autogen import UserProxyAgent 1dabc

5from autogen import ConversableAgent, LLMConfig 1dabc

6 

7from fastagency import UI, FastAgency 1dabc

8from fastagency.api.openapi import OpenAPI 1dabc

9from fastagency.runtimes.ag2 import Workflow 1dabc

10from fastagency.ui.mesop import MesopUI 1dabc

11 

12llm_config = LLMConfig( 1abc

13 model="gpt-4o-mini", 

14 api_key=os.getenv("OPENAI_API_KEY"), 

15 temperature=0.8, 

16) 

17 

18openapi_url = "https://weather.tools.fastagency.ai/openapi.json" 1abc

19 

20weather_api = OpenAPI.create(openapi_url=openapi_url) 1abc

21 

22weather_agent_system_message = """You are a weather agent. When asked 1abc

23for weather, always call the function to get real-time data immediately. 

24Do not respond until the data is retrieved. Provide the actual weather 

25concisely based only on the real-time data from the function. Do not 

26use any pre-existing knowledge or memory.""" 

27 

28wf = Workflow() 1abc

29 

30 

31@wf.register(name="simple_weather", description="Weather chat") # type: ignore[type-var] 1abc

32def weather_workflow( 1abc

33 ui: UI, params: dict[str, Any] 

34) -> str: 

35 initial_message = ui.text_input( 

36 sender="Workflow", 

37 recipient="User", 

38 prompt="I can help you with the weather. What would you like to know?", 

39 ) 

40 

41 with llm_config: 

42 user_agent = UserProxyAgent( 

43 name="User_Agent", 

44 system_message="You are a user agent", 

45 human_input_mode="NEVER", 

46 code_execution_config=False 

47 ) 

48 weather_agent = ConversableAgent( 

49 name="Weather_Agent", 

50 system_message=weather_agent_system_message, 

51 human_input_mode="NEVER", 

52 ) 

53 

54 wf.register_api( # type: ignore[attr-defined] 

55 api=weather_api, 

56 callers=[user_agent], 

57 executors=[weather_agent], 

58 functions=[ 

59 { 

60 "get_daily_weather_daily_get": { 

61 "name": "get_daily_weather", 

62 "description": "Get the daily weather", 

63 } 

64 }, 

65 "get_hourly_weather_hourly_get", 

66 ], 

67 ) 

68 

69 response = user_agent.run( 

70 weather_agent, 

71 message=initial_message, 

72 summary_method="reflection_with_llm", 

73 max_turns=3, 

74 ) 

75 

76 return ui.process(response) # type: ignore[no-any-return] 

77 

78 

79app = FastAgency(provider=wf, ui=MesopUI()) 1abc