-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathmesop_crewai.py
108 lines (93 loc) · 3.18 KB
/
mesop_crewai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#
# pip install --upgrade crewai==0.30.8 mesop=0.12.0
#
from crewai import Crew, Process, Agent, Task
from langchain_openai import ChatOpenAI
from langchain_core.callbacks import BaseCallbackHandler
from typing import Any, Dict, Optional
import mesop as me
import mesop.labs as mel
llm = ChatOpenAI(model="gpt-4o", openai_api_key="sk-your-openai-key")
@me.stateclass
class State:
agent_messages: list[str]
_DEFAULT_BORDER = me.Border.all(
me.BorderSide(color="#e0e0e0", width=1, style="solid")
)
_BOX_STYLE = me.Style(display="grid",border=_DEFAULT_BORDER,
padding=me.Padding.all(15),
overflow_y="scroll",
box_shadow=("0 3px 1px -2px #0003, 0 2px 2px #00000024, 0 1px 5px #0000001f"),
)
class MyCustomHandler(BaseCallbackHandler):
def __init__(self, agent_name: str) -> None:
self.agent_name = agent_name
def on_chain_start(
self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any
) -> None:
state = me.state(State)
state.agent_messages.append(f"## Assistant: \r{inputs['input']}")
def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> None:
state = me.state(State)
state.agent_messages.append(f"## {self.agent_name}: \r{outputs['output']}")
writer = Agent(
role='Tech Writer',
backstory='''You are a tech writer who is capable of writing
tech blog post in depth.
''',
goal="Write and iterate a high quality blog post.",
llm=llm,
callbacks=[MyCustomHandler("Writer")],
)
researcher = Agent(
role='Tech Researcher',
backstory='''You are a professional researcher for many technical topics.
You are good at list potential knowledge and trend of
the given topic
''',
goal="list builtins about what key knowledge and trend of a given topic",
llm=llm,
callbacks=[MyCustomHandler("Researcher")],
)
def StartCrew(prompt):
task1 = Task(
description=f"""list builtins about what key knowledge
and trend of a given topic: {prompt}.
""",
agent=researcher,
expected_output="Builtin points about where need to be improved.",
)
task2 = Task(
description=f"""Based on the given research outcomes,
write a blog post of {prompt}.
""",
agent=writer,
expected_output="an article"
)
project_crew = Crew(
tasks=[task1, task2],
agents=[researcher, writer],
manager_llm=llm,
process=Process.sequential
)
result = project_crew.kickoff()
return result
@me.page(
security_policy=me.SecurityPolicy(
allowed_iframe_parents=["https://google.github.io"]
),
path="/crewai",
title="CrewAI on Mesop",
)
def app():
state = me.state(State)
with me.box():
mel.text_to_text(
StartCrew,
title="CrewAI Chat",
)
with me.box(style=_BOX_STYLE):
me.text(text="Workflow...", type="headline-6")
for message in state.agent_messages:
with me.box(style=_BOX_STYLE):
me.markdown(message)