11. LCEL:LangChain Expression Language

这篇文章覆盖了LCEL的理解和他是如何工作的。

LCEL(LangChain Expression Language):是把一些有趣python概念抽象成一种格式,从而为构建LangChain组件链提供一种“简约”代码层。

LCEL在下面方面有着强大的支撑:

  1. 链的快速开发
  2. 流式输出、异步,以及并发执行等高阶特性
  3. 和LangSmith和LangServe的快速集成

在这篇文章中,我们将学习LCEL是什么、他是如何工作的,以及LCEL链、pipe和RUnnable的要点。

LCEL语法

我们首先安装本次演示所需的所有必备库。注意,也可以通过我们的Jupyter笔记来操作。

!pip install -qU \langchain==0.0.345 \anthropic==0.7.7 \cohere==4.37 \docarray==0.39.1

为了理解LCEL预发,我们首先构建一个简单的链使用LangChain的传统语法。我们将使用Claude 2.1初始化一个简单的LLMChain。

from langchain.chat_models import ChatAnthropic
from langchain.prompts import ChatPromptTemplate
from langchain.schema.output_parser import StrOutputParserANTHROPIC_API_KEY = "<<YOUR_ANTHROPIC_API_KEY>>"prompt = ChatPromptTemplate.from_template("Give me small report about {topic}"
)
model = ChatAnthropic(model="claude-2.1",max_tokens_to_sample=512,anthropic_api_key=ANTHROPIC_API_KEY
)  # swap Anthropic for OpenAI with `ChatOpenAI` and `openai_api_key`
output_parser = StrOutputParser()

通过使用这个链,我们可以通过在LLMChain上面调用chain.run方法来生成关于某个特定主题(如,“人工智能”)的小型报告。

from langchain.chains import LLMChainchain = LLMChain(prompt=prompt,llm=model,output_parser=output_parser
)# and run
out = chain.run(topic="Artificial Intelligence")
print(out)

Here is a brief report on some key aspects of artificial intelligence (AI):Introduction
- AI refers to computer systems that are designed to perform tasks that would otherwise require human intelligence, such as visual perception, speech recognition, decision-making, and language translation. Major AI Techniques
- Machine learning uses statistical techniques and neural networks to enable systems to improve at tasks with experience. Common techniques include deep learning, reinforcement learning, and supervised learning.
- Computer vision focuses on extracting information from digital images and videos. It powers facial recognition, self-driving vehicles, and other visual AI tasks.
- Natural language processing enables computers to understand, interpret, and generate human languages. Key applications include machine translation, search engines, and voice assistants like Siri.Current Capabilities
- AI programs have matched or exceeded human capabilities in narrow or well-defined tasks like playing chess and Go, identifying objects in images, and transcribing speech. 
- However, general intelligence comparable to humans across different areas remains an unsolved challenge, often referred to as artificial general intelligence (AGI).Future Directions
- Ongoing AI research is focused on developing stronger machine learning techniques, achievingexplainability and transparency in AI decision-making, and addressing potential ethical issues like bias.
- If achieved, AGI could have significant societal and economic impacts, potentially enhancing and automating intellectual work. However safety, control and alignment with human values remain active research priorities.I hope this brief overview of some major aspects of the current state of AI technology and research provides useful context and information. Let me know if you would like me to elaborate on or clarify anything further.

通过LCEL,我们使用管道操作符(|)而不是Chains来以不同的方式创建链:

lcel_chain = prompt | model | output_parser# and run
out = lcel_chain.invoke({"topic": "Artificial Intelligence"})
print(out)
Here is a brief report on artificial intelligence:Artificial intelligence (AI) refers to computer systems that can perform human-like cognitive functions such as learning, reasoning, and self-correction. AI has advanced significantly in recent years due to increases in computing power and the availability of large datasets and open source machine learning libraries.Some key highlights about the current state of AI:- Applications of AI - AI is being utilized in a wide variety of industries including finance, healthcare, transportation, criminal justice, and social media platforms. Use cases include personalized recommendations, predictive analytics, automated customer service agents, medical diagnosis, self-driving vehicles, and content moderation.- Machine Learning - The ability for AI systems to learn from data without explicit programming is driving much of the recent progress. Machine learning methods like deep learning neural networks have achieved new breakthroughs in areas like computer vision, speech recognition, and natural language processing. - Limitations - While AI has made great strides, current systems still have major limitations compared to human intelligence including lack of general world knowledge, difficulties dealing with novelty, bias issues from flawed datasets, and lack of skills for complex reasoning, empathy, creativity, etc. Ensuring the safety and controllability of AI systems remains an ongoing challenge.  - Future Outlook - Experts predict key areas for AI advancement to include gaining contextual understanding and reasoning skills, achieving more human-like communication abilities, algorithmic fairness and transparency, as well as advances in specialized fields like robotics, autonomous vehicles, and human-AI collaboration. Careful management of risks posed by more advanced AI systems remains crucial. Global competition for AI talent and computing resources continues to intensify.That covers some of the key trends, strengths and limitations, and future trajectories for artificial intelligence technology based on the current landscape. Please let me know if you would like me to elaborate on any part of this overview.

