使用 LangChain 建構 LLM 應用程式,整合 RAG、Agent、Chain 等功能,支援 600+ 整合
專案簡介
LangChain 是一個用於開發大型語言模型(LLM)應用程式的框架。它提供了標準化的介面來連接各種 LLM、向量資料庫、工具和 API,讓開發者能快速建構複雜的 AI 應用。
GitHub Stars: 126K+
核心概念
- Models - 連接各種 LLM(OpenAI、Anthropic、Ollama 等)
- Prompts - 管理和優化提示詞
- Chains - 串連多個元件形成工作流程
- Agents - 讓 LLM 自主決定使用哪些工具
- RAG - 檢索增強生成,結合外部知識
安裝
1
| pip install langchain langchain-openai langchain-community
|
基本使用
連接 LLM
1
2
3
4
5
6
7
8
9
10
11
12
| from langchain_openai import ChatOpenAI
from langchain_community.llms import Ollama
# OpenAI
llm = ChatOpenAI(model="gpt-4o", api_key="your-key")
# 本地 Ollama
llm = Ollama(model="llama3.3")
# 呼叫
response = llm.invoke("什麼是 Kubernetes?")
print(response)
|
Prompt Templates
1
2
3
4
5
6
7
8
9
10
11
12
| from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages([
("system", "你是一位專業的{role}"),
("user", "{question}")
])
chain = prompt | llm
response = chain.invoke({
"role": "資安顧問",
"question": "如何防範 SQL Injection?"
})
|
RAG(檢索增強生成)
載入文件
1
2
3
4
5
6
7
8
9
10
11
12
13
| from langchain_community.document_loaders import (
PyPDFLoader,
TextLoader,
WebBaseLoader
)
# PDF
loader = PyPDFLoader("document.pdf")
docs = loader.load()
# 網頁
loader = WebBaseLoader("https://example.com")
docs = loader.load()
|
文件切割
1
2
3
4
5
6
7
| from langchain.text_splitter import RecursiveCharacterTextSplitter
splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200
)
chunks = splitter.split_documents(docs)
|
建立向量資料庫
1
2
3
4
5
6
7
8
| from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(chunks, embeddings)
# 檢索
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
完整 RAG Chain
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
template = """根據以下內容回答問題:
{context}
問題:{question}
"""
prompt = ChatPromptTemplate.from_template(template)
rag_chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
response = rag_chain.invoke("什麼是零信任架構?")
|
Agents
建立工具
1
2
3
4
5
6
7
8
9
10
11
12
| from langchain_core.tools import tool
@tool
def search_web(query: str) -> str:
"""搜尋網頁內容"""
# 實作搜尋邏輯
return f"搜尋結果:{query}"
@tool
def calculate(expression: str) -> str:
"""計算數學表達式"""
return str(eval(expression))
|
建立 Agent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| from langchain.agents import create_react_agent, AgentExecutor
from langchain import hub
# 取得 ReAct prompt
prompt = hub.pull("hwchase17/react")
# 建立 agent
agent = create_react_agent(llm, [search_web, calculate], prompt)
agent_executor = AgentExecutor(agent=agent, tools=[search_web, calculate])
# 執行
result = agent_executor.invoke({
"input": "搜尋最新的 CVE 漏洞並計算今年已發布多少個"
})
|
對話記憶
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
history = ChatMessageHistory()
chain_with_history = RunnableWithMessageHistory(
chain,
lambda session_id: history,
input_messages_key="question",
history_messages_key="history"
)
response = chain_with_history.invoke(
{"question": "我叫小明"},
config={"configurable": {"session_id": "user123"}}
)
response = chain_with_history.invoke(
{"question": "我叫什麼名字?"},
config={"configurable": {"session_id": "user123"}}
)
|
LangServe 部署
1
2
3
4
5
6
7
8
9
| from fastapi import FastAPI
from langserve import add_routes
app = FastAPI()
add_routes(app, chain, path="/chat")
# 運行
# uvicorn main:app --reload
|
常用整合
| 類型 | 工具 |
|---|
| LLM | OpenAI, Anthropic, Ollama, HuggingFace |
| Embeddings | OpenAI, Cohere, HuggingFace |
| Vector Store | Chroma, Pinecone, Milvus, Qdrant |
| Document Loaders | PDF, CSV, JSON, Notion, Confluence |
| Tools | Google Search, Wikipedia, Wolfram Alpha |
相關連結
延伸閱讀