Building a Customer Service Agent Inspired by Westworld
Steps to Build a Smart, Automated Agent Using CrewAI
As a fan of the Westworld TV series, I’ve always dreamt about visiting the Westworld resort.
This fascination sparked the idea to build a hypothetical customer service agent capable of writing professional emails in response to customer inquiries.
Here’s a detailed look at how I brought this concept to life using the CrewAI framework.
作为《西部世界》电视连续剧的粉丝,我一直梦想着能亲自体验西部世界度假村。
这种兴趣激发了我创建一个能够回复客户查询的假想客服代理的想法。
以下是我如何使用 CrewAI 框架将这一概念变为现实的详细介绍。
Concept and Goals/概念与目标
The primary goal was to create an agent with Retrieval-Augmented Generation (RAG) capability.
This means the agent can retrieve relevant information from a database, specifically a westworld_resort_facts.csv file, before crafting responses to customer questions.
The idea is to simulate a highly efficient and knowledgeable customer service representative for a fictional Westworld resort.
主要目标是创建一个具有检索增强生成(RAG)功能的代理。
也就是说,这个代理可以在编写回复客户问题的邮件之前,从数据库(特别是 westworld_resort_facts.csv 文件)中检索相关信息。
这个想法是模拟一个高效且知识渊博的虚拟客服代表,为一个虚构的西部世界度假村提供服务。
Choosing CrewAI Framework/选择智能体框架
I chose CrewAI as the framework for this project.
CrewAI is designed to enable AI agents to assume specific roles, share goals, and operate cohesively, much like a well-oiled crew.
It provides a robust structure for sophisticated multi-agent interactions, which was perfect for my needs.
Key Components of CrewAI:
Agents: Each agent has a defined role, goal, and backstory, enhancing their functionality and realism.
Tasks: Detailed descriptions and expected outputs guide agents on what needs to be done.
Tools: External integrations, like API calls, assist agents in accomplishing their tasks.
Crew: A group of agents working together to complete tasks.
Processes: The workflow and sequence in which tasks are executed by the agents.
我选择了 CrewAI 作为这个项目的框架。
CrewAI 设计用于使 AI 代理能够承担特定角色,共享目标,并以一个紧密协作的团队形式运行。
这为复杂的多代理交互提供了坚实的结构,非常适合我的需求。
CrewAI 的关键组件
代理(Agents): 每个代理都有定义的角色、目标和背景故事,以增强其功能和现实感。
任务(Tasks): 详细描述和预期输出指导代理完成所需的工作。
工具(Tools): 外部集成(如 API 调用)帮助代理完成任务。
团队(Crew): 一组代理共同完成任务。
流程(Processes): 代理执行任务的工作流程和顺序。
Photo by Campaign Creators on Unsplash
Step-by-Step Implementation/实施步骤
- Setting Up the Environment/环境设置
First, I set up the environment with necessary imports and environment variables:
首先,我设置了必要的导入和环境变量:
from crewai import Agent, Crew, Process, Task
from crewai_tools import CSVSearchTool
from dotenv import load_dotenv
import os
load_dotenv()
os.environ["OPENAI_MODEL_NAME"] = "gpt-4o-mini"
2. Defining Tools/定义工具
I defined a CSV search tool to allow agents to search the westworld_resort_facts.csv file:
我定义了一个 CSV 搜索工具,允许代理搜索 westworld_resort_facts.csv 文件:
csv_search_tool = CSVSearchTool(
csv="westworld_resort_facts.csv",
)
3. Building the Agents/建构智能体
Two agents were created for this project:
Research Agent: Searches the CSV file for relevant answers.
Professional Writer Agent: Writes professional emails based on the research agent’s findings.
为这个项目创建了两个代理:
研究代理(Research Agent): 在 CSV 文件中查找相关答案。
专业写作代理(Professional Writer Agent): 基于研究代理的发现撰写专业邮件。
research_agent = Agent(
role="Research Agent",
goal="Search through the CSV to find relevant answers",
allow_delegation=False,
verbose=True,
memory=True,
backstory=(
"""
The research agent is adept at searching and
extracting data from documents, ensuring accurate and prompt responses.
"""
),
tools=[csv_search_tool],
)
professional_writer_agent = Agent(
role="Professional Writer",
goal="Write professional emails based on the research agent's findings",
allow_delegation=False,
verbose=True,
memory=True,
backstory=(
"""
The professional writer agent has excellent writing skills and is able to craft
clear and concise emails based on the provided information.
"""
),
tools=[],
)
4. Defining Tasks/定义任务
Two tasks were defined for the agents:
Answer Customer Question Task: The research agent finds relevant answers in the CSV file.
Write Email Task: The professional writer agent crafts a professional email based on the research agent’s findings.
为代理定义了两个任务:
回答客户问题任务(Answer Customer Question Task): 研究代理在 CSV 文件中查找相关答案。
写邮件任务(Write Email Task): 专业写作代理根据研究代理的发现撰写专业邮件。
5. Setting Up the Crew and Initiating the Chat/设置团队并启动对话
Finally, I set up the crew and initiated the chat to process customer questions:
最后,我设置了团队并启动对话,以处理客户问题:
crew = Crew(
agents=[research_agent, professional_writer_agent],
tasks=[answer_customer_question_task, write_email_task],
process=Process.sequential,
)
customer_question = input(
"What would you like to know?\n"
)
result = crew.kickoff(inputs={"customer_question": customer_question})
print(result)
When the code is executed for the first time, it builds a database folder in the directory, creating a chroma.sqlite3
file.
This file automates embedding the westworld_resort_facts.csv
file and storing it in a Chroma vector database, simplifying the entire process compared to manually handling embeddings and storage.
To test the agent, I asked, “Can I meet Dolores in the park and can I stay overnight in the park?” Here’s the result:
Research Agent:
> Entering new CrewAgentExecutor chain...
I need to search the CSV for information regarding whether guests can meet Dolores in the park and if overnight stays are allowed.
Action: Search a CSV's content
Action Input: {"search_query": "meet Dolores in the park"}
Relevant Content:
with a host modeled after Dolores, the rancher's daughter. You can explore her storyline, which often involves a journey of self-discovery and adventure in the park.
the park.
engage with a host based on Peter Abernathy, Dolores' father, known for his deep, contemplative nature. You can dive into profound discussions about the nature of reality, existence, and the role of hosts within the park.
Thought: I need to search the CSV for information regarding whether guests can stay overnight in the park.
Action: Search a CSV's content
Action Input: {"search_query": "stay overnight in the park"}
Relevant Content:
the park.
feel real and can leave a lasting impact long after you've left the park.
range of luxurious accommodations within the park, from rustic lodges to upscale hotels, allowing guests to fully immerse themselves in the experience 24/7.
Thought: I now know the final answer
Final Answer: Yes, you can meet Dolores in the park as there is a host modeled after her, and you can explore her storyline, which often involves a journey of self-discovery and adventure. Additionally, you can stay overnight in the park, as there is a range of luxurious accommodations available, from rustic lodges to upscale hotels, allowing guests to fully immerse themselves in the experience 24/7.
> Finished chain.
当代码第一次执行时,它会在目录中创建一个数据库文件夹,并创建 chroma.sqlite3 文件。
该文件自动将 westworld_resort_facts.csv 文件嵌入并存储到 Chroma 向量数据库中,简化了整个过程,相比手动处理嵌入和存储更加方便。
为了测试代理,我问了“我可以在公园里见到 Dolores 吗?我可以在公园里过夜吗?”以下是结果:
研究代理:
> 进入新的 CrewAgentExecutor 链...
我需要搜索 CSV 以了解客人是否可以在公园里见到 Dolores 以及是否允许过夜。
操作:搜索 CSV 的内容
操作输入:{"search_query": "在公园里见到 Dolores"}
相关内容:
有一个以农场主的女儿 Dolores 为原型的接待员,你可以探索她的故事情节,这通常涉及自我发现和冒险的旅程。
操作:搜索 CSV 的内容
操作输入:{"search_query": "在公园里过夜"}
相关内容:
公园内有一系列豪华住宿,从质朴的小屋到高档酒店,让客人可以 24/7 完全沉浸在体验中。
想法:我现在知道最终答案了。
最终答案:是的,你可以在公园里见到 Dolores,因为有一个以她为原型的接待员,你可以探索她的故事情节,这通常涉及自我发现和冒险。此外,你可以在公园里过夜,因为这里有一系列豪华住宿,从质朴的小屋到高档酒店,让客人可以 24/7 完全沉浸在体验中。
> 链条完成
Professional Writer Agent:
> Entering new CrewAgentExecutor chain...
I now can give a great answer
Final Answer:
Subject: Your Inquiry about Dolores and Accommodations at West World Resort
Dear [Customer's Name],
Thank you for reaching out to us with your inquiry about your upcoming visit to West World Resort. We are excited to provide you with the information you need to make the most of your experience.
Yes, you can indeed meet Dolores in the park! We have a host modeled after her, and you will have the opportunity to explore her engaging storyline, which features elements of self-discovery and adventure. This immersive experience is one of the many highlights of our resort.
Additionally, we offer a range of luxurious accommodations for guests wishing to stay overnight. You can choose from rustic lodges to upscale hotels, ensuring that you have a comfortable and memorable stay while fully immersing yourself in the park's offerings 24/7.
If you have any further questions or need assistance with your booking, please do not hesitate to contact us. We look forward to welcoming you to West World Resort!
Best regards,
Customer Support Team,
West World Resort
> Finished chain.
专业写作代理:
> 进入新的 CrewAgentExecutor 链...
主题:关于 Dolores 和西部世界度假村住宿的咨询
亲爱的[客户的名字],
感谢您就即将到来的西部世界度假村之行向我们咨询。我们很高兴为您提供所需的信息,以便您充分体验我们的度假村。
是的,您确实可以在公园里见到 Dolores!我们有一个以她为原型的接待员,您将有机会探索她的引人入胜的故事情节,其中包含自我发现和冒险的元素。这种沉浸式体验是我们度假村的众多亮点之一。
此外,我们为希望过夜的客人提供一系列豪华住宿。您可以选择从质朴的小屋到高档酒店,确保您在享受公园全天候提供的各种服务时,拥有舒适而难忘的住宿体验。
如果您有任何进一步的问题或需要预订方面的帮助,请随时与我们联系。我们期待着欢迎您来到西部世界度假村!
此致,
客户支持团队,
西部世界度假村
> 链条完成
Conclusion/结论
Using the CrewAI framework, I successfully created a hypothetical customer service agent inspired by Westworld.
The agent efficiently retrieves relevant information from a CSV file and crafts professional email responses, demonstrating the power and potential of multi-agent systems in customer service applications.
通过使用 CrewAI 框架,我成功创建了一个受《西部世界》启发的假想客服代理。
这个代理能够高效地从 CSV 文件中检索相关信息,并编写专业的邮件回复,展示了多代理系统在客户服务应用中的强大潜力和应用前景。