手上有兩本 LangChain 的書看好久了, 都沒有時間做測試, 今天做完 OpenAI API 的測試, 就開始來玩 LangChain 吧, 因為只是看書卻沒動手做的話, 讀到第三章就讀不下去了, 沒實作手感印象會很生疏, 前面看過的很快就忘記了.
LangChain 是一個開源的應用程式框架, 專門設計用來建構以大型語言模型 (LLMs) 為核心的應用程式, 例如像 ChatGPT 之類的聊天機器人, 智慧型搜尋引擎與問答系統, 以及 AI 代理人 (agents) 等, 讓開發者更容易將 LLM 與外部資料 (如向量資料庫, API, 文件) 結合.
LangChain 支援主要的雲端 LLM API, 例如 OpenAI, Gemini, Claude 等, 也支援本地自架模型例如 Ollama 或 Hugging Face Transformer 等, 可用的程式語言目前支援 Python 與 Javascript/TypeScript, 其發展大事記摘要如下 :
- 2022 年任職於舊金山新創公司 Robust Intelligence (現已被思科併購) 的 Harrison Chase 將其發展的 LangChain 框架於 GitHub 開源.
- 2023 年經由社群貢獻, 大量地整合更多功能, 支援向量資料庫 (例如 FAISS, Pinecone, Chroma 等), 雲端 LLM (例如 OpenAI, Anthropic) 與文件載入器 (PDF, Notion, 網頁) 等, 加入 RAG, Agents 架構與 Memory 記憶體系統, 以及推出 LangChainHub 與 LangSmith 平台.
- 2024 年進行模組化重構, 以輕核心 + 插件式擴充理念將系統切分成 langchain-core, langchain-openai, 與 langchain-community 等子套件, 推出 LangServe (可將 LLM 應用包裝成 REST API), 支援更多雲端 (Claude 3 與 Gemini) 與本地 (Ollama 與 llama.cpp) LLM.
- 2025 迄今與 Hugging Face, OpenAI, AWS, Google Cloud 等平台進行深度整合, 成為業界最常用的 LLM 應用開發框架之一.
LangChain 本身只是一個 LLM 應用的 orchestrator (協調器), 主要是負責統一調度和串聯多個元件, 資源或服務, 它不包含 LLM 模型本身, 也不訓練模型, 而是負責 "整合, 調度, 組裝, 記憶與回應". 以人體來比喻, LLM 相當於大腦, LangChain 就是調度神經反應的腦幹, 而資料來源, 資料庫, 與工具是則是感官與四肢.
參考 :
對於入門者而言可用 pip install langchain 指令進行完整安裝, 這會安裝下列 Langchain 的三大模組 (包含 Langchain 本體與許多相依套件), 大約會占用 300~500MB 磁碟空間 :
- langchain-core (底層的輕量核心函式庫) :
是 LangChain 最基本的運作骨架, 包含 Runnable, PromptTemplate, BaseMessage, ChatMessage, OutputParser,tool, callback, memory 等核心類別 (約 10MB 左右). - langchain-community (第三方工具與服務的整合支援) :
包含許多第三方套件的整合套件, 例如向量資料庫 (Chroma), 模型 (OpenAI, Hugging Face 等) 與文件載入器等. - langchain (應用邏輯與 Legacy 相容) :
這是 LangChain 的主套件, 包含許多語法糖例如 chains, agents, memory 等, 以及與過去版本相容的介面, 用來建構與串接各種高階應用邏輯.
對於想自己控制相依套件以減少套件大小的進階使用者而言 (例如考慮 Docker 映像檔要盡量小, 或在嵌入式裝置如樹莓派上使用 LangChain), 可先只安裝 langchain-core (只包含核心抽象如 Chain, Prompt, Runnable 等), 然後再視需要自行安裝其他套件.
我選擇在本機下安裝完整的 langchain 套件, 因為 Langchain 很多教學用的元件如 Memory, Retrievers, 與 loaders 等都在完整版內, 初學者完整安裝 langchain 可以直接跑範例, 練習 RAG, ChatBot, Agents 等功能. 而且 LangChain 本身只是一個 orchestrator, 它不做模型推論故用不到 GPU, 可以連接雲端 LLM 完全不吃本機算力, 所以即使是文書型筆電也能跑得嚇嚇叫.
注意, 因為 Python v3.8 已於 2024 年底步入 EOL (End Of Life), 因此安裝最新版 LangChain 要求 Python v3.9 或更高版本.
D:\python\test>pip install langchain
Collecting langchain
Downloading langchain-0.3.25-py3-none-any.whl.metadata (7.8 kB)
Collecting langchain-core<1.0.0,>=0.3.58 (from langchain)
Downloading langchain_core-0.3.58-py3-none-any.whl.metadata (5.9 kB)
Collecting langchain-text-splitters<1.0.0,>=0.3.8 (from langchain)
Downloading langchain_text_splitters-0.3.8-py3-none-any.whl.metadata (1.9 kB)
Collecting langsmith<0.4,>=0.1.17 (from langchain)
Downloading langsmith-0.3.42-py3-none-any.whl.metadata (15 kB)
Collecting pydantic<3.0.0,>=2.7.4 (from langchain)
Downloading pydantic-2.11.4-py3-none-any.whl.metadata (66 kB)
Requirement already satisfied: SQLAlchemy<3,>=1.4 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from langchain) (1.4.41)
Requirement already satisfied: requests<3,>=2 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from langchain) (2.31.0)
Requirement already satisfied: PyYAML>=5.3 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from langchain) (6.0.1)
Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from langchain) (4.0.3)
Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from langchain-core<1.0.0,>=0.3.58->langchain) (8.2.3)
Collecting jsonpatch<2.0,>=1.33 (from langchain-core<1.0.0,>=0.3.58->langchain)
Downloading jsonpatch-1.33-py2.py3-none-any.whl.metadata (3.0 kB)
Collecting packaging<25,>=23.2 (from langchain-core<1.0.0,>=0.3.58->langchain)
Downloading packaging-24.2-py3-none-any.whl.metadata (3.2 kB)
Requirement already satisfied: typing-extensions>=4.7 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from langchain-core<1.0.0,>=0.3.58->langchain) (4.12.2)
Requirement already satisfied: httpx<1,>=0.23.0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from langsmith<0.4,>=0.1.17->langchain) (0.28.1)
Collecting orjson<4.0.0,>=3.9.14 (from langsmith<0.4,>=0.1.17->langchain)
Downloading orjson-3.10.18-cp310-cp310-win_amd64.whl.metadata (43 kB)
Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from langsmith<0.4,>=0.1.17->langchain) (1.0.0)
Collecting zstandard<0.24.0,>=0.23.0 (from langsmith<0.4,>=0.1.17->langchain)
Downloading zstandard-0.23.0-cp310-cp310-win_amd64.whl.metadata (3.0 kB)
Collecting annotated-types>=0.6.0 (from pydantic<3.0.0,>=2.7.4->langchain)
Downloading annotated_types-0.7.0-py3-none-any.whl.metadata (15 kB)
Collecting pydantic-core==2.33.2 (from pydantic<3.0.0,>=2.7.4->langchain)
Downloading pydantic_core-2.33.2-cp310-cp310-win_amd64.whl.metadata (6.9 kB)
Collecting typing-inspection>=0.4.0 (from pydantic<3.0.0,>=2.7.4->langchain)
Downloading typing_inspection-0.4.0-py3-none-any.whl.metadata (2.6 kB)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from requests<3,>=2->langchain) (3.2.0)
Requirement already satisfied: idna<4,>=2.5 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from requests<3,>=2->langchain) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from requests<3,>=2->langchain) (1.26.19)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from requests<3,>=2->langchain) (2023.7.22)
Requirement already satisfied: greenlet!=0.4.17 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from SQLAlchemy<3,>=1.4->langchain) (2.0.2)
Requirement already satisfied: anyio in c:\users\tony1\appdata\roaming\python\python310\site-packages (from httpx<1,>=0.23.0->langsmith<0.4,>=0.1.17->langchain) (3.7.1)
Requirement already satisfied: httpcore==1.* in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from httpx<1,>=0.23.0->langsmith<0.4,>=0.1.17->langchain) (1.0.7)
Requirement already satisfied: h11<0.15,>=0.13 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith<0.4,>=0.1.17->langchain) (0.14.0)
Requirement already satisfied: jsonpointer>=1.9 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<1.0.0,>=0.3.58->langchain) (2.4)
Requirement already satisfied: sniffio>=1.1 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from anyio->httpx<1,>=0.23.0->langsmith<0.4,>=0.1.17->langchain) (1.3.0)
Requirement already satisfied: exceptiongroup in c:\users\tony1\appdata\roaming\python\python310\site-packages (from anyio->httpx<1,>=0.23.0->langsmith<0.4,>=0.1.17->langchain) (1.1.3)
Downloading langchain-0.3.25-py3-none-any.whl (1.0 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.0/1.0 MB 6.9 MB/s eta 0:00:00
Downloading langchain_core-0.3.58-py3-none-any.whl (437 kB)
Downloading langchain_text_splitters-0.3.8-py3-none-any.whl (32 kB)
Downloading langsmith-0.3.42-py3-none-any.whl (360 kB)
Downloading pydantic-2.11.4-py3-none-any.whl (443 kB)
Downloading pydantic_core-2.33.2-cp310-cp310-win_amd64.whl (2.0 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.0/2.0 MB 7.2 MB/s eta 0:00:00
Downloading annotated_types-0.7.0-py3-none-any.whl (13 kB)
Downloading jsonpatch-1.33-py2.py3-none-any.whl (12 kB)
Downloading orjson-3.10.18-cp310-cp310-win_amd64.whl (134 kB)
Downloading packaging-24.2-py3-none-any.whl (65 kB)
Downloading typing_inspection-0.4.0-py3-none-any.whl (14 kB)
Downloading zstandard-0.23.0-cp310-cp310-win_amd64.whl (495 kB)
Installing collected packages: zstandard, typing-inspection, pydantic-core, packaging, orjson, jsonpatch, annotated-types, pydantic, langsmith, langchain-core, langchain-text-splitters, langchain
Attempting uninstall: pydantic-core
Found existing installation: pydantic_core 2.14.6
Uninstalling pydantic_core-2.14.6:
Successfully uninstalled pydantic_core-2.14.6
WARNING: Failed to remove contents in a temporary directory 'C:\Users\tony1\AppData\Roaming\Python\Python310\site-packages\~ydantic_core'.
You can safely remove it manually.
Attempting uninstall: packaging
Found existing installation: packaging 23.1
Uninstalling packaging-23.1:
Successfully uninstalled packaging-23.1
Attempting uninstall: orjson
Found existing installation: orjson 3.9.7
Uninstalling orjson-3.9.7:
Successfully uninstalled orjson-3.9.7
Attempting uninstall: annotated-types
Found existing installation: annotated-types 0.5.0
Uninstalling annotated-types-0.5.0:
Successfully uninstalled annotated-types-0.5.0
Attempting uninstall: pydantic
Found existing installation: pydantic 2.5.3
Uninstalling pydantic-2.5.3:
Successfully uninstalled pydantic-2.5.3
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
line-bot-sdk 3.7.0 requires urllib3<3,>=2.0.5, but you have urllib3 1.26.19 which is incompatible.
reflex 0.2.8 requires fastapi<0.97.0,>=0.96.0, but you have fastapi 0.115.5 which is incompatible.
reflex 0.2.8 requires httpx<0.25.0,>=0.24.0, but you have httpx 0.28.1 which is incompatible.
reflex 0.2.8 requires packaging<24.0,>=23.1, but you have packaging 24.2 which is incompatible.
reflex 0.2.8 requires pydantic<2.0.0,>=1.10.2, but you have pydantic 2.11.4 which is incompatible.
reflex 0.2.8 requires python-multipart<0.0.6,>=0.0.5, but you have python-multipart 0.0.12 which is incompatible.
reflex 0.2.8 requires typer==0.4.2, but you have typer 0.13.1 which is incompatible.
sqlmodel 0.0.8 requires pydantic<2.0.0,>=1.8.2, but you have pydantic 2.11.4 which is incompatible.
streamlit 1.27.0 requires packaging<24,>=16.8, but you have packaging 24.2 which is incompatible.
streamlit 1.27.0 requires protobuf<5,>=3.20, but you have protobuf 5.28.1 which is incompatible.
tensorflow-intel 2.13.0 requires protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.20.3, but you have protobuf 5.28.1 which is incompatible.
tensorflow-intel 2.13.0 requires typing-extensions<4.6.0,>=3.6.6, but you have typing-extensions 4.12.2 which is incompatible.
finmind 1.6.9 requires ta~=0.5.25, but you have ta 0.11.0 which is incompatible.
Successfully installed annotated-types-0.7.0 jsonpatch-1.33 langchain-0.3.25 langchain-core-0.3.58 langchain-text-splitters-0.3.8 langsmith-0.3.42 orjson-3.10.18 packaging-24.2 pydantic-2.11.4 pydantic-core-2.33.2 typing-inspection-0.4.0 zstandard-0.23.0
看起來已經成功安裝 LangChain 了, 但過程中出現了一個 Warning (可忽略) 與 Error, 貼給 ChatGPT 分析, 原來 Error 原因是系統中已安裝的 streamlit, reflex, tensorflow, sqlmodel 這幾個套件使用的相依套件如 packaging, pydantic 等都在安裝 LangChain 時升版了, 可能會影響到 streamlit 與 tensorflow 等套件之使用, 但可將 streamlit 與 tensorflow 升到最新版解決.
其實避免版本衝突最好的方式是在虛擬環境中安裝 LangChain (我打算在 MSI 電競桌機上用虛擬環境安裝 LangChain 安裝).
安裝好 langchain 後匯入並檢視版本 :
Python 3.10.11 (C:\Users\tony1\AppData\Local\Programs\Thonny\python.exe)
>>> import langchain
>>> langchain.__version__
'0.3.25'
是目前最新的 0.3.25 版.
沒有留言:
張貼留言