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

自然語言處理從入門到應(yīng)用——LangChain:記憶(Memory)-[自定義對(duì)話記憶與自定義記憶類]

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

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

LangChain系列文章:

  • 基礎(chǔ)知識(shí)
  • 快速入門
    • 安裝與環(huán)境配置
    • 鏈(Chains)、代理(Agent:)和記憶(Memory)
    • 快速開發(fā)聊天模型
  • 模型(Models)
    • 基礎(chǔ)知識(shí)
    • 大型語言模型(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ù)庫的文本生成
  • 代理(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)

自定義對(duì)話記憶

本節(jié)介紹了幾種自定義對(duì)話記憶的方法:

from langchain.llms import OpenAI
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory

llm = OpenAI(temperature=0)
AI前綴

第一種方法是通過更改對(duì)話摘要中的AI前綴來實(shí)現(xiàn)。默認(rèn)情況下,它設(shè)置為AI,但你可以將其設(shè)置為任何你想要的內(nèi)容。需要注意的是,如果我們更改了這個(gè)前綴,我們還應(yīng)該相應(yīng)地更改鏈條中使用的提示來反映這個(gè)命名更改。讓我們通過下面的示例來演示這個(gè)過程。

# Here it is by default set to "AI"
conversation = ConversationChain(
    llm=llm, 
    verbose=True, 
    memory=ConversationBufferMemory()
)
conversation.predict(input="Hi there!")

日志輸出:

> Entering new ConversationChain chain...
Prompt after formatting:
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.

Current conversation:

Human: Hi there!
AI:

> Finished ConversationChain chain.

輸出:

" Hi there! It's nice to meet you. How can I help you today?"

輸入:

conversation.predict(input="What's the weather?")

日志輸出:

> Entering new ConversationChain chain...
Prompt after formatting:
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.

Current conversation:

Human: Hi there!
AI:  Hi there! It's nice to meet you. How can I help you today?
Human: What's the weather?
AI:

> Finished ConversationChain chain.

輸出:

' The current weather is sunny and warm with a temperature of 75 degrees Fahrenheit. The forecast for the next few days is sunny with temperatures in the mid-70s.'

輸入:

# Now we can override it and set it to "AI Assistant"
from langchain.prompts.prompt import PromptTemplate

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.

Current conversation:
{history}
Human: {input}
AI Assistant:"""
PROMPT = PromptTemplate(
    input_variables=["history", "input"], template=template
)
conversation = ConversationChain(
    prompt=PROMPT,
    llm=llm, 
    verbose=True, 
    memory=ConversationBufferMemory(ai_prefix="AI Assistant")
)
conversation.predict(input="Hi there!")

日志輸出:

> Entering new ConversationChain chain...
Prompt after formatting:
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.

Current conversation:

Human: Hi there!
AI Assistant:

> Finished ConversationChain chain.
" Hi there! It's nice to meet you. How can I help you today?"
conversation.predict(input="What's the weather?")
> Entering new ConversationChain chain...
Prompt after formatting:
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.

Current conversation:

Human: Hi there!
AI Assistant:  Hi there! It's nice to meet you. How can I help you today?
Human: What's the weather?
AI Assistant:

> Finished ConversationChain chain.

輸出:

The current weather is sunny and warm with a temperature of 75 degrees Fahrenheit. The forecast for the rest of the day is sunny with a high of 78 degrees and a low of 65 degrees.'
人類前綴

第二種方法是通過更改對(duì)話摘要中的人類前綴來實(shí)現(xiàn)。默認(rèn)情況下,它設(shè)置為Human,但我們可以將其設(shè)置為任何我們想要的內(nèi)容。需要注意的是,如果我們更改了這個(gè)前綴,我們還應(yīng)該相應(yīng)地更改鏈條中使用的提示來反映這個(gè)命名更改。讓我們通過下面的示例來演示這個(gè)過程。

# Now we can override it and set it to "Friend"
from langchain.prompts.prompt import PromptTemplate

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.

Current conversation:
{history}
Friend: {input}
AI:"""
PROMPT = PromptTemplate(
    input_variables=["history", "input"], template=template
)
conversation = ConversationChain(
    prompt=PROMPT,
    llm=llm, 
    verbose=True, 
    memory=ConversationBufferMemory(human_prefix="Friend")
)
conversation.predict(input="Hi there!")

