【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 模板封装
  1. 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)
好的!这里有一个关于小明的经典笑话:**老师**:小明,用“果然”造个句。  
**小明**:我先吃苹果,然后喝凉水……  
**老师**:这不对,不能拆开词语!  
**小明**:那我重新造——昨天我吃水果,然后拉肚子了!  
**老师**:……这是“果然”吗??  
**小明**(自信):是啊!水果+然后=果然!  (冷场中,全班同学默默翻开了词典)  希望这个“硬核造句”能让你笑一下! 😄
  1. 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={})]
你好呀!我是五年高考三年模拟的客服助手小高~ 很高兴为你服务!有什么关于五三教辅的问题都可以问我哦,比如教材版本、使用方法、购买咨询等等,我都会尽力帮你解答!(๑•̀ㅂ•́)و✧
  1. 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 小结

  1. LangChain 统一封装了各种模型的调用接口,包括补全型和对话型两种
  2. LangChain 提供了 PromptTemplate 类,可以自定义带变量的模板
  3. LangChain 提供了一些列输出解析器,用于将大模型的输出解析成结构化对象
  4. LangChain 提供了 Function Calling 的封装
  5. 上述模型属于 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 的一些亮点包括:

  1. 流支持:使用 LCEL 构建 Chain 时,你可以获得最佳的首个令牌时间(即从输出开始到首批输出生成的时间)。对于某些 Chain,这意味着可以直接从 LLM 流式传输令牌到流输出解析器,从而以与 LLM 提供商输出原始令牌相同的速率获得解析后的、增量的输出。

  2. 异步支持:任何使用 LCEL 构建的链条都可以通过同步 API(例如,在 Jupyter 笔记本中进行原型设计时)和异步 API(例如,在 LangServe 服务器中)调用。这使得相同的代码可用于原型设计和生产环境,具有出色的性能,并能够在同一服务器中处理多个并发请求。

  3. 优化的并行执行:当你的 LCEL 链条有可以并行执行的步骤时(例如,从多个检索器中获取文档),我们会自动执行,无论是在同步还是异步接口中,以实现最小的延迟。

  4. 重试和回退:为 LCEL 链的任何部分配置重试和回退。这是使链在规模上更可靠的绝佳方式。目前我们正在添加重试/回退的流媒体支持,因此你可以在不增加任何延迟成本的情况下获得增加的可靠性。

  5. 访问中间结果:对于更复杂的链条,访问在最终输出产生之前的中间步骤的结果通常非常有用。这可以用于让最终用户知道正在发生一些事情,甚至仅用于调试链条。你可以流式传输中间结果,并且在每个 LangServe 服务器上都可用。

  6. 输入和输出模式:输入和输出模式为每个 LCEL 链提供了从链的结构推断出的 Pydantic 和 JSONSchema 模式。这可以用于输入和输出的验证,是 LangServe 的一个组成部分。

  7. 无缝 LangSmith 跟踪集成:随着链条变得越来越复杂,理解每一步发生了什么变得越来越重要。通过 LCEL,所有步骤都自动记录到 LangSmith,以实现最大的可观察性和可调试性。

  8. 无缝 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,还可以实现

  1. 配置运行时变量:https://python.langchain.com/docs/how_to/configure/
  2. 故障回退:https://python.langchain.com/docs/how_to/fallbacks/
  3. 并行调用:https://python.langchain.com/docs/how_to/parallel/
  4. 逻辑分支:https://python.langchain.com/docs/how_to/routing/
  5. 动态创建 Chain: https://python.langchain.com/docs/how_to/dynamic_chain/

更多例子:https://python.langchain.com/docs/how_to/lcel_cheatsheet/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/web/92561.shtml
繁体地址,请注明出处:http://hk.pswp.cn/web/92561.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

十九、MySQL-DQL-基本查询

基本查询代码:DQL:基本查询 1.查询指定字段 name,entrydate 并返回 -- 1.查询指定字段 name,entrydate 并返回 select name,entrydate from tb_emp;2.查询返回所有字段 -- 2.查询返回所有字段 -- 推荐 select id, username, password, name, gender, image, job, e…

CamX-骁龙相机修改

