以下是一些關鍵指標,表明您應該使用 Claude 等 LLM 來自動化客戶支援流程的部分:
選擇 Claude 而不是其他 LLM 的一些考慮因素:
概述理想的客戶互動,以定義您期望客戶與 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。