以下是一些關鍵指標,顯示您應該採用像 Claude 這樣的 LLM 來自動化部分客戶支援流程:
選擇 Claude 而非其他 LLM 的一些考量因素:
勾勒出理想的客戶互動,以定義您期望客戶如何以及何時與 Claude 互動。此大綱將有助於確定您解決方案的技術需求。
以下是汽車保險客戶支援的聊天互動範例:
客戶支援聊天是多種不同任務的集合,從回答問題到資訊擷取,再到針對請求採取行動,全部包裝在單一的客戶互動中。在開始建置之前,請將您理想的客戶互動拆解為您希望 Claude 能夠執行的每一項任務。這可確保您能針對每項任務對 Claude 進行提示和評估,並讓您清楚了解在撰寫測試案例時需要考量的互動範圍。
以下是與上述保險互動範例相關的關鍵任務:
問候與一般指引
產品資訊
對話管理
報價產生
與您的支援團隊合作,定義成功標準並撰寫詳細的評估,包含可衡量的基準和目標。
以下是可用於評估 Claude 執行已定義任務成功程度的標準和基準:
以下是可用於評估採用 Claude 進行支援的業務影響的標準和基準:
模型的選擇取決於成本、準確度和回應時間之間的權衡。
對於客戶支援聊天,Claude Opus 4.8 非常適合在智慧、延遲和成本之間取得平衡。然而,對於具有多個提示的對話流程(包括 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"""
# 您可以呼叫 HTTP 端點或資料庫來取得報價。
# 在此,我們模擬 1 秒的延遲並回傳固定報價 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")使用 main 方法透過 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)
# 顯示使用者與助理訊息,略過前兩則
for message in st.session_state.messages[2:]:
# 忽略工具使用區塊
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提示通常需要測試和最佳化才能達到生產就緒狀態。為了確定您解決方案的就緒程度,請使用結合量化和質化方法的系統化流程來評估聊天機器人的效能。根據您定義的成功標準建立強大的實證評估,將使您能夠最佳化您的提示。
在複雜的情境中,除了標準的提示工程技術和防護措施實作策略之外,考慮其他策略來改善效能可能會有所幫助。以下是一些常見的情境:
在處理大量靜態和動態上下文時,將所有資訊包含在提示中可能會導致高成本、較慢的回應時間,以及達到上下文視窗限制。在此情境中,實作「Retrieval Augmented Generation」(檢索增強生成),即 RAG 技術,可以顯著改善效能和效率。
透過使用像 Voyage 這樣的嵌入模型將資訊轉換為向量表示,您可以建立更具擴展性和回應性的系統。此方法允許根據目前的查詢動態擷取相關資訊,而不是在每個提示中包含所有可能的上下文。
針對支援使用案例實作 RAG(RAG 範例)已被證明可以在具有大量上下文需求的系統中提高準確度、縮短回應時間並降低 API 成本。
在處理需要即時資訊的查詢時(例如帳戶餘額或保單詳情),基於嵌入的 RAG 方法並不足夠。相反地,您可以運用工具使用來顯著增強聊天機器人提供準確、即時回應的能力。例如,您可以使用工具使用來查詢客戶資訊、擷取訂單詳情,以及代表客戶取消訂單。
此方法(在工具使用:客戶服務代理範例中概述)可讓您將即時資料無縫整合到 Claude 的回應中,並提供更個人化和高效的客戶體驗。
在部署聊天機器人時,特別是在客戶服務情境中,防止與濫用、超出範圍的查詢和不當回應相關的風險至關重要。雖然 Claude 本身對此類情境具有韌性,但以下是強化聊天機器人防護措施的額外步驟:
在處理可能較長的回應時,實作串流可以顯著改善使用者參與度和滿意度。在此情境中,使用者會逐步接收答案,而不是等待整個回應產生完成。
以下是實作串流的方法:
在某些情況下,串流可讓您使用具有較高基礎延遲的更進階模型,因為漸進式顯示可減輕較長處理時間的影響。
隨著聊天機器人複雜度的增加,您的應用程式架構可以隨之演進。在為架構新增更多層級之前,請考慮以下較不繁瑣的選項:
如果您的聊天機器人處理極為多樣化的任務,您可能需要考慮新增獨立的意圖分類器來路由初始客戶查詢。對於現有的應用程式,這將涉及建立一個決策樹,透過分類器將客戶查詢路由到專門的對話(具有各自的工具集和系統提示)。請注意,此方法需要額外呼叫 Claude,可能會增加延遲。
雖然上述範例著重於可在 Streamlit 環境中呼叫的 Python 函式,但部署 Claude 作為即時支援聊天機器人需要 API 服務。
以下是您可以採取的方法:
建立 API 包裝器:圍繞您的分類函式開發一個簡單的 API 包裝器。例如,您可以使用 Flask API 或 Fast API 將您的程式碼包裝成 HTTP 服務。您的 HTTP 服務可以接受使用者輸入並完整回傳助理回應。因此,您的服務可以具有以下特性:
建置網頁介面:實作使用者友善的網頁 UI,以與由 Claude 驅動的代理進行互動。
Was this page helpful?