Coverage for docs/docs_src/tutorials/giphy/simple_main.py: 47%

19 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.api.openapi.security import APIKeyQuery 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 

18giphy_api_key = os.getenv("GIPHY_API_KEY", "") 1abc

19openapi_url="https://raw.githubusercontent.com/ag2ai/fastagency/refs/heads/main/examples/openapi/giphy_openapi.json" 1abc

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

21giphy_api.set_security_params(APIKeyQuery.Parameters(value=giphy_api_key)) 1abc

22 

23 

24wf = Workflow() 1abc

25 

26@wf.register(name="giphy_chat", description="Giphy chat") 1abc

27def giphy_workflow( 1abc

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

29) -> str: 

30 def is_termination_msg(msg: dict[str, Any]) -> bool: 

31 return msg["content"] is not None and "TERMINATE" in msg["content"] 

32 

33 user_proxy = UserProxyAgent( 

34 name="User_Proxy", 

35 human_input_mode="ALWAYS", 

36 is_termination_msg=is_termination_msg, 

37 ) 

38 

39 system_message="""You are a helper agent that communicates with the user and 

40 Giphy API. When using API calls, always limit the response size to 5 items. 

41 When finished, write 'TERMINATE' to end the conversation.""" 

42 

43 with llm_config: 

44 giphy_agent = ConversableAgent( 

45 name="Giphy_Agent", 

46 system_message=system_message, 

47 human_input_mode="NEVER", 

48 is_termination_msg=is_termination_msg, 

49 ) 

50 

51 wf.register_api( 

52 api=giphy_api, 

53 callers=giphy_agent, 

54 executors=user_proxy, 

55 functions=["random_gif", "search_gifs", "trending_gifs"], 

56 ) 

57 

58 initial_message = ui.text_input( 

59 sender="Workflow", 

60 recipient="User", 

61 prompt="I can help you find images related to a certain subject. What kind of images would you like to find?", 

62 ) 

63 

64 response = user_proxy.run( 

65 giphy_agent, 

66 message=initial_message, 

67 summary_method="reflection_with_llm", 

68 max_turns=10, 

69 ) 

70 

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

72 

73 

74app = FastAgency(provider=wf, ui=MesopUI(), title="Giphy chat") 1abc