日志輸出:

> Entering new ConversationChain chain...
Prompt after formatting:
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.

Current conversation:

Friend: Hi there!
AI:

> Finished ConversationChain chain.
" Hi there! It's nice to meet you. How can I help you today?"
conversation.predict(input="What's the weather?")
> Entering new ConversationChain chain...
Prompt after formatting:
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.

Current conversation:

Friend: Hi there!
AI:  Hi there! It's nice to meet you. How can I help you today?
Friend: What's the weather?
AI:

> Finished ConversationChain chain.

輸出:

  ' The weather right now is sunny and warm with a temperature of 75 degrees Fahrenheit. The forecast for the rest of the day is mostly sunny with a high of 82 degrees.'

創(chuàng)建自定義記憶類

盡管在LangChain中有幾種預(yù)定義的記憶類型,但我們很可能希望添加自己的記憶類型,以使其適用于我們的應(yīng)用程序。在本節(jié)中,我們將向ConversationChain添加一個(gè)自定義的記憶類型。為了添加自定義的記憶類,我們需要導(dǎo)入基本的記憶類并對(duì)其進(jìn)行子類化。

from langchain import OpenAI, ConversationChain
from langchain.schema import BaseMemory
from pydantic import BaseModel
from typing import List, Dict, Any

在這個(gè)示例中,我們將編寫一個(gè)自定義的記憶類,使用spacy提取實(shí)體并將有關(guān)它們的信息保存在一個(gè)簡(jiǎn)單的哈希表中。然后,在對(duì)話過程中,我們將查看輸入文本,提取任何實(shí)體,并將關(guān)于它們的任何信息放入上下文中。需要注意的是,這種實(shí)現(xiàn)相當(dāng)簡(jiǎn)單且脆弱,可能在生產(chǎn)環(huán)境中不太有用。它的目的是展示我們可以添加自定義的記憶實(shí)現(xiàn)。為此,我們需要首先安裝spacy

# !pip install spacy
# !python -m spacy download en_core_web_lg
import spacy
nlp = spacy.load('en_core_web_lg')
class SpacyEntityMemory(BaseMemory, BaseModel):
    """Memory class for storing information about entities."""

    # Define dictionary to store information about entities.
    entities: dict = {}
    # Define key to pass information about entities into prompt.
    memory_key: str = "entities"
        
    def clear(self):
        self.entities = {}

    @property
    def memory_variables(self) -> List[str]:
        """Define the variables we are providing to the prompt."""
        return [self.memory_key]

    def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]:
        """Load the memory variables, in this case the entity key."""
        # Get the input text and run through spacy
        doc = nlp(inputs[list(inputs.keys())[0]])
        # Extract known information about entities, if they exist.
        entities = [self.entities[str(ent)] for ent in doc.ents if str(ent) in self.entities]
        # Return combined information about entities to put into context.
        return {self.memory_key: "\n".join(entities)}

    def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:
        """Save context from this conversation to buffer."""
        # Get the input text and run through spacy
        text = inputs[list(inputs.keys())[0]]
        doc = nlp(text)
        # For each entity that was mentioned, save this information to the dictionary.
        for ent in doc.ents:
            ent_str = str(ent)
            if ent_str in self.entities:
                self.entities[ent_str] += f"\n{text}"
            else:
                self.entities[ent_str] = text

我們現(xiàn)在定義一個(gè)提示,其中包含有關(guān)實(shí)體的信息以及用戶的輸入:

from langchain.prompts.prompt import PromptTemplate

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. You are provided with information about entities the Human mentions, if relevant.

Relevant entity information:
{entities}

