🎯 Mục tiêu bài học
LangSmith và W&B là hai platforms hàng đầu cho LLM observability. LangSmith focus vào chain tracing, W&B cho experiment tracking.
Sau bài này, bạn sẽ:
✅ Setup và sử dụng LangSmith tracing cho LangChain apps ✅ Tạo evaluation datasets và chạy evaluations ✅ Setup Weights & Biases logging cho metrics ✅ Kết hợp LangSmith và W&B cho full observability
🔍 LangSmith Overview
Checkpoint
Bạn đã hiểu các capabilities chính của LangSmith chưa?
🛠️ Setup LangSmith
1pip install langsmith2 3# Environment variables4export LANGCHAIN_TRACING_V2=true5export LANGCHAIN_API_KEY=lsv2_pt_xxx6export LANGCHAIN_PROJECT=my-ai-project1import os2os.environ["LANGCHAIN_TRACING_V2"] = "true"3os.environ["LANGCHAIN_API_KEY"] = "lsv2_pt_xxx"4os.environ["LANGCHAIN_PROJECT"] = "my-ai-project"56# Moi LangChain call se tu dong duoc traced7from langchain_openai import ChatOpenAI8from langchain_core.prompts import ChatPromptTemplate910llm = ChatOpenAI(model="gpt-4o-mini")11chain = ChatPromptTemplate.from_messages([12 ("system", "Ban la AI assistant."),13 ("human", "{question}")14]) | llm1516# This call is automatically traced in LangSmith17result = chain.invoke({"question": "Hello!"})Checkpoint
Bạn đã setup được LangSmith tracing cho project chưa?
📊 LangSmith Tracing
1from langsmith import traceable23@traceable(name="process_document")4def process_document(doc_text: str):5 # Step 1: Summarize6 summary = summarize_chain.invoke({"text": doc_text})7 8 # Step 2: Extract entities9 entities = extract_chain.invoke({"text": doc_text})10 11 # Step 3: Classify12 category = classify_chain.invoke({"text": doc_text})13 14 return {15 "summary": summary,16 "entities": entities,17 "category": category18 }1920# Moi call se tao trace voi child spans21result = process_document("Long document text...")Checkpoint
Bạn đã hiểu cách sử dụng @traceable decorator để trace functions chưa?
🧪 LangSmith Evaluation
1from langsmith import Client2from langsmith.evaluation import evaluate34client = Client()56# Create dataset7dataset = client.create_dataset("qa-test")89# Add examples10client.create_examples(11 inputs=[12 {"question": "AI la gi?"},13 {"question": "LangChain la gi?"},14 ],15 outputs=[16 {"answer": "AI la tri tue nhan tao"},17 {"answer": "LangChain la framework cho LLM apps"},18 ],19 dataset_id=dataset.id20)2122# Define evaluator23def check_relevance(run, example):24 prediction = run.outputs["output"]25 reference = example.outputs["answer"]26 # Simple check27 return {"score": 1 if len(prediction) > 10 else 0}2829# Run evaluation30results = evaluate(31 lambda inputs: chain.invoke(inputs),32 data=dataset.name,33 evaluators=[check_relevance]34)Checkpoint
Bạn đã hiểu cách tạo evaluation datasets và chạy evaluations với LangSmith chưa?
🛠️ Weights & Biases Setup
1pip install wandb2wandb login1import wandb23# Initialize run4wandb.init(5 project="ai-deployment",6 config={7 "model": "gpt-4o-mini",8 "temperature": 0.7,9 "max_tokens": 100010 }11)1213# Log metrics14wandb.log({15 "tokens_used": 150,16 "latency": 0.45,17 "cost": 0.001,18 "quality_score": 8.519})2021# Finish22wandb.finish()Checkpoint
Bạn đã setup được W&B logging cho AI project chưa?
📊 W&B Table Logging
1import wandb23# Log results as table4table = wandb.Table(columns=["input", "output", "tokens", "latency", "score"])56for result in results:7 table.add_data(8 result["input"],9 result["output"],10 result["tokens"],11 result["latency"],12 result["score"]13 )1415wandb.log({"results": table})Checkpoint
Bạn đã hiểu cách log structured data dưới dạng tables trong W&B chưa?
📊 Combined Monitoring
1from langchain_core.callbacks import BaseCallbackHandler23class CombinedMonitor(BaseCallbackHandler):4 """Log to both LangSmith (auto) and W&B"""5 6 def __init__(self):7 self.start_time = None8 9 def on_llm_start(self, serialized, prompts, **kwargs):10 import time11 self.start_time = time.time()12 13 def on_llm_end(self, response, **kwargs):14 import time15 latency = time.time() - self.start_time16 17 usage = {}18 if response.llm_output:19 usage = response.llm_output.get("token_usage", {})20 21 # Log to W&B22 wandb.log({23 "tokens": usage.get("total_tokens", 0),24 "latency": latency,25 "prompt_tokens": usage.get("prompt_tokens", 0),26 "completion_tokens": usage.get("completion_tokens", 0)27 })Checkpoint
Bạn đã hiểu cách kết hợp LangSmith và W&B cho full observability chưa?
🎯 Tổng kết
Bài tập thực hành
- Setup LangSmith tracing cho LangChain app
- Tạo evaluation dataset và chạy eval
- Setup W&B logging cho metrics
- Build monitoring dashboard
Target: Full observability với LangSmith traces + W&B dashboards
Câu hỏi tự kiểm tra
- LangSmith tracing giúp debug LangChain applications như thế nào? Traces hiển thị những thông tin gì?
- LangSmith evaluation cho phép đánh giá chất lượng output của LLM bằng cách nào? Dataset và evaluators hoạt động ra sao?
- Weights & Biases (W&B) khác gì với LangSmith và khi nào nên dùng cả hai công cụ cùng lúc?
- CombinedMonitor callback handler kết hợp LangSmith và W&B như thế nào để có full observability?
🎉 Tuyệt vời! Bạn đã hoàn thành bài học LangSmith và W&B!
Tiếp theo: Chúng ta sẽ học về Security và Guardrails để bảo vệ AI applications.
