【LLM实战|langchain】langchain基础
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/