every blog every motto: You can do more than you think.
https://blog.csdn.net/weixin_39190382?type=blog
0. 前言
【LLM實戰|langchain】langchain基礎
1. 模型 I/O 封裝
把不同的模型,統一封裝成一個接口,方便更換模型而不用重構代碼。
1.1 模型 API: ChatModel
1.1.1 OpenAI 模型封裝
# !pip install -U langchain
# !pip install -U langchain-openai
from langchain.chat_models import init_chat_model
model = init_chat_model("gpt-4o-mini", model_provider="openai")
response = model.invoke("你是誰")
print(response.content)
1.1.2 多輪對話sessoin
from langchain.schema import (AIMessage, # 等價于OpenAI接口中的assistant roleHumanMessage, # 等價于OpenAI接口中的user roleSystemMessage # 等價于OpenAI接口中的system role
)messages = [SystemMessage(content="你是AI課程助理。"),HumanMessage(content="我是學員,我叫小a。"),AIMessage(content="歡迎!"),HumanMessage(content="我是誰?")
]ret = model.invoke(messages)print(ret.content)
1.1.3 換個國產模型
# !pip install -U langchain-deepseek
from langchain.chat_models import init_chat_modelmodel = init_chat_model(model="deepseek-chat", model_provider="deepseek")response = model.invoke("你是誰")
print(response.content)
我是DeepSeek Chat,由深度求索公司(DeepSeek)研發的智能AI助手!? 我可以幫你解答各種問題,無論是學習、工作、生活,還是科技、娛樂、編程等,我都會盡力提供準確、有用的信息。 有什么我可以幫你的嗎?😊
1.1.4 流式輸出流式輸出
for token in model.stream("你是誰"):print(token.content, end="")
我是DeepSeek Chat,由深度求索公司(DeepSeek)開發的智能AI助手!? 我的使命是幫助你解答各種問題,無論是學習、工作,還是日常生活中的小疑惑,我都會盡力提供準確、有用的信息。 有什么我可以幫你的嗎?😊
1.2 模型的輸入與輸出