这里的语法对于Python来说并不典型,但只使用了原生Python的功能。我们的管道操作符(|)只是简单的获取左侧的输出,并将其输入到右侧的函数中。

管道操作符如何工作

为了理解LCEL和管道操作符正在发生什么情况,我们创建自己的兼容管道的函数。

当Python解释器看到两个对象之间的“|”操作符(如a|b)是,它将尝试将对象a传入对象b的__or__方法。这意味着这些模式是等效的:

# object approach
chain = a.__or__(b)
chain("some input")# pipe approach
chain = a | b
chain("some input")

考虑到这一点,我们可以构建一个可运行(Runnable)类,这个类接收一个函数,并将它转换为一个可使用管道操作符(|)与其他函数进行链式调用的函数。

class Runnable:def __init__(self, func):self.func = funcdef __or__(self, other):def chained_func(*args, **kwargs):# the other func consumes the result of this funcreturn other(self.func(*args, **kwargs))return Runnable(chained_func)def __call__(self, *args, **kwargs):return self.func(*args, **kwargs)

让我们来实现这个操作,取值为3,加5(返回8),然后乘以2--得到16.

def add_five(x):return x + 5def multiply_by_two(x):return x * 2# wrap the functions with Runnable
add_five = Runnable(add_five)
multiply_by_two = Runnable(multiply_by_two)# run them using the object approach
chain = add_five.__or__(multiply_by_two)
chain(3)  # should return 16
16

直接使用__or__我们得到了正确的答案,让我们来尝试使用管道操作来将这些链接起来:

chain = add_five | multiply_by_two
chain(3)
16

通过任意一种方法,我们都会得到正确的响应,从本质上讲,这就是LCEL将组件链接在一起使用的管道逻辑。然而,这并非LCEL的全部,他还有更多内涵。

深入LCEL

现在我们已经了解了LCEL语法的底层逻辑,让我们在LangChain的语境中对其进行深入探究并了解一些为在使用LCEL时实现最大化的灵活性而提供的附加方法。

Runnables

在使用LCEL,我们可能会发现,当值在组件之间传递时,我们需要修改值的流向或者值本身--为此,我们使用可执行对象(runnable)。让我们首先初始化几个简单的向量存储组件。

from langchain.embeddings import CohereEmbeddings
from langchain.vectorstores import DocArrayInMemorySearchCOHERE_API_KEY = "<<COHERE_API_KEY>>"embedding = CohereEmbeddings(model="embed-english-light-v3.0",cohere_api_key=COHERE_API_KEY
)vecstore_a = DocArrayInMemorySearch.from_texts(["half the info will be here", "James' birthday is the 7th December"],embedding=embedding
)
vecstore_b = DocArrayInMemorySearch.from_texts(["and half here", "James was born in 1994"],embedding=embedding
)

我们初始化两个本地的向量存储,并将两个重要信息片段分开存储在这两个向量存储中。我们后面很快会明白为什么要这么做,现在我们仅仅需要其中之一。让我们尝试将一个问题通过vecstore_a传递给RAG流水线.

from langchain_core.runnables import (RunnableParallel,RunnablePassthrough
)retriever_a = vecstore_a.as_retriever()
retriever_b = vecstore_b.as_retriever()prompt_str = """Answer the question below using the context:Context: {context}Question: {question}Answer: """
prompt = ChatPromptTemplate.from_template(prompt_str)retrieval = RunnableParallel({"context": retriever_a, "question": RunnablePassthrough()}
)chain = retrieval | prompt | model | output_parser

我们在这里使用两个新的对象,RunnableParallelRunnablePasstrhroughRunnableParalle对象允许我们定义多个值和操作,并将他们全部并行运行。在这里,我们使用chain的输入调用retriever_a,然后通过“context”参数将retriever_a的结果传递给链中的下一个组件。

LCEL流使用RunnableParallel和RunnablePassthrough

RunnablePassthrough对象被用作一种“传递(直通)”机制,他接受当前组件(检索组件)的任何输入,并允许我们通过“question”键将他提供给组件。

