LangChain入门:10.基于Chain of Thought构建店铺运营助手

本文介绍了如何开发一个AI运营助手,它通过理解用户的情感需求和个性化偏好,使用思维链模板进行推理,为花店电商提供定制化的花卉推荐,并提供了代码实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在本次实战中,我们将开发一个 AI 运营助手,它能根据用户的需求进行推理和生成答案。将在用户提出花卉选择需求时,根据事先定义的思维链模板进行推理,并给出最合适的答案。

项目需求

作为一个为花店电商公司工作的 AI 助手,它的目标是帮助客户根据他们的喜好做出明智的决定。具体需求如下:

  1. 理解客户的需求。
  2. 按部就班的思考,考虑各种鲜花的涵义。
  3. 根据客户的需求,给出最符合逻辑的花卉推荐。
  4. 需要向客户解释推荐的原因。
实现思路

为了实现运营助手,我们可以按照以下步骤进行:

  1. 定义思维链模板:设计思维链模板,用于指导运营助手在用户提出需求时进行推理和生成答案。
  2. 编写代码逻辑:根据思维链模板编写代码逻辑,实现运营助手的核心功能。
  3. 测试和优化:测试运营助手功能,并根据测试结果进行优化和改进。
示例对话及代码实现

针对这次的需求,我设计了下面这样的思维链模板。

示例 1:
人类:我想找一种象征爱情的花。
AI:首先,我理解你正在寻找一种可以象征爱情的花。在许多文化中,红玫瑰被视为爱情的象征,
这是因为它们的红色通常与热情和浓烈的感情联系在一起。因此,考虑到这一点,我会推荐红玫瑰。
红玫瑰不仅能够象征爱情,同时也可以传达出强烈的感情,这是你在寻找的。

示例 2:
人类:我想要一些独特和奇特的花。
AI:从你的需求中,我理解你想要的是独一无二和引人注目的花朵。兰花是一种非常独特并且颜色鲜艳的花,
它们在世界上的许多地方都被视为奢侈品和美的象征。因此,我建议你考虑兰花。
选择兰花可以满足你对独特和奇特的要求,而且,兰花的美丽和它们所代表的力量和奢侈也可能会吸引你。

下面是示例代码,实现了运营助手的核心功能:

#创建聊天模型
from langchain_openai import ChatOpenAI
llm=ChatOpenAI(
    openai_api_key='替换为你的API密钥', 
    base_url='https://api.chatanywhere.tech/v1',
    model='gpt-3.5-turbo',
    temperature=0,
)

#设定AI的角色和目标
role_template = "你是一个为花店电商公司工作的AI助手, 你的目标是帮助客户根据他们的喜好做出明智的决定"

#CoT的关键部分,AI解释推理过程,并加入一些先前的对话提示(Few-Shot Learning)
cot_template = """
作为一个为花店电商公司工作的AI助手,我的目标是帮助客户根据他们的喜好做出明智的决定。 
我会按部就班的思考,先理解客户的需求,然后考虑各种鲜花的涵义,最后根据这个需求,给出我的推荐。
同时,我也会向客户解释我这样推荐的原因。
示例 1: 
人类:我想找一种象征爱情的花。 
AI:首先,我理解你正在寻找一种可以象征爱情的花。在许多文化中,红玫瑰被视为爱情的象征,
这是因为它们的红色通常与热情和浓烈的感情联系在一起。因此,考虑到这一点,我会推荐红玫瑰。
红玫瑰不仅能够象征爱情,同时也可以传达出强烈的感情,这是你在寻找的。

示例 2: 
人类:我想要一些独特和奇特的花。 
AI:从你的需求中,我理解你想要的是独一无二和引人注目的花朵。兰花是一种非常独特并且颜色鲜艳的花,
它们在世界上的许多地方都被视为奢侈品和美的象征。因此,我建议你考虑兰花。
选择兰花可以满足你对独特和奇特的要求,而且,兰花的美丽和它们所代表的力量和奢侈也可能会吸引你。
"""

