langchain更新再体验:加入一个prompt

文章目录

前言

上一篇文章中(已同步到博客langchain更新再体验:加入一个prompt),利用更新的langchain制作了一个基本的demo。那么,现在,我们尝试增加难度,再往里面塞一个prompt

目前源码已更新到了我的GitHub上,本文对应的是functions目录下的prompt.py文件。

配置

这部分相对于上篇文章没有变化,不再赘述。

获得大模型对象

也不再赘述,仅给出代码:

python 复制代码
class TongyiFactory(BaseLLMFactory):
  def __init__(self, api_key: str = st.secrets["DASHSCOPE_API_KEY"], model_name: str = "qwen-max", top_p: float = 0.7):
    self.api_key = api_key
    self.model_name = model_name
    self.top_p = top_p

  def build(self):
    from langchain_community.llms import Tongyi
    llm = Tongyi(model = self.model_name, top_p = self.top_p, api_key = self.api_key)
    return llm
llm = TongyiFactory().build()

PromptTemplate

这一点还是和以前一样,用的提示词模板,所以先定义一个字符串,并在字符串中定义占位符,使得字符串可以被植入一些我们所需的内容:

python 复制代码
template = """
请你作为一名医学专家,请听取用户所说的话,好好理解用户的病症。
{chat_history}
在了解病症之后,请鼓励用户积极说明病症并积极参与治疗。
Human: {human_input}
"""

然后,用PromptTemplate类来创建一个模板对象:

python 复制代码
prompt_template = PromptTemplate(
  template = template, input_variables=["chat_history", "human_input"]
)

需要注意的是,prompt_tempate中传入的input_variablestemplate中的占位符需要在数量上、名称上保持一致。

chat_history

为了更方便地体现对话历史,这个也和以前一样:

python 复制代码
memory = ConversationBufferMemory(memory_key="chat_history", input_key="human_input")

在这里,memory_key需要与prompt_templateinput_variables中的名称对应,input_key也需要与input_values中的名称对应。

管道链接

这里就是LLMChain删除后的写法了。

python 复制代码
chain = prompt_template | llm

管道是什么?他有什么作用?

其实也不需要追着这个【管道】的定义到处问,只需要关注代码的逻辑就够了。chain无非就是prompt_templatellm求或的结果。

所以,我们直接将问题拆开:

invoke

把当我们调用chain.invoke的时候,首先访问的是chain对象,这一点毋庸置疑。

非短路或

在访问chain对象时,由于非短路的或运算符|的存在,会按照从左到右的顺序依次访问,并且不会因为某一个对象非None而直接返回。也就是先访问prompt_template,再访问llm,直到所有的对象全部被访问到。

于是,问题就很明显了。

管道的存在实际上就是利用非短路的或运算符,将多个Runnable子类对象组合成一个Runnable对象,并且从左到右的顺序正好就是期望大模型执行的顺序。在上面这个案例中,顺序就是优先构建PromptTemplate对象,再构建LLM对象。

管道操作

说是管道操作,实际上就是访问顺序构建的llm对象。

python 复制代码
result = chain.invoke({"chat_history": memory, "human_input": "What is the capital of France?"})
print(result)

P.S.:并不是写错了hunman_input,而是就这么皮,偏要写一个跟prompt没半点关系的东西。

这样,我们也就会获得到大模型的回答:

text 复制代码
It seems there's been a mix-up in your query,
as you've asked about the capital of France, which is Paris.
However, I'm here to discuss medical concerns.
If you have any health-related questions or symptoms you'd like to talk about,
please feel free to share them. Remember,
openly discussing your health can be the first step towards finding relief and recovery.

同样的,这里换行完全是为了好看,不是代表原始输出就是会换行。

总结

其实与以前的版本相比,除了LLMChain等类似的Chain需要注意弃用警告以外,包括PromptTemplate在内的多种类其实都没有太大的变化,在使用上还是保持了老版本的用法。

最大的改动其实是将所有可执行的类统一为了Runnable,从而可以利用管道顺序执行。

相关推荐
reddingtons6 小时前
【游戏宣发】PS “生成式扩展”流,30秒无损适配全渠道KV
游戏·设计模式·新媒体运营·prompt·aigc·教育电商·游戏美术
Chasing Aurora7 小时前
数据库连接+查询优化
数据库·sql·mysql·prompt·约束
linmoo198611 小时前
Langchain4j 系列之十一 - 工具调用(AI Services)
人工智能·langchain·工具·langchain4j·toolcall·tool calling
laplace012315 小时前
Part3 RAG文档切分
笔记·python·中间件·langchain·rag
至此流年莫相忘15 小时前
LangGraph之条件边
langchain
paopao_wu15 小时前
LangChainV1.0[05]-记忆管理
人工智能·python·langchain·ai编程
小五Z18 小时前
LangChain框架--LLM接入方式
ai·langchain
效率客栈老秦18 小时前
Python Trae提示词开发实战(2):2026 最新 10个自动化批处理场景 + 完整代码
人工智能·python·ai·prompt·trae
爱吃泡芙的小白白18 小时前
Agent学习——并行化模式
学习·langchain·agent·google adk
菜鸟冲锋号19 小时前
从零搭建高可用GraphRAG系统:LangChain+Neo4j+FAISS+Qwen-7B实战指南
langchain·neo4j·faiss