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,从而可以利用管道顺序执行。

相关推荐
梵得儿SHI10 小时前
(第三篇)Spring AI 基础入门:PromptTemplate 与对话工程实战(从字符串拼接到底层模板引擎的进阶之路)
人工智能·prompt·大模型应用·spring ai·prompttemplate·ai 响应的质量与准确性·上下文管理策略
大模型真好玩12 小时前
LangChain1.0速通指南(二)——LangChain1.0 create_agent api 基础知识
人工智能·langchain·mcp
FreeCode15 小时前
LangChain1.0智能体开发:模型使用
人工智能·langchain·agent
chenchihwen20 小时前
AI代码开发宝库系列:LangChain 工具链:从LCEL到实际应用
人工智能·python·langchain·rag
风雨中的小七1 天前
解密prompt系列63. Agent训练方案:RStar2 & Early Experience etc
prompt
扯蛋4381 天前
LangChain的学习之路( 一 )
前端·langchain·mcp
oliveray2 天前
ATPrompt:基于属性的视觉提示
人工智能·prompt·vlm
梦子yumeko2 天前
第六章langchain4j之Tools和prompt
大数据·prompt
serve the people2 天前
Formatting Outputs for ChatPrompt Templates(one)
langchain·prompt
云烟飘渺o2 天前
生活视角下Prompt 提示词思考
人工智能·prompt·生活