1. 修改视频模式预览尺寸和分辨率 vendor/codeaurora/packages/apps/SnapdragonCamera/src/com/android/camera/CaptureModule.javaprivate void updatePreviewSize() {int width mPreviewSize.getWidth();int height mPreviewSize.getHeight(); - mPreviewSize new …

容器技术基础与实践:从镜像管理到自动运行配置全攻略

1. 相比较虚拟机,容器有哪些技术优势?(1)直接在操作系统上运行,从而跨系统上的所有容器共享资源,‘(2)共享主机的内核。(3)与虚拟机相比,它需要的…

书生浦语第五期-L1G4-InternLM 论文分类微调实践(XTuner 版)

XTuner介绍一句话介绍XTuner:XTuner 是一个高效、灵活、全能的轻量化大模型微调工具库。核心特点:高效:支持在有限资源下微调大模型,如在8GB显存上微调7B参数模型,也支持多节点微调70B模型;自动分发高性能算…

从灵感枯竭到批量产出:无忧秘书创作平台如何重构内容生产者的工作流程?全环节赋能分析

在当今快节奏的数字时代,内容创作者面临着前所未有的挑战。无论是自媒体运营者、自由撰稿人还是企业营销人员,都需要高效地生产高质量的内容以满足市场需求。然而,灵感枯竭、效率低下以及内容质量不稳定等问题常常困扰着这些内容生产者。为了…

【开源工具】基于Python的PDF清晰度增强工具全解析(附完整源码)

📄✨ 【开源工具】基于Python的PDF清晰度增强工具全解析(附完整源码) 🌈 个人主页:创客白泽 - CSDN博客 🔥 系列专栏:🐍《Python开源项目实战》 💡 热爱不止于代码,热情源自每一个灵感闪现的夜晚。愿以开源之火,点亮前行之路。 🐋 希望大家多多支持,我们一起进…

Qwen-Image开源模型实战

Qwen-Image开源模型实战:ComfyUI低显存量化部署与中文海报生成指南 阿里云通义千问团队最新开源的Qwen-Image模型以其卓越的中英文文本渲染能力在AI绘图领域掀起了一场革命。这款200亿参数的MMDiT架构模型不仅能够生成高质量图像,更突破了AI绘图长期存在…

JavaWeb03——javascript基础语法

1.什么是JavaScript?JavaScript(简称 JS)是一种 编程语言,它主要用来为网页添加交互功能。它可以让网页变得动态,让它不仅仅是静态的文字和图片,还能响应用户操作(比如点击按钮、弹框警告等&…

数据库入门:从零开始构建你的第一个数据库

欢迎来到数据库的世界!今天,我们将一起探索如何创建、管理和查询数据库。无论你是初学者还是希望加深理解的开发者,这篇博客都将帮助你更好地掌握数据库的基础知识。一、数据库的基本操作创建数据库首先,让我们从创建一个新数据库…

从汇编角度揭秘C++构造函数(1)

C的构造函数一直比较神秘,今天我们通过汇编的角度来揭秘一下,它的本质是什么。与常规函数有什么不同。从以下这段代码说起: class Person { public:Person(int age) { _age age; }void printAge(){ printf("age %d\r\n",_age); …

java10学习笔记

Java 10 于 2018 年 3 月发布,是 Java 平台按照新的六个月发布周期发布的第一个版本。虽然相比 Java 8 和 Java 9 的大型更新,Java 10 的变化较小,但仍然引入了一些重要的特性,特别是本地变量类型推断(var)…

Flutter Listview的基本使用

Listview() 前端页面常见的一个以列表方式显示内容的组件。可垂直或水平滚动的列表。属性说明scrollDirection设置滚动的方向,取值包括horizontal、verticalreverse设置是否翻转,默认值falseitemExtent设置滚动方向子元素的长度,垂直方向为高…

强化学习笔记:从Q学习到GRPO

推荐学习huggingface的强化学习课程,全面了解强化学习的发展史。 以下是个人笔记,内容不一定完整,有些是个人理解。 基于值函数(value function)的强化学习 基于值函数(value function)的强化学习:学习的是一个值函数&#xff0…

MySQL索引底层原理与性能优化实践

#技术栈深潜计划一、前言 在日常开发中,MySQL数据库以其高效、易用、可扩展等特性成为最主流的关系型数据库之一。而索引作为数据库查询优化的核心工具,被誉为“数据库的加速器”。但你真的了解MySQL索引的底层原理吗?为什么InnoDB默认采用B树…

Ext系列文件系统

1.硬件常见的硬件有磁盘、服务器、机柜、机房机械磁盘但是计算机中唯一的一个机械设备磁盘外设的特点就是外设慢容量大,价格便宜1.1.磁盘的物理结构磁盘的物理图:磁盘的存储图扇区:是磁盘存储数据的基本单位,512字节,块设备磁盘的…

前缀函数——KMP的本质

前缀函数我个人觉得 oiwiki 上的学习顺序是很合理的,学 KMP 之前先了解前缀函数是非常便于理解的。前后缀定义 前缀 prefixprefixprefix 指的是从字符串 SSS 的首位到某个位置 iii 的一个子串,这样的子串写作 prefix(S,i)prefix(S,i)prefix(S,i)。 后缀 …

解决chrome下载crx文件被自动删除,加载未打包的扩展程序时提示“无法安装扩展程序,因为它使用了不受支持的清单版本解决方案”

解决chrome下载crx文件被自动删除 【chrome设置-隐私与安全-安全浏览】,选择 不保护 【chrome设置-下载内容】,勾选 下载前询问每个文件的保存位置 下载crx文件时,选择保存文件夹,将 .crx后缀 改为 .zip后缀,再确定。 …

嵌入式学习day23-shell命令

linux软件编程学习大纲:1.IO操作文件2.多任务编程3.网络编程4.数据库编程5.硬件设备管理学习目标:1.学习接口调用(第一层)2.软件操作流程和思想(第二层)3.软件设计思想和流程架构(第三层&#x…

GPT-5 系列深度详解:第1章-引言(目录)

1 引言2 模型数据与训练3 观察到的安全挑战与评估 3.1 从强制拒绝到安全完成 3.2 禁⽌内容 3.3 拍⻢屁 3.4 越狱 3.5 指令层级 3.6 幻觉 3.7 欺骗 3.7.1 欺骗思维链监控 3.8 图像输入 3.9 健康 3.10 多语言性能 3.1.1公平性与偏见: BBQ评估4 红队测试与外部评估…

NineData 新增支持 AWS ElastiCache 复制链路

2025 年,绝大多数企业已完成业务上云,以获取更高的弹性、可扩展性和成本效益。AWS ElastiCache 作为 AWS 提供的全托管式内存数据库服务,已成为许多企业在云上构建高并发、低延迟应用的理想选择。NineData 数据复制现已全面支持从自建 Redis …