首页 / 资讯中心 / 文章详情

【RAG开发】LangChain通用提示词模板

【RAG开发】LangChain通用提示词模板 ★ FEATURED ARTICLE
提示词优化在模型应用中非常重要LangChain提供了PromptTemplate类用来协助优化提示词。标准写法如下from langchain_community.llms.tongyiimportTongyi from langchain_core.promptsimportPromptTemplate templatePromptTemplate.from_template(我的邻居姓{lastname},今年半马成绩是{score}了帮我祝福他)prompt_texttemplate.format(lastname丁,score133)# 创建模型对象modelTongyi(modelqwen-max)# 调用模型获取结果resmodel.invoke(prompt_text)print(res)使用PromptTemplate还不如自己手动拼接字符串使用Template模板构建提示词在大型工程中更容易做标准化模板Template模板类支持LangChain框架的链式调用加入链总结基于PromptTemplate类可以得到提示词模板支持基于模板注入变量得到最终提示词zero-shot思想下可以基于PromptTemplate直接完成few-shot思想下需要更换为FewShotPromptTemplate
阅读完成 · 觉得有帮助?
咨询建站