1.2.1 Prompt 模板封裝
- PromptTemplate 可以在模板中自定義變量
from langchain.prompts import PromptTemplatetemplate = PromptTemplate.from_template("給我講個關于{subject}的笑話")
print("===Template===")
print(template)
print("===Prompt===")
print(template.format(subject='小明'))
===Template===
input_variables=['subject'] input_types={} partial_variables={} template='給我講個關于{subject}的笑話'
===Prompt===
給我講個關于小明的笑話
from langchain.chat_models import init_chat_model# 定義 LLM
llm = init_chat_model("deepseek-chat", model_provider="deepseek")
# 通過 Prompt 調用 LLM
ret = llm.invoke(template.format(subject='小明'))
# 打印輸出
print(ret.content)
好的!這里有一個關于小明的經典笑話:**老師**:小明,用“果然”造個句。
**小明**:我先吃蘋果,然后喝涼水……
**老師**:這不對,不能拆開詞語!
**小明**:那我重新造——昨天我吃水果,然后拉肚子了!
**老師**:……這是“果然”嗎??
**小明**(自信):是啊!水果+然后=果然! (冷場中,全班同學默默翻開了詞典) 希望這個“硬核造句”能讓你笑一下! 😄
- ChatPromptTemplate 用模板表示的對話上下文
from langchain.prompts import (ChatPromptTemplate,HumanMessagePromptTemplate,SystemMessagePromptTemplate,
)
from langchain.chat_models import init_chat_model
# llm = init_chat_model("gpt-4o-mini", model_provider="openai")
llm = init_chat_model("deepseek-chat", model_provider="deepseek")template = ChatPromptTemplate.from_messages([SystemMessagePromptTemplate.from_template("你是{product}的客服助手。你的名字叫{name}"),HumanMessagePromptTemplate.from_template("{query}")]
)prompt = template.format_messages(product="五年高考三年模擬",name="小高",query="你是誰"
)print(prompt)ret = llm.invoke(prompt)print(ret.content)
[SystemMessage(content='你是五年高考三年模擬的客服助手。你的名字叫小高', additional_kwargs={}, response_metadata={}), HumanMessage(content='你是誰', additional_kwargs={}, response_metadata={})]
你好呀!我是五年高考三年模擬的客服助手小高~ 很高興為你服務!有什么關于五三教輔的問題都可以問我哦,比如教材版本、使用方法、購買咨詢等等,我都會盡力幫你解答!(??????)??
- MessagesPlaceholder 把多輪對話變成模板
from langchain.prompts import (ChatPromptTemplate,HumanMessagePromptTemplate,MessagesPlaceholder,
)human_prompt = "Translate your answer to {language}."
human_message_template = HumanMessagePromptTemplate.from_template(human_prompt)chat_prompt = ChatPromptTemplate.from_messages(# variable_name 是 message placeholder 在模板中的變量名# 用于在賦值時使用[MessagesPlaceholder("history"), human_message_template]
)
from langchain_core.messages import AIMessage, HumanMessagehuman_message = HumanMessage(content="Who is Elon Musk?")
ai_message = AIMessage(content="Elon Musk is a billionaire entrepreneur, inventor, and industrial designer"
)messages = chat_prompt.format_prompt(# 對 "history" 和 "language" 賦值history=[human_message, ai_message], language="中文"
)print(messages.to_messages())
[HumanMessage(content='Who is Elon Musk?', additional_kwargs={}, response_metadata={}), AIMessage(content='Elon Musk is a billionaire entrepreneur, inventor, and industrial designer', additional_kwargs={}, response_metadata={}), HumanMessage(content='Translate your answer to 中文.', additional_kwargs={}, response_metadata={})]
result = llm.invoke(messages)
print(result.content)
埃隆·馬斯克(Elon Musk)是一位億萬富翁企業家、發明家和工業設計師。他是多家高科技公司的創始人或領導者,包括特斯拉(Tesla)、SpaceX、Neuralink和The Boring Company。馬斯克以推動電動汽車、太空探索、人工智能和可再生能源等領域的創新而聞名。
1.2 從文件加載prompt模板
from langchain.prompts import PromptTemplatetemplate = PromptTemplate.from_file("example_prompt_template.txt")
print("===Template===")
print(template)
print("===Prompt===")
print(template.format(topic='黑色幽默'))
===Template===
input_variables=['topic'] input_types={} partial_variables={} template='舉一個關于{topic}的例子'
===Prompt===
舉一個關于黑色幽默的例子
1.3 結構化輸出
1.3.1 直接輸出pydantic對象
from pydantic import BaseModel, Field# 定義你的輸出對象
class Date(BaseModel):year: int = Field(description="Year")month: int = Field(description="Month")day: int = Field(description="Day")era: str = Field(description="BC or AD")
from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate
from langchain_core.output_parsers import PydanticOutputParserfrom langchain.chat_models import init_chat_model
# llm = init_chat_model("gpt-4o-mini", model_provider="openai")
llm = init_chat_model("deepseek-chat", model_provider="deepseek")# 定義結構化輸出的模型
structured_llm = llm.with_structured_output(Date)template = """提取用戶輸入中的日期。
用戶輸入:
{query}"""prompt = PromptTemplate(template=template,
)query = "2023年四月6日天氣晴..."
input_prompt = prompt.format_prompt(query=query)structured_llm.invoke(input_prompt)
Date(year=2023, month=4, day=6, era='AD')
1.3.2 輸出指定格式的 JSON
# OpenAI 模型的JSON格式
json_schema = {"title": "Date","description": "Formated date expression","type": "object","properties": {"year": {"type": "integer","description": "year, YYYY",},"month": {"type": "integer","description": "month, MM",},"day": {"type": "integer","description": "day, DD",},"era": {"type": "string","description": "BC or AD",},},
}
structured_llm = llm.with_structured_output(json_schema)structured_llm.invoke(input_prompt)
{'year': 2023, 'month': 4, 'day': 6}
1.3.3 使用 OutputParser
OutputParser
可以按指定格式解析模型的輸出
from langchain_core.output_parsers import JsonOutputParserparser = JsonOutputParser(pydantic_object=Date)prompt = PromptTemplate(template="提取用戶輸入中的日期。\n用戶輸入:{query}\n{format_instructions}",input_variables=["query"],partial_variables={"format_instructions": parser.get_format_instructions()},
)input_prompt = prompt.format_prompt(query=query)
output = llm.invoke(input_prompt)
print("原始輸出:\n"+output.content)print("\n解析后:")
parser.invoke(output)
原始輸出:
```json
{"year": 2023,"month": 4,"day": 6,"era": "AD"
}
```解析后:{'year': 2023, 'month': 4, 'day': 6, 'era': 'AD'}
也可以用 PydanticOutputParser
from langchain_core.output_parsers import PydanticOutputParserparser = PydanticOutputParser(pydantic_object=Date)input_prompt = prompt.format_prompt(query=query)
output = llm.invoke(input_prompt)
print("原始輸出:\n"+output.content)print("\n解析后:")
parser.invoke(output)
原始輸出:
```json
{"year": 2023,"month": 4,"day": 6,"era": "AD"
}
```解析后:Date(year=2023, month=4, day=6, era='AD')
OutputFixingParser
利用大模型做格式自動糾錯
from langchain.output_parsers import OutputFixingParser
from langchain.chat_models import init_chat_modelllm = init_chat_model(model="deepseek-chat", model_provider="deepseek")# 糾錯能力與大模型能力相關
new_parser = OutputFixingParser.from_llm(parser=parser, llm=llm)bad_output = output.content.replace("4","四")
print("PydanticOutputParser:")
try:parser.invoke(bad_output)
except Exception as e:print(e)print("OutputFixingParser:")
new_parser.invoke(bad_output)
PydanticOutputParser:
Invalid json output: ```json
{"year": 2023,"month": 四,"day": 6,"era": "AD"
}
```
For troubleshooting, visit: https://python.langchain.com/docs/troubleshooting/errors/OUTPUT_PARSING_FAILURE
OutputFixingParser:Date(year=2023, month=4, day=6, era='AD')
1.4 Function Calling
from langchain_core.tools import tool@tool
def add(a: int, b: int) -> int:"""Add two integers.Args:a: First integerb: Second integer"""return a + b@tool
def multiply(a: float, b: float) -> float:"""Multiply two integers.Args:a: First integerb: Second integer"""return a * b
import jsonllm_with_tools = llm.bind_tools([add, multiply])query = "3.5的4倍是多少?"
messages = [HumanMessage(query)]output = llm_with_tools.invoke(messages)print(json.dumps(output.tool_calls, indent=4))
[{"name": "multiply","args": {"a": 3.5,"b": 4},"id": "call_0_2ff05fac-e682-4fa2-9274-b20e2dba817c","type": "tool_call"}
]
messages.append(output)available_tools = {"add": add, "multiply": multiply}for tool_call in output.tool_calls:selected_tool = available_tools[tool_call["name"].lower()]tool_msg = selected_tool.invoke(tool_call)messages.append(tool_msg)new_output = llm_with_tools.invoke(messages)
for message in messages:print(json.dumps(message.model_dump(), indent=4, ensure_ascii=False))
print(new_output.content)
{"content": "3.5的4倍是多少?","additional_kwargs": {},"response_metadata": {},"type": "human","name": null,"id": null,"example": false
}
{"content": "","additional_kwargs": {"tool_calls": [{"id": "call_0_2ff05fac-e682-4fa2-9274-b20e2dba817c","function": {"arguments": "{\"a\":3.5,\"b\":4}","name": "multiply"},"type": "function","index": 0}],"refusal": null},"response_metadata": {"token_usage": {"completion_tokens": 25,"prompt_tokens": 250,"total_tokens": 275,"completion_tokens_details": null,"prompt_tokens_details": {"audio_tokens": null,"cached_tokens": 0},"prompt_cache_hit_tokens": 0,"prompt_cache_miss_tokens": 250},"model_name": "deepseek-chat","system_fingerprint": "fp_8802369eaa_prod0623_fp8_kvcache","id": "c420227e-444d-4d8a-9a91-54735949b8c5","service_tier": null,"finish_reason": "tool_calls","logprobs": null},"type": "ai","name": null,"id": "run--92a3e96d-0f04-470e-b411-2b6766e23d6d-0","example": false,"tool_calls": [{"name": "multiply","args": {"a": 3.5,"b": 4},"id": "call_0_2ff05fac-e682-4fa2-9274-b20e2dba817c","type": "tool_call"}],"invalid_tool_calls": [],"usage_metadata": {"input_tokens": 250,"output_tokens": 25,"total_tokens": 275,"input_token_details": {"cache_read": 0},"output_token_details": {}}
}
{"content": "14.0","additional_kwargs": {},"response_metadata": {},"type": "tool","name": "multiply","id": null,"tool_call_id": "call_0_2ff05fac-e682-4fa2-9274-b20e2dba817c","artifact": null,"status": "success"
}
3.5的4倍是14.0。
1.5 小結
- LangChain 統一封裝了各種模型的調用接口,包括補全型和對話型兩種
- LangChain 提供了 PromptTemplate 類,可以自定義帶變量的模板
- LangChain 提供了一些列輸出解析器,用于將大模型的輸出解析成結構化對象
- LangChain 提供了 Function Calling 的封裝
- 上述模型屬于 LangChain 中較為實用的部分
2. 數據連接封裝

