LLM之LangChain(五)| 使用LangChain Agent分析非结构化数据

       想象一下,你有一家面包店,你派出了甜食商情报团队来收集竞争对手的数据。他们会汇报竞争情况,他们有很多很棒的想法,你想把它们应用到你的业务中。然而,数据是非结构化的!您如何分析这些数据,以了解最需要什么,并为您业务的下一步计划做出最佳的策略?在第1部分中,我们使用“PydanticOutputParser”来分析我们的数据并添加所需的结构。在第2部分中,我们将创建一个LangChain Agent来进行数据分析。

       为了探索这个用例,创建了一个玩具数据集[1]。以下是数据集中的一个示例样本:

At Velvet Frosting Cupcakes, our team learned about the unveiling of a seasonal pastry menu that changes monthly. Introducing a rotating seasonal menu at our bakery using the “SeasonalJoy” subscription platform and adding a special touch to our cookies with the “FloralStamp” cookie stamper could keep our offerings fresh and exciting for customers.

第一部分:从非结构化数据抽取结构化信息

方法一:create_extract_chain

      定义数据抽取的结构,并且使用LangChain创建一个提取链。

from langchain.chains import create_extraction_chainfrom langchain.chat_models import ChatOpenAI# Schemaschema = {
      "properties": {
          "company": {"type": "string"},        "offering": {"type": "string"},        "advantage": {"type": "string"},        "products_and_services": {"type": "string"},        "additional_details": {"type": "string"},    }}

        定义测试样本

# Inputsin1 = """Sweet Delights Bakery introduced lavender-infused vanilla cupcakes with a honey buttercream frosting, using the "Frosting-Spreader-3000". This innovation could inspire our next cupcake creation"""in2 = """Whisked Away Cupcakes introduced a dessert subscription service, ensuring regular customers receive fresh batches of various sweets. Exploring a similar subscription model using the "SweetSubs" program could boost customer loyalty."""in3 = """At Velvet Frosting Cupcakes, our team learned about the unveiling of a seasonal pastry menu that changes monthly. Introducing a rotating seasonal menu at our bakery using the "SeasonalJoy" subscription platform and adding a special touch to our cookies with the "FloralStamp" cookie stamper could keep our offerings fresh and exciting for customers."""inputs = [in1, in2
### 关于LangChain Agent的概述 LangChain Agent是一种基于大型语言模型(LLM)构建的应用程序组件,旨在执行特定任务或一系列操作。通过集成不同的工具和技术,Agent能够处理复杂的工作流程并自动化多种业务逻辑[^1]。 ### 初始化与配置Agent实例 对于最新版本中的初始化过程而言,推荐采用`AgentExecutor`类来创建和管理Agent对象。这不仅简化了设置步骤还提供了更灵活的功能选项: ```python from langchain.agents import initialize_agent, Tool from langchain.llms import OpenAI llm = OpenAI(temperature=0) # 定义一些简单的工具函数作为示例 def search(query): return "search results" tools = [ Tool( name="Search", func=search, description="useful for when you need to answer questions about the world" ) ] agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) ``` 此段代码展示了如何利用预定义好的工具列表以及选择合适的参数完成Agent Executor 的实例化工作[^2]。 ### 设定Prompt模板 为了使Agent更好地理解用户的意图并给出恰当的回答,在设计对话系统时通常会涉及到定制化的提示语(Prompt)编写。这里提供了一个来自开源项目【hwchase17/structured-chat-agent】的具体实现方案供参考[^3]: ```json { "$schema": "./prompt-schema.json", "type": "object", "properties": { "input": {"type": "string"}, "history": { "type": "array", "items": { "type": "object", "properties": { "role": {"enum": ["human", "ai"]}, "content": {"type": "string"} }, "required": ["role", "content"] } } }, "required": ["input"] } ``` 上述JSON结构描述了一种适用于聊天场景下的输入输出格式,其中包含了当前轮次的人机交互文本(`input`)以及过往交流记录(`history`)等字段信息。 ### 实际应用场景举例说明 假设现在要开发一款智能客服机器人,那么就可以借助LangChain框架所提供的API接口和服务快速搭建起原型产品。比如针对客户提出的商品查询请求,可以通过调用搜索引擎API获取相关信息;而对于订单状态跟踪这类问题,则可以直接访问内部数据库获得最准确的结果反馈给用户。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wshzd

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

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

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

打赏作者

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

抵扣说明:

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

余额充值