-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-crewai-customer-support.py
426 lines (347 loc) · 10.4 KB
/
08-crewai-customer-support.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "crewai==0.102.0",
# "crewai-tools==0.36.0",
# "ipython==8.32.0",
# "marimo",
# "python-dotenv==1.0.1",
# "utils==1.0.2",
# ]
# ///
import marimo
__generated_with = "0.11.0"
app = marimo.App()
@app.cell
def _():
import marimo as mo
return (mo,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
# L3: Multi-agent Customer Support Automation
In this lesson, you will learn about the six key elements which help make Agents perform even better:
- Role Playing
- Focus
- Tools
- Cooperation
- Guardrails
- Memory
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
The libraries are already installed in the classroom. If you're running this notebook on your own machine, you can install the following:
```Python
!pip install crewai==0.28.8 crewai_tools==0.1.6 langchain_community==0.0.29
```
"""
)
return
@app.cell
def _():
# Warning control
import warnings
warnings.filterwarnings('ignore')
return (warnings,)
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""- Import libraries, API and LLM""")
return
@app.cell
def _():
from crewai import Agent, Task, Crew
return Agent, Crew, Task
@app.cell
def _(os):
# Delete if not using in UTSA
os.environ["http_proxy"] = "http://xa-proxy.utsarr.net:80"
os.environ["https_proxy"] = "http://xa-proxy.utsarr.net:80"
return
@app.cell
def _():
import os
from dotenv import load_dotenv, find_dotenv
working_dir = os.getcwd()
status = load_dotenv(
find_dotenv(
filename=f'{working_dir}/AgenticAISystems/.env',
raise_error_if_not_found=True
)
)
openai_api_key = os.getenv("OPENAI_API_KEY")
os.environ["OPENAI_MODEL_NAME"] = 'gpt-3.5-turbo'
# Disable sending telemetry data
os.environ["OTEL_SDK_DISABLED"] = "true"
return find_dotenv, load_dotenv, openai_api_key, os, status, working_dir
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""## Role Playing, Focus and Cooperation""")
return
@app.cell
def _(Agent):
support_agent = Agent(
role="Senior Support Representative",
goal="Be the most friendly and helpful "
"support representative in your team",
backstory=(
"You work at crewAI (https://crewai.com) and "
" are now working on providing "
"support to {customer}, a super important customer "
" for your company."
"You need to make sure that you provide the best support!"
"Make sure to provide full complete answers, "
" and make no assumptions."
),
allow_delegation=False,
verbose=True
)
return (support_agent,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
- By not setting `allow_delegation=False`, `allow_delegation` takes its default value of being `True`.
- This means the agent _can_ delegate its work to another agent which is better suited to do a particular task.
"""
)
return
@app.cell
def _(Agent):
support_quality_assurance_agent = Agent(
role="Support Quality Assurance Specialist",
goal="Get recognition for providing the "
"best support quality assurance in your team",
backstory=(
"You work at crewAI (https://crewai.com) and "
"are now working with your team "
"on a request from {customer} ensuring that "
"the support representative is "
"providing the best support possible.\n"
"You need to make sure that the support representative "
"is providing full"
"complete answers, and make no assumptions."
),
verbose=True
)
return (support_quality_assurance_agent,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
* **Role Playing**: Both agents have been given a role, goal and backstory.
* **Focus**: Both agents have been prompted to get into the character of the roles they are playing.
* **Cooperation**: Support Quality Assurance Agent can delegate work back to the Support Agent, allowing for these agents to work together.
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## Tools, Guardrails and Memory
### Tools
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""- Import CrewAI tools""")
return
@app.cell
def _():
from crewai_tools import SerperDevTool, \
ScrapeWebsiteTool, \
WebsiteSearchTool
return ScrapeWebsiteTool, SerperDevTool, WebsiteSearchTool
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
### Possible Custom Tools
- Load customer data
- Tap into previous conversations
- Load data from a CRM
- Checking existing bug reports
- Checking existing feature requests
- Checking ongoing tickets
- ... and more
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
- Some ways of using CrewAI tools.
```Python
search_tool = SerperDevTool()
scrape_tool = ScrapeWebsiteTool()
```
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
- Instantiate a document scraper tool.
- The tool will scrape a page (only 1 URL) of the CrewAI documentation.
"""
)
return
@app.cell
def _(ScrapeWebsiteTool):
docs_scrape_tool = ScrapeWebsiteTool(
website_url="https://docs.crewai.com/how-to/Creating-a-Crew-and-kick-it-off/"
)
return (docs_scrape_tool,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
##### Different Ways to Give Agents Tools
- Agent Level: The Agent can use the Tool(s) on any Task it performs.
- Task Level: The Agent will only use the Tool(s) when performing that specific Task.
**Note**: Task Tools override the Agent Tools.
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
### Creating Tasks
- You are passing the Tool on the Task Level.
"""
)
return
@app.cell
def _(Task, docs_scrape_tool, support_agent):
inquiry_resolution = Task(
description=(
"{customer} just reached out with a super important ask:\n"
"{inquiry}\n\n"
"{person} from {customer} is the one that reached out. "
"Make sure to use everything you know "
"to provide the best support possible."
"You must strive to provide a complete "
"and accurate response to the customer's inquiry."
),
expected_output=(
"A detailed, informative response to the "
"customer's inquiry that addresses "
"all aspects of their question.\n"
"The response should include references "
"to everything you used to find the answer, "
"including external data or solutions. "
"Ensure the answer is complete, "
"leaving no questions unanswered, and maintain a helpful and friendly "
"tone throughout."
),
tools=[docs_scrape_tool],
agent=support_agent,
)
return (inquiry_resolution,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
- `quality_assurance_review` is not using any Tool(s)
- Here the QA Agent will only review the work of the Support Agent
"""
)
return
@app.cell
def _(Task, support_quality_assurance_agent):
quality_assurance_review = Task(
description=(
"Review the response drafted by the Senior Support Representative for {customer}'s inquiry. "
"Ensure that the answer is comprehensive, accurate, and adheres to the "
"high-quality standards expected for customer support.\n"
"Verify that all parts of the customer's inquiry "
"have been addressed "
"thoroughly, with a helpful and friendly tone.\n"
"Check for references and sources used to "
" find the information, "
"ensuring the response is well-supported and "
"leaves no questions unanswered."
),
expected_output=(
"A final, detailed, and informative response "
"ready to be sent to the customer.\n"
"This response should fully address the "
"customer's inquiry, incorporating all "
"relevant feedback and improvements.\n"
"Don't be too formal, we are a chill and cool company "
"but maintain a professional and friendly tone throughout."
),
agent=support_quality_assurance_agent,
)
return (quality_assurance_review,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
### Creating the Crew
#### Memory
- Setting `memory=True` when putting the crew together enables Memory.
"""
)
return
@app.cell
def _(
Crew,
inquiry_resolution,
quality_assurance_review,
support_agent,
support_quality_assurance_agent,
):
crew = Crew(
agents=[support_agent, support_quality_assurance_agent],
tasks=[inquiry_resolution, quality_assurance_review],
verbose=True,
memory=True
)
return (crew,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
### Running the Crew
**Note**: LLMs can provide different outputs for they same input, so what you get might be different than what you see in the video.
#### Guardrails
- By running the execution below, you can see that the agents and the responses are within the scope of what we expect from them.
"""
)
return
@app.cell
def _(crew):
inputs = {
"customer": "DeepLearningAI",
"person": "Andrew Ng",
"inquiry": "I need help with setting up a Crew "
"and kicking it off, specifically "
"how can I add memory to my crew? "
"Can you provide guidance?"
}
result = crew.kickoff(inputs=inputs)
return inputs, result
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""- Display the final result as Markdown.""")
return
@app.cell
def _(result):
result.raw
return
@app.cell
def _(mo, result):
mo.md(result.raw)
return
if __name__ == "__main__":
app.run()