【langchain】入门初探实战笔记(Chain, Retrieve, Memory, Agent)

news2024/9/20 15:02:00

1. 简介

1.1 大语言模型技术栈

大语言模型技术栈
大语言模型技术栈由四个主要部分组成:

  • 数据预处理流程(data preprocessing pipeline)
  • 嵌入端点(embeddings endpoint )+向量存储(vector store)
  • LLM 终端(LLM endpoints)
  • LLM 编程框架(LLM programming framework)

1.2 Langchain的简介和部署

LangChain 就是一个 LLM 编程框架,你想开发一个基于 LLM 应用,需要什么组件它都有,直接使用就行;甚至针对常规的应用流程,它利用链(LangChain中Chain的由来)这个概念已经内置标准化方案了。

安装langchain包

pip install langchain

调用openai接口

pip install openai

在代码里调用需要设置openai的api key,有两种方法,一种是在bashrc的文件中配置,另一种直接在调用的函数里设置openai的key值配置

openai的key可以通过https://platform.openai.com/api-keys 官网的连接获取

2. 大模型交互

openai接口代码调用示例

# openai_api_key = "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"
# Importing modules
from langchain.llms import OpenAI
# Here we are using text-ada-001 but you can change it
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

output_parser = StrOutputParser()
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are world class technical documentation writer."),
    ("user", "{input}")
])
llm = ChatOpenAI(openai_api_key = "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")
# chain = prompt | llm
# print("prompt:", prompt)
# print("chain:", chain)
chain = prompt | llm | output_parser
res = chain.invoke("how can langsmith help with testing?")
print(res)

输出

content="Langsmith can help with testing in several ways:\n\n1. Test Automation: Langsmith can generate automated test scripts that cover various test scenarios and execute them repeatedly. This helps in reducing manual effort and time required for testing.\n\n2. Test Data Generation: Langsmith can generate test data that covers a wide range of input values and boundary conditions. This helps in testing the robustness and reliability of the system.\n\n3. Test Case Generation: Langsmith can generate test cases based on the specifications or requirements provided. These test cases can cover various combinations of inputs and expected outputs, ensuring comprehensive testing.\n\n4. Regression Testing: Langsmith can automate the execution of regression tests, which are performed to ensure that existing functionality is not impacted by any new changes or updates.\n\n5. Performance Testing: Langsmith can generate test scripts to simulate high loads and stress on the system, enabling performance testing and identifying potential bottlenecks or performance issues.\n\n6. Security Testing: Langsmith can help in identifying security vulnerabilities by generating test cases that simulate different attack scenarios, such as SQL injection or cross-site scripting.\n\nOverall, Langsmith's capabilities in generating automated test scripts, test data, test cases, and supporting various types of testing can significantly improve the efficiency and effectiveness of the testing process.

3. Chain

3.1 LLM Chain

最基本的链为 LLMChain,由 PromptTemplate、LLM 和 OutputParser 组成。LLM 的输出一般为文本,OutputParser 用于让 LLM 结构化输出并进行结果解析,方便后续的调用。

在这里插入图片描述

from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.output_parsers import ResponseSchema, StructuredOutputParser
# from azure_chat_llm import llm
from langchain.chat_models import ChatOpenAI

#output parser
keyword_schema = ResponseSchema(name="keyword", description="评论的关键词列表")
emotion_schema = ResponseSchema(name="emotion", description="评论的情绪,正向为1,中性为0,负向为-1")
response_schemas = [keyword_schema, emotion_schema]
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
format_instructions = output_parser.get_format_instructions()

#prompt template
prompt_template_txt = '''
作为资深客服,请针对 >>> 和 <<< 中间的文本识别其中的关键词,以及包含的情绪是正向、负向还是中性。
>>> {text} <<<
RESPONSE:
{format_instructions}
'''

prompt = PromptTemplate(template=prompt_template_txt, input_variables=["text"],
                        partial_variables={"format_instructions": format_instructions})
llm = ChatOpenAI(openai_api_key = "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")
#llmchain
llm_chain = LLMChain(prompt=prompt, llm=llm)
comment = "京东物流没的说,速度态度都是杠杠滴!这款路由器颜值贼高,怎么说呢,就是泰裤辣!这线条,这质感,这速度,嘎嘎快!以后妈妈再也不用担心家里的网速了!"
result = llm_chain.run(comment)
data = output_parser.parse(result)
print(f"type={type(data)}, keyword={data['keyword']}, emotion={data['emotion']}")

在这里插入图片描述

3.2 Sequential Chain

