Coverage for docs/docs_src/user_guide/external_rest_apis/security.py: 100%

14 statements  

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

1import os 1bacde

2 

3from autogen import UserProxyAgent, ConversableAgent, LLMConfig 1bacde

4 

5from fastagency import UI, FastAgency 1bacde

6from fastagency.api.openapi import OpenAPI 1bacde

7from fastagency.api.openapi.security import APIKeyHeader 1bacde

8from fastagency.runtimes.ag2 import Workflow 1bacde

9from fastagency.ui.console import ConsoleUI 1bacde

10 

11llm_config = LLMConfig( 1bacde

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

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

14 temperature=0.8, 

15) 

16 

17WEATHER_OPENAPI_URL = "https://weather.tools.fastagency.ai/openapi.json" 1bacde

18weather_api = OpenAPI.create(openapi_url=WEATHER_OPENAPI_URL) 1bacde

19 

20# Set global security params for all methods 

21weather_api.set_security_params(APIKeyHeader.Parameters(value="secure weather key")) 1bacde

22 

23# Set security params for a specific method 

24# weather_api.set_security_params( 

25# APIKeyHeader.Parameters(value="secure weather key"), 

26# "get_daily_weather_daily_get", 

27# ) 

28 

29wf = Workflow() 1bacde

30 

31 

32@wf.register( 1bacde

33 name="simple_weather_with_security", description="Weather chat with security" 

34) 

35def weather_workflow_with_security( 1bacde

36 ui: UI, params: dict[str, str] 

37) -> str: 

38 initial_message = ui.text_input( 1a

39 sender="Workflow", 

40 recipient="User", 

41 prompt="What do you want to know about the weather?", 

42 ) 

43 

44 user_agent = UserProxyAgent( 1a

45 name="User_Agent", 

46 system_message="You are a user agent", 

47 human_input_mode="NEVER", 

48 llm_config=llm_config, 

49 ) 

50 weather_agent = ConversableAgent( 1a

51 name="Weather_Agent", 

52 system_message="You are a weather agent", 

53 human_input_mode="NEVER", 

54 llm_config=llm_config, 

55 ) 

56 

57 wf.register_api( 1a

58 api=weather_api, 

59 callers=user_agent, 

60 executors=weather_agent, 

61 ) 

62 

63 response = user_agent.run( 1a

64 weather_agent, 

65 message=initial_message, 

66 summary_method="reflection_with_llm", 

67 max_turns=3, 

68 ) 

69 

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

71 

72 

73app = FastAgency(provider=wf, ui=ConsoleUI()) 1bacde