from langchain.prompts import ChatPromptTemplate,HumanMessagePromptTemplate,SystemMessagePromptTemplate
system_prompt_role=SystemMessagePromptTemplate.from_template(role_template)
system_prompt_cot=SystemMessagePromptTemplate.from_template(cot_template)

#用户提问
human_trmplate="{question}"
human_prompt=HumanMessagePromptTemplate.from_template(human_trmplate)

#将以上所有信息结合为一个聊天提示
chat_prompt= ChatPromptTemplate.from_messages([system_prompt_role,system_prompt_cot,human_prompt])

prompt = chat_prompt.format_prompt(question="我想为我的女朋友购买一些花。她喜欢粉色和紫色。你有什么建议吗?")

#接收用户的询问,返回回答结果
response = llm.invoke(prompt)
print(response)

在这里插入图片描述

注意要点
  • 设置 temperature=0 可以让模型生成更确定性的回答,即输出更倾向于最可能的结果。
  • 定义 CoT 模板,其中包括了 AI 的角色和目标描述、思考链条以及遵循思考链条的一些示例,显示了 AI 如何理解问题,并给出建议。
总结

在本次实战中,我们开发了一个 AI 运营助手,它能根据用户的需求进行推理和生成答案。 能够按部就班地思考,理解用户的需求,并给出最合适的花卉推荐。

### LangChain vs Chain of Thought: Differences and Relationship in AI Programming #### Definition and Context LangChain refers to an architectural pattern or framework that facilitates chaining together different components within language models for more complex tasks. This approach allows developers to build pipelines where outputs from one model serve as inputs into another, creating sophisticated workflows tailored towards specific applications such as summarization, translation, question answering systems etc.[^1] On the other hand, Chain of Thought (CoT) represents a cognitive science concept adapted by artificial intelligence researchers which involves breaking down problem-solving processes into smaller reasoning steps. In machine learning contexts, particularly natural language processing, CoT enables models not only to generate answers but also articulate intermediate logical deductions leading up to those conclusions. #### Key Distinctions The primary distinction between these two lies primarily in their scope and application domain: - **Purpose**: While both aim at enhancing computational linguistics capabilities through structured methodologies, LangChains focus on connecting various linguistic tools sequentially; whereas Chains of Thoughts emphasize decomposing abstract problems logically before attempting resolution. - **Implementation Level**: Implementations involving langchains typically occur at higher levels—integrating pre-existing APIs or modules designed specifically around certain languages or functionalities. Conversely, implementing chains-of-thought usually requires lower-level manipulations directly influencing how algorithms process information internally during inference phases. #### Interrelation Despite apparent differences, there exists significant overlap when considering practical implementations: - Combining elements from each can lead to powerful hybrid approaches capable of addressing diverse challenges across domains like conversational agents development or automated document analysis platforms. - For instance, integrating chain-of-thought mechanisms within individual stages of larger langchain architectures could result in enhanced interpretability without sacrificing performance efficiency since explicit rationale provided alongside final responses aids debugging efforts while simultaneously offering valuable insights regarding decision-making pathways taken throughout computations. ```python def combine_langchain_and_cot(): """ Demonstrates integration point between LangChain architecture and Chain of Thought methodology. Returns: str: Final output after applying combined logic. """ # Example stage using external API call via LangChain setup api_result = make_api_call() # Process results following Chain of Thought principles processed_data = apply_reasoning_steps(api_result) return format_output(processed_data) # Hypothetical function calls representing key operations make_api_call() -> dict # External service interaction returning JSON object apply_reasoning_steps(data:dict) -> list[str] # Breaks task into manageable parts based on input structure format_output(steps:list[str]) -> str # Prepares human-readable explanation combining all previous actions ``` --related questions-- 1. How does incorporating Chain of Thought improve transparency in AI-driven solutions? 2. What are some real-world examples demonstrating effective use cases for LangChain structures? 3. Can you provide detailed comparisons focusing solely on technical aspects rather than conceptual ones? 4. Are there any potential drawbacks associated with merging LangChain patterns along with Chain of Thought techniques?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hugo_Hoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值