LangChain Prompt提示词工程

本文未完待续

引言

本文基于Python 1.13.x和LangChain 1.1.2,并采用DeekSeep大模型,介绍LangChain提示词工程的实现。

pip install langchain==1.1.2
pip install langchain-openai

langchain-openai底层也是对openai官方OpenAI Python API的封装,也可以直接用官方OpenAI Python,但是没有必要

类似的其他语言和框架的提示词工程实现案例,可以移步:

1.阻塞式对话

一个基于init_chat_model的简单的对话实现

  • model 大模型名称
  • model_provider 采用协议
  • api_key API_KEY
  • base_url 接口地址
  • temperature 温度,越高内容越随机,越低,内容越确定
  • max_tokens 生成内容的最大token数
  • timeout 请求超时时间
  • max_retries 最大重试次数

上述这些参数,可能对某些大语言模型无效,仅对于langchain官方提供的集成包例如langchain-openai,langchain-anthropic等有效,对于langchain-community下定义的第三方模型可能无效

from langchain.chat_models import init_chat_model
import os

llm = init_chat_model(
    model = 'deepseek-chat',
    model_provider = 'openai',
    api_key = os.getenv('DSKEY'),
    base_url = 'https://api.deepseek.com'
)

msg = llm.invoke('你是谁')
print(msg)

model.invoke()返回的是一个AIMessage对象,包括以下常见内容

  • type 描述是哪个类型的消息:user/ai/system/tool
  • content 大模型的输出,一般是字符串,多模态模型可能是其他内容
  • name 当消息类型相同,对消息进行区分,不是所有模型都支持
  • response_metadata ai消息才会包含的属性,附加元数据,不同模型内容也是不一样的
  • tool_calls ai消息才会包含的属性,当大模型决定调用某个工具时,就会包含,每个元素是一个字典,包含工具名name,参数args,工具唯一标识id等

2.流式输出

遍历llm.stream返回的迭代器对象Iterator[AIMessageChunk],得到实时返回的输出,打印追加


from langchain.chat_models import init_chat_model
import os

llm = init_chat_model(
    model = 'deepseek-chat',
    model_provider = 'openai',
    api_key = os.getenv('DSKEY'),
    base_url = 'https://api.deepseek.com'
)

for trunk in llm.stream('你是谁'):
    print(trunk.content, end='')

print('结束')

还可以每次返回和之前的返回拼接在一起

无数trunk对象通过+加在一起,底层是用重写__add__()方法运算符重载实现

from langchain.chat_models import init_chat_model
import os

llm = init_chat_model(
    model = 'deepseek-chat',
    model_provider = 'openai',
    api_key = os.getenv('DSKEY'),
    base_url = 'https://api.deepseek.com'
)

full = None
for trunk in llm.stream('用一句话介绍自己'):
    full = trunk if full is None else full + trunk
    print(full.text)
    print(full.content_blocks)

print('结束')
print(full.content_blocks)

运行结果:

[]
你好
[{'type': 'text', 'text': '你好'}]
你好,
[{'type': 'text', 'text': '你好,'}]
你好,我是
[{'type': 'text', 'text': '你好,我是'}]
你好,我是Deep
[{'type': 'text', 'text': '你好,我是Deep'}]
你好,我是DeepSe
[{'type': 'text', 'text': '你好,我是DeepSe'}]
你好,我是DeepSeek
[{'type': 'text', 'text': '你好,我是DeepSeek'}]
你好,我是DeepSeek,
[{'type': 'text', 'text': '你好,我是DeepSeek,'}]
你好,我是DeepSeek,一个
[{'type': 'text', 'text': '你好,我是DeepSeek,一个'}]
你好,我是DeepSeek,一个由
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由'}]
你好,我是DeepSeek,一个由深度
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度'}]
你好,我是DeepSeek,一个由深度求
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求'}]
你好,我是DeepSeek,一个由深度求索
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索'}]
你好,我是DeepSeek,一个由深度求索公司
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司'}]
你好,我是DeepSeek,一个由深度求索公司创造的
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的'}]
你好,我是DeepSeek,一个由深度求索公司创造的AI
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的AI'}]
你好,我是DeepSeek,一个由深度求索公司创造的AI助手
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的AI助手'}]
你好,我是DeepSeek,一个由深度求索公司创造的AI助手,
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的AI助手,'}]
你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于'}]
你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用'}]
你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情'}]
你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻'}]
你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式'}]
你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式为你
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式为你'}]
你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式为你提供
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式为你提供'}]
你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式为你提供帮助
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式为你提供帮助'}]
你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式为你提供帮助!
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式为你提供帮助!'}]
你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式为你提供帮助!😊
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式为你提供帮助!😊'}]
你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式为你提供帮助!😊
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式为你提供帮助!😊'}]
你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式为你提供帮助!😊
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式为你提供帮助!😊'}]
结束
[{'type': 'text', 'text': '你好,我是DeepSeek,一个由深度求索公司创造的AI助手,乐于用热情细腻的方式为你提供帮助!😊'}]

