บทที่ 2: การติดตั้งและตั้งค่าสภาพแวดล้อม
ความต้องการของระบบ
Section titled “ความต้องการของระบบ”ก่อนเริ่มต้น ให้ตรวจสอบว่าคุณมีสิ่งเหล่านี้:
| รายการ | เวอร์ชันขั้นต่ำ | หมายเหตุ |
|---|---|---|
| Python | 3.10+ | แนะนำ 3.12 |
| pip | 23.0+ | Python package manager |
| Node.js | 18+ | (ถ้าใช้ LangServe) |
| Git | 2.0+ | สำหรับ version control |
Version Baseline (Release 2026-02-13)
Section titled “Version Baseline (Release 2026-02-13)”เพื่อให้ผลลัพธ์ของตัวอย่างในหนังสือใกล้เคียงกัน แนะนำให้ยึด baseline นี้เป็นค่าเริ่มต้น และค่อยอัปเดตเป็นรอบ
| Component | Recommended | หมายเหตุ |
|---|---|---|
| Python | 3.12.x | baseline หลักของหนังสือ |
| langchain | 1.0.x | framework หลัก |
| langchain-core | 1.0.x | core abstractions |
| langgraph | 0.2.x | workflow/state graph |
| langchain-openai | 0.3.x | OpenAI integration |
| langchain-community | 0.3.x | community integrations |
| langchain-google-genai | 2.0.x | Gemini integration |
| chromadb | 0.5.x | vector store ตัวอย่าง |
| python-dotenv | 1.0.x | env management |
การติดตั้ง Python
Section titled “การติดตั้ง Python”# ดาวน์โหลดจาก python.org หรือใช้ wingetwinget install Python.Python.3.12
# ตรวจสอบเวอร์ชันpython --version# ใช้ Homebrewbrew install python@3.12
# ตรวจสอบเวอร์ชันpython3 --version# Ubuntu/Debiansudo apt updatesudo apt install python3.12 python3.12-venv python3-pip
# ตรวจสอบเวอร์ชันpython3 --versionสร้างโปรเจกต์ใหม่
Section titled “สร้างโปรเจกต์ใหม่”-
สร้างโฟลเดอร์โปรเจกต์
Terminal window mkdir langchain-tutorialcd langchain-tutorial -
สร้าง Virtual Environment
Terminal window # สร้าง venvpython -m venv .venv# เปิดใช้งาน (Windows).venv\Scripts\activate# เปิดใช้งาน (macOS/Linux)source .venv/bin/activate -
ติดตั้ง LangChain
Terminal window # ติดตั้ง LangChain corepip install langchain langchain-core# ติดตั้ง LLM providerspip install langchain-openai # สำหรับ OpenAIpip install langchain-google-genai # สำหรับ Google Geminipip install langchain-anthropic # สำหรับ Anthropic Claude# ติดตั้ง Community packagespip install langchain-community -
สร้างไฟล์ requirements.txt
requirements.txt langchain==1.0.*langchain-core==1.0.*langchain-openai==0.3.*langchain-community==0.3.*langchain-google-genai==2.0.*python-dotenv==1.0.*chromadb==0.5.*langgraph==0.2.*Terminal window # สร้าง lock file สำหรับ release buildpip freeze > requirements.lock.txt# ติดตั้งแบบ lock เพื่อให้ผลลัพธ์เหมือนกันทุกเครื่องpip install -r requirements.lock.txt
การตั้งค่า API Keys
Section titled “การตั้งค่า API Keys”รับ API Key จาก OpenAI
Section titled “รับ API Key จาก OpenAI”- ไปที่ platform.openai.com
- สมัครบัญชีหรือเข้าสู่ระบบ
- ไปที่ API Keys → Create new secret key
- Copy key ที่ได้ (เริ่มต้นด้วย
sk-)
รับ API Key จาก Google AI
Section titled “รับ API Key จาก Google AI”- ไปที่ aistudio.google.com
- เข้าสู่ระบบด้วย Google Account
- คลิก Get API key → Create API key
- Copy key ที่ได้
จัดเก็บ API Key อย่างปลอดภัย
Section titled “จัดเก็บ API Key อย่างปลอดภัย”สร้างไฟล์ .env ในโฟลเดอร์โปรเจกต์:
# OpenAIOPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxx
# Google AIGOOGLE_API_KEY=AIzaxxxxxxxxxxxxxxxxxxxxxxxx
# Anthropic (ถ้าใช้)ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxxxxxx
# LangSmith (สำหรับ monitoring)LANGSMITH_API_KEY=lsv2_xxxxxxxxxxxxxxxxLANGSMITH_TRACING=trueLANGSMITH_PROJECT=langchain-tutorialโหลด API Key ในโค้ด Python
Section titled “โหลด API Key ในโค้ด Python”คำอธิบาย: โค้ดตัวอย่างด้านล่างแสดงวิธีใช้งานด้วย Python ตามหัวข้อนี้แบบทีละขั้นตอน
import osfrom dotenv import load_dotenv
# โหลดค่าจากไฟล์ .envload_dotenv()
# ตรวจสอบว่า API Key ถูกโหลดแล้วapi_key = os.getenv("OPENAI_API_KEY")if api_key: print("✅ API Key loaded successfully!")else: print("❌ API Key not found. Check your .env file.")ทดสอบการติดตั้ง
Section titled “ทดสอบการติดตั้ง”ทดสอบกับ OpenAI
Section titled “ทดสอบกับ OpenAI”คำอธิบาย: โค้ดตัวอย่างด้านล่างแสดงวิธีใช้งานด้วย Python ตามหัวข้อนี้แบบทีละขั้นตอน
from langchain_openai import ChatOpenAI
# สร้าง Chat Modelllm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
# ทดสอบเรียกใช้response = llm.invoke("สวัสดี! ช่วยแนะนำตัวหน่อย")print(response.content)ผลลัพธ์ตัวอย่าง:
สวัสดีครับ! ผมเป็น AI Assistant ที่พัฒนาโดย OpenAIพร้อมช่วยเหลือคุณในหลายเรื่อง ไม่ว่าจะเป็นการตอบคำถามเขียนโค้ด แปลภาษา หรือให้คำแนะนำต่างๆ ครับทดสอบกับ Google Gemini
Section titled “ทดสอบกับ Google Gemini”คำอธิบาย: โค้ดตัวอย่างด้านล่างแสดงวิธีใช้งานด้วย Python ตามหัวข้อนี้แบบทีละขั้นตอน
from langchain_google_genai import ChatGoogleGenerativeAI
# สร้าง Chat Modelllm = ChatGoogleGenerativeAI(model="gemini-2.0-flash")
# ทดสอบเรียกใช้response = llm.invoke("สวัสดี! ช่วยแนะนำตัวหน่อย")print(response.content)โครงสร้างโปรเจกต์แนะนำ
Section titled “โครงสร้างโปรเจกต์แนะนำ”คำอธิบาย: แผนภาพด้านล่างสรุปโฟลว์การทำงานให้เห็นภาพรวมของหัวข้อนี้อย่างชัดเจน
graph TD
Root["langchain-tutorial/"]
Root --> Env[".env (API Keys, don't commit)"]
Root --> Gitignore[".gitignore"]
Root --> Req["requirements.txt"]
Root --> Notebooks["notebooks/ (Jupyter notebooks)"]
Notebooks --> NB1["01_basics.ipynb"]
Notebooks --> NB2["02_chains.ipynb"]
Notebooks --> NBX["..."]
Root --> Src["src/ (Source code)"]
Src --> Init["__init__.py"]
Src --> Chains["chains/ (Custom chains)"]
Src --> Agents["agents/ (Custom agents)"]
Src --> Tools["tools/ (Custom tools)"]
Src --> Utils["utils/ (Utility functions)"]
Root --> Data["data/ (RAG data)"]
Data --> Docs["documents/"]
Data --> VS["vectorstore/"]
เครื่องมือพัฒนาที่แนะนำ
Section titled “เครื่องมือพัฒนาที่แนะนำ”| เครื่องมือ | ประเภท | เหตุผล |
|---|---|---|
| VS Code | Code Editor | Extension มากมาย, Python support ดี |
| Jupyter Notebook | Interactive Coding | เหมาะสำหรับทดลองโค้ด LLM |
| LangSmith | Monitoring | Debug และ trace LLM calls |
| Postman | API Testing | ทดสอบ API endpoints |
VS Code Extensions ที่แนะนำ
Section titled “VS Code Extensions ที่แนะนำ”คำอธิบาย: ตัวอย่างคอนฟิกด้านล่างคือรูปแบบที่แนะนำให้ใช้ในหัวข้อนี้
{ "recommendations": [ "ms-python.python", "ms-python.vscode-pylance", "ms-toolsai.jupyter", "charliermarsh.ruff", "github.copilot" ]}