要遵循本指南,您需要:
ANTHROPIC_API_KEY 環境變數)安裝所需的套件:
pip install anthropic streamlit python-dotenv以下是一些關鍵指標,表明您應該採用像 Claude 這樣的 LLM 來自動化部分客戶支援流程:
選擇 Claude 而非其他 LLM 的一些考量因素:
概述理想的客戶互動,以定義您期望客戶如何以及何時與 Claude 互動。此概述將有助於確定您解決方案的技術需求。
以下是汽車保險客戶支援的聊天互動範例:
客戶支援聊天是多個不同任務的集合,從問題回答到資訊檢索再到對請求採取行動,全部包含在單一客戶互動中。在開始建構之前,將您理想的客戶互動分解為您希望 Claude 能夠執行的每項任務。這可確保您可以針對每項任務提示和評估 Claude,並讓您清楚了解在撰寫測試案例時需要考慮的互動範圍。
以下是與範例保險互動相關的關鍵任務:
問候和一般指引
產品資訊
對話管理
報價生成
與您的支援團隊合作,定義成功標準並撰寫詳細的評估,包含可衡量的基準和目標。
以下是可用於評估 Claude 執行已定義任務成功程度的標準和基準:
以下是可用於評估採用 Claude 進行支援的業務影響的標準和基準:
模型的選擇取決於成本、準確度和回應時間之間的權衡。
對於客戶支援聊天,Claude Opus 4.8 非常適合平衡智慧、「latency」(延遲)和成本。然而,對於具有多個提示的對話流程(包括 RAG、工具使用或長上下文提示)的情況,Claude Haiku 4.5 可能更適合優化延遲。
使用 Claude 進行客戶支援需要 Claude 擁有足夠的指引和上下文以適當回應,同時具有足夠的靈活性來處理廣泛的客戶詢問。
首先撰寫強大提示的元素,從「system prompt」(系統提示)開始。建立一個名為 config.py 的檔案,並將以下每個區塊新增到其中:
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 完成任務所需的提示部分會取得更大的成功。對於此汽車保險客戶支援範例,您將從「問候和一般指引」任務開始,逐步撰寫提示的所有部分。這也使除錯您的提示更容易,因為您可以更快地調整整體提示的個別部分。
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 能夠使用客戶端「tool use」(工具使用)功能動態地採取行動和檢索資訊。首先列出提示應使用的任何外部工具或 API。
對於此範例,從一個用於計算報價的工具開始。
將模型名稱、工具定義和存根實作新增到 config.py:
import time
MODEL = "claude-opus-4-8"
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提示通常需要測試和優化才能達到生產就緒狀態。要確定您解決方案的就緒程度,請使用結合定量和定性方法的系統化流程來評估聊天機器人的效能。根據您定義的成功標準建立強大的實證評估將使您能夠優化您的提示。
在複雜的情境中,除了標準的提示工程技術和防護措施實作策略之外,考慮其他策略來改善效能可能會有所幫助。以下是一些常見情境:
在處理大量靜態和動態上下文時,將所有資訊包含在提示中可能導致高成本、較慢的回應時間以及達到「context window」(上下文視窗)限制。在這種情況下,實作「Retrieval Augmented Generation」(檢索增強生成),即 RAG 技術可以改善效能和效率。
透過使用像 Voyage 這樣的嵌入模型將資訊轉換為向量表示,您可以建立更具可擴展性和回應性的系統。這種方法允許根據當前查詢動態檢索相關資訊,而不是在每個提示中包含所有可能的上下文。
在具有大量上下文需求的系統中,為支援使用案例實作 RAG 已被證明可以提高準確度、減少回應時間並降低 API 成本。請參閱 RAG 範例以獲取實作範例。
在處理需要即時資訊的查詢時,例如帳戶餘額或保單詳情,基於嵌入的 RAG 方法是不夠的。相反,工具使用可以增強您的聊天機器人提供準確、即時回應的能力。例如,您可以使用工具使用來查詢客戶資訊、檢索訂單詳情以及代表客戶取消訂單。
這種方法在工具使用:客戶服務代理範例中有所概述,可讓您將即時資料整合到 Claude 的回應中,並提供更個人化和高效的客戶體驗。
在部署聊天機器人時,特別是在客戶服務情境中,防止與濫用、超出範圍的查詢和不當回應相關的風險非常重要。雖然 Claude 本身對此類情境具有韌性,但以下是強化您的聊天機器人防護措施的額外步驟:
在處理可能冗長的回應時,實作「streaming」(串流)可以改善使用者參與度和滿意度。在這種情況下,使用者會逐步接收答案,而不是等待整個回應生成完成。
以下是實作串流的方法:
在某些情況下,串流使得能夠使用具有較高基礎延遲的更進階模型,因為漸進式顯示減輕了較長處理時間的影響。
隨著您的聊天機器人複雜性增加,您的應用程式架構可以隨之演進。在您為架構新增更多層級之前,請考慮以下較不繁瑣的選項:
如果您的聊天機器人處理極其多樣化的任務,您可能需要考慮新增單獨的意圖分類器來路由初始客戶查詢。對於現有應用程式,這將涉及建立一個決策樹,透過分類器路由客戶查詢,然後路由到專門的對話(具有自己的工具集和系統提示)。請注意,此方法需要對 Claude 進行額外呼叫,這可能會增加延遲。
雖然這些範例專注於可在 Streamlit 環境中呼叫的 Python 函式,但部署 Claude 用於即時支援聊天機器人需要 API 服務。
以下是您可以採取的方法:
建立 API 包裝器:圍繞您的分類函式開發一個簡單的 API 包裝器。例如,您可以使用 Flask API 或 Fast API 將您的程式碼包裝成 HTTP 服務。您的 HTTP 服務可以接受使用者輸入並完整返回 Assistant 回應。因此,您的服務可以具有以下特性:
建立網頁介面:實作一個使用者友善的網頁 UI,用於與 Claude 驅動的代理互動。
讓 Claude 存取您的 API,以便它可以代表客戶採取行動。
建立評估以根據您定義的成功標準衡量您的支援代理。
串流回應,讓客戶在答案生成時即可看到。
優化您的系統提示和範例以獲得更好的任務效能。
Was this page helpful?