Conversation:
Human: {input}
AI:"""
prompt = PromptTemplate(
    input_variables=["entities", "input"], template=template
)

現(xiàn)在,我們把它們整合起來:

llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=llm, prompt=prompt, verbose=True, memory=SpacyEntityMemory())

在第一個(gè)例子中,由于對(duì)Harrison沒有先前的了解,"Relevant entity information"部分是空的:

conversation.predict(input="Harrison likes machine learning")

日志輸出:

> Entering new ConversationChain chain...
Prompt after formatting:
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. You are provided with information about entities the Human mentions, if relevant.

Relevant entity information:


Conversation:
Human: Harrison likes machine learning
AI:

> Finished ConversationChain chain.

輸出:

" That's great to hear! Machine learning is a fascinating field of study. It involves using algorithms to analyze data and make predictions. Have you ever studied machine learning, Harrison?"

現(xiàn)在在第二個(gè)例子中,我們可以看到它提取了關(guān)于Harrison的信息。

conversation.predict(input="What do you think Harrison's favorite subject in college was?")

日志輸出:

> Entering new ConversationChain chain...
Prompt after formatting:
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. You are provided with information about entities the Human mentions, if relevant.

Relevant entity information:
Harrison likes machine learning

Conversation:
Human: What do you think Harrison's favorite subject in college was?
AI:

> Finished ConversationChain chain.

輸出:

' From what I know about Harrison, I believe his favorite subject in college was machine learning. He has expressed a strong interest in the subject and has mentioned it often.'

這個(gè)實(shí)現(xiàn)方式相對(duì)簡(jiǎn)單且容易出錯(cuò),可能在實(shí)際生產(chǎn)環(huán)境中沒有太大的用途,但它展示了我們可以添加自定義的內(nèi)存實(shí)現(xiàn)方式。

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

到了這里,關(guān)于自然語言處理從入門到應(yīng)用——LangChain:記憶(Memory)-[自定義對(duì)話記憶與自定義記憶類]的文章就介紹完了。如果您還想了解更多內(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)文章

  • 自然語言處理從入門到應(yīng)用——LangChain:記憶(Memory)-[聊天消息記錄]

    分類目錄:《大模型從入門到應(yīng)用》總目錄 LangChain系列文章: 基礎(chǔ)知識(shí) 快速入門 安裝與環(huán)境配置 鏈(Chains)、代理(Agent:)和記憶(Memory) 快速開發(fā)聊天模型 模型(Models) 基礎(chǔ)知識(shí) 大型語言模型(LLMs) 基礎(chǔ)知識(shí) LLM的異步API、自定義LLM包裝器、虛假LLM和人類輸入LLM(

    2024年02月12日
    瀏覽(56)
  • 自然語言處理從入門到應(yīng)用——LangChain:記憶(Memory)-[基礎(chǔ)知識(shí)]

    分類目錄:《大模型從入門到應(yīng)用》總目錄 LangChain系列文章: 基礎(chǔ)知識(shí) 快速入門 安裝與環(huán)境配置 鏈(Chains)、代理(Agent:)和記憶(Memory) 快速開發(fā)聊天模型 模型(Models) 基礎(chǔ)知識(shí) 大型語言模型(LLMs) 基礎(chǔ)知識(shí) LLM的異步API、自定義LLM包裝器、虛假LLM和人類輸入LLM(

    2024年02月14日
    瀏覽(61)
  • 自然語言處理從入門到應(yīng)用——LangChain:記憶(Memory)-[自定義對(duì)話記憶與自定義記憶類]

    分類目錄:《大模型從入門到應(yīng)用》總目錄 LangChain系列文章: 基礎(chǔ)知識(shí) 快速入門 安裝與環(huán)境配置 鏈(Chains)、代理(Agent:)和記憶(Memory) 快速開發(fā)聊天模型 模型(Models) 基礎(chǔ)知識(shí) 大型語言模型(LLMs) 基礎(chǔ)知識(shí) LLM的異步API、自定義LLM包裝器、虛假LLM和人類輸入LLM(

    2024年02月13日
    瀏覽(51)
  • 自然語言處理從入門到應(yīng)用——LangChain:快速入門-[快速開發(fā)聊天模型]

    分類目錄:《大模型從入門到應(yīng)用》總目錄 LangChain系列文章: 基礎(chǔ)知識(shí) 快速入門 安裝與環(huán)境配置 鏈(Chains)、代理(Agent:)和記憶(Memory) 快速開發(fā)聊天模型 模型(Models) 基礎(chǔ)知識(shí) 大型語言模型(LLMs) 基礎(chǔ)知識(shí) LLM的異步API、自定義LLM包裝器、虛假LLM和人類輸入LLM(

    2024年02月15日
    瀏覽(68)
  • 自然語言處理從入門到應(yīng)用——LangChain:快速入門-[安裝與環(huán)境配置]

    分類目錄:《大模型從入門到應(yīng)用》總目錄 LangChain系列文章: 基礎(chǔ)知識(shí) 快速入門 安裝與環(huán)境配置 鏈(Chains)、代理(Agent:)和記憶(Memory) 快速開發(fā)聊天模型 模型(Models) 基礎(chǔ)知識(shí) 大型語言模型(LLMs) 基礎(chǔ)知識(shí) LLM的異步API、自定義LLM包裝器、虛假LLM和人類輸入LLM(

    2024年02月13日
    瀏覽(129)
  • 自然語言處理從入門到應(yīng)用——LangChain:基礎(chǔ)知識(shí)與介紹

    分類目錄:《大模型從入門到應(yīng)用》總目錄 LangChain系列文章: 基礎(chǔ)知識(shí) 快速入門 安裝與環(huán)境配置 鏈(Chains)、代理(Agent:)和記憶(Memory) 快速開發(fā)聊天模型 模型(Models) 基礎(chǔ)知識(shí) 大型語言模型(LLMs) 基礎(chǔ)知識(shí) LLM的異步API、自定義LLM包裝器、虛假LLM和人類輸入LLM(

    2024年02月13日
    瀏覽(107)
  • 自然語言處理從入門到應(yīng)用——LangChain:代理(Agents)-[代理類型]

    分類目錄:《大模型從入門到應(yīng)用》總目錄 LangChain系列文章: 基礎(chǔ)知識(shí) 快速入門 安裝與環(huán)境配置 鏈(Chains)、代理(Agent:)和記憶(Memory) 快速開發(fā)聊天模型 模型(Models) 基礎(chǔ)知識(shí) 大型語言模型(LLMs) 基礎(chǔ)知識(shí) LLM的異步API、自定義LLM包裝器、虛假LLM和人類輸入LLM(

    2024年02月15日
    瀏覽(63)
  • 自然語言處理從入門到應(yīng)用——LangChain:索引(Indexes)-[基礎(chǔ)知識(shí)]

    分類目錄:《大模型從入門到應(yīng)用》總目錄 LangChain系列文章: 基礎(chǔ)知識(shí) 快速入門 安裝與環(huán)境配置 鏈(Chains)、代理(Agent:)和記憶(Memory) 快速開發(fā)聊天模型 模型(Models) 基礎(chǔ)知識(shí) 大型語言模型(LLMs) 基礎(chǔ)知識(shí) LLM的異步API、自定義LLM包裝器、虛假LLM和人類輸入LLM(

    2024年02月12日
    瀏覽(64)
  • 自然語言處理從入門到應(yīng)用——LangChain:代理(Agents)-[基礎(chǔ)知識(shí)]

    分類目錄:《大模型從入門到應(yīng)用》總目錄 LangChain系列文章: 基礎(chǔ)知識(shí) 快速入門 安裝與環(huán)境配置 鏈(Chains)、代理(Agent:)和記憶(Memory) 快速開發(fā)聊天模型 模型(Models) 基礎(chǔ)知識(shí) 大型語言模型(LLMs) 基礎(chǔ)知識(shí) LLM的異步API、自定義LLM包裝器、虛假LLM和人類輸入LLM(

    2024年02月13日
    瀏覽(75)
  • 自然語言處理從入門到應(yīng)用——LangChain:鏈(Chains)-[基礎(chǔ)知識(shí)]

    分類目錄:《大模型從入門到應(yīng)用》總目錄 LangChain系列文章: 基礎(chǔ)知識(shí) 快速入門 安裝與環(huán)境配置 鏈(Chains)、代理(Agent:)和記憶(Memory) 快速開發(fā)聊天模型 模型(Models) 基礎(chǔ)知識(shí) 大型語言模型(LLMs) 基礎(chǔ)知識(shí) LLM的異步API、自定義LLM包裝器、虛假LLM和人類輸入LLM(

    2024年02月12日
    瀏覽(64)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包