在大模型应用开发中,我们经常会把一次请求的结果交到下一次请求,实现多次请求大模型,来完成一个复杂的任务,
这里我们使用langchain 的顺序链,来做测试。
示例中的任务为:我们把一篇英文文章发送给大模型,让大模型首先翻译为中文,然后再将翻译后的中文交给大模型,让其生成摘要,最后让其用摘要写一篇文案输出,最终结果我们是要得到一篇中文文案。
代码示例:
一.引入包
from langchain.chains import LLMChain, SimpleSequentialChain, SequentialChain,ConversationChain from lc_deepseek_ai import CreateChartDeepSeek from langchain.prompts import PromptTemplate,ChatPromptTemplate二.使用SequentialChain
def use_SequentialChain(): chart_prompt=ChatPromptTemplate.from_messages([ ("system","你是一个翻译专家,请你将用户输入的文本翻译成中文。"), ("user","{input}") ]) chart_chain1=LLMChain(llm=llm,prompt=chart_prompt,verbose=True,output_key="keywords") chart_prompt2=ChatPromptTemplate.from_messages([ ("system","你是一个文本分析专业,请你分析用户输入的文本,并总结文本的中摘要信息。"), ("user","{keywords}") ]) chart_chain2=LLMChain(llm=llm,prompt=chart_prompt2,verbose=True,output_key="summary") chart_prompt3=ChatPromptTemplate.from_messages([ ("system","你是一个文案写作高手,请你根据用户输入的摘要信息,生成一篇关文,字数不超过200字。"), ("user","{summary}") ]) chart_chain3=LLMChain(llm=llm,prompt=chart_prompt3,verbose=True,output_key="content") chain=SequentialChain(chains=[chart_chain1,chart_chain2,chart_chain3],verbose=True ,input_variables=["input"],output_variables=["keywords","summary","content"]) txt="One day a crow stood on a branch near his nest and felt very happy with the meat in his mouth.
At that time, a fox saw the crow with the meat, so he swallowed and eagerly thought of a plan to get
the meat. However, whatever the fox said to the crow, the crow just kept silent. Until the fox thought
highly of the crows beautiful voice, the crow felt flattered and opened his mouth to sing.
As soon as the meat fell down to the ground, the fox took the meat and went into his hole"
result=chain.invoke({"input":txt}) print(result)三.同样可以使用LECL方式或简单顺序链实现一些较为简单的业务
def use_SimpleSequentialChain(): chart_prompt=ChatPromptTemplate.from_messages([ ("system","你是一个文本分析专业,请你分析用户输入的文本,并抽取其中关键词。"), ("user","{input}") ]) chart_chain1=chart_prompt|llm chart_prompt2=ChatPromptTemplate.from_messages([ ("system","你是一个写作大师,根据用户输入的关键词,生成一篇小故事。"), ("user","{keywords}") ]) chart_chain2=chart_prompt2|llm chain=chart_chain1|chart_chain2 result=chain.invoke({"input":"《小马过河》是一则经典寓言故事,讲述小马在妈妈鼓励下独立尝试过河,最终发现河水既不像老牛说的那样浅,也不像松鼠说的那样深,从而领悟到实践与独立思考的重要性"}) print(result) def use_SimpleSequentialChain1(): chart_prompt=ChatPromptTemplate.from_messages([ ("system","你是一个文本分析专业,请你分析用户输入的文本,并抽取其中关键词。"), ("user","{input}") ]) chart_chain1=LLMChain(llm=llm,prompt=chart_prompt,verbose=True) chart_prompt2=ChatPromptTemplate.from_messages([ ("system","你是一个写作大师,根据用户输入的关键词,生成一篇小故事。"), ("user","{keywords}") ]) chart_chain2=LLMChain(llm=llm,prompt=chart_prompt2,verbose=True) chain=SimpleSequentialChain(chains=[chart_chain1,chart_chain2],verbose=True) result=chain.invoke({"input":"《小马过河》是一则经典寓言故事,讲述小马在妈妈鼓励下独立尝试过河,最终发现河水既不像老牛说的那样浅,也不像松鼠说的那样深,从而领悟到实践与独立思考的重要性"}) print(result)四.测试
if __name__ == "__main__": use_SequentialChain()五.输出结果为
> Finished chain. {'input': 'One day a crow stood on a branch near his nest and felt very happy with the meat in his mouth. At that time, a fox saw the crow with the meat, so he swallowed and eagerly thought of a plan
to get the meat. However, whatever the fox said to the crow, the crow just kept silent. Until the fox
thought highly of the crows beautiful voice, the crow felt flattered and opened his mouth to sing.
As soon as the meat fell down to the ground, the fox took the meat and went into his hole',
'keywords': '一天,乌鸦站在窝旁的树枝上,嘴里叼着一片肉,心里非常高兴。这时候,狐狸看见了乌鸦嘴里的肉,
馋得直流口水,非常想得到那片肉。但是,无论狐狸说什么,乌鸦就是不理睬狐狸。后来,狐狸便赞美乌鸦的嗓音优美,
并要求乌鸦唱几句让他欣赏。乌鸦听了狐狸的奉承话得意极了,就唱起歌来。没想到,它一张嘴肉就掉了下来,
狐狸叼起肉就钻到洞里去了。', 'summary': '【文本摘要分析】\n\n核心事件:\n1. 乌鸦获得食物(树枝上叼着肉)\n2. 狐狸实施欺骗(通过赞美诱使乌鸦开口)\n3. 结果反转(乌鸦失去食物,狐狸得逞)
\n\n人物特征:\n- 乌鸦:易被奉承影响判断(因听到赞美而放松警惕)\n- 狐狸:善于利用心理弱点(精准抓住乌鸦的虚荣心)\n\n寓意提示:\n▶ 警惕甜言蜜语背后的动机\n▶ 保持理性判断的重要性\n▶
虚荣心可能导致损失\n\n关键转折点:\n狐狸从直接索要(失败)转为心理策略(成功)\n\n高频意象:\n肉(利益象征)、歌声(虚荣心触发点)、树与洞(强弱地位转换的空间象征)\n\n(分析维度包含:
叙事结构、角色塑造、隐喻层次三个层面)'
, 'content': '《乌鸦与狐狸》新编\n\n老树枯枝上,乌鸦正得意地叼着刚得的肥肉。树下的狐狸眼珠一转,忽然开口赞叹:"您的羽毛多么光亮!"见乌鸦不为所动,又夸:"您的歌喉定比百灵更美!"乌鸦终于按捺不住虚荣,
刚张嘴炫耀,肉块便坠入狐狸口中。
这则寓言揭示:甜言蜜语常包裹着算计的毒饵,保持清醒才能守住真正的收获。当别人突然赞美你最在意的长处时,
记得先捂紧口袋里的"肉"。\n\n(198字)'} 进程已结束,退出代码为 0