还可以加上一个提示词模板PromptTemplate

要想流式,只要改成chain.stream(...)即可

import os

from langchain.chat_models import init_chat_model
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableSequence

prompt_template = PromptTemplate(
    template='做一个关于{topic}的小诗',
    input_variables=['topic']
)

llm = init_chat_model(
    model = 'deepseek-chat',
    model_provider = 'openai',
    api_key = os.getenv('DSKEY'),
    base_url = 'https://api.deepseek.com'
)

parser = StrOutputParser()

# 提示词=》大模型=》格式化输出
chain = RunnableSequence(prompt_template, llm, parser)

resp = chain.invoke({'topic': '霸道总裁爱上做保洁的我'})

print(resp)

3.LCEL增强对话功能

要理解LCEL,首先要了解Runable,Runable(langchain_core.runnables.base.Runnable)是langchain中可以调用,批处理,流式输出,转换和组合的工作单元,是实现LCEL的基础,通过重写__or__()方法,实现了|运算符的重载,实现了Runable的类对象之间便可以进行一些类似linux命令中的管道(|)操作。

LCEL,全称LangChain Express Language,即LangChain表达式语言,也是LangChain官方推荐的写法,是一种从Runable而来的声明式方法,用于声明,组合和执行各种组件(模型,提示词,工具等),如果要使用LCEL,对应的组件必须实现Runable,使用LCEL创建的Runable称之为链。

例如,将刚刚提示词模板的例子用LCEL重写


import os

from langchain.chat_models import init_chat_model
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser

prompt_template = PromptTemplate(
    template='做一个关于{topic}的小诗',
    input_variables=['topic']
)

llm = init_chat_model(
    model = 'deepseek-chat',
    model_provider = 'openai',
    api_key = os.getenv('DSKEY'),
    base_url = 'https://api.deepseek.com'
)

parser = StrOutputParser()

# LCEL重写
chain = prompt_template | llm | parser

resp = chain.invoke({'topic': '霸道总裁爱上做保洁的我'})

print(resp)

还可以自定义一个word_count(text: str) -> int函数,通过langchain的RunnableLambda对象包装,使得函数变为获得链式的执行能力的Runable对象,拼入链中,统计大模型回复的字数


import os

from langchain.chat_models import init_chat_model
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableLambda

prompt_template = PromptTemplate(
    template='做一个关于{topic}的小诗',
    input_variables=['topic']
)

llm = init_chat_model(
    model = 'deepseek-chat',
    model_provider = 'openai',
    api_key = os.getenv('DSKEY'),
    base_url = 'https://api.deepseek.com'
)

parser = StrOutputParser()


def word_count(text: str) -> int:
    print('----------word_count---------')
    return len(text)

word_counter = RunnableLambda(word_count)

# LCEL重写
chain = prompt_template | llm | parser | word_counter

resp = chain.invoke({'topic': '霸道总裁爱上做保洁的我'})

print(resp)

运行:

----------word_count---------
232

"如果文章对您有帮助,可以请作者喝杯咖啡吗?"

微信二维码

微信支付

支付宝二维码

支付宝


LangChain Prompt提示词工程
https://blog.liuzijian.com/post/2025/12/12/langchain-prompt/
作者
Liu Zijian
发布于
2025年12月12日
许可协议