[LangChain核心模块]模型的输入和输出->Language models

这篇具有很好参考价值的文章主要介绍了[LangChain核心模块]模型的输入和输出->Language models。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

⭐作者介绍:大二本科网络工程专业在读,持续学习Java,努力输出优质文章
⭐作者主页:@逐梦苍穹
⭐所属专栏:人工智能。

1、简介

语言模型 ( Language models )
LangChain提供了两种类型模型的接口和集成:
● LLMs: 输入为文本字符串,输出为文本字符串的模型
● Chat models: 由语言模型支持,输入为聊天消息列表,输出为聊天消息的模型
LLMs vs Chat Models:
LLMs和Chat Models有微妙但重要的区别。
LangChain中的LLMs是指纯文本补全模型。
它们包装的API接受字符串提示作为输入,并输出字符串补全。OpenAI的GPT-3就是LLM的实现。 Chat models通常由LLMs支持,但专门用于进行对话。 关键是,它们的提供者API公开了与纯文本补全模型不同的接口。它们的输入不是单个字符串,而是聊天消息的列表。通常,这些消息带有发言者(通常为"System"、“AI"和"Human"之一)。 它们返回一个(“AI”)聊天消息作为输出。GPT-4和Anthropic的Claude都是作为Chat Models实现的。
为了能够交换LLMs和Chat Models,两者都实现了Base Language Model接口。这暴露了公共方法"predict”(输入字符串,返回字符串)和"predict messages"(输入消息,返回消息)。 如果使用特定模型,建议使用该模型类别的特定方法(即LLMs的"predict"和Chat Models的"predict messages"), 但如果正在创建一个应该适用于不同类型模型的应用程序,共享接口可能会很有帮助。

2、LLMs

大型语言模型(LLMs-> Large Language Model)是LangChain的核心组件。 LangChain不提供自己的LLMs,而是提供了与许多不同的LLMs进行交互的标准接口。

2.1、入门

有许多LLM提供商(OpenAI、Cohere、Hugging Face等)- LLM类旨在为所有提供商提供标准接口。
在本演示中,我们将使用OpenAI LLM包装器,尽管突出显示的功能对于所有LLM类型都是通用的。
设置:
首先,我们需要安装OpenAI Python包:
pip install openai
使用API需要一个API密钥,可以通过创建帐户并转到此处获取。一旦我们获得密钥,我们将希望通过运行以下命令将其设置为环境变量:
export OPENAI_API_KEY="..."
如果不想设置环境变量,可以在初始化OpenAI LLM类时直接通过openai_api_key命名参数传递密钥:

from langchain.llms import OpenAI

llm = OpenAI(openai_api_key="...")

否则,可以不使用任何参数进行初始化:

from langchain.llms import OpenAI

llm = OpenAI()

call:输入字符串 -> 输出字符串
使用LLM的最简单方法是可调用的:输入一个字符串,获得一个字符串完成结果。
llm("Tell me a joke")
'Why did the chicken cross the road?\n\nTo get to the other side.'
generate:批量调用,更丰富的输出
generate允许使用字符串列表调用模型,获得比仅文本更完整的响应。这个完整的响应可以包括多个顶部响应和其他LLM提供程序特定的信息:

llm_result = llm.generate(["Tell me a joke", "Tell me a poem"]*15)
len(llm_result.generations)
#30
llm_result.generations[0]
[Generation(text='\n\nWhy did the chicken cross the road?\n\nTo get to the other side!'),
     Generation(text='\n\nWhy did the chicken cross the road?\n\nTo get to the other side.')]
llm_result.generations[-1]
[Generation(text="\n\nWhat if love neverspeech\n\nWhat if love never ended\n\nWhat if love was only a feeling\n\nI'll never know this love\n\nIt's not a feeling\n\nBut it's what we have for each other\n\nWe just know that love is something strong\n\nAnd we can't help but be happy\n\nWe just feel what love is for us\n\nAnd we love each other with all our heart\n\nWe just don't know how\n\nHow it will go\n\nBut we know that love is something strong\n\nAnd we'll always have each other\n\nIn our lives."),
     Generation(text='\n\nOnce upon a time\n\nThere was a love so pure and true\n\nIt lasted for centuries\n\nAnd never became stale or dry\n\nIt was moving and alive\n\nAnd the heart of the love-ick\n\nIs still beating strong and true.')]

还可以访问返回的特定于提供程序的信息。此信息在不同提供程序之间是标准化的。\

llm_result.llm_output
{'token_usage': {'completion_tokens': 3903,
      'total_tokens': 4023,
      'prompt_tokens': 120}}

2.2、缓存

缓存 llm_caching:
LangChain为LLM提供了一个可选的缓存层。这个功能有两个好处:
如果经常多次请求相同的补全,它可以通过减少对LLM提供者的API调用次数来节省费用。 它可以通过减少对LLM提供者的API调用次数来加速的应用程序。

import langchain
from langchain.llms import OpenAI

# To make the caching really obvious, lets use a slower model.
llm = OpenAI(model_name="text-davinci-002", n=2, best_of=2)

内存缓存(In Memory Cache)

from langchain.cache import InMemoryCache
langchain.llm_cache = InMemoryCache()

# The first time, it is not yet in cache, so it should take longer
llm("Tell me a joke")
CPU times: user 35.9 ms, sys: 28.6 ms, total: 64.6 ms
    Wall time: 4.83 s
    

    "\n\nWhy couldn't the bicycle stand up by itself? It was...two tired!"
# The second time it is, so it goes faster
llm("Tell me a joke")
CPU times: user 238 µs, sys: 143 µs, total: 381 µs
    Wall time: 1.76 ms


    '\n\nWhy did the chicken cross the road?\n\nTo get to the other side.'

SQLite缓存(SQLite Cache)
rm .langchain.db

# We can do the same thing with a SQLite cache
from langchain.cache import SQLiteCache
langchain.llm_cache = SQLiteCache(database_path=".langchain.db")
# The first time, it is not yet in cache, so it should take longer
llm("Tell me a joke")
CPU times: user 17 ms, sys: 9.76 ms, total: 26.7 ms
    Wall time: 825 ms


    '\n\nWhy did the chicken cross the road?\n\nTo get to the other side.'
# The second time it is, so it goes faster
llm("Tell me a joke")
CPU times: user 2.46 ms, sys: 1.23 ms, total: 3.7 ms
    Wall time: 2.67 ms


    '\n\nWhy did the chicken cross the road?\n\nTo get to the other side.'

链中的可选缓存(Optional Caching in Chains)
还可以关闭链中特定节点的缓存。请注意,由于某些接口的原因,先构建链,然后再编辑LLM通常更容易。
例如,我们将加载一个摘要映射-减少链。我们将对映射步骤的结果进行缓存,但不对合并步骤进行冻结。

llm = OpenAI(model_name="text-davinci-002")
no_cache_llm = OpenAI(model_name="text-davinci-002", cache=False)
from langchain.text_splitter import CharacterTextSplitter
from langchain.chains.mapreduce import MapReduceChain

text_splitter = CharacterTextSplitter()
with open('../../../state_of_the_union.txt') as f:
    state_of_the_union = f.read()
texts = text_splitter.split_text(state_of_the_union)
from langchain.docstore.document import Document
docs = [Document(page_content=t) for t in texts[:3]]
from langchain.chains.summarize import load_summarize_chain
chain = load_summarize_chain(llm, chain_type="map_reduce", reduce_llm=no_cache_llm)
chain.run(docs)
CPU times: user 452 ms, sys: 60.3 ms, total: 512 ms
    Wall time: 5.09 s


    '\n\nPresident Biden is discussing the American Rescue Plan and the Bipartisan Infrastructure Law, which will create jobs and help Americans. He also talks about his vision for America, which includes investing in education and infrastructure. In response to Russian aggression in Ukraine, the United States is joining with European allies to impose sanctions and isolate Russia. American forces are being mobilized to protect NATO countries in the event that Putin decides to keep moving west. The Ukrainians are bravely fighting back, but the next few weeks will be hard for them. Putin will pay a high price for his actions in the long run. Americans should not be alarmed, as the United States is taking action to protect its interests and allies.'

当我们再次运行它时,我们发现它运行得更快,但最终的答案是不同的。这是由于在映射步骤中进行了缓存,但在减少步骤中没有进行缓存。
chain.run(docs)

CPU times: user 11.5 ms, sys: 4.33 ms, total: 15.8 ms
    Wall time: 1.04 s


    '\n\nPresident Biden is discussing the American Rescue Plan and the Bipartisan Infrastructure Law, which will create jobs and help Americans. He also talks about his vision for America, which includes investing in education and infrastructure.'

rm .langchain.db sqlite.db

2.3、流式传输

流式传输(Streaming):
一些LLM提供流式响应。这意味着可以在整个响应返回之前开始处理它,而不是等待它完全返回。如果希望在生成响应时向用户显示响应,或者希望在生成响应时处理响应,这将非常有用。
目前,我们支持对 OpenAI、ChatOpenAI 和 ChatAnthropic 实现的流式传输。要使用流式传输,请使用一个实现了 on_llm_new_token 的 CallbackHandler。在这个示例中,我们使用的是 StreamingStdOutCallbackHandler。

from langchain.llms import OpenAI
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler


llm = OpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], temperature=0)
resp = llm("Write me a song about sparkling water.")

如果使用 generate,我们仍然可以访问最终的 LLMResult。但是,目前不支持对流式传输的 token_usage。

Verse 1
    I'm sippin' on sparkling water,
    It's so refreshing and light,
    It's the perfect way to quench my thirst
    On a hot summer night.
    
    Chorus
    Sparkling water, sparkling water,
    It's the best way to stay hydrated,
    It's so crisp and so clean,
    It's the perfect way to stay refreshed.
    
    Verse 2
    I'm sippin' on sparkling water,
    It's so bubbly and bright,
    It's the perfect way to cool me down
    On a hot summer night.
    
    Chorus
    Sparkling water, sparkling water,
    It's the best way to stay hydrated,
    It's so crisp and so clean,
    It's the perfect way to stay refreshed.
    
    Verse 3
    I'm sippin' on sparkling water,
    It's so light and so clear,
    It's the perfect way to keep me cool
    On a hot summer night.
    
    Chorus
    Sparkling water, sparkling water,
    It's the best way to stay hydrated,
    It's so crisp and so clean,
    It's the perfect way to stay refreshed.

llm.generate(["Tell me a joke."])

Q: What did the fish say when it hit the wall?
    A: Dam!


    LLMResult(generations=[[Generation(text='\n\nQ: What did the fish say when it hit the wall?\nA: Dam!', generation_info={'finish_reason': 'stop', 'logprobs': None})]], llm_output={'token_usage': {}, 'model_name': 'text-davinci-003'})

3、Chat models

聊天模型 Chat models:
尽管聊天模型在内部使用语言模型,但它们所提供的接口有些不同。 它们不是提供"输入文本,输出文本"的API,而是提供一个以"聊天消息"作为输入和输出的接口。
聊天模型的API相对较新,因此我们仍在确定正确的抽象层次。
提供以下文档部分:
操作指南:核心功能的实例介绍,例如流式传输、创建聊天提示等。
集成:如何使用不同的聊天模型提供者(OpenAI、Anthropic等)。

3.1、入门

设置
首先,我们需要安装OpenAI Python包:
pip install openai
访问API需要API密钥,可以通过创建帐户并转到此处(https://platform.openai.com/account/api-keys)获取密钥。一旦我们有了密钥,我们将希望通过运行以下命令将其设置为环境变量:
export OPENAI_API_KEY="..."
如果不想设置环境变量,可以在初始化OpenAI LLM类时直接通过“openai_api_key”命名参数传递密钥:

from langchain.chat_models import ChatOpenAI

chat = ChatOpenAI(open_api_key="...")

否则,可以不使用任何参数进行初始化:

from langchain.chat_models import ChatOpenAI

chat = ChatOpenAI()

消息
聊天模型界面基于消息而不是原始文本。 LangChain目前支持的消息类型有“AIMessage”,“HumanMessage”,“SystemMessage”和“ChatMessage” - “ChatMessage”接受一个任意角色参数。大多数时候,只需处理“HumanMessage”,“AIMessage”和“SystemMessage”
call
输入消息 -> 输出消息
通过将一个或多个消息传递给聊天模型,可以获得聊天完成。响应将是一条消息。

from langchain.schema import (
    AIMessage,
    HumanMessage,
    SystemMessage
)

chat([HumanMessage(content="Translate this sentence from English to French: I love programming.")])
AIMessage(content="J'aime programmer.", additional_kwargs={})

OpenAI的聊天模型支持多个消息作为输入。有关更多信息,请参见此处(https://platform.openai.com/docs/guides/chat/chat-vs-completions)。以下是向聊天模型发送系统消息和用户消息的示例:

messages = [
    SystemMessage(content="You are a helpful assistant that translates English to French."),
    HumanMessage(content="I love programming.")
]
chat(messages)

AIMessage(content="J'aime programmer.", additional_kwargs={})
generate
批量调用,更丰富的输出
可以进一步使用generate为多组消息生成完成。这将返回一个带有额外message参数的LLMResult。

batch_messages = [
    [
        SystemMessage(content="You are a helpful assistant that translates English to French."),
        HumanMessage(content="I love programming.")
    ],
    [
        SystemMessage(content="You are a helpful assistant that translates English to French."),
        HumanMessage(content="I love artificial intelligence.")
    ],
]
result = chat.generate(batch_messages)
result
LLMResult(generations=[[ChatGeneration(text="J'aime programmer.", generation_info=None, message=AIMessage(content="J'aime programmer.", additional_kwargs={}))], [ChatGeneration(text="J'aime l'intelligence artificielle.", generation_info=None, message=AIMessage(content="J'aime l'intelligence artificielle.", additional_kwargs={}))]], llm_output={'token_usage': {'prompt_tokens': 57, 'completion_tokens': 20, 'total_tokens': 77}})

可以从这个LLMResult中恢复诸如令牌使用情况之类的东西
result.llm_output

{'token_usage': {'prompt_tokens': 57,
      'completion_tokens': 20,
      'total_tokens': 77}}

3.2、LLMChain

可以以非常类似的方式使用现有的LLMChain - 提供一个提示和一个模型。

chain = LLMChain(llm=chat, prompt=chat_prompt)
chain.run(input_language="English", output_language="French", text="I love programming.")
# "J'adore la programmation."

3.3、提示(Prompts)

Chat模型的提示(Prompts)是围绕消息而构建的,而不仅仅是普通文本。
你可以使用 MessagePromptTemplate 来利用模板。你可以从一个或多个 MessagePromptTemplates 构建一个 ChatPromptTemplate。可以使用 ChatPromptTemplate 的 format_prompt 方法,这将返回一个 PromptValue,可以将其转换为字符串或消息对象,具体取决于想要将格式化的值用作 LLM 或聊天模型的输入。
为方便起见,模板上还公开了一个 from_template 方法。如果要使用此模板,则如下所示:

from langchain import PromptTemplate
from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    AIMessagePromptTemplate,
    HumanMessagePromptTemplate,
)

template="You are a helpful assistant that translates {input_language} to {output_language}."
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
human_template="{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

# 获取来自格式化消息的聊天完成。
chat(chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_messages())
AIMessage(content="J'adore la programmation.", additional_kwargs={})

如果想要更直接地构建 MessagePromptTemplate,可以在外部创建一个 PromptTemplate 然后将其传入,例如:

prompt=PromptTemplate(
    template="You are a helpful assistant that translates {input_language} to {output_language}.",
    input_variables=["input_language", "output_language"],
)
system_message_prompt = SystemMessagePromptTemplate(prompt=prompt)

3.4、实时流媒体 streaming

一些聊天模型提供实时流媒体响应。这意味着无需等待完整响应返回,而是可以在其可用时开始处理响应。如果希望在生成响应时将其显示给用户,或者希望在生成响应时处理响应,这将非常有用。文章来源地址https://www.toymoban.com/news/detail-575859.html

from langchain.chat_models import ChatOpenAI
from langchain.schema import (
    HumanMessage,
)


from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
chat = ChatOpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], temperature=0)
resp = chat([HumanMessage(content="Write me a song about sparkling water.")])
Verse 1:
    Bubbles rising to the top
    A refreshing drink that never stops
    Clear and crisp, it's pure delight
    A taste that's sure to excite
    
    Chorus:
    Sparkling water, oh so fine
    A drink that's always on my mind
    With every sip, I feel alive
    Sparkling water, you're my vibe
    
    Verse 2:
    No sugar, no calories, just pure bliss
    A drink that's hard to resist
    It's the perfect way to quench my thirst
    A drink that always comes first
    
    Chorus:
    Sparkling water, oh so fine
    A drink that's always on my mind
    With every sip, I feel alive
    Sparkling water, you're my vibe
    
    Bridge:
    From the mountains to the sea
    Sparkling water, you're the key
    To a healthy life, a happy soul
    A drink that makes me feel whole
    
    Chorus:
    Sparkling water, oh so fine
    A drink that's always on my mind
    With every sip, I feel alive
    Sparkling water, you're my vibe
    
    Outro:
    Sparkling water, you're the one
    A drink that's always so much fun
    I'll never let you go, my friend
    Sparkling

到了这里,关于[LangChain核心模块]模型的输入和输出->Language models的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LLM(Large Language Model)大语言模型

    语言模型够大,训练的语料够多,涌现出推理能力飙升等   Generative Pre-trained Transformer 3(GPT-3)是最著名的LLM之一,拥有1750亿个参数。该模型在文本生成、翻译和其他任务中表现出显著的性能,在全球范围内引起了热烈的反响,目前OpenAI已经迭代到了GPT-4版本 Generative :能产

    2024年02月15日
    浏览(34)
  • 【LangChain】P1 LangChain 应用程序的核心构建模块 LLMChain 以及其三大部分

    LangChain 应用程序的核心构建模块 LLMChain 由三部分组成: 语言模型 - LLMs: 语言模型是这里的核心推理引擎。为了使用 LangChain,您需要了解不同类型的语言模型以及如何使用它们。 提示模板 - Prompt templates: 它为语言模型提供指令。它控制着语言模型的输出,因此了解如何构

    2024年02月12日
    浏览(35)
  • 自然语言处理从入门到应用——LangChain:模型(Models)-[聊天模型(Chat Models):基础知识]

    分类目录:《大模型从入门到应用》总目录 LangChain系列文章: 基础知识 快速入门 安装与环境配置 链(Chains)、代理(Agent:)和记忆(Memory) 快速开发聊天模型 模型(Models) 基础知识 大型语言模型(LLMs) 基础知识 LLM的异步API、自定义LLM包装器、虚假LLM和人类输入LLM(

    2024年02月15日
    浏览(36)
  • 神经网络语言模型(Neural Network Language Model,NNLM)

    神经网络语言模型(Neural Network Language Model,NNLM)是一种用神经网络建模语言的方法。NNLM 通过学习文本序列中的词汇之间的概率关系,能够捕捉到语言的结构和语境,从而能够生成自然语言文本或进行其他与语言相关的任务。 想象一下,你正在阅读一本小说。每当你读到一

    2024年02月02日
    浏览(30)
  • 大型语言模型(LLM, Large Language Models)基模和 Chat 模型之间的区别

    最近看大模型相关的知识,有看到大模型都有基础模型(base)和对话模型(chat),不太清楚什么时候用到基础模型,什么时候用到对话模型,故有此文。 通过了解,最简单的概述就是基于基础模型会训练出一个对话(Chat)模型,对话模型主要用于对话场景,基础模型主要做

    2024年02月21日
    浏览(27)
  • 自然语言处理从入门到应用——LangChain:模型(Models)-[文本嵌入模型Ⅱ]

    分类目录:《大模型从入门到应用》总目录 LangChain系列文章: 基础知识 快速入门 安装与环境配置 链(Chains)、代理(Agent:)和记忆(Memory) 快速开发聊天模型 模型(Models) 基础知识 大型语言模型(LLMs) 基础知识 LLM的异步API、自定义LLM包装器、虚假LLM和人类输入LLM(

    2024年02月15日
    浏览(33)
  • 自然语言处理从入门到应用——LangChain:模型(Models)-[文本嵌入模型Ⅰ]

    分类目录:《大模型从入门到应用》总目录 LangChain系列文章: 基础知识 快速入门 安装与环境配置 链(Chains)、代理(Agent:)和记忆(Memory) 快速开发聊天模型 模型(Models) 基础知识 大型语言模型(LLMs) 基础知识 LLM的异步API、自定义LLM包装器、虚假LLM和人类输入LLM(

    2024年02月15日
    浏览(44)
  • 自然语言处理从入门到应用——LangChain:模型(Models)-[聊天模型(Chat Models):使用少量示例和响应流式传输]

    分类目录:《大模型从入门到应用》总目录 LangChain系列文章: 基础知识 快速入门 安装与环境配置 链(Chains)、代理(Agent:)和记忆(Memory) 快速开发聊天模型 模型(Models) 基础知识 大型语言模型(LLMs) 基础知识 LLM的异步API、自定义LLM包装器、虚假LLM和人类输入LLM(

    2024年02月15日
    浏览(47)
  • 大模型 LLM 综述, A Survey of Large Language Models

    一般认为NLP领域的大模型=10 Billion参数(也有人认为是6B、7B, 工业界用, 开始展现涌现能力); 经典大模型有GPT-3、BLOOM、Flan-T5、GPT-NeoX、OPT、GLM-130B、PaLM、LaMDA、LLaMA等; 大模型时间线, 图来自A Survey of Large Language Models,下同。 2.1 涌现 涌现, emerge(abilities), 即一般指在大模型中出现

    2024年02月08日
    浏览(44)
  • Language Models as Knowledge Embeddings:语言模型用作知识嵌入 IJCAI 2022

    1)基于结构的知识嵌入 进一步分成基于翻译的模型和基于语义匹配的模型 基于翻译的模型采用基于距离的评分函数,TransE把实体和关系嵌入到一个维度为d的共享向量空间中;TransH,TransR,RotatE. 语义匹配模型采用基于相似性的评分函数,RESCAL,DistMult,CoKE. 2)基于描述的知识嵌入

    2024年02月07日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包