MinAI - Về trang chủ
Lý thuyết
9/1335 phút
Đang tải...

LangSmith va Weights & Biases

LLM observability platforms - LangSmith tracing, W&B experiment tracking

0

🎯 Mục tiêu bài học

TB5 min

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

1

🔍 LangSmith Overview

TB5 min
Diagram
Đang vẽ diagram...

Checkpoint

Bạn đã hiểu các capabilities chính của LangSmith chưa?

2

🛠️ Setup LangSmith

TB5 min
Bash
1pip install langsmith
2
3# Environment variables
4export LANGCHAIN_TRACING_V2=true
5export LANGCHAIN_API_KEY=lsv2_pt_xxx
6export LANGCHAIN_PROJECT=my-ai-project
python.py
1import os
2os.environ["LANGCHAIN_TRACING_V2"] = "true"
3os.environ["LANGCHAIN_API_KEY"] = "lsv2_pt_xxx"
4os.environ["LANGCHAIN_PROJECT"] = "my-ai-project"
5
6# Moi LangChain call se tu dong duoc traced
7from langchain_openai import ChatOpenAI
8from langchain_core.prompts import ChatPromptTemplate
9
10llm = ChatOpenAI(model="gpt-4o-mini")
11chain = ChatPromptTemplate.from_messages([
12 ("system", "Ban la AI assistant."),
13 ("human", "{question}")
14]) | llm
15
16# This call is automatically traced in LangSmith
17result = chain.invoke({"question": "Hello!"})

Checkpoint

Bạn đã setup được LangSmith tracing cho project chưa?

3

📊 LangSmith Tracing

TB5 min
python.py
1from langsmith import traceable
2
3@traceable(name="process_document")
4def process_document(doc_text: str):
5 # Step 1: Summarize
6 summary = summarize_chain.invoke({"text": doc_text})
7
8 # Step 2: Extract entities
9 entities = extract_chain.invoke({"text": doc_text})
10
11 # Step 3: Classify
12 category = classify_chain.invoke({"text": doc_text})
13
14 return {
15 "summary": summary,
16 "entities": entities,
17 "category": category
18 }
19
20# Moi call se tao trace voi child spans
21result = process_document("Long document text...")

Checkpoint

Bạn đã hiểu cách sử dụng @traceable decorator để trace functions chưa?

4

🧪 LangSmith Evaluation

TB5 min
python.py
1from langsmith import Client
2from langsmith.evaluation import evaluate
3
4client = Client()
5
6# Create dataset
7dataset = client.create_dataset("qa-test")
8
9# Add examples
10client.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.id
20)
21
22# Define evaluator
23def check_relevance(run, example):
24 prediction = run.outputs["output"]
25 reference = example.outputs["answer"]
26 # Simple check
27 return {"score": 1 if len(prediction) > 10 else 0}
28
29# Run evaluation
30results = 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?

5

🛠️ Weights & Biases Setup

TB5 min
Bash
1pip install wandb
2wandb login
python.py
1import wandb
2
3# Initialize run
4wandb.init(
5 project="ai-deployment",
6 config={
7 "model": "gpt-4o-mini",
8 "temperature": 0.7,
9 "max_tokens": 1000
10 }
11)
12
13# Log metrics
14wandb.log({
15 "tokens_used": 150,
16 "latency": 0.45,
17 "cost": 0.001,
18 "quality_score": 8.5
19})
20
21# Finish
22wandb.finish()

Checkpoint

Bạn đã setup được W&B logging cho AI project chưa?

6

📊 W&B Table Logging

TB5 min
python.py
1import wandb
2
3# Log results as table
4table = wandb.Table(columns=["input", "output", "tokens", "latency", "score"])
5
6for result in results:
7 table.add_data(
8 result["input"],
9 result["output"],
10 result["tokens"],
11 result["latency"],
12 result["score"]
13 )
14
15wandb.log({"results": table})

Checkpoint

Bạn đã hiểu cách log structured data dưới dạng tables trong W&B chưa?

7

📊 Combined Monitoring

TB5 min
python.py
1from langchain_core.callbacks import BaseCallbackHandler
2
3class CombinedMonitor(BaseCallbackHandler):
4 """Log to both LangSmith (auto) and W&B"""
5
6 def __init__(self):
7 self.start_time = None
8
9 def on_llm_start(self, serialized, prompts, **kwargs):
10 import time
11 self.start_time = time.time()
12
13 def on_llm_end(self, response, **kwargs):
14 import time
15 latency = time.time() - self.start_time
16
17 usage = {}
18 if response.llm_output:
19 usage = response.llm_output.get("token_usage", {})
20
21 # Log to W&B
22 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?

8

🎯 Tổng kết

TB5 min

Bài tập thực hành

Hands-on Exercise
  1. Setup LangSmith tracing cho LangChain app
  2. Tạo evaluation dataset và chạy eval
  3. Setup W&B logging cho metrics
  4. Build monitoring dashboard

Target: Full observability với LangSmith traces + W&B dashboards

Câu hỏi tự kiểm tra

  1. LangSmith tracing giúp debug LangChain applications như thế nào? Traces hiển thị những thông tin gì?
  2. 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?
  3. 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?
  4. 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.


🚀 Bài tiếp theo

Security va Guardrails →