国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

自然語(yǔ)言處理從入門到應(yīng)用——LangChain:模型(Models)-[聊天模型(Chat Models):基礎(chǔ)知識(shí)]

這篇具有很好參考價(jià)值的文章主要介紹了自然語(yǔ)言處理從入門到應(yīng)用——LangChain:模型(Models)-[聊天模型(Chat Models):基礎(chǔ)知識(shí)]。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

分類目錄:《大模型從入門到應(yīng)用》總目錄

LangChain系列文章:

  • 基礎(chǔ)知識(shí)
  • 快速入門
    • 安裝與環(huán)境配置
    • 鏈(Chains)、代理(Agent:)和記憶(Memory)
    • 快速開發(fā)聊天模型
  • 模型(Models)
    • 基礎(chǔ)知識(shí)
    • 大型語(yǔ)言模型(LLMs)
      • 基礎(chǔ)知識(shí)
      • LLM的異步API、自定義LLM包裝器、虛假LLM和人類輸入LLM(Human Input LLM)
      • 緩存LLM的調(diào)用結(jié)果
      • 加載與保存LLM類、流式傳輸LLM與Chat Model響應(yīng)和跟蹤tokens使用情況
    • 聊天模型(Chat Models)
      • 基礎(chǔ)知識(shí)
      • 使用少量示例和響應(yīng)流式傳輸
    • 文本嵌入模型
      • Aleph Alpha、Amazon Bedrock、Azure OpenAI、Cohere等
      • Embaas、Fake Embeddings、Google Vertex AI PaLM等
  • 提示(Prompts)
    • 基礎(chǔ)知識(shí)
    • 提示模板
      • 基礎(chǔ)知識(shí)
      • 連接到特征存儲(chǔ)
      • 創(chuàng)建自定義提示模板和含有Few-Shot示例的提示模板
      • 部分填充的提示模板和提示合成
      • 序列化提示信息
    • 示例選擇器(Example Selectors)
    • 輸出解析器(Output Parsers)
  • 記憶(Memory)
    • 基礎(chǔ)知識(shí)
    • 記憶的類型
      • 會(huì)話緩存記憶、會(huì)話緩存窗口記憶和實(shí)體記憶
      • 對(duì)話知識(shí)圖譜記憶、對(duì)話摘要記憶和會(huì)話摘要緩沖記憶
      • 對(duì)話令牌緩沖存儲(chǔ)器和基于向量存儲(chǔ)的記憶
    • 將記憶添加到LangChain組件中
    • 自定義對(duì)話記憶與自定義記憶類
    • 聊天消息記錄
    • 記憶的存儲(chǔ)與應(yīng)用
  • 索引(Indexes)
    • 基礎(chǔ)知識(shí)
    • 文檔加載器(Document Loaders)
    • 文本分割器(Text Splitters)
    • 向量存儲(chǔ)器(Vectorstores)
    • 檢索器(Retrievers)
  • 鏈(Chains)
    • 基礎(chǔ)知識(shí)
    • 通用功能
      • 自定義Chain和Chain的異步API
      • LLMChain和RouterChain
      • SequentialChain和TransformationChain
      • 鏈的保存(序列化)與加載(反序列化)
    • 鏈與索引
      • 文檔分析和基于文檔的聊天
      • 問答的基礎(chǔ)知識(shí)
      • 圖問答(Graph QA)和帶來源的問答(Q&A with Sources)
      • 檢索式問答
      • 文本摘要(Summarization)、HyDE和向量數(shù)據(jù)庫(kù)的文本生成
  • 代理(Agents)
    • 基礎(chǔ)知識(shí)
    • 代理類型
    • 自定義代理(Custom Agent)
    • 自定義MRKL代理
    • 帶有ChatModel的LLM聊天自定義代理和自定義多操作代理(Custom MultiAction Agent)
    • 工具
      • 基礎(chǔ)知識(shí)
      • 自定義工具(Custom Tools)
      • 多輸入工具和工具輸入模式
      • 人工確認(rèn)工具驗(yàn)證和Tools作為OpenAI函數(shù)
    • 工具包(Toolkit)
    • 代理執(zhí)行器(Agent Executor)
      • 結(jié)合使用Agent和VectorStore
      • 使用Agents的異步API和創(chuàng)建ChatGPT克隆
      • 處理解析錯(cuò)誤、訪問中間步驟和限制最大迭代次數(shù)
      • 為代理程序設(shè)置超時(shí)時(shí)間和限制最大迭代次數(shù)和為代理程序和其工具添加共享內(nèi)存
    • 計(jì)劃與執(zhí)行
  • 回調(diào)函數(shù)(Callbacks)