out = chain.invoke("when was James born?")
print(out)
 Unfortunately I do not have enough context to definitively state when James was born. The only potentially relevant information is "James' birthday is the 7th December", but this does not specify the year he was born. To answer the question of when specifically James was born, I would need more details or context such as his current age or the year associated with his birthday.

使用这些信息,推理已经接近回答了问题,但是他没有足够的信息,它缺失我们存储在retriever_b的信息。幸运的是,我们可以使用RunnableParllel对象来实现多个并行信息流。

prompt_str = """Answer the question below using the context:Context:
{context_a}
{context_b}Question: {question}Answer: """
prompt = ChatPromptTemplate.from_template(prompt_str)retrieval = RunnableParallel({"context_a": retriever_a, "context_b": retriever_b,"question": RunnablePassthrough()}
)chain = retrieval | prompt | model | output_parser

在这里,我们通过context_acontext_b传递两组上下文到我们的prompt组件。通过这种方式,我们可以向LLM提供更多信息(尽管奇怪的是,该打语言模型并不能将这些信息联系起来进行综合分析)。

out = chain.invoke("when was James born?")
print(out)
Based on the context provided, James was born in 1994. This is stated in the second document with the page content "James was born in 1994". Therefore, the answer to the question "when was James born?" is 1994.

out = chain.invoke("what date exactly was James born?")
print(out)
Unfortunately, the given context does not provide definitive information to answer the question "what date exactly was James born?". The context includes:- James' birthday is the 7th December (no year specified)
- James was born in 1994While it states James was born in 1994, there is no additional detail provided about the exact date. The context only specifies that his birthday, referring to the day and month he was born, is December 7th. But without the specific year provided with his birthday, there is not enough information to determine the exact date he was born.Since an exact date of James' birth is not able to be determined from the given context, I do not have enough information to provide an answer specifying his exact birth date. The context provides his year of birth but does not include the required detail about the day and month in order for me to state his complete exact date of birth.

使用这种方式,我们可以能够进行多个并行执行,并且相当容易得构建更复杂的链。

Runnable Lambdas

RunnableLambda是LangChain关于将Python函数转换为管道适配的函数的一种抽象,与我们在文章前面Runnable类相似。

from langchain_core.runnables import RunnableLambdadef add_five(x):return x + 5def multiply_by_two(x):return x * 2# wrap the functions with RunnableLambda
add_five = RunnableLambda(add_five)
multiply_by_two = RunnableLambda(multiply_by_two)

使用我们早前的add_fivemultiply_by_two函数来试一下。

chain = add_five | multiply_by_two

Runnable抽象不一样的是我们不能直接调用RunnableLambda来运行它,取而代之,我们需要调用chain.invoke:

chain.invoke(3)
16

跟之前一样,我们得到了相同的答案。自然地,我们可以使用这种方法把自定义函数插入到我们的链中。让我们尝试一个短一点的链,并看看我们可能想在哪里插入自定义函数:

prompt_str = "Tell me an short fact about {topic}"
prompt = ChatPromptTemplate.from_template(prompt_str)chain = prompt | model | output_parser

我们多次运行这个链,看看他会返回什么类型的答案:

chain.invoke({"topic": "Artificial Intelligence"})
" Here's a short fact about artificial intelligence:\n\nAI systems can analyze huge amounts of data and detect patterns that humans may miss. For example, AI is helping doctors diagnose diseases earlier by processing medical images and spotting subtle signs that a human might not notice."
chain.invoke({"topic": "Artificial Intelligence"})
" Here's a short fact about artificial intelligence:\n\nAI systems are able to teach themselves over time. Through machine learning, algorithms can analyze large amounts of data and improve their own processes and decision making without needing to be manually updated by humans. This self-learning ability is a key attribute enabling AI progress."

返回的文本经常包含一个“Here's a short fact about ...\n\n”作为开头--让我们添加一个函数,按照两个连续的“\n\n”进行分割,并且仅返回事实本身。

def extract_fact(x):if "\n\n" in x:return "\n".join(x.split("\n\n")[1:])else:return xget_fact = RunnableLambda(extract_fact)chain = prompt | model | output_parser | get_fact

现在再次尝试调用我们的链:

chain.invoke({"topic": "Artificial Intelligence"})
'Most AI systems today are narrow AI, meaning they are focused on and trained for a specific task like computer vision, natural language processing or playing chess. General artificial intelligence that has human-level broad capabilities across many domains does not yet exist. AI has made tremendous progress in recent years thanks to advances in deep learning, big data and computing power, but still has limitations and scientists are working to make AI systems safer and more beneficial to humanity.'
chain.invoke({"topic": "Artificial Intelligence"})
'AI systems can analyze massive amounts of data and detect patterns that humans may miss. This ability to find insights in large datasets is one of the key strengths of AI and enables many practical applications like personalized recommendations, fraud detection, and medical diagnosis.'

