Coverage for docs/docs_src/user_guide/ui/mesop/main_mesop_basic_auth.py: 57%

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 

4import mesop as me 1dabc

5from autogen import ConversableAgent, LLMConfig 1abc

6 

7from fastagency import UI, FastAgency 1abc

8from fastagency.runtimes.ag2 import Workflow 1abc

9from fastagency.ui.mesop import MesopUI 1abc

10from fastagency.ui.mesop.auth.basic_auth import BasicAuth 1abc

11from fastagency.ui.mesop.styles import ( 1abc

12 MesopHomePageStyles, 

13 MesopMessagesStyles, 

14 MesopSingleChoiceInnerStyles, 

15) 

16 

17llm_config = LLMConfig( 1abc

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

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

20 temperature=0.8, 

21) 

22 

23wf = Workflow() 1abc

24 

25 

26@wf.register(name="simple_learning", description="Student and teacher learning chat") 1abc

27def simple_workflow( 1abc

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

29) -> str: 

30 initial_message = ui.text_input( 

31 sender="Workflow", 

32 recipient="User", 

33 prompt="What do you want to learn today?", 

34 ) 

35 

36 with llm_config: 

37 student_agent = ConversableAgent( 

38 name="Student_Agent", 

39 system_message="You are a student willing to learn.", 

40 ) 

41 teacher_agent = ConversableAgent( 

42 name="Teacher_Agent", 

43 system_message="You are a math teacher.", 

44 ) 

45 

46 response = student_agent.run( 

47 teacher_agent, 

48 message=initial_message, 

49 summary_method="reflection_with_llm", 

50 max_turns=5, 

51 ) 

52 

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

54 

55 

56security_policy=me.SecurityPolicy(allowed_iframe_parents=["https://acme.com"], allowed_script_srcs=["https://cdn.jsdelivr.net"]) 1abc

57 

58styles=MesopHomePageStyles( 1abc

59 stylesheets=[ 

60 "https://fonts.googleapis.com/css2?family=Inter:wght@100..900&display=swap" 

61 ], 

62 root=me.Style( 

63 background="#e7f2ff", 

64 height="100%", 

65 font_family="Inter", 

66 display="flex", 

67 flex_direction="row", 

68 ), 

69 message=MesopMessagesStyles( 

70 single_choice_inner=MesopSingleChoiceInnerStyles( 

71 disabled_button=me.Style( 

72 margin=me.Margin.symmetric(horizontal=8), 

73 padding=me.Padding.all(16), 

74 border_radius=8, 

75 background="#64b5f6", 

76 color="#fff", 

77 font_size=16, 

78 ), 

79 ) 

80 ), 

81) 

82 

83# Initialize auth with username and password 

84auth = BasicAuth( 1abc

85 # TODO: Replace `allowed_users` with the desired usernames and their 

86 # bcrypt-hashed passwords. One way to generate bcrypt-hashed passwords 

87 # is by using online tools such as https://bcrypt.online 

88 allowed_users={ 

89 "harish": "$2y$10$4aH/.C.WritjZAYskA0Dq.htlFDJTa49UuxSVUlp9JCa2K3PgUkaG", # nosemgrep 

90 "davor@ag2.ai": "$2y$10$Yz9GuF/bWmRFmnXFkauOwePT/U.VSUHdpMOX7GPB8GiklJE4HJZmG" # nosemgrep 

91 } 

92) 

93 

94ui = MesopUI(security_policy=security_policy, styles=styles, auth=auth) 1abc

95 

96app = FastAgency(provider=wf, ui=ui, title="Learning Chat") 1abc