LangChain学习笔记day02
LangChain学习笔记day02
1. 更换LLM为本地模型
(1) 配置环境
① 前往ollama官网下载并安装ollama客户端
ollama:用于在本地运行和管理大语言模型的轻量推理框架
② 从官网左上角“Models”标签进入大模型列表,选择合适的大模型
③ 在终端控制台输入 ollama pull modelName (其中“modelName”替换为选用的大模型的名字)下载模型,下载完成后可输入 ollama ls 查看当前可用大模型,校验是否下载成功
④ 启动ollama应用
(2) 导入ChatOllama组件
from langchain_ollama import ChatOllama
ChatOllama 是 LangChain 中用于调用 Ollama 本地模型的 Runnable 封装类
(3) 查阅组件源码,获取配置模板
r"""
Key init args — completion params:
model: str
Name of Ollama model to use.
reasoning: bool | None
Controls the reasoning/thinking mode for
[supported models](https://ollama.com/search?c=thinking).
- `True`: Enables reasoning mode. The model's reasoning process will be
captured and returned separately in the `additional_kwargs` of the
response message, under `reasoning_content`. The main response
content will not include the reasoning tags.
- `False`: Disables reasoning mode. The model will not perform any reasoning,
and the response will not include any reasoning content.
- `None` (Default): The model will use its default reasoning behavior. Note
however, if the model's default behavior *is* to perform reasoning, think tags
(`<think>` and `</think>`) will be present within the main response content
unless you set `reasoning` to `True`.
temperature: float
Sampling temperature. Ranges from `0.0` to `1.0`.
num_predict: int | None
Max number of tokens to generate.
See full list of supported init args and their descriptions in the params section.
"""
(4) 实例化大模型
localLLM = ChatOllama(
model="gemma3:4b",
reasoning=None,
temperature=0,
num_predict=None,
)
reasoning 参数:用于控制模型是否输出思维链
(5) 修改链式调用
# chain = prompt_template | apiLLM
chain = prompt_template | localLLM
(6) 主要代码及运行结果
代码
def main():
# print("Hello World")
game1 = "鸣潮"
game2 = "原神"
template = "请简要告诉我游戏{game}主要讲了一个什么样的故事。"
prompt_template = PromptTemplate(
input_variables=["game"],
template=template
)
apiLLM = ChatOpenAI(
model="deepseek-v4-flash",
temperature=0,
max_tokens=None,
timeout=None,
max_retries=2,
base_url="https://api.deepseek.com",
# api_key="...",
# organization="...",
# other params...
)
localLLM = ChatOllama(
model="gemma3:4b",
reasoning=None,
temperature=0,
num_predict=None,
)
# chain = prompt_template | apiLLM
chain = prompt_template | localLLM
response1 = chain.invoke(input={"game": game1})
print(response1.content)
response2 = chain.invoke(input={"game": game2})
print(response2.content)
运行结果
《鸣潮》的故事核心围绕着“血脉”展开,讲述了在末世中,人类为了寻找失落的“血脉”,踏上了一场充满危险和背叛的冒险之旅。
具体来说:
- 末世背景: 世界被一场名为“血潮”的灾难摧毁,大部分人类灭绝,只剩下少数幸存者。
- 血脉传承: 玩家扮演的“猎人”拥有特殊的血脉,能够感知到“血潮”的源头和失落的“血脉”,并以此为目标进行探索。
- 势力纷争: 在寻找“血脉”的过程中,玩家会与其他势力(如“血影”、“圣堂”)发生冲突,这些势力也同样觊觎着“血脉”的力量。
- 背叛与救赎: 故事中充满了复杂的角色关系和背叛情节,玩家需要做出选择,并最终决定自己的命运和世界的未来。
总而言之,《鸣潮》的故事是一个关于末世求生、探索未知、以及在混乱中寻找希望和救赎的史诗冒险。
请注意: 《鸣潮》目前处于测试阶段,剧情仍在不断完善和更新。
《原神》的故事核心围绕着“神”和“人类”之间的关系展开,讲述了旅行者(玩家扮演的角色)在神秘的开放世界提瓦特大陆上寻找失散的亲人,并逐渐揭开这个世界的秘密。
具体来说:
- 起源之谜: 旅行者来到提瓦特大陆时,发现自己与这里的人民有着千丝万缕的联系,并且被卷入了一场关于“神”的古老战争。
- 七国纷争: 提瓦特大陆由七大国家组成,它们之间存在着复杂的政治和宗教冲突,而这些冲突都与“神”的力量有关。
- 混沌入侵: 一个名为“混沌”的存在试图吞噬提瓦特大陆的秩序,旅行者必须联合各国的英雄,对抗混沌的威胁。
- 探索与成长: 玩家在探索世界、完成任务的过程中,会逐渐了解提瓦特的历史、文化和各种神秘力量,并不断提升自己的能力。
总而言之,《原神》的故事是一个充满冒险、探索和战斗的史诗故事,探讨了秩序与混沌、信仰与现实等深刻的主题。
希望这个简要介绍对您有所帮助!
2. 使用LangSmith进行链路追踪
引言:什么是LangSmith
定义
LangSmith 是 LangChain 官方提供的 LLM应用调试与监控平台
作用
- 追踪每一次 LLM 调用
- 记录 Prompt / Response
- 分析 Token / 延迟 / 成本
- 调试 Agent 执行链路
- 可视化 RAG / Tool 调用过程
(1) 配置环境
① 注册并登录LangSmith官网
② 根据官网提示下载openai与langsmith依赖,并在.env中配置环境变量
uv add openai langsmith
- openai:主要用于 LangSmith tracing hook 兼容,LangChain 的很多 tracing 自动依赖 openai SDK hook
- langsmith:追踪&记录链路
LANGSMITH_TRACING=true
LANGSMITH_ENDPOINT=https://apac.api.smith.langchain.com
LANGSMITH_API_KEY=<your-api-key>
LANGSMITH_PROJECT="pr-proper-weedkiller-13"
OPENAI_API_KEY=<your-openai-api-key>
- LANGSMITH_TRACING:是否开启追踪
- LANGSMITH_ENDPOINT:LangSmith服务地址,根据所在区域修改链接前缀,美国为us,欧洲为eu,亚太为apac
- LANGSMITH_API_KEY:从官网生成的api密钥
- LANGSMITH_PROJECT:项目名称,可自定义,用于区分不同实验或项目的 trace 数据
(2) 开始追踪
① 将LANGSMITH_TRACING设置为true后直接运行项目
② 前往LangSmith Home → Tracing 查看追踪结果

③ 选择项目“LangChainLearning”,选择一条记录即可查看追踪详情



posted on 2026-06-24 01:49 AQHuiguang 阅读(3) 评论(0) 收藏 举报
浙公网安备 33010602011771号