LangChain 的環(huán)境部署
LangChain 的部署相對靈活,因?yàn)樗饕且粋€ Python 庫,并且有不同語言(Python 和 JavaScript/TypeScript)的 SDK。大多數(shù)情況下,您會以 Python 的方式來部署和使用 LangChain。
pip install langchain langchain-community langchain-ollama dashscope chromadb
pip install langchain_chroma
- langchain:核心包
- langchain-community:社區(qū)支持包,提供了更多的第三方模型調(diào)用(阿里云千問模型就需要這個包)
- langchain-ollama:Ollama支持包,支持調(diào)用ollama托管部署的本地模型
- dashscope:阿里云通義千問的PythonSDK
- chromadb:輕量向量數(shù)據(jù)庫(后續(xù)使用)
以下是 LangChain 的核心環(huán)境部署內(nèi)容:
1. Python 環(huán)境準(zhǔn)備
LangChain 是一個 Python 庫,因此您需要一個配置好的 Python 環(huán)境。
- Python 版本: 推薦使用 Python 3.8 或更高版本。
- 安裝 Python: 如果您的系統(tǒng)沒有 Python,可以從 python.org 下載并安裝。
2. 創(chuàng)建和激活虛擬環(huán)境 (強(qiáng)烈推薦)
為了避免不同項(xiàng)目之間的依賴沖突,使用虛擬環(huán)境是最佳實(shí)踐。
-
使用
venv(Python 自帶):# 1. 在您的項(xiàng)目目錄中創(chuàng)建虛擬環(huán)境 python -m venv .venv # 2. 激活虛擬環(huán)境 # Windows: .\.venv\Scripts\activate # macOS/Linux: source .venv/bin/activate激活后,您的命令行提示符前面會顯示
(.venv)。 -
使用
conda(Anaconda/Miniconda):# 1. 創(chuàng)建 conda 虛擬環(huán)境 (例如,指定 Python 版本) conda create -n langchain_env python=3.9 # 2. 激活 conda 虛擬環(huán)境 conda activate langchain_env
3. 安裝 LangChain 庫
一旦您的 Python 虛擬環(huán)境激活,您就可以使用 pip 來安裝 LangChain。
-
安裝核心庫:
pip install langchain -
安裝特定集成 (根據(jù)您的需求):
LangChain 的強(qiáng)大之處在于它集成了許多外部服務(wù),您需要根據(jù)您使用的具體服務(wù)來安裝額外的庫。以下是一些常見的例子:-
OpenAI API:
pip install openai -
LLMs (其他):
- Hugging Face models:
pip install transformers - Anthropic (Claude):
pip install anthropic - Google (Generative AI):
pip install google-generativeai - Cohere:
pip install cohere
- Hugging Face models:
-
Vector Stores:
- Chroma:
pip install chromadb - FAISS:
pip install faiss-cpu(或faiss-gpu如果您有 GPU) - Pinecone:
pip install pinecone-client - Weaviate:
pip install weaviate-client
- Chroma:
-
Document Loaders:
- PDF:
pip install pypdf - Web Pages:
pip install beautifulsoup4 - Google Drive:
pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib
- PDF:
-
Agents (Tools 及其依賴):
- Google Search:
pip install google-search-results(需要 SerpAPI 密鑰) - Python REPL: 通常不需要額外安裝,LangChain 內(nèi)置。
- Bash/Shell: LangChain 提供了
ShellTool,可能需要確保您的系統(tǒng)中有相應(yīng)的 shell。
- Google Search:
建議: 在開始項(xiàng)目前,根據(jù) LangChain 的文檔(https://python.langchain.com/docs/get_started/installation)確定您需要的集成,并一并安裝。
-
OpenAI API:
4. 配置 API 密鑰和環(huán)境變量
許多 LLM 服務(wù)和工具需要 API 密鑰才能使用。LangChain 推薦使用環(huán)境變量來管理這些密鑰,這既安全又方便。
-
獲取 API 密鑰:
- OpenAI: 注冊 OpenAI 賬號,在 https://platform.openai.com/account/api-keys 創(chuàng)建 API Key。
- SerpAPI (Google Search): 注冊 SerpAPI 賬號,獲取 API Key。
- Pinecone: 注冊 Pinecone 賬號,獲取 API Key 和 Environment。
- ...等等,具體取決于您使用的服務(wù)。
-
設(shè)置環(huán)境變量:
-
Temporary (當(dāng)前終端會話):
# Windows (Command Prompt) set OPENAI_API_KEY=your_openai_api_key # Windows (PowerShell) $env:OPENAI_API_KEY="your_openai_api_key" # macOS/Linux export OPENAI_API_KEY=your_openai_api_key -
Permanent (在操作系統(tǒng)層面):
- Windows: 在“環(huán)境變量”設(shè)置中添加新的用戶變量或系統(tǒng)變量。
-
macOS/Linux: 編輯您的 shell 配置文件 (如
.bashrc,.zshrc,.profile),添加export KEY_NAME=your_key語句,然后運(yùn)行source ~/.your_shell_config_file或重啟終端。
-
使用
.env文件 (推薦):
在您的項(xiàng)目根目錄下創(chuàng)建一個名為.env的文件,并在其中添加您的密鑰:OPENAI_API_KEY=your_openai_api_key SERPAPI_API_KEY=your_serpapi_api_key # ...然后,安裝
python-dotenv庫:pip install python-dotenv在您的 Python 腳本的開頭加載這些變量:
from dotenv import load_dotenv load_dotenv() # 會自動從當(dāng)前目錄或父目錄加載 .env 文件 import os openai_api_key = os.getenv("OPENAI_API_KEY")
-
5. 啟動 LangChain 項(xiàng)目 (示例)
您可以在一個 Python 文件中編寫您的 LangChain 代碼。
示例:一個簡單的 LangChain 鏈 (需要 OpenAI API Key)
確保您已安裝:
pip install langchain openai python-dotenv-
創(chuàng)建
.env文件:OPENAI_API_KEY=sk-your_actual_openai_api_key_here -
創(chuàng)建 Python 文件 (例如
main.py):import os from dotenv import load_dotenv from langchain_openai import OpenAI from langchain.prompts import PromptTemplate from langchain.chains import LLMChain # 1. 加載環(huán)境變量 load_dotenv() # 2. 檢查 API Key 是否加載成功 (可選) if os.getenv("OPENAI_API_KEY") is None: print("OpenAI API Key not found. Please set it in your .env file or as an environment variable.") exit() # 3. 初始化 LLM (例如 OpenAI) # temperature=0 表示輸出更確定,temperature=1 表示更隨機(jī)/創(chuàng)造性 llm = OpenAI(temperature=0.7) # 4. 定義 Prompt Template prompt_template = PromptTemplate( input_variables=["topic"], template="講一個關(guān)于 {topic} 的有趣的故事。", ) # 5. 創(chuàng)建 LLMChain # chain_type="stuff" 是最簡單的鏈類型,將所有輸入文檔“塞”進(jìn)一個上下文窗口 story_chain = LLMChain(llm=llm, prompt=prompt_template) # 6. 運(yùn)行鏈 topic_to_ask = "一只會飛的貓" print(f"Generating a story about: {topic_to_ask}") story = story_chain.invoke({"topic": topic_to_ask}) # 使用 .invoke for single input # story = story_chain.batch([{"topic": topic_to_ask}, {"topic": "一個聰明的小機(jī)器人"}]) # 使用 .batch for multiple inputs print("\n--- Generated Story ---") print(story['text']) # Output is a dictionary, the story is in 'text' key -
運(yùn)行腳本:
python main.py
6. JavaScript/TypeScript 環(huán)境部署
LangChain 也提供了 JavaScript/TypeScript 的 SDK (LangChain.js)。部署方式類似:
- Node.js 環(huán)境: 確保安裝了 Node.js。
-
創(chuàng)建項(xiàng)目:
npm init或yarn init。 -
安裝 LangChain.js:
npm install langchain @langchain/openai(取決于您使用的 LLM) -
配置 API Key: 使用
.env文件配合dotenv包,或者直接在代碼中設(shè)置環(huán)境變量。 - 編寫代碼: 使用 LangChain.js 的 API 來構(gòu)建您的應(yīng)用。
總結(jié)部署步驟:
- 安裝 Python
- 創(chuàng)建并激活 Python 虛擬環(huán)境
- 使用
pip安裝langchain核心庫 - 安裝您需要的特定集成庫 (LLM, Vector Store, Document Loader 等)
- 獲取并配置 API 密鑰 (推薦使用
.env文件) - 編寫 Python 代碼,使用 LangChain 的模塊構(gòu)建應(yīng)用
- 運(yùn)行您的 Python 腳本
LangChain 的環(huán)境部署主要就是圍繞著 Python 包的管理和 API 密鑰的配置。一旦這些基礎(chǔ)搭建好,您就可以專注于使用 LangChain 的強(qiáng)大功能來構(gòu)建您的 LLM 應(yīng)用了。