使用这个get_fact函数,我们并没有得到格式化良好的响应。


这些涵盖了你开始使用LCEL进行构建所需的基本内容。有了它,我们可以轻松的组合链--LangChain团队目前的重点是进一步进行LCEL的开发和支持。

LCEL的优缺点各不相同。喜欢他的人往往关注其极简的代码风格、对流、并行操作和异步的支持,以及它与LangChain专注于将组件链接在一起的理念的良好整合。

有些人不喜欢LCEL。这些人通常支出,LangChain本来已经是一个非常抽象的库,LCEL是在它之上的又一层抽象,语法令人困惑,与Python的宗旨相悖,并且需要花费太多的精力去学习新的(不常见)的语法。

两种观点都是完全合理的,LCEL是一种非常不同的方法--既有明显的优点也有明显的缺点。无论如何,如果你愿意花一些时间学习这种语法,他可以让我们快速的进行开发,考虑到这一点,它是很值得学习的。

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

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

相关文章

线性方程组的迭代方法

目录 直接方法与迭代方法 常规迭代算法 选择迭代求解器 预条件子 预条件子示例 均衡和重新排序 使用线性运算函数取代矩阵 数值线性代数最重要也是最常见的应用之一是可求解以 A*x b 形式表示的线性方程组。当 A 为大型稀疏矩阵时&#xff0c;您可以使用迭代方法求解线…

【路径规划】基于球向量的粒子群优化(SPSO)算法在无人机路径规划中的实现

摘要 本文介绍了基于球形矢量的粒子群优化&#xff08;Spherical Particle Swarm Optimization, SPSO&#xff09;算法&#xff0c;用于无人机&#xff08;UAV&#xff09;路径规划。SPSO算法通过引入球形矢量的概念&#xff0c;增强了粒子群在多维空间中的探索和利用能力&…

excel统计分析(1):列联表分析与卡方检验

列联表&#xff1a;用于展示两个或多个分类变量之间频数关系的表格。——常用于描述性分析卡方检验&#xff1a;通过实际频数和期望频数&#xff08;零假设为真情况下的频数&#xff09;&#xff0c;反映了观察频数与期望频数之间的差异程度&#xff0c;来评估两个变量是否独立…

Android实现图片滚动和页签控件功能的实现代码

首先题外话&#xff0c;今天早上起床的时候&#xff0c;手滑一下把我的手机甩了出去&#xff0c;结果陪伴我两年半的摩托罗拉里程碑一代就这么安息了&#xff0c;于是我今天决定怒更一记&#xff0c;纪念我死去的爱机。 如果你是网购达人&#xff0c;你的手机上一定少不了淘宝…

protobuff中的required有什么用?

大家在proto2 应该经常看到如下msg表达: message MsgType3 { required int32 value1 1; required int32 value2 2; } 在protobuff中的required 有什么作用&#xff1f;在 Protocol Buffers&#xff08;protobuf&#xff09;中&#xff0c;required 关键字用于指定某个字段是…

经济不好,但是遍地都是赚钱的机会

现在职场越来越内卷&#xff0c;裁员风波也是不断&#xff0c;前些天看到一个帖子&#xff0c;裁员都裁到应届生头上了。 都说00后整治职场&#xff0c;在如今环境下也要掂量一下了。 大家都在抱怨环境&#xff0c;可是你有没有想过&#xff0c;有些人在闷声发着小财。 下面…

信息安全工程师(24)网络安全体系建设原则与安全策略

一、网络安全体系建设原则 网络空间主权原则&#xff1a;维护网络空间主权是网络安全的首要原则。这要求国家在网络空间的管理、运营、建设和使用等方面具有完全自主的权利和地位&#xff0c;不受任何外部势力的干涉和侵犯。网络安全与信息化发展并重原则&#xff1a;网络安全与…

异常断链吐血经历!拯救Air780EP模块紧急项目

我最近被老板驱使&#xff0c;要用合宙Air780EP模块做几个紧急项目。由于时间紧任务重&#xff0c;遇到了一些棘手问题&#xff0c;可把我给折腾死了…… 这里把遇到的问题&#xff0c;排查记录下来&#xff0c;看能不能帮到因遇到类似的问题&#xff0c;并且一直没找到原因&a…

深度学习------------------------RNN(循环神经网络)