2.1 文檔加載器:Document Loaders
# !pip install -U langchain-community pymupdf
from langchain_community.document_loaders import PyMuPDFLoaderloader = PyMuPDFLoader("./data/deepseek-v3-1-4.pdf")
pages = loader.load_and_split()print(pages[0].page_content)
DeepSeek-V3 Technical Report
DeepSeek-AI
research@deepseek.com
Abstract
We present DeepSeek-V3, a strong Mixture-of-Experts (MoE) language model with 671B total
parameters with 37B activated for each token. To achieve efficient inference and cost-effective
training, DeepSeek-V3 adopts Multi-head Latent Attention (MLA) and DeepSeekMoE architec-
tures, which were thoroughly validated in DeepSeek-V2. Furthermore, DeepSeek-V3 pioneers
an auxiliary-loss-free strategy for load balancing and sets a multi-token prediction training
objective for stronger performance. We pre-train DeepSeek-V3 on 14.8 trillion diverse and
high-quality tokens, followed by Supervised Fine-Tuning and Reinforcement Learning stages to
fully harness its capabilities. Comprehensive evaluations reveal that DeepSeek-V3 outperforms
other open-source models and achieves performance comparable to leading closed-source
models. Despite its excellent performance, DeepSeek-V3 requires only 2.788M H800 GPU hours
for its full training. In addition, its training process is remarkably stable. Throughout the entire
training process, we did not experience any irrecoverable loss spikes or perform any rollbacks.
The model checkpoints are available at https://github.com/deepseek-ai/DeepSeek-V3.
MMLU-Pro
(EM)
GPQA-Diamond
(Pass@1)
MATH 500
(EM)
AIME 2024
(Pass@1)
Codeforces
(Percentile)
SWE-bench Verified
(Resolved)
0
20
40
60
80
100
Accuracy / Percentile (%)
75.9
59.1
90.2
39.2
51.6
42.0
66.2
41.3
74.7
16.7
35.6
22.6
71.6
49.0
80.0
23.3
24.8
23.8
73.3
51.1
73.8
23.3
25.3
24.5
72.6
49.9
74.6
9.3
23.6
38.8
78.0
65.0
78.3
16.0
20.3
50.8
DeepSeek-V3
DeepSeek-V2.5
Qwen2.5-72B-Inst
Llama-3.1-405B-Inst
GPT-4o-0513
Claude-3.5-Sonnet-1022
Figure 1 | Benchmark performance of DeepSeek-V3 and its counterparts.
arXiv:2412.19437v2 [cs.CL] 18 Feb 2025
2.2 文檔處理器
2.2.1 TextSplitter
# !pip install --upgrade langchain-text-splitters
from langchain_text_splitters import RecursiveCharacterTextSplittertext_splitter = RecursiveCharacterTextSplitter(chunk_size=512,chunk_overlap=200, length_function=len,add_start_index=True,
)paragraphs = text_splitter.create_documents([pages[0].page_content])
for para in paragraphs:print(para.page_content)print('-------')
DeepSeek-V3 Technical Report
DeepSeek-AI
research@deepseek.com
Abstract
We present DeepSeek-V3, a strong Mixture-of-Experts (MoE) language model with 671B total
parameters with 37B activated for each token. To achieve efficient inference and cost-effective
training, DeepSeek-V3 adopts Multi-head Latent Attention (MLA) and DeepSeekMoE architec-
tures, which were thoroughly validated in DeepSeek-V2. Furthermore, DeepSeek-V3 pioneers
-------
training, DeepSeek-V3 adopts Multi-head Latent Attention (MLA) and DeepSeekMoE architec-
tures, which were thoroughly validated in DeepSeek-V2. Furthermore, DeepSeek-V3 pioneers
an auxiliary-loss-free strategy for load balancing and sets a multi-token prediction training
objective for stronger performance. We pre-train DeepSeek-V3 on 14.8 trillion diverse and
high-quality tokens, followed by Supervised Fine-Tuning and Reinforcement Learning stages to
-------
objective for stronger performance. We pre-train DeepSeek-V3 on 14.8 trillion diverse and
high-quality tokens, followed by Supervised Fine-Tuning and Reinforcement Learning stages to
fully harness its capabilities. Comprehensive evaluations reveal that DeepSeek-V3 outperforms
other open-source models and achieves performance comparable to leading closed-source
models. Despite its excellent performance, DeepSeek-V3 requires only 2.788M H800 GPU hours
-------
other open-source models and achieves performance comparable to leading closed-source
models. Despite its excellent performance, DeepSeek-V3 requires only 2.788M H800 GPU hours
for its full training. In addition, its training process is remarkably stable. Throughout the entire
training process, we did not experience any irrecoverable loss spikes or perform any rollbacks.
The model checkpoints are available at https://github.com/deepseek-ai/DeepSeek-V3.
MMLU-Pro
(EM)
GPQA-Diamond
(Pass@1)
MATH 500
(EM)
2.3、向量數據庫與向量檢索
# !pip install dashscope
# !pip install faiss-cpu
import os
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.embeddings import DashScopeEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import PyMuPDFLoader# 加載文檔
loader = PyMuPDFLoader("./data/deepseek-v3-1-4.pdf")
pages = loader.load_and_split()# 文檔切分
text_splitter = RecursiveCharacterTextSplitter(chunk_size=512,chunk_overlap=200,length_function=len,add_start_index=True,
)texts = text_splitter.create_documents([page.page_content for page in pages[:1]]
)# 灌庫
embeddings = DashScopeEmbeddings(model="text-embedding-v1", dashscope_api_key=os.getenv("DASHSCOPE_API_KEY")
)
index = FAISS.from_documents(texts, embeddings)# 檢索 top-5 結果
retriever = index.as_retriever(search_kwargs={"k": 5})docs = retriever.invoke("deepseek v3有多少參數")for doc in docs:print(doc.page_content)print("----")
22.6
71.6
49.0
80.0
23.3
24.8
23.8
73.3
51.1
73.8
23.3
25.3
24.5
72.6
49.9
74.6
9.3
23.6
38.8
78.0
65.0
78.3
16.0
20.3
50.8
DeepSeek-V3
DeepSeek-V2.5
Qwen2.5-72B-Inst
Llama-3.1-405B-Inst
GPT-4o-0513
Claude-3.5-Sonnet-1022
Figure 1 | Benchmark performance of DeepSeek-V3 and its counterparts.
arXiv:2412.19437v2 [cs.CL] 18 Feb 2025
----
objective for stronger performance. We pre-train DeepSeek-V3 on 14.8 trillion diverse and
high-quality tokens, followed by Supervised Fine-Tuning and Reinforcement Learning stages to
fully harness its capabilities. Comprehensive evaluations reveal that DeepSeek-V3 outperforms
other open-source models and achieves performance comparable to leading closed-source
models. Despite its excellent performance, DeepSeek-V3 requires only 2.788M H800 GPU hours
----
training, DeepSeek-V3 adopts Multi-head Latent Attention (MLA) and DeepSeekMoE architec-
tures, which were thoroughly validated in DeepSeek-V2. Furthermore, DeepSeek-V3 pioneers
an auxiliary-loss-free strategy for load balancing and sets a multi-token prediction training
objective for stronger performance. We pre-train DeepSeek-V3 on 14.8 trillion diverse and
high-quality tokens, followed by Supervised Fine-Tuning and Reinforcement Learning stages to
----
DeepSeek-V3 Technical Report
DeepSeek-AI
research@deepseek.com
Abstract
We present DeepSeek-V3, a strong Mixture-of-Experts (MoE) language model with 671B total
parameters with 37B activated for each token. To achieve efficient inference and cost-effective
training, DeepSeek-V3 adopts Multi-head Latent Attention (MLA) and DeepSeekMoE architec-
tures, which were thoroughly validated in DeepSeek-V2. Furthermore, DeepSeek-V3 pioneers
----
other open-source models and achieves performance comparable to leading closed-source
models. Despite its excellent performance, DeepSeek-V3 requires only 2.788M H800 GPU hours
for its full training. In addition, its training process is remarkably stable. Throughout the entire
training process, we did not experience any irrecoverable loss spikes or perform any rollbacks.
The model checkpoints are available at https://github.com/deepseek-ai/DeepSeek-V3.
MMLU-Pro
(EM)
GPQA-Diamond
(Pass@1)
MATH 500
(EM)
----
3. Chain 和 LangChain Expression Language (LCEL)
LangChain Expression Language(LCEL)是一種聲明式語言,可輕松組合不同的調用順序構成 Chain。LCEL 自創立之初就被設計為能夠支持將原型投入生產環境,無需代碼更改,從最簡單的“提示+LLM”鏈到最復雜的鏈(已有用戶成功在生產環境中運行包含數百個步驟的 LCEL Chain)。
LCEL 的一些亮點包括:
-
流支持:使用 LCEL 構建 Chain 時,你可以獲得最佳的首個令牌時間(即從輸出開始到首批輸出生成的時間)。對于某些 Chain,這意味著可以直接從 LLM 流式傳輸令牌到流輸出解析器,從而以與 LLM 提供商輸出原始令牌相同的速率獲得解析后的、增量的輸出。
-
異步支持:任何使用 LCEL 構建的鏈條都可以通過同步 API(例如,在 Jupyter 筆記本中進行原型設計時)和異步 API(例如,在 LangServe 服務器中)調用。這使得相同的代碼可用于原型設計和生產環境,具有出色的性能,并能夠在同一服務器中處理多個并發請求。
-
優化的并行執行:當你的 LCEL 鏈條有可以并行執行的步驟時(例如,從多個檢索器中獲取文檔),我們會自動執行,無論是在同步還是異步接口中,以實現最小的延遲。
-
重試和回退:為 LCEL 鏈的任何部分配置重試和回退。這是使鏈在規模上更可靠的絕佳方式。目前我們正在添加重試/回退的流媒體支持,因此你可以在不增加任何延遲成本的情況下獲得增加的可靠性。
-
訪問中間結果:對于更復雜的鏈條,訪問在最終輸出產生之前的中間步驟的結果通常非常有用。這可以用于讓最終用戶知道正在發生一些事情,甚至僅用于調試鏈條。你可以流式傳輸中間結果,并且在每個 LangServe 服務器上都可用。
-
輸入和輸出模式:輸入和輸出模式為每個 LCEL 鏈提供了從鏈的結構推斷出的 Pydantic 和 JSONSchema 模式。這可以用于輸入和輸出的驗證,是 LangServe 的一個組成部分。
-
無縫 LangSmith 跟蹤集成:隨著鏈條變得越來越復雜,理解每一步發生了什么變得越來越重要。通過 LCEL,所有步驟都自動記錄到 LangSmith,以實現最大的可觀察性和可調試性。
-
無縫 LangServe 部署集成:任何使用 LCEL 創建的鏈都可以輕松地使用 LangServe 進行部署。
原文:https://python.langchain.com/docs/expression_language/
3.1 Pipeline 式調用 PromptTemplate, LLM 和 OutputParser
from langchain.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from pydantic import BaseModel, Field
from typing import List, Dict, Optional
from enum import Enum
import json
from langchain.chat_models import init_chat_model
# 輸出結構
class SortEnum(str, Enum):data = 'data'price = 'price'class OrderingEnum(str, Enum):ascend = 'ascend'descend = 'descend'class Semantics(BaseModel):name: Optional[str] = Field(description="流量包名稱", default=None)price_lower: Optional[int] = Field(description="價格下限", default=None)price_upper: Optional[int] = Field(description="價格上限", default=None)data_lower: Optional[int] = Field(description="流量下限", default=None)data_upper: Optional[int] = Field(description="流量上限", default=None)sort_by: Optional[SortEnum] = Field(description="按價格或流量排序", default=None)ordering: Optional[OrderingEnum] = Field(
description="升序或降序排列", default=None)# Prompt 模板
prompt = ChatPromptTemplate.from_messages([("system", "你是一個語義解析器。你的任務是將用戶的輸入解析成JSON表示。不要回答用戶的問題。"),("human", "{text}"),]
)# 模型
llm = init_chat_model("deepseek-chat", model_provider="deepseek")structured_llm = llm.with_structured_output(Semantics)# LCEL 表達式
runnable = ({"text": RunnablePassthrough()} | prompt | structured_llm
)# 直接運行
ret = runnable.invoke("不超過100元的流量大的套餐有哪些")
print(json.dumps(ret.model_dump(),indent = 4,ensure_ascii=False)
)
{"name": null,"price_lower": null,"price_upper": 100,"data_lower": null,"data_upper": null,"sort_by": "data","ordering": "descend"
}
3.2 用 LCEL 實現 RAG
import os
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import FAISS
from langchain.chains import RetrievalQA
from langchain_community.document_loaders import PyMuPDFLoader
from langchain_community.embeddings.dashscope import DashScopeEmbeddings# 加載文檔
loader = PyMuPDFLoader("./data/deepseek-v3-1-4.pdf")
pages = loader.load_and_split()# 文檔切分
text_splitter = RecursiveCharacterTextSplitter(chunk_size=512,chunk_overlap=200,length_function=len,add_start_index=True,
)texts = text_splitter.create_documents([page.page_content for page in pages[:1]]
)# 灌庫
embeddings = DashScopeEmbeddings(model="text-embedding-v1", dashscope_api_key=os.getenv("DASHSCOPE_API_KEY")
)
db = FAISS.from_documents(texts, embeddings)# 檢索 top-5 結果
retriever = db.as_retriever(search_kwargs={"k": 5})
docs = retriever.invoke("deepseek v3有多少參數")for doc in docs:print(doc.page_content)print("----")
from langchain.schema.output_parser import StrOutputParser
from langchain.schema.runnable import RunnablePassthrough
from langchain.prompts import ChatPromptTemplate# Prompt模板
template = """Answer the question based only on the following context:
{context}Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)# Chain
rag_chain = ({"question": RunnablePassthrough(), "context": retriever}| prompt| llm| StrOutputParser()
)rag_chain.invoke("deepseek V3有多少參數")
'DeepSeek-V3 是一個混合專家(MoE)語言模型,總參數為 **6710 億(671B)**,其中每個 token 激活 **370 億(37B)** 參數。'
3.3 用 LCEL 實現模型切換(工廠模式)
from langchain_core.runnables.utils import ConfigurableField
from langchain_community.chat_models import QianfanChatEndpoint
from langchain.prompts import (ChatPromptTemplate,HumanMessagePromptTemplate,
)
from langchain.chat_models import init_chat_model
from langchain.schema import HumanMessage
import os# 模型1
ds_model = init_chat_model("deepseek-chat", model_provider="deepseek")# 模型2
gpt_model = init_chat_model("gpt-4o-mini", model_provider="openai")# 通過 configurable_alternatives 按指定字段選擇模型
model = gpt_model.configurable_alternatives(ConfigurableField(id="llm"), default_key="gpt", deepseek=ds_model,# claude=claude_model,
)# Prompt 模板
prompt = ChatPromptTemplate.from_messages([HumanMessagePromptTemplate.from_template("{query}"),]
)# LCEL
chain = ({"query": RunnablePassthrough()} | prompt| model | StrOutputParser()
)# 運行時指定模型 "gpt" or "deepseek"
ret = chain.with_config(configurable={"llm": "deepseek"}).invoke("請自我介紹")print(ret)
你好!我是 **DeepSeek Chat**,由深度求索(DeepSeek)公司研發的智能AI助手。我的知識截止到 **2024年7月**,可以幫助你解答各種問題,包括**學習、工作、編程、寫作、生活小技巧**等。 ### ? **我的特點**:
- **免費使用**:目前不收費,隨時為你提供幫助!
- **超長上下文支持**:可以處理 **128K** 長度的文本,適合分析長文檔、論文或復雜對話。
- **文件閱讀能力**:支持上傳 **PDF、Word、Excel、PPT、TXT** 等文件,并從中提取關鍵信息。
- **邏輯清晰**:擅長數學推理、代碼編寫、論文潤色等任務。
- **中文優化**:對中文理解和生成特別友好,同時也能流利使用英文和其他語言。 ### 🚀 **我能幫你做什么?**
? **學習輔導**:解題思路、論文寫作、語言翻譯
? **工作助手**:寫郵件、做PPT、整理數據
? **編程支持**:代碼調試、算法講解、Python/Java/C++等
? **創意寫作**:小說、詩歌、廣告文案
? **生活百科**:旅行攻略、健康建議、美食推薦 如果你有任何問題,盡管問我吧!😊 你今天想了解什么呢?
3.4 通過 LCEL,還可以實現
- 配置運行時變量:https://python.langchain.com/docs/how_to/configure/
- 故障回退:https://python.langchain.com/docs/how_to/fallbacks/
- 并行調用:https://python.langchain.com/docs/how_to/parallel/
- 邏輯分支:https://python.langchain.com/docs/how_to/routing/
- 動態創建 Chain: https://python.langchain.com/docs/how_to/dynamic_chain/
更多例子:https://python.langchain.com/docs/how_to/lcel_cheatsheet/