SequentialChains 是按预定义顺序执行的链。SimpleSequentialChain 为顺序链的最简单形式,其中每个步骤都有一个单一的输入 / 输出,一个步骤的输出是下一个步骤的输入。SequentialChain 为顺序链更通用的形式,允许多个输入 / 输出。

from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.chains import SimpleSequentialChain
from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(openai_api_key = "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")
first_prompt = PromptTemplate.from_template(
    "翻译下面的内容到中文:"
    "\n\n{content}"
)
# chain 1: 输入:Review 输出: 英文的 Review
chain_trans = LLMChain(llm=llm, prompt=first_prompt, output_key="content_zh")

second_prompt = PromptTemplate.from_template(
    "一句话总结下面的内容:"
    "\n\n{content_zh}"
)

chain_summary = LLMChain(llm=llm, prompt=second_prompt)
overall_simple_chain = SimpleSequentialChain(chains=[chain_trans, chain_summary],verbose=True)
content = '''In a blog post authored back in 2011, Marc Andreessen warned that, “Software is eating the world.” Over a decade later, we are witnessing the emergence of a new type of technology that’s consuming the world with even greater voracity: generative artificial intelligence (AI). This innovative AI includes a unique class of large language models (LLM), derived from a decade of groundbreaking research, that are capable of out-performing humans at certain tasks. And you don’t have to have a PhD in machine learning to build with LLMs—developers are already building software with LLMs with basic HTTP requests and natural language prompts.
In this article, we’ll tell the story of GitHub’s work with LLMs to help other developers learn how to best make use of this technology. This post consists of two main sections: the first will describe at a high level how LLMs function and how to build LLM-based applications. The second will dig into an important example of an LLM-based application: GitHub Copilot code completions.
Others have done an impressive job of cataloging our work from the outside. Now, we’re excited to share some of the thought processes that have led to the ongoing success of GitHub Copilot.
'''
result = overall_simple_chain.run(content)
print(f'result={result}')

在这里插入图片描述

3.3 RouterChain

在这里插入图片描述

4. 外部文档检索

索引和外部数据进行集成,用于从外部数据获取答案。

  • 通过 Document Loaders 加载各种不同类型的数据源,

LangChain 通过 Loader 加载外部的文档,转化为标准的 Document 类型。Document 类型主要包含两个属性:page_content 包含该文档的内容。meta_data 为文档相关的描述性数据,类似文档所在的路径等。

  • 通过 Text Splitters 进行文本语义分割

LLM 一般都会限制上下文窗口的大小,有 4k、16k、32k 等。针对大文本就需要进行文本分割,常用的文本分割器为 RecursiveCharacterTextSplitter,可以通过 separators 指定分隔符。其先通过第一个分隔符进行分割,不满足大小的情况下迭代分割。

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 通过 Vectorstore 进行非结构化数据的向量存储

通过 Text Embedding models,将文本转为向量,可以进行语义搜索,在向量空间中找到最相似的文本片段。目前支持常用的向量存储有 Faiss、Chroma 等。

  • 通过 Retriever 进行文档数据检索

Retriever 接口用于根据非结构化的查询获取文档,一般情况下是文档存储在向量数据库中。可以调用 get_relevant_documents 方法来检索与查询相关的文档。

在这里插入图片描述

from langchain import FAISS
from langchain.document_loaders import WebBaseLoader
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter

#通过 Document Loaders 加载各种不同类型的数据源
#LangChain 通过 Loader 加载外部的文档,转化为标准的 Document 类型。
# Document 类型主要包含两个属性:page_content 包含该文档的内容。meta_data 为文档相关的描述性数据,类似文档所在的路径等。
loader = WebBaseLoader("https://in.m.jd.com/help/app/register_info.html")
data = loader.load()

#针对大文本就需要进行文本分割,常用的文本分割器为 RecursiveCharacterTextSplitter,可以通过 separators 指定分隔符。
# 其先通过第一个分隔符进行分割,不满足大小的情况下迭代分割。
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
    model_name="gpt-3.5-turbo",
    allowed_special="all",
    separators=["\n\n", "\n", "。", ","],
    chunk_size=800,
    chunk_overlap=0
)
docs = text_splitter.split_documents(data)
#通过cache_folder设置自己的本地模型路径
#embeddings = HuggingFaceEmbeddings(model_name="text2vec-base-chinese", cache_folder="models")

#通过 Text Embedding models,将文本转为向量,可以进行语义搜索,在向量空间中找到最相似的文本片段。
# 目前支持常用的向量存储有 Faiss、Chroma 等。
embeddings = HuggingFaceEmbeddings()

