自然语言处理从入门到应用——LangChain:快速入门-[快速开发聊天模型]

这篇具有很好参考价值的文章主要介绍了自然语言处理从入门到应用——LangChain:快速入门-[快速开发聊天模型]。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

分类目录:《大模型从入门到应用》总目录

LangChain系列文章:

  • 基础知识
  • 快速入门
    • 安装与环境配置
    • 链(Chains)、代理(Agent:)和记忆(Memory)
    • 快速开发聊天模型
  • 模型(Models)
    • 基础知识
    • 大型语言模型(LLMs)
      • 基础知识
      • LLM的异步API、自定义LLM包装器、虚假LLM和人类输入LLM(Human Input LLM)
      • 缓存LLM的调用结果
      • 加载与保存LLM类、流式传输LLM与Chat Model响应和跟踪tokens使用情况
    • 聊天模型(Chat Models)
      • 基础知识
      • 使用少量示例和响应流式传输
    • 文本嵌入模型
      • Aleph Alpha、Amazon Bedrock、Azure OpenAI、Cohere等
      • Embaas、Fake Embeddings、Google Vertex AI PaLM等
  • 提示(Prompts)
    • 基础知识
    • 提示模板
      • 基础知识
      • 连接到特征存储
      • 创建自定义提示模板和含有Few-Shot示例的提示模板
      • 部分填充的提示模板和提示合成
      • 序列化提示信息
    • 示例选择器(Example Selectors)
    • 输出解析器(Output Parsers)
  • 记忆(Memory)
    • 基础知识
    • 记忆的类型
      • 会话缓存记忆、会话缓存窗口记忆和实体记忆
      • 对话知识图谱记忆、对话摘要记忆和会话摘要缓冲记忆
      • 对话令牌缓冲存储器和基于向量存储的记忆
    • 将记忆添加到LangChain组件中
    • 自定义对话记忆与自定义记忆类
    • 聊天消息记录
    • 记忆的存储与应用
  • 索引(Indexes)
    • 基础知识
    • 文档加载器(Document Loaders)
    • 文本分割器(Text Splitters)
    • 向量存储器(Vectorstores)
    • 检索器(Retrievers)
  • 链(Chains)
    • 基础知识
    • 通用功能
      • 自定义Chain和Chain的异步API
      • LLMChain和RouterChain
      • SequentialChain和TransformationChain
      • 链的保存(序列化)与加载(反序列化)
    • 链与索引
      • 文档分析和基于文档的聊天
      • 问答的基础知识
      • 图问答(Graph QA)和带来源的问答(Q&A with Sources)
      • 检索式问答
      • 文本摘要(Summarization)、HyDE和向量数据库的文本生成
  • 代理(Agents)
    • 基础知识
    • 代理类型
    • 自定义代理(Custom Agent)
    • 自定义MRKL代理
    • 带有ChatModel的LLM聊天自定义代理和自定义多操作代理(Custom MultiAction Agent)
    • 工具
      • 基础知识
      • 自定义工具(Custom Tools)
      • 多输入工具和工具输入模式
      • 人工确认工具验证和Tools作为OpenAI函数
    • 工具包(Toolkit)
    • 代理执行器(Agent Executor)
      • 结合使用Agent和VectorStore
      • 使用Agents的异步API和创建ChatGPT克隆
      • 处理解析错误、访问中间步骤和限制最大迭代次数
      • 为代理程序设置超时时间和限制最大迭代次数和为代理程序和其工具添加共享内存
    • 计划与执行
  • 回调函数(Callbacks)

在《自然语言处理从入门到应用——LangChain:快速入门》系列文章中我们会用最简练的语言与示例带领大家快速调试并上手LangChain,读者读完本系列的文章后,就会对LangChain有一个大致的了解并可以将LangChain运用到自己开发的程序中。但如果读者想对LangChain的各个模块进行更深入的了解,可以继续学习《自然语言处理从入门到应用——LangChain》系列文章。本文主要是阐述了如何快速通过LangChain快速开发一个聊天模型。

聊天模型是语言模型的一种变化形式,虽然聊天模型底层使用的是语言模型,但它们提供的接口有些不同:它们没有提供一个“文本输入、文本输出”API,而是提供了一个接口,其中“聊天消息”是输入和输出。聊天模型API是相当新的,所以LangChain仍然在找出正确的抽象。

从聊天模型获取消息完成

我们可以通过向聊天模型传递一条或多条消息来完成聊天,而响应也将是一条消息。LangChain中当前支持的消息类型是AIMessageHumanMessageSystemMessageChatMessage,其中ChatMessage接受任意角色参数。大多数时候,我们只需要处理HumanMessageAIMessageSystemMessage即可.

from langchain.chat_models import ChatOpenAI
from langchain.schema import (
    AIMessage,
    HumanMessage,
    SystemMessage
)
chat = ChatOpenAI(temperature=0)

我们通过传入单个消息来完成:

chat([HumanMessage(content="Translate this sentence from English to French. I love programming.")])

输出:

AIMessage(content="J'aime programmer.", additional_kwargs={})

我们还可以为OpenAI的gpt-3.5-turbo和gpt-4传递多条消息:

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

输出:

AIMessage(content="J'aime programmer.", additional_kwargs={})

我们还可以更进一步,使用generate为多组消息生成完成,这将返回一个带有附加message参数的LLMResult

batch_messages = [
    [
        SystemMessage(content="You are a helpful assistant that translates English to French."),
        HumanMessage(content="Translate this sentence from English to French. I love programming.")
    ],
    [
        SystemMessage(content="You are a helpful assistant that translates English to French."),
        HumanMessage(content="Translate this sentence from English to French. 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': 71, 'completion_tokens': 18, 'total_tokens': 89}})

我们还可以从这个LLMResult中获取字符token的使用情况token_usage

result.llm_output['token_usage']

输出:

{'prompt_tokens': 71, 'completion_tokens': 18, 'total_tokens': 89}

聊天提示模板

与LLM类似,我们可以通过使用MessagePromptTemplate来使用模板。可以从一个或多个 MessagePromptTemplate生成ChatPromptTemplate。我们可以使用ChatPromptTemplateformat_tip,这将返回一个PromptValue,我们可以将其转换为字符串或Message对象,具体取决于我们是想将格式化的值用作llm或聊天模型的输入。为了方便起见,我们在模板上公开了一个from_template方法。如果我们使用这个模板,它看起来是这样的:

from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate
)
 
chat = ChatOpenAI(temperature=0)
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])
 
# get a chat completion from the formatted messages
chat(chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_messages())
 

输出:

AIMessage(content="J'aime programmer.", additional_kwargs={})

带聊天模型的链

我们之前讨论的LLMChain也可以用于聊天模型:

from langchain.chat_models import ChatOpenAI
from langchain import LLMChain
from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate,
)
 
chat = ChatOpenAI(temperature=0)
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])
chain = LLMChain(llm=chat, prompt=chat_prompt)
chain.run(input_language="English", output_language="French", text="I love programming.")
 

输出:

"J'aime programmer."

具有聊天模型的代理

代理也可以与聊天模型一起使用,我们可以使用AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION作为代理类型来初始化一个聊天模型:

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.chat_models import ChatOpenAI
from langchain.llms import OpenAI
 
# 首先,我们加载我们要用来控制代理的语言模型
chat = ChatOpenAI(temperature=0)
 
# 其次,我们加载一些要使用的工具。请注意,“llm-math”工具使用LLM,所以我们需要传递它
llm = OpenAI(temperature=0)
tools = load_tools(["serpapi", "llm-math"], llm=llm)
 
# 最后,我们使用工具、语言模型和我们要使用的代理类型来初始化代理
agent = initialize_agent(tools, chat, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
 
# 测试代理
agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?")

我们将会得到输出:

> Entering new AgentExecutor chain...
Thought: I need to use a search engine to find Olivia Wilde's boyfriend and a calculator to raise his age to the 0.23 power.
Action:
{
 "action": "Search",
 "action_input": "Olivia Wilde boyfriend"
}
Observation: Sudeikis and Wilde's relationship ended in November 2020. Wilde was publicly served with court documents regarding child custody while she was presenting Don't Worry Darling at CinemaCon 2022. In January 2021, Wilde began dating singer Harry Styles after meeting during the filming of Don't Worry Darling.
Thought:I need to use a search engine to find Harry Styles' current age.
Action:
{
 "action": "Search",
 "action_input": "Harry Styles age"
}
Observation: 29 years
Thought:Now I need to calculate 29 raised to the 0.23 power.
Action:
{
 "action": "Calculator",
 "action_input": "29^0.23"
}
Observation: Answer: 2.169459462491557
Thought:I now know the final answer.
Final Answer: 2.169459462491557
> Finished chain.
'2.169459462491557'

记忆: 向链和代理添加状态

我们也可以对链使用Memory,对代理使用聊天模型进行初始化。这与LLM的Memory之间的主要区别在于我们不需要将以前的所有消息压缩成一个字符串,而是可以将它们保留为自己独特的记忆对象。

from langchain.prompts import (
    ChatPromptTemplate, 
    MessagesPlaceholder, 
    SystemMessagePromptTemplate, 
    HumanMessagePromptTemplate
)
from langchain.chains import ConversationChain
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
 
prompt = ChatPromptTemplate.from_messages([
    SystemMessagePromptTemplate.from_template("The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know."),
    MessagesPlaceholder(variable_name="history"),
    HumanMessagePromptTemplate.from_template("{input}")
])
llm = ChatOpenAI(temperature=0)
memory = ConversationBufferMemory(return_messages=True)
conversation = ConversationChain(memory=memory, prompt=prompt, llm=llm)
conversation.predict(input="Hi there!")
 
# -> 'Hello! How can I assist you today?'
conversation.predict(input="I'm doing well! Just having a conversation with an AI.")
 
# -> "That sounds like fun! I'm happy to chat with you. Is there anything specific you'd like to talk about?"
conversation.predict(input="Tell me about yourself.")
 
# -> "Sure! I am an AI language model created by OpenAI. I was trained on a large dataset of text from the internet, which allows me to understand and generate human-like language. I can answer questions, provide information, and even have conversations like this one. Is there anything else you'd like to know about me?"

参考文献:
[1] LangChain 🦜️🔗 中文网,跟着LangChain一起学LLM/GPT开发:https://www.langchain.com.cn/
[2] LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架:http://www.cnlangchain.com/文章来源地址https://www.toymoban.com/news/detail-553219.html

到了这里,关于自然语言处理从入门到应用——LangChain:快速入门-[快速开发聊天模型]的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

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

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

相关文章

  • 自然语言处理从入门到应用——LangChain:记忆(Memory)-[基础知识]

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

    2024年02月14日
    浏览(59)
  • 自然语言处理从入门到应用——LangChain:代理(Agents)-[基础知识]

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

    2024年02月13日
    浏览(57)
  • 自然语言处理从入门到应用——LangChain:提示(Prompts)-[基础知识]

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

    2024年02月15日
    浏览(36)
  • 自然语言处理从入门到应用——LangChain:索引(Indexes)-[基础知识]

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

    2024年02月12日
    浏览(39)
  • 自然语言处理从入门到应用——LangChain:链(Chains)-[基础知识]

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

    2024年02月12日
    浏览(40)
  • 自然语言处理从入门到应用——LangChain:记忆(Memory)-[聊天消息记录]

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

    2024年02月12日
    浏览(32)
  • 自然语言处理从入门到应用——LangChain:记忆(Memory)-[记忆的存储与应用]

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

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

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

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

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

    2024年02月15日
    浏览(29)
  • 自然语言处理从入门到应用——LangChain:索引(Indexes)-[检索器(Retrievers)]

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

    2024年02月12日
    浏览(30)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包