Coverage for docs/docs_src/user_guide/runtimes/ag2/mesop/using_non_openai_models.py: 53%

15 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 ConversableAgent, LLMConfig, UserProxyAgent 1dabc

5 

6from fastagency import UI, FastAgency 1dabc

7from fastagency.api.openapi import OpenAPI 1dabc

8from fastagency.runtimes.ag2 import Workflow 1dabc

9from fastagency.ui.mesop import MesopUI 1dabc

10 

11llm_config = LLMConfig( 1abc

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

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

14 temperature=0.8, 

15) 

16 

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

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

19 

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

21about the weather for a specific city, NEVER provide any information from 

22memory. ALWAYS respond with: "Please hold on while I retrieve the real-time 

23weather data for [city name]." and immediately call the provided function to 

24retrieve real-time data for that city. Be concise in your response.""" 

25 

26wf = Workflow() 1abc

27 

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

29def weather_workflow( 1abc

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

31) -> str: 

32 initial_message = ui.text_input( 

33 sender="Workflow", 

34 recipient="User", 

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

36 ) 

37 

38 with llm_config: 

39 user_agent = UserProxyAgent( 

40 name="User_Agent", 

41 system_message="You are a user agent", 

42 human_input_mode="NEVER", 

43 code_execution_config=False 

44 ) 

45 weather_agent = ConversableAgent( 

46 name="Weather_Agent", 

47 system_message=weather_agent_system_message, 

48 human_input_mode="NEVER", 

49 ) 

50 

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

52 api=weather_api, 

53 callers=[user_agent], 

54 executors=[weather_agent], 

55 functions=[ 

56 { 

57 "get_daily_weather_daily_get": { 

58 "name": "get_daily_weather", 

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

60 } 

61 }, 

62 "get_hourly_weather_hourly_get", 

63 ], 

64 ) 

65 

66 response = user_agent.run( 

67 weather_agent, 

68 message=initial_message, 

69 summary_method="reflection_with_llm", 

70 max_turns=3, 

71 ) 

72 

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

74 

75 

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