聊天模型是語(yǔ)言模型的一種變體。雖然聊天模型在內(nèi)部使用語(yǔ)言模型,但它們公開的接口略有不同。它們不是提供一個(gè)“輸入文本,輸出文本”的API,而是提供一個(gè)以“聊天消息”作為輸入和輸出的接口。 聊天模型的API還比較新,因此我們?nèi)栽诖_定正確的抽象層次。本問將介紹如何開始使用聊天模型,該接口是基于消息而不是原始文本構(gòu)建的:

from langchain.chat_models import ChatOpenAI
from langchain import PromptTemplate, LLMChain
from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    AIMessagePromptTemplate,
    HumanMessagePromptTemplate,
)
from langchain.schema import (
    AIMessage,
    HumanMessage,
    SystemMessage
)
chat = ChatOpenAI(temperature=0)

通過向聊天模型傳遞一個(gè)或多個(gè)消息,可以獲取聊天完成的結(jié)果。響應(yīng)將是一個(gè)消息。LangChain目前支持的消息類型有AIMessage、HumanMessage、SystemMessageChatMessage,其中ChatMessage接受一個(gè)任意的角色參數(shù)。大多數(shù)情況下,我們只需要處理HumanMessageAIMessageSystemMessage

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

輸出:

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

OpenAI的聊天模型支持多個(gè)消息作為輸入。更多信息請(qǐng)參見這里。以下是向聊天模型發(fā)送系統(tǒng)消息和用戶消息的示例:

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={})

您還可以進(jìn)一步生成多組消息的完成結(jié)果,使用generate方法實(shí)現(xiàn)。該方法將返回一個(gè)帶有額外message參數(shù)的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中獲取諸如標(biāo)記使用情況之類的信息:

result.llm_output

輸出:

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

PromptTemplates

我們可以使用模板來構(gòu)建MessagePromptTemplate。我們可以從一個(gè)或多個(gè)MessagePromptTemplate構(gòu)建一個(gè)ChatPromptTemplate。我們還可以使用ChatPromptTemplateformat_prompt方法,它將返回一個(gè)PromptValue,我們可以將其轉(zhuǎn)換為字符串或消息對(duì)象,具體取決于我們是否希望將格式化后的值作為輸入傳遞給LLM或Chat模型的輸入。為了方便起見,模板上公開了一個(gè)from_template方法。如果您要使用此模板,代碼如下所示:

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])

# 獲取格式化后的消息的聊天完成結(jié)果
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={})

如果我們想直接更直接地構(gòu)建MessagePromptTemplate,我們可以在外部創(chuàng)建一個(gè)PromptTemplate,然后將其傳遞進(jìn)去,例如:

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)

LLMChain

我們可以以與以前非常相似的方式使用現(xiàn)有的LLMChain,即提供一個(gè)提示和一個(gè)模型:

chain = LLMChain(llm=chat, prompt=chat_prompt)
chain.run(input_language="English", output_language="French", text="I love programming.")

輸出:

"J'adore la programmation."

Streaming

通過回調(diào)處理,ChatOpenAI支持流式處理。

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

參考文獻(xiàn):
[1] LangChain ????? 中文網(wǎng),跟著LangChain一起學(xué)LLM/GPT開發(fā):https://www.langchain.com.cn/
[2] LangChain中文網(wǎng) - LangChain 是一個(gè)用于開發(fā)由語(yǔ)言模型驅(qū)動(dòng)的應(yīng)用程序的框架:http://www.cnlangchain.com/文章來源地址http://www.zghlxwxcb.cn/news/detail-607220.html

到了這里,關(guān)于自然語(yǔ)言處理從入門到應(yīng)用——LangChain:模型(Models)-[聊天模型(Chat Models):基礎(chǔ)知識(shí)]的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包