目录 潜变量自回归模型循环神经网络困惑度梯度剪裁循环神经网络的从零开始实现初始化循环神经网络模型的模型参数初始化隐藏状态创建一个类来包装这些函数该部分总代码 定义预测函数该部分总代码 梯度裁剪定义一个函数在一个迭代周期内训练模型训练函数 循环神经网络的简洁实现…

Redis高级特性及应用

一、Redis慢查询 1.1 Redis命令流程 1.2 慢查询配置&#xff1a; 可以通过以下命令配置慢查询时间阈值&#xff08;慢查询值得是上图中执行命令的时间&#xff0c;不包含其他时间&#xff09; config set slowlog-log-slower-than 10000 //单位微秒 config rewrite //写入…

大模型与智能体的市场调研分析

2024年&#xff0c;很多人都在谈论智能体&#xff0c;我老婆这样的美术老师&#xff0c;也让我给她科普一下&#xff0c;于是我花了几天时间&#xff0c;系统学习和深入调研了一下&#xff0c;在此分享给大家。 时代背景 人工智能就像电力一样&#xff0c;如果你的竞争对手正…

木材检测系统源码分享

木材检测检测系统源码分享 [一条龙教学YOLOV8标注好的数据集一键训练_70全套改进创新点发刊_Web前端展示] 1.研究背景与意义 项目参考AAAI Association for the Advancement of Artificial Intelligence 项目来源AACV Association for the Advancement of Computer Vision …

物联网智能项目全面解析

目录 引言 一、物联网概述 1.1 什么是物联网 1.2 物联网的历史与发展 二、物联网智能项目分类 三、关键组件与技术 3.1 传感器和执行器 3.2 连接技术 3.3 数据处理与分析 3.4 用户界面 四、物联网智能项目案例分析 4.1 智能家居 4.2 智慧城市 4.3 工业物联网 4.4…

C# 字符串(String)的应用说明一

一.字符串&#xff08;String&#xff09;的应用说明&#xff1a; 在 C# 中&#xff0c;更常见的做法是使用 string 关键字来声明一个字符串变量&#xff0c;也可以使用字符数组来表示字符串。string 关键字是 System.String 类的别名。 二.创建 String 对象的方法说明&#x…

$attrs 和 $listeners

通常情况下&#xff0c;父子组件之间的数据是通过 props 由父向子传递的&#xff0c;当子组件想要修改数据时&#xff0c;则需要通过 $emit 以事件形式交由父组件完成&#xff0c;而这种交互方式只存在于父子组件之间&#xff0c;多层嵌套的时候&#xff0c;处于内层的组件想要…

SQL进阶技巧:如何获取状态一致的分组? | 最大、最小值法

目录 0 需求描述 1 数据准备 2 问题分析 方法1&#xff1a;最大、最小值法&#xff08;技巧&#xff09; 方法2&#xff1a;常规思路 3 小结 如果觉得本文对你有帮助&#xff0c;那么不妨也可以选择去看看我的博客专栏 &#xff0c;部分内容如下&#xff1a; 数字化建设通…

(JAVA)队列 和 符号表 两种数据结构的实现

1. 队列 1.1 队列的概述 队列是一种基于先进先出&#xff08;FIFO&#xff09;的数据结构&#xff0c;是一种只能在一端进行插入&#xff0c;在另一端进行删除操作的特殊线性表。 它按照先进先出的原则存储数据&#xff0c;先进入的数据&#xff0c;在读取时先被读出来 1.2 …

【anki】显示 “连接超时,请更换网络后重试” 怎么办

文章目录 前言一、问题描述二、解决方案 前言 在 anki同步 时遇到的问题 一、问题描述 二、解决方案 从电信换为了移动热点&#xff0c;电脑手机都同步成功了

图像超分辨率(SR)

图像超分辨率&#xff08;Image Super-Resolution, SR&#xff09;是一种图像处理技术&#xff0c;旨在从低分辨率&#xff08;LR&#xff09;图像中恢复出高分辨率&#xff08;HR&#xff09;图像。这种技术通过增加图像中的细节和清晰度来提高图像的视觉质量&#xff0c;从而…

Stable Diffusion扩散模型【详解】新手也能看懂!!

前言 文章目录 1、Diffusion的整体过程2、加噪过程 2.1 加噪的具体细节2.2 加噪过程的公式推导 3、去噪过程 3.1 图像概率分布 4、损失函数5、 伪代码过程 此文涉及公式推导&#xff0c;需要参考这篇文章&#xff1a; Stable Diffusion扩散模型推导公式的基础知识 1、Diffu…