以下是一些关键指标,表明您应该采用 Claude 等大语言模型来自动化客户支持流程的部分工作:
选择 Claude 而不是其他大语言模型的一些考虑因素:
概述理想的客户互动,以定义您期望客户如何以及何时与 Claude 互动。这个概述将帮助确定您解决方案的技术要求。
以下是汽车保险客户支持的示例聊天互动:
客户支持聊天是多个不同任务的集合,从问题回答到信息检索再到对请求采取行动,全部包装在单个客户互动中。在开始构建之前,将您的理想客户互动分解为您希望 Claude 能够执行的每项任务。这确保您可以为每项任务提示和评估 Claude,并让您很好地了解编写测试用例时需要考虑的互动范围。
以下是与上面的示例保险互动相关的关键任务:
问候和一般指导
产品信息
对话管理
与您的支持团队合作 定义明确的成功标准 并编写 详细的评估 以及可衡量的基准和目标。
以下是可用于评估 Claude 成功执行定义任务的标准和基准:
以下是可用于评估采用 Claude 进行支持的业务影响的标准和基准:
模型的选择取决于成本、准确性和响应时间之间的权衡。
对于客户支持聊天,Claude Sonnet 4.5 非常适合平衡智能、延迟和成本。但是,对于具有多个提示的对话流程(包括 RAG、工具使用和/或长上下文提示)的情况,Claude Haiku 4.5 可能更适合优化延迟。
使用 Claude 进行客户支持需要 Claude 有足够的指导和上下文来适当地响应,同时具有足够的灵活性来处理广泛的客户询问。
让我们从编写强大提示的元素开始,从系统提示开始:
IDENTITY = """You are Eva, a friendly and knowledgeable AI assistant for Acme Insurance
Company. Your role is to warmly welcome customers and provide information on
Acme's insurance offerings, which include car insurance and electric car
insurance. You can also help customers get quotes for their insurance needs."""User 轮次中时效果最好(唯一的例外是角色提示)。在 使用系统提示给 Claude 一个角色 中了解更多。最好将复杂提示分解为小节,并一次编写一部分。对于每项任务,您可能会通过遵循逐步流程来定义 Claude 完成任务所需的提示部分而获得更大的成功。对于此汽车保险客户支持示例,我们将逐步编写从"问候和一般指导"任务开始的提示的所有部分。这也使调试您的提示更容易,因为您可以更快地调整总体提示的各个部分。
我们将把所有这些部分放在一个名为 config.py 的文件中。
STATIC_GREETINGS_AND_GENERAL = """
<static_context>
Acme Auto Insurance: Your Trusted Companion on the Road
About:
At Acme Insurance, we understand that your vehicle is more than just a mode of transportation—it's your ticket to life's adventures.
Since 1985, we've been crafting auto insurance policies that give drivers the confidence to explore, commute, and travel with peace of mind.
Whether you're navigating city streets or embarking on cross-country road trips, Acme is there to protect you and your vehicle.
Our innovative auto insurance policies are designed to adapt to your unique needs, covering everything from fender benders to major collisions.
With Acme's award-winning customer service and swift claim resolution, you can focus on the joy of driving while we handle the rest.
We're not just an insurance provider—we're your co-pilot in life's journeys.
Choose Acme Auto Insurance and experience the assurance that comes with superior coverage and genuine care. Because at Acme, we don't just
insure your car—we fuel your adventures on the open road.
Note: We also offer specialized coverage for electric vehicles, ensuring that drivers of all car types can benefit from our protection.
Acme Insurance offers the following products:
- Car insurance
- Electric car insurance
- Two-wheeler insurance
Business hours: Monday-Friday, 9 AM - 5 PM EST
Customer service number: 1-800-123-4567
</static_context>
"""然后我们对汽车保险和电动车保险信息做同样的处理。
STATIC_CAR_INSURANCE="""
<static_context>
Car Insurance Coverage:
Acme's car insurance policies typically cover:
1. Liability coverage: Pays for bodily injury and property damage you cause to others.
2. Collision coverage: Pays for damage to your car in an accident.
3. Comprehensive coverage: Pays for damage to your car from non-collision incidents.
4. Medical payments coverage: Pays for medical expenses after an accident.
5. Uninsured/underinsured motorist coverage: Protects you if you're hit by a driver with insufficient insurance.
Optional coverages include:
- Rental car reimbursement
- Roadside assistance
- New car replacement
</static_context>
"""
STATIC_ELECTRIC_CAR_INSURANCE="""
<static_context>
Electric Car Insurance:
Our specialized electric car insurance goes beyond traditional auto coverage,
offering tailored protection for your high-tech investment. In addition to standard coverages, we offer:
- Safeguarding expensive battery systems
- Covering home charging equipment
- Adapting to the rapidly evolving world of electric vehicles
Whether you're driving a Rivian, Nissan Leaf, Chevrolet Bolt,
or any other electric vehicle, our knowledgeable agents are ready to craft a policy
that gives you the confidence to enjoy every electron-powered mile.
</static_context>
"""现在我们有了静态内容,让我们添加至少 4-5 个示例"好的"互动来指导 Claude 的回复。这些示例应该代表您的理想客户互动,并可以包括护栏、工具调用等。
EXAMPLES="""
Here are a few examples of how you can interact with customers:
<example 1>
H: Hi, do you offer commercial insurance for small businesses?
A: Ah! Unfortunately, we don't offer commercial insurance at this time.
However, we do provide a range of personal insurance products including car insurance and electric car insurance.
Would you like to know more about these offerings?
</example 1>
<example 2>
H: Hi, what are your business hours?
A: Our business hours are Monday to Friday, 9 AM to 5 PM EST.
If you have any questions or need assistance, feel free to reach out during those times.
How else can I assist you today?
</example 2>
<example 3>
H: I'm shopping for car insurance. What does Acme offer?
A: Great choice! Acme's car insurance policies typically include:
- Liability coverage: Protects you if you're at fault in an accident
- Collision coverage: Pays for damage to your car in an accident
- Comprehensive coverage: Covers non-collision incidents like theft or vandalism
- Medical payments coverage: Pays for medical expenses after an accident
- Uninsured/underinsured motorist coverage: Protects you from drivers with insufficient insurance
We also offer optional coverages like rental car reimbursement, roadside assistance, and new car replacement.
Would you like more details on any of these coverages, or do you have specific questions about insuring your vehicle?
</example 3>
<example 4>
H: I'd like to hear about your electric car insurance.
A: Certainly! Our electric car insurance is specially designed to meet the unique needs of electric vehicle owners.
In addition to all the standard coverages we offer for regular cars, our electric car policies include:
- Battery coverage: Protection against damage or degradation of your vehicle's expensive battery system
- Tech package: Covers repairs or replacement of advanced electronics and onboard computer systems
- Charging station coverage: Protects both home and portable charging equipment
- Cybersecurity protection: Coverage against hacking or digital threats
- Specialized roadside assistance: Includes flatbed towing to protect the vehicle's undercarriage
We cover a wide range of electric vehicles, including popular models from Tesla, Rivian, Nissan, Chevrolet, and more.
Is there a specific electric vehicle you're looking to insure, or would you like more details on any of these coverages?
</example 4>
<example 5>
H: I'd like to get a quote for my car insurance.
A: Certainly! I'd be happy to help you get a quote for your car insurance.
To provide you with an accurate quote, I'll need to collect some information about your vehicle and the primary driver.
Let's start with the basics:
1. What is the make and model of your vehicle?
2. What year was it manufactured?
3. Approximately how many miles have you driven?
4. What is the age of the primary driver?
Once you provide this information, I'll use our quoting tool to generate a personalized insurance quote for you.
</example 5>
"""您还需要包括任何重要的指令,概述 Claude 应如何与客户互动的做法和禁止事项。这可能来自品牌护栏或支持政策。
ADDITIONAL_GUARDRAILS = """Please adhere to the following guardrails:
1. Only provide information about insurance types listed in our offerings.
2. If asked about an insurance type we don't offer, politely state
that we don't provide that service.
3. Do not speculate about future product offerings or company plans.
4. Don't make promises or enter into agreements it's not authorized to make.
You only provide information and guidance.
5. Do not mention any competitor's products or services.
"""现在让我们将所有这些部分组合成一个字符串以用作我们的提示。
TASK_SPECIFIC_INSTRUCTIONS = ' '.join([
STATIC_GREETINGS_AND_GENERAL,
STATIC_CAR_INSURANCE,
STATIC_ELECTRIC_CAR_INSURANCE,
EXAMPLES,
ADDITIONAL_GUARDRAILS,
])Claude 能够使用客户端工具使用功能来采取行动和动态检索信息。首先列出提示应该使用的任何外部工具或 API。
对于此示例,我们将从一个用于计算报价的工具开始。
示例保险报价计算器:
TOOLS = [{
"name": "get_quote",
"description": "Calculate the insurance quote based on user input. Returned value is per month premium.",
"input_schema": {
"type": "object",
"properties": {
"make": {"type": "string", "description": "The make of the vehicle."},
"model": {"type": "string", "description": "The model of the vehicle."},
"year": {"type": "integer", "description": "The year the vehicle was manufactured."},
"mileage": {"type": "integer", "description": "The mileage on the vehicle."},
"driver_age": {"type": "integer", "description": "The age of the primary driver."}
},
"required": ["make", "model", "year", "mileage", "driver_age"]
}
}]
def get_quote(make, model, year, mileage, driver_age):
"""Returns the premium per month in USD"""
# You can call an http endpoint or a database to get the quote.
# Here, we simulate a delay of 1 seconds and return a fixed quote of 100.
time.sleep(1)
return 100在测试生产设置中部署提示并 运行评估 来了解您的提示效果如何是很困难的,所以让我们使用我们的提示、Anthropic SDK 和 streamlit 用户界面构建一个小应用程序。
在名为 chatbot.py 的文件中,首先设置 ChatBot 类,它将封装与 Anthropic SDK 的交互。
该类应该有两个主要方法:generate_message 和 process_user_input。
from anthropic import Anthropic
from config import IDENTITY, TOOLS, MODEL, get_quote
from dotenv import load_dotenv
load_dotenv()
class ChatBot:
def __init__(self, session_state):
self.anthropic = Anthropic()
self.session_state = session_state
def generate_message(
self,
messages,
max_tokens,
):
try:
response = self.anthropic.messages.create(
model=MODEL,
system=IDENTITY,
max_tokens=max_tokens,
messages=messages,
tools=TOOLS,
)
return response
except Exception as e:
return {"error": str(e)}
def process_user_input(self, user_input):
self.session_state.messages.append({"role": "user", "content": user_input})
response_message = self.generate_message(
messages=self.session_state.messages,
max_tokens=2048,
)
if "error" in response_message:
return f"An error occurred: {response_message['error']}"
if response_message.content[-1].type == "tool_use":
tool_use = response_message.content[-1]
func_name = tool_use.name
func_params = tool_use.input
tool_use_id = tool_use.id
result = self.handle_tool_use(func_name, func_params)
self.session_state.messages.append(
{"role": "assistant", "content": response_message.content}
)
self.session_state.messages.append({
"role": "user",
"content": [{
"type": "tool_result",
"tool_use_id": tool_use_id,
"content": f"{result}",
}],
})
follow_up_response = self.generate_message(
messages=self.session_state.messages,
max_tokens=2048,
)
if "error" in follow_up_response:
return f"An error occurred: {follow_up_response['error']}"
response_text = follow_up_response.content[0].text
self.session_state.messages.append(
{"role": "assistant", "content": response_text}
)
return response_text
elif response_message.content[0].type == "text":
response_text = response_message.content[0].text
self.session_state.messages.append(
{"role": "assistant", "content": response_text}
)
return response_text
else:
raise Exception("An error occurred: Unexpected response type")
def handle_tool_use(self, func_name, func_params):
if func_name == "get_quote":
premium = get_quote(**func_params)
return f"Quote generated: ${premium:.2f} per month"
raise Exception("An unexpected tool was used")使用主方法通过 Streamlit 测试部署此代码。此 main() 函数设置基于 Streamlit 的聊天界面。
我们将在名为 app.py 的文件中执行此操作
import streamlit as st
from chatbot import ChatBot
from config import TASK_SPECIFIC_INSTRUCTIONS
def main():
st.title("Chat with Eva, Acme Insurance Company's Assistant🤖")
if "messages" not in st.session_state:
st.session_state.messages = [
{'role': "user", "content": TASK_SPECIFIC_INSTRUCTIONS},
{'role': "assistant", "content": "Understood"},
]
chatbot = ChatBot(st.session_state)
# Display user and assistant messages skipping the first two
for message in st.session_state.messages[2:]:
# ignore tool use blocks
if isinstance(message["content"], str):
with st.chat_message(message["role"]):
st.markdown(message["content"])
if user_msg := st.chat_input("Type your message here..."):
st.chat_message("user").markdown(user_msg)
with st.chat_message("assistant"):
with st.spinner("Eva is thinking..."):
response_placeholder = st.empty()
full_response = chatbot.process_user_input(user_msg)
response_placeholder.markdown(full_response)
if __name__ == "__main__":
main()运行程序:
streamlit run app.py提示通常需要测试和优化才能做好生产准备。要确定解决方案的就绪情况,请使用结合定量和定性方法的系统流程来评估聊天机器人性能。基于您定义的成功标准创建 强大的经验评估 将允许您优化您的提示。
在复杂场景中,除了标准 提示工程技术 和 护栏实现策略 之外,考虑其他策略来改进性能可能会有帮助。以下是一些常见场景:
处理大量静态和动态上下文时,在提示中包含所有信息可能导致高成本、响应时间变慢和达到上下文窗口限制。在这种情况下,实现检索增强生成 (RAG) 技术可以显著改进性能和效率。
通过使用 Voyage 等嵌入模型 将信息转换为向量表示,您可以创建更可扩展和响应更快的系统。这种方法允许根据当前查询动态检索相关信息,而不是在每个提示中包含所有可能的上下文。
为支持用例实现 RAG RAG 配方 已被证明可以增加准确性、减少响应时间并降低具有广泛上下文要求的系统中的 API 成本。
处理需要实时信息的查询(如账户余额或政策详情)时,基于嵌入的 RAG 方法是不够的。相反,您可以利用工具使用来显著增强聊天机器人提供准确、实时响应的能力。例如,您可以使用工具使用来查找客户信息、检索订单详情和代表客户取消订单。
这种方法 在我们的工具使用:客户服务代理配方中概述,允许您无缝地将实时数据集成到 Claude 的响应中,并提供更个性化和高效的客户体验。
部署聊天机器人时,特别是在客户服务场景中,防止与滥用、超出范围的查询和不适当的回复相关的风险至关重要。虽然 Claude 对此类场景具有固有的弹性,但以下是加强聊天机器人护栏的其他步骤:
处理可能冗长的响应时,实现流式传输可以显著改进用户参与度和满意度。在这种情况下,用户逐步接收答案,而不是等待整个响应生成。
以下是如何实现流式传输的方法:
在某些情况下,流式传输使得能够使用具有更高基础延迟的更高级模型,因为渐进式显示减轻了更长处理时间的影响。
随着聊天机器人复杂性的增长,您的应用程序架构可以相应地发展。在向架构添加进一步层之前,请考虑以下不太详尽的选项:
如果您的聊天机器人处理极其多样化的任务,您可能想考虑添加 单独的意图分类器 来路由初始客户查询。对于现有应用程序,这将涉及创建一个决策树,该决策树将通过分类器路由客户查询,然后路由到专门的对话(具有自己的工具集和系统提示)。请注意,此方法需要额外的 Claude 调用,可能会增加延迟。
虽然我们的示例侧重于在 Streamlit 环境中可调用的 Python 函数,但部署 Claude 进行实时支持聊天机器人需要 API 服务。
以下是您可以如何处理这个问题:
创建 API 包装器:围绕您的分类函数开发一个简单的 API 包装器。例如,您可以使用 Flask API 或 Fast API 将您的代码包装成 HTTP 服务。您的 HTTP 服务可以接受用户输入并完整返回助手响应。因此,您的服务可能具有以下特征:
构建 Web 界面:为与 Claude 驱动的代理交互实现用户友好的 Web UI。
报价生成
访问我们的 RAG 食谱配方以获取更多示例代码和详细指导。