正义也会迟到 發表於 2026-1-22 22:51:00

Langchain 快速入门(一): 运行你第一个LLM模型

<h1 id="简介">简介</h1>
<p>langchain专门用于构建LLM大语言模型,其中提供了大量的prompt模板,和组件,通过chain(链)的方式将流程连接起来,操作简单,开发便捷。</p>
<h1 id="环境配置">环境配置</h1>
<p><strong>安装langchain框架</strong></p>
<pre><code class="language-bash">pip install langchain langchain-community
</code></pre>
<p>其中langchain可以提供了各种大模型语言库选择,(这里只列举几个)例如:</p>
<pre><code class="language-bash">#chatgpt
pip install langchain-openai
#hugging face
pip install langchain-huggingface
#千问
pip install langchain-qwq
</code></pre>
<h1 id="1-让模型跑起来">1. 让模型跑起来</h1>
<p>如何让你llm跑起来,这里用的是千问,来演示</p>
<h3 id="案例">案例</h3>
<pre><code class="language-python">import os
from langchain_community.chat_models.tongyi import ChatTongyi
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

#这里是你的千问apikey
os.environ["DASHSCOPE_API_KEY"] = "apikey"

model = ChatTongyi(model="qwen-plus")

prompt = ChatPromptTemplate.from_messages([
&nbsp; &nbsp; ("system", "你是一个精通{topic}的资深技术专家。"),
&nbsp; &nbsp; ("user", "请用三句话解释一下什么是{concept}。")
])

output_parser = StrOutputParser()

chain = prompt | model | output_parser

#文本输出
response = chain.invoke({"topic": "Python", "concept": "列表"})
print(response)

#分割
print("="*30)

#流式输出
for chunk in chain.stream({"topic": "人工智能", "concept": "神经网络"}):
&nbsp; &nbsp; print(chunk, end="", flush=True)
</code></pre>
<h3 id="代码解释">代码解释</h3>
<p>整个代码的流程如下:<br>
<strong>创建模型-&gt;构建提示词-&gt;构建chain链-&gt;使用大模型</strong></p>
<h5 id="创建模型">创建模型</h5>
<p><mark>这一步用不同的模型可能会不同</mark><br>
这里利用langchain的千问库创建模型,可能会不同</p>
<pre><code class="language-python">model = ChatTongyi(model="qwen-plus")

#例如用chatgpt
llm = init_chat_model("gpt-4o", model_provider="openai")
</code></pre>
<h5 id="构建提示词">构建提示词</h5>
<p>这一步构建利用了langchain库提供提示词模板:<br>
其中用<code>{}</code>阔起来的在调用时可以动态用字典替换</p>
<pre><code class="language-python">prompt = ChatPromptTemplate.from_messages([
&nbsp; &nbsp; ("system", "你是一个精通{topic}的资深技术专家。"),
&nbsp; &nbsp; ("user", "请用三句话解释一下什么是{concept}。")
])
</code></pre>
<p>各个角色功能如下:</p>
<table>
<thead>
<tr>
<th><strong>角色名称 (Role)</strong></th>
<th><strong>对应的类</strong></th>
<th><strong>作用说明</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>system</strong></td>
<td><code>SystemMessage</code></td>
<td><strong>系统提示词</strong>。用于设定 AI 的“人格”、专业背景、行为准则或约束条件。它通常优先级最高,决定了后续对话的基调。</td>
</tr>
<tr>
<td><strong>user</strong></td>
<td><code>HumanMessage</code></td>
<td><strong>用户消息</strong>。代表人类发送的内容。这是模型需要直接回答或处理的问题。</td>
</tr>
<tr>
<td><strong>ai</strong></td>
<td><code>AIMessage</code></td>
<td><strong>AI 消息</strong>。代表模型之前的回复。在构建多轮对话(带记忆)时,需要把模型之前的回复传回去。</td>
</tr>
</tbody>
</table>
<h5 id="构建chain链">构建chain链</h5>
<p><mark>这个是langchain的灵魂,这里简单说明,后面会发更详细的教学文章</mark><br>
chain链的运行流程如下:<br>
<strong>将输入填充prompt-&gt;将完整prompt喂给LLM-&gt;直接解析返回文本</strong></p>
<p><code>StrOutputParser()</code>这个是langchain提供的文本解析器,用于将上面的结果解析为文本</p>
<pre><code class="language-python">output_parser = StrOutputParser()
chain = prompt | model | output_parser
</code></pre>
<h5 id="使用大模型">使用大模型</h5>
<p>这里有两种方式:</p>
<ol>
<li>直接输出完整的文本</li>
</ol>
<pre><code class="language-python">response = chain.invoke({"topic": "Python", "concept": "列表"})
print(response)
</code></pre>
<ol start="2">
<li>流文本输出(打字机)</li>
</ol>
<pre><code class="language-python">for chunk in chain.stream({"topic": "人工智能", "concept": "神经网络"}):
&nbsp; &nbsp; print(chunk, end="", flush=True)
</code></pre>
<p><strong>如果❤喜欢❤本系列教程,就点个关注吧,后续不定期更新~</strong></p>


</div>
<div id="MySignature" role="contentinfo">
    <p>本文来自博客园,作者:ClownLMe,转载请注明原文链接:https://www.cnblogs.com/ClownLMe/p/19519224</p><br><br>
来源:https://www.cnblogs.com/ClownLMe/p/19519224
頁: [1]
查看完整版本: Langchain 快速入门(一): 运行你第一个LLM模型