首页 > 其他分享 >langchain中的chat models介绍和使用

langchain中的chat models介绍和使用

时间:2023-11-29 12:01:49浏览次数:38  
标签:prompt chat models langchain LLM template

简介

之前我们介绍了LLM模式,这种模式是就是文本输入,然后文本输出。

chat models是基于LLM模式的更加高级的模式。他的输入和输出是格式化的chat messages。

一起来看看如何在langchain中使用caht models吧。

chat models的使用

首先langchain对chat models下支持的模型就少很多了。一方面是可能有些语言模型本身是不支持chat models的。另外一方面langchain也还是在一个发展中的过程,所以有些模型还需要适配。

目前看来langchain支持的chat models有:ChatAnthropic,AzureChatOpenAI,ChatVertexAI,JinaChat,ChatOpenAI和PromptLayerChatOpenAI这几种。

langchain把chat消息分成了这几种:AIMessage, HumanMessage, SystemMessage 和 ChatMessage。

HumanMessage就是用户输入的消息,AIMessage是大语言模型的消息,SystemMessage是系统的消息。ChatMessage是一种可以自定义类型的消息。

在使用的时候,只需要在chat中传入对应的消息即可:

from langchain.chat_models import ChatOpenAI

chat = ChatOpenAI()

messages = [
    SystemMessage(content="你是一个小说家"),
    HumanMessage(content="帮我写篇小说")
]
chat(messages)

当然和LLM一样,你也可以使用批量模式如下:

batch_messages = [
    [
        SystemMessage(content="你是一个小说家"),
        HumanMessage(content="帮我写篇小说")
    ],
    [
        SystemMessage(content="你是一个诗人"),
        HumanMessage(content="帮我写首诗")
    ],
]
result = chat.generate(batch_messages)
result

chat models的高级功能

其实和LLM类似,基本上LLM有的高级功能chat models都有。

比如有用的比如缓存功能,可以缓存之前的输入和输出,避免每次都调用LLM,从而可以减少token的开销。

以InMemoryCache为例子:

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

# 第一次调用,不是用cache
llm.predict("Tell me a joke")

# 第二次调用,使用cache
llm.predict("Tell me a joke")

除了InMemoryCache,langchain还支持FullLLMCache,SQLAlchemyCache,SQLiteCache和RedisCache等等。

同样的,chat models也是支持流模式的:

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="帮忙我写首诗")])

只需要在构建ChatOpenAI的时候,把StreamingStdOutCallbackHandler传入callbacks即可。

如果要在chat models中使用PromptTemplate,因为chat models的消息格式跟LLM是不一样的,所以对应的PromptTemplate也是不一样的。

和对应的chat models消息对应的PromptTemplate是ChatPromptTemplate,SystemMessagePromptTemplate,
AIMessagePromptTemplate和HumanMessagePromptTemplate。

我们看下是如何使用prompt template来构建prompt:

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

# 构建各种prompt
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])

# 使用format_prompt把prompt传给chat
chat(chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_messages())

chat models下消息构建确实比直接使用LLM要复杂点,大家在使用的时候需要注意。

总结

chat models是LLM的高阶表现形式。如果我们需要进行对话模型的话,就可以考虑使用这个。

最通俗的解读,最深刻的干货,最简洁的教程,众多你不知道的小技巧等你来发现!

欢迎关注我的公众号:「程序那些事」,懂技术,更懂你!



标签:prompt,chat,models,langchain,LLM,template
From: https://blog.51cto.com/flydean/8613813

相关文章

  • Chat2DB高颜值、免费开源、集成AI的数据库客户端
     Chat2DB是一款有开源免费的多数据库客户端工具,支持windows、mac本地安装,也支持服务器端部署,web网页访问。和传统的数据库客户端软件Navicat、DBeaver相比Chat2DB集成了AIGC的能力,能够将自然语言转换为SQL,也可以将SQL转换为自然语言,可以给出研发人员SQL的优化建......
  • 使用LangChain与ChatGLM实现本地知识库(一)
      本篇主要内容为介绍ChatGLM3的安装使用,后续才会涉及到使用LangChain实现本地知识库的内容;  ChatGLM为智谱与清华大学开源的一个大语言模型,支持多轮对话、内容创作等,ChatGLM3-6B为ChatGLM3系列中门槛相对较低的一个,本地部署提供兼容OpenAI的API;  LangChain用于快速开发基......
  • 使用chat2db的感受
    作为一个chat2db的使用者,我深深地感受到了这款工具带给我的便利和高效。chat2db让我能够轻松地将聊天数据转化为结构化的数据库信息,从而更好地进行数据分析和利用。   首先,chat2db的使用非常简单。它的接口设计友好且直观,使得即使没有编程基础的人也能够轻松上手,并且还有视......
  • 小白必知:AIGC 和 ChatGPT 的区别
    原文:https://openaigptguide.com/chatgpt-aigc-difference/AIGC和ChatGPT都是人工智能技术,但它们的功能和应用场景不同。AIGC(AI-GeneratedContent,人工智能自动生成内容)是人工智能、计算机图形学和深度学习等领域的技术结合,用于生成大量的文本、图片或视频等内容,例如新闻稿......
  • ChatGPT优秀开源仓库
    一、pandala项目仓库地址:pandora-nextdocker部署方案拉取镜像dockerpullpengzhile/pandora-next创建配置文件创建目录mkdir~/data~/sessions创建默认配置文件cd~/data/vimconfig.json{"bind":"0.0.0.0:8181","timeout":600,"p......
  • Can Pre-Trained Text-to-Image Models Generate Visual Goals for Reinforcement Lea
    概述LearningformtheVoid(LfVoid)根据给定的languageinstruction对observation进行appearance-basedandstructure-based修改得到goalimages,为RL提供奖励信号。提升了example-basedRLmethods,无需rewardfunction或者demonstration就可以解决一些robotcontroltasks问......
  • Computer vision: models, learning and inference
    http://www.computervisionmodels.com/13.2.3SIFTdetectorSIFT尺度不变特征转换sasecondmethodforidentifyinginterestpoints一个尺度和对应兴趣点定位141516......
  • 文心一言 VS 讯飞星火 VS chatgpt (145)-- 算法导论12.1 5题
    五、用go语言,因为在基于比较的排序模型中,完成n个元素的排序,其最坏情况下需要Ω(nlgn)时间。试证明:任何基于比较的算法从n个元素的任意序列中构造一棵二又搜索树,其最坏情况下需要Ω(nlgn)的时间。我个人喜欢灵小智。文心一言:首先,我们要明白二叉搜索树(BST)的性质:每个节点......
  • TS版LangChain实战:基于文档的增强检索(RAG)
    LangChainLangChain是一个以LLM(大语言模型)模型为核心的开发框架,LangChain的主要特性:可以连接多种数据源,比如网页链接、本地PDF文件、向量数据库等允许语言模型与其环境交互封装了ModelI/O(输入/输出)、Retrieval(检索器)、Memory(记忆)、Agents(决策和调度)等核心组件可以使用链......
  • 软件测试/人工智能|探究 LangChain 核心模块:PromptsModelsParsers
    简介LangChain是一种新兴的语言处理平台,其核心模块之一即PromptsModelsParsers。这一模块扮演着关键的角色,为LangChain的功能和性能提供了坚实的基础。在这篇文章中,我们将深入探讨PromptsModelsParsers模块的工作原理、功能和其对语言处理的重要性。什么是PromptsModels......