langchain中的chat?models介紹和使用實(shí)例
簡介
之前我們介紹了LLM模式,這種模式是就是文本輸入,然后文本輸出。
chat models是基于LLM模式的更加高級的模式。他的輸入和輸出是格式化的chat messages。
一起來看看如何在langchain中使用caht models吧。
chat models的使用
首先langchain對chat models下支持的模型就少很多了。一方面是可能有些語言模型本身是不支持chat models的。另外一方面langchain也還是在一個(gè)發(fā)展中的過程,所以有些模型還需要適配。
目前看來langchain支持的chat models有:ChatAnthropic,AzureChatOpenAI,ChatVertexAI,JinaChat,ChatOpenAI和PromptLayerChatOpenAI這幾種。
langchain把chat消息分成了這幾種:AIMessage, HumanMessage, SystemMessage 和 ChatMessage。
HumanMessage就是用戶輸入的消息,AIMessage是大語言模型的消息,SystemMessage是系統(tǒng)的消息。ChatMessage是一種可以自定義類型的消息。
在使用的時(shí)候,只需要在chat中傳入對應(yīng)的消息即可:
from langchain.chat_models import ChatOpenAI
chat = ChatOpenAI()
messages = [
SystemMessage(content="你是一個(gè)小說家"),
HumanMessage(content="幫我寫篇小說")
]
chat(messages)當(dāng)然和LLM一樣,你也可以使用批量模式如下:
batch_messages = [
[
SystemMessage(content="你是一個(gè)小說家"),
HumanMessage(content="幫我寫篇小說")
],
[
SystemMessage(content="你是一個(gè)詩人"),
HumanMessage(content="幫我寫首詩")
],
]
result = chat.generate(batch_messages)
resultchat models的高級功能
其實(shí)和LLM類似,基本上LLM有的高級功能chat models都有。
比如有用的比如緩存功能,可以緩存之前的輸入和輸出,避免每次都調(diào)用LLM,從而可以減少token的開銷。
以InMemoryCache為例子:
from langchain.cache import InMemoryCache
langchain.llm_cache = InMemoryCache()
# 第一次調(diào)用,不是用cache
llm.predict("Tell me a joke")
# 第二次調(diào)用,使用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="幫忙我寫首詩")])只需要在構(gòu)建ChatOpenAI的時(shí)候,把StreamingStdOutCallbackHandler傳入callbacks即可。
如果要在chat models中使用PromptTemplate,因?yàn)閏hat models的消息格式跟LLM是不一樣的,所以對應(yīng)的PromptTemplate也是不一樣的。
和對應(yīng)的chat models消息對應(yīng)的PromptTemplate是ChatPromptTemplate,SystemMessagePromptTemplate,
AIMessagePromptTemplate和HumanMessagePromptTemplate。
我們看下是如何使用prompt template來構(gòu)建prompt:
from langchain import PromptTemplate
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
AIMessagePromptTemplate,
HumanMessagePromptTemplate,
)
# 構(gòu)建各種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下消息構(gòu)建確實(shí)比直接使用LLM要復(fù)雜點(diǎn),大家在使用的時(shí)候需要注意。
總結(jié)
chat models是LLM的高階表現(xiàn)形式。如果我們需要進(jìn)行對話模型的話,就可以考慮使用這個(gè)。
相關(guān)文章
python連接clickhouse數(shù)據(jù)庫的兩種方式小結(jié)
這篇文章主要介紹了python連接clickhouse數(shù)據(jù)庫的兩種方式小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
淺談python對象數(shù)據(jù)的讀寫權(quán)限
下面小編就為大家?guī)硪黄獪\談python對象數(shù)據(jù)的讀寫權(quán)限。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
二種python發(fā)送郵件實(shí)例講解(python發(fā)郵件附件可以使用email模塊實(shí)現(xiàn))
這篇文章主要介紹了使用Python email模塊、smtplib庫發(fā)送郵件的實(shí)例,大家參考使用2013-12-12
python算法學(xué)習(xí)之桶排序算法實(shí)例(分塊排序)
本代碼介紹了python算法學(xué)習(xí)中的桶排序算法實(shí)例,大家參考使用吧2013-12-12
如何基于windows實(shí)現(xiàn)python定時(shí)爬蟲
這篇文章主要介紹了如何基于windows實(shí)現(xiàn)python定時(shí)爬蟲,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Python實(shí)現(xiàn)計(jì)算長方形面積(帶參數(shù)函數(shù)demo)
今天小編就為大家分享一篇Python實(shí)現(xiàn)計(jì)算長方形面積(帶參數(shù)函數(shù)demo),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python實(shí)現(xiàn)將xml導(dǎo)入至excel
本文給大家講解的是使用Python的Testlink實(shí)現(xiàn)將實(shí)現(xiàn)將xml導(dǎo)入至excel表格中,方法非常的簡單,另外附上其他小伙伴的方法,有需要的童鞋們可以參考下。2015-11-11
python3之讀取redis數(shù)據(jù)帶有‘b’的問題
這篇文章主要介紹了python3之讀取redis數(shù)據(jù)帶有‘b’的問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Python可視化學(xué)習(xí)之seaborn調(diào)色盤
seaborn是在matplotlib基礎(chǔ)上封裝的,所以matplotlib的調(diào)色盤seaborn都可以使用。本文系統(tǒng)介紹seaborn調(diào)色盤,相較于matplotlib,有諸多不同,需要的可以參考一下2022-02-02