vectorstore = FAISS.from_documents(docs, embeddings)

#Retriever 接口用于根据非结构化的查询获取文档,一般情况下是文档存储在向量数据库中。
# 可以调用 get_relevant_documents 方法来检索与查询相关的文档。
result = vectorstore.as_retriever().get_relevant_documents("用户注册资格")
print(result)
print(len(result))

在这里插入图片描述

5. 外部文档交互

5.1 Stuff

StuffDocumentsChain 这种链最简单直接,是将所有获取到的文档作为 context 放入到 Prompt 中,传递到 LLM 获取答案

这种方式可以完整的保留上下文,调用 LLM 的次数也比较少,建议能使用 stuff 的就使用这种方式。其适合文档拆分的比较小,一次获取文档比较少的场景,不然容易超过 token 的限制。

在这里插入图片描述

5.2 Refine

RefineDocumentsChain 是通过迭代更新的方式获取答案。先处理第一个文档,作为 context 传递给 llm,获取中间结果 intermediate answer。然后将第一个文档的中间结果以及第二个文档发给 llm 进行处理,后续的文档类似处理。

Refine 这种方式能部分保留上下文,以及 token 的使用能控制在一定范围。

在这里插入图片描述

5.3 MapReduce

MapReduceDocumentsChain 先通过 LLM 对每个 document 进行处理,然后将所有文档的答案在通过 LLM 进行合并处理,得到最终的结果。

MapReduce 的方式将每个 document 单独处理,可以并发进行调用。但是每个文档之间缺少上下文。

在这里插入图片描述

5.4 MapRerank

MapRerankDocumentsChain 和 MapReduceDocumentsChain 类似,先通过 LLM 对每个 document 进行处理,每个答案都会返回一个 score,最后选择 score 最高的答案。

MapRerank 和 MapReduce 类似,会大批量的调用 LLM,每个 document 之间是独立处理

在这里插入图片描述

6. Memory

正常情况下 Chain 无状态的,每次交互都是独立的,无法知道之前历史交互的信息。LangChain 使用 Memory 组件保存和管理历史消息,这样可以跨多轮进行对话,在当前会话中保留历史会话的上下文。Memory 组件支持多种存储介质,可以与 Monogo、Redis、SQLite 等进行集成,以及简单直接形式就是 Buffer Memory。常用的 Buffer Memory 有:

  • ConversationSummaryMemory :以摘要的信息保存记录
  • ConversationBufferWindowMemory:以原始形式保存最新的 n 条记录
  • ConversationBufferMemory:以原始形式保存所有记录
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory

from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(openai_api_key = "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")

memory = ConversationBufferMemory()
conversation = ConversationChain(llm=llm, memory=memory, verbose=True)
print(conversation.prompt)
print(conversation.predict(input="我的姓名是tiger"))
print(conversation.predict(input="1+1=?"))
print(conversation.predict(input="我的姓名是什么"))

请添加图片描述

7. Agents

传统使用 LLM,需要给定 Prompt 一步一步的达成目标,通过 Agent 是给定目标,其会自动规划并达到目标。

目前这个领域特别活跃,诞生了类似 AutoGPT、BabyAGI、AgentGPT 等一堆优秀的项目。

目前的大模型一般都存在知识过时、逻辑计算能力低等问题,通过 Agent 访问工具,可以去解决这些问题。

  • Agent:代理,负责调用 LLM 以及决定下一步的 Action。其中 LLM 的 prompt 必须包含 agent_scratchpad 变量,记录执行的中间过程
  • Tools:工具,Agent 可以调用的方法。LangChain 已有很多内置的工具,也可以自定义工具。注意 Tools 的 description 属性,LLM 会通过描述决定是否使用该工具。
  • ToolKits:工具集,为特定目的的工具集合。类似 Office365、Gmail 工具集等
  • Agent Executor:Agent 执行器,负责进行实际的执行。
from langchain.agents import load_tools, initialize_agent, tool
from langchain.agents.agent_types import AgentType
from datetime import date
from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(openai_api_key = "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")

#有多种方式可以自定义 Tool,最简单的方式是通过 @tool 装饰器,将一个函数转为 Tool。
# 注意函数必须得有 docString,其为 Tool 的描述。
@tool
def time(text: str) -> str:
    """
    返回今天的日期。
    """
    return str(date.today())

tools = load_tools(['llm-math'], llm=llm)
tools.append(time)
#一般通过 initialize_agent 函数进行 Agent 的初始化,除了 llm、tools 等参数,还需要指定 AgentType。
#该 Agent 为一个 zero-shot-react-description 类型的 Agent,其中 zero-shot 表明只考虑当前的操作,不会记录以及参考之前的操作。react 表明通过 ReAct 框架进行推理,description 表明通过工具的 description 进行是否使用的决策。
agent_math = initialize_agent(agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
                                   tools=tools,
                                   llm=llm,
                                   verbose=True)
print(agent_math("计算45 * 54"))
print(agent_math("今天是哪天?"))
#可以通过 agent.agent.llm_chain.prompt.template 方法,获取其推理决策所使用的模板。
print(agent_math.agent.llm_chain.prompt.template)

请添加图片描述

参考文献

知乎langchain到底该怎么使用,大家在项目中实践有成功的案例吗?https://www.zhihu.com/question/609483833/answer/3146379316?utm_psn=1725522909580394496

知乎小白入门大模型:LangChain https://zhuanlan.zhihu.com/p/656646499

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1354601.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

mac环境下安装部署mysql5.7

下载安装包 进入官网下载MySQL5.7的安装包 https://www.mysql.com/downloads/ 安装包下载完成后双击pkg文件进行安装&#xff0c;无脑点下一步即可&#xff0c;注意安装完成后记得保存最后弹出框的密码 进入系统偏好设置&#xff0c;找到mysql&#xff0c;开启mysql服务…

人工智能教程(四):概率论入门

目录 前言 TensorFlow 入门 SymPy 入门 概率论入门 前言 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。 点击跳转到网站 在本系列的 上一篇文章 中&#xff0c;我们进一步讨论了矩阵和线性代数&#…

macbook录屏快捷键大全,教你快速录制视频

“有人知道macbook电脑有录屏快捷键吗&#xff0c;现在录屏的速度太慢了&#xff0c;每次打开都要浪费不少时间&#xff0c;要是有录屏快捷键&#xff0c;应该会快很多&#xff0c;有哪位大佬知道吗&#xff1f;教教我&#xff01;” 无论是在工作还是生活中&#xff0c;电脑已…

安卓Android Studio读写FM1208CPU卡源码

本示例使用的发卡器&#xff1a;https://item.taobao.com/item.htm?spma1z10.5-c-s.w4002-21818769070.11.6c46789elLwMzv&id615391857885 <?xml version"1.0" encoding"utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout x…

redis复习笔记02(小滴课堂)

分布式缓存Redis6常见核心配置讲解 查看配置文件&#xff1a; 创建配置文件&#xff1a; 配置完我们去验证一下&#xff1a; 启动成功就没有问题了。 可以看到redis日志。 然后我们就可以连接我们的redis了&#xff1a; 设置了密码就需要密码登录了。 如果登录了错误的密码也无…

C/C++汇编学习(二)——学习使用IDA pro

学习使用IDA Pro是一项很有价值的技能&#xff0c;特别是对于那些对逆向工程和软件安全分析感兴趣的人。以下是一些基本步骤和概念&#xff0c;帮助你熟悉IDA Pro的界面和操作。 1. 熟悉IDA Pro界面和基本操作 主界面布局 IDA Pro的主界面包含多个组件&#xff0c;每个组件都…

静态网页设计——个人简介网站

前言 使用经典前端三件套HTMLCSSJavascript编写了一个关于个人简介的静态网页&#xff0c;可以根据自己的需要&#xff0c;十分简单的进行修改。 首页 首页由上方的菜单栏以及菜单栏下面的轮播图组成&#xff0c;再往下走&#xff0c;是关于自己的兴趣爱好的部分&#xff0c…

UG/NX许可证使用效率提升新技术

UG/NX许可证使用效率提升新技术 UG&#xff08;Unigraphics NX&#xff09;是Siemens PLM Software公司出品的一个产品工程解决方案&#xff0c;它为用户的产品设计及加工过程提供了数字化造型和验证手段。近年来随着国家对知识产品保护的不断加强&#xff0c;以前使用盗版软件…

vue3+Ts+Hook的方式实现商城核心功能sku选择器

前言 Hooks是React等函数式编程框架中非常受欢迎的工具&#xff0c;随着VUE3 Composition API 函数式编程风格的推出&#xff0c;现在也受到越来越多VUE3开发者的青睐&#xff0c;它让开发者的代码具有更高的复用度且更加清晰、易于维护。 本文将通过CRMEB商城商品详情sku选择…

Bee的批量插入与事务使用

* Bee 在2.2之前,调用批量插入在每个批都会提交commit,但在2.2改为只调用一次且在事务中,在批量插入的方法内容不再提交,而由事务控制. * * 2.2之前,批量插入使用每一个批次提交一次事务; * 这样,当违反主键约束等就忽略的大批量插入效率是很高的; * 但当事务中有批量插…

Golang Leetcode19 删除链表的倒数第N个节点 递归 双指针法+迭代

删除链表的倒数第N个节点 leetcode19 递归 由于本体是倒数第几个节点&#xff0c;非常适合递归 从终到始 的运行方式 func removeNthFromEnd(head *ListNode, n int) *ListNode {// 创建一个虚拟头节点&#xff0c;简化边界条件处理dummy : &ListNode{Next: head}//检查…

【卫星科普】什么是农业一号卫星和农业二号卫星?

农业一号卫星和农业二号卫星是中国自主研发的两颗重要卫星&#xff0c;主要用于农业领域的监测和研究。 农业一号卫星是中国第一颗具备红边波段传感器的卫星&#xff0c;也是世界上第一颗具备红边波段的宽视场多光谱中高分辨率卫星。这对农业农村遥感监测非常重要&#xff0c;…

阿里云ECS服务器无法访问端口(防火墙在关闭状态也启作用)

问题&#xff1a;一直用得好好的端口&#xff0c;突然在某一时间不可以访问这个端口了 &#xff0c;在服务器录入外网地址访问如下图&#xff1a; 先按正常流程检测&#xff1a; 1 先云服务商的管理网站查看防火墙端口是否开放 看了正常开放了端口&#xff0c;如下图&#xff…

HTML5-简单文件操作

文件操作 简介 概念&#xff1a;可以通过file类型的input控件或者拖放的方式选择文件进行操作 语法格式&#xff1a; <input type"file" multiple>属性 multiple&#xff1a;表示是否选择多个文件 accept&#xff1a;用于设置文件的过滤类型&#xff08;MI…

Linux 如何 kill 指定的 python 进程

文章目录 写在前面一、显示python相关的进程二、找到自己想要 kill 的进程&#xff0c;执行下述指令 写在前面 自己的系统是 Ubuntu 20.04 一、显示python相关的进程 ps -ef | grep python显示结果如下 其中&#xff0c;第二列分别是各个进程的 PID 号。 二、找到自己想要…

【38 Pandas+Pyecharts | 奥迪汽车销量数据分析可视化】

文章目录 &#x1f3f3;️‍&#x1f308; 1. 导入模块&#x1f3f3;️‍&#x1f308; 2. Pandas数据处理2.1 读取数据2.2 查看数据信息2.3 数据处理 &#x1f3f3;️‍&#x1f308; 3. Pyecharts数据可视化3.1 奥迪用户购车时间分布3.2 奥迪各系销量占比饼图3.3 奥迪各系销量…

使用Python和Pygame库创建简单的的彩球效果

简介 Pygame是一款强大的游戏开发库&#xff0c;可以用于创建各种有趣的图形效果。为了更好地了解Pygame的功能&#xff0c;今天我们将要做的是在屏幕上随机生成一些彩色的小球&#xff0c;并使它们以不同的速度和方向移动。当小球碰到屏幕边缘时&#xff0c;它们将反弹。 功能…

小型洗衣机哪个牌子质量好?五款最好用的迷你洗衣机品牌

不得不说洗衣机的发明解放了我们的双手&#xff0c;而我们从小到大就有这个意识&#xff0c;贴身衣物不可以和普通的衣服一起丢进去洗衣机一起&#xff0c;而内衣裤上不仅有肉眼看见的污渍还有手上根本无法消灭的细菌&#xff0c;但是有一款专门可以将衣物上的细菌杀除的内衣小…

仓库出入库登记系统的推荐

在信息时代&#xff0c;仓库管理已成为企业不可缺少的一项工作。我们如何高效、准确地管理仓库的进货、出货以及库存&#xff0c;是每个企业或仓管都需要面对的问题。而一个优秀的仓库出入库登记系统&#xff0c;则能够大大提升仓库管理的效率和准确性。本文将为您推荐一款实用…

NFC物联网开发在智慧校园中的应用

近年来&#xff0c;校园信息化建设速度加快&#xff0c;以物联网为基础、以各种应用服务系统为载体的智慧校园将教学、管理和校园生活充分融合&#xff0c;形成了工作、学习和生活的一体化环境。沉寂已久的NEC 技术&#xff0c;得益于智能手机的普及、无线网络数据速率提高&…