Skip to content

บทที่ 2: การติดตั้งและตั้งค่าสภาพแวดล้อม

ความต้องการของระบบ

Section titled “ความต้องการของระบบ”

ก่อนเริ่มต้น ให้ตรวจสอบว่าคุณมีสิ่งเหล่านี้:

รายการเวอร์ชันขั้นต่ำหมายเหตุ
Python3.10+แนะนำ 3.12
pip23.0+Python package manager
Node.js18+(ถ้าใช้ LangServe)
Git2.0+สำหรับ version control

เพื่อให้ผลลัพธ์ของตัวอย่างในหนังสือใกล้เคียงกัน แนะนำให้ยึด baseline นี้เป็นค่าเริ่มต้น และค่อยอัปเดตเป็นรอบ

ComponentRecommendedหมายเหตุ
Python3.12.xbaseline หลักของหนังสือ
langchain1.0.xframework หลัก
langchain-core1.0.xcore abstractions
langgraph0.2.xworkflow/state graph
langchain-openai0.3.xOpenAI integration
langchain-community0.3.xcommunity integrations
langchain-google-genai2.0.xGemini integration
chromadb0.5.xvector store ตัวอย่าง
python-dotenv1.0.xenv management

Terminal window
# ดาวน์โหลดจาก python.org หรือใช้ winget
winget install Python.Python.3.12
# ตรวจสอบเวอร์ชัน
python --version

สร้างโปรเจกต์ใหม่

Section titled “สร้างโปรเจกต์ใหม่”
  1. สร้างโฟลเดอร์โปรเจกต์

    Terminal window
    mkdir langchain-tutorial
    cd langchain-tutorial
  2. สร้าง Virtual Environment

    Terminal window
    # สร้าง venv
    python -m venv .venv
    # เปิดใช้งาน (Windows)
    .venv\Scripts\activate
    # เปิดใช้งาน (macOS/Linux)
    source .venv/bin/activate
  3. ติดตั้ง LangChain

    Terminal window
    # ติดตั้ง LangChain core
    pip install langchain langchain-core
    # ติดตั้ง LLM providers
    pip install langchain-openai # สำหรับ OpenAI
    pip install langchain-google-genai # สำหรับ Google Gemini
    pip install langchain-anthropic # สำหรับ Anthropic Claude
    # ติดตั้ง Community packages
    pip install langchain-community
  4. สร้างไฟล์ 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 build
    pip freeze > requirements.lock.txt
    # ติดตั้งแบบ lock เพื่อให้ผลลัพธ์เหมือนกันทุกเครื่อง
    pip install -r requirements.lock.txt

  1. ไปที่ platform.openai.com
  2. สมัครบัญชีหรือเข้าสู่ระบบ
  3. ไปที่ API KeysCreate new secret key
  4. Copy key ที่ได้ (เริ่มต้นด้วย sk-)
  1. ไปที่ aistudio.google.com
  2. เข้าสู่ระบบด้วย Google Account
  3. คลิก Get API keyCreate API key
  4. Copy key ที่ได้

จัดเก็บ API Key อย่างปลอดภัย

Section titled “จัดเก็บ API Key อย่างปลอดภัย”

สร้างไฟล์ .env ในโฟลเดอร์โปรเจกต์:

.env
# OpenAI
OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxx
# Google AI
GOOGLE_API_KEY=AIzaxxxxxxxxxxxxxxxxxxxxxxxx
# Anthropic (ถ้าใช้)
ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxxxxxx
# LangSmith (สำหรับ monitoring)
LANGSMITH_API_KEY=lsv2_xxxxxxxxxxxxxxxx
LANGSMITH_TRACING=true
LANGSMITH_PROJECT=langchain-tutorial

โหลด API Key ในโค้ด Python

Section titled “โหลด API Key ในโค้ด Python”

คำอธิบาย: โค้ดตัวอย่างด้านล่างแสดงวิธีใช้งานด้วย Python ตามหัวข้อนี้แบบทีละขั้นตอน

main.py
import os
from dotenv import load_dotenv
# โหลดค่าจากไฟล์ .env
load_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 “ทดสอบการติดตั้ง”

คำอธิบาย: โค้ดตัวอย่างด้านล่างแสดงวิธีใช้งานด้วย Python ตามหัวข้อนี้แบบทีละขั้นตอน

test_openai.py
from langchain_openai import ChatOpenAI
# สร้าง Chat Model
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
# ทดสอบเรียกใช้
response = llm.invoke("สวัสดี! ช่วยแนะนำตัวหน่อย")
print(response.content)

ผลลัพธ์ตัวอย่าง:

สวัสดีครับ! ผมเป็น AI Assistant ที่พัฒนาโดย OpenAI
พร้อมช่วยเหลือคุณในหลายเรื่อง ไม่ว่าจะเป็นการตอบคำถาม
เขียนโค้ด แปลภาษา หรือให้คำแนะนำต่างๆ ครับ

คำอธิบาย: โค้ดตัวอย่างด้านล่างแสดงวิธีใช้งานด้วย Python ตามหัวข้อนี้แบบทีละขั้นตอน

test_gemini.py
from langchain_google_genai import ChatGoogleGenerativeAI
# สร้าง Chat Model
llm = 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 CodeCode EditorExtension มากมาย, Python support ดี
Jupyter NotebookInteractive Codingเหมาะสำหรับทดลองโค้ด LLM
LangSmithMonitoringDebug และ trace LLM calls
PostmanAPI Testingทดสอบ API endpoints

VS Code Extensions ที่แนะนำ

Section titled “VS Code Extensions ที่แนะนำ”

คำอธิบาย: ตัวอย่างคอนฟิกด้านล่างคือรูปแบบที่แนะนำให้ใช้ในหัวข้อนี้

.vscode/extensions.json
{
"recommendations": [
"ms-python.python",
"ms-python.vscode-pylance",
"ms-toolsai.jupyter",
"charliermarsh.ruff",
"github.copilot"
]
}