Introducción a LangChain

1

Desarrollo de aplicaciones con LLM utilizando LangChain

2

Estructura y módulos de LangChain

3

Uso de modelos Open Source de Hugging Face

4

Uso de modelos de OpenAI API

5

Prompt templates de LangChain

6

Cadenas en LangChain

7

Utility chains

8

RetrievalQA chain

9

Foundational chains

Quiz: Introducción a LangChain

Casos de uso de LangChain

10

Casos de uso de LangChain

11

¿Cómo utilizar LangChain en mi equipo?

Quiz: Casos de uso de LangChain

Manejo de documentos con índices

12

¿Cómo manejar documentos con índices en LangChain?

13

La clase Document

14

Document Loaders: PDF

15

Document Loaders: CSV con Pandas DataFrames

16

Document Loaders: JSONL

17

Document Transformers: TextSplitters

18

Proyecto de Chatbot: configuración de entorno para LangChain y obtención de datos

19

Proyecto de Chatbot: creación de documents de Hugging Face

Quiz: Manejo de documentos con índices

Embeddings y bases de datos vectoriales

20

Uso de embeddings y bases de datos vectoriales con LangChain

21

¿Cómo usar embeddings de OpenAI en LangChain?

22

¿Cómo usar embeddings de Hugging Face en LangChaing?

23

Chroma vector store en LangChain

24

Proyecto de Chatbot: ingesta de documents en Chroma

25

RetrievalQA: cadena para preguntar

26

Proyecto de Chatbot: cadena de conversación

27

Proyecto de Chatbot: RetrievalQA chain

Quiz: Embeddings y bases de datos vectoriales

Chats y memoria con LangChain

28

¿Para qué sirve la memoria en cadenas y chats?

29

Uso de modelos de chat con LangChain

30

Chat prompt templates

31

ConversationBufferMemory

32

ConversationBufferWindowMemory

33

ConversationSummaryMemory

34

ConversationSummaryBufferMemory

35

Entity memory

36

Proyecto de Chatbot: chat history con ConversationalRetrievalChain

Quiz: Chats y memoria con LangChain

Evolución del uso de LLM

37

LangChain y LLM en evolución constante

You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

1 Días
17 Hrs
26 Min
7 Seg
Curso de LangChain

Curso de LangChain

Omar Espejel

Omar Espejel

ConversationBufferWindowMemory

32/37
Resources

What is chat window memory?

In the world of chatbot development, it is critical to understand how chatbots remember and respond to previous interactions. One efficient technique for managing memory is the use of ConversationBufferWindowMemory. This option allows storing only a specific number of interactions, keeping only the most recent in memory. The buffer window technology is essential for those cases where resource management is critical, such as in extensive interactions with language models.

How is window memory implemented in a chatbot?

To implement this memory, it is necessary to create an instance of ConversationBufferWindowMemory from a specific memory library. The parameter K defines how many interactions will be stored in memory. It is recommended that the number of interactions to be remembered depends on the specific context of the chatbot being developed:

  • Short interactions: if the chatbot is used for short interactions, it may be sufficient to remember only 2 or 3 messages.
  • Long interactions: For more advanced applications, up to 5 or 6 messages might be remembered.

What are the benefits of window memory?

The use of a well-configured window memory brings multiple benefits:

  1. Resource optimization: By limiting the amount of data in memory, unnecessary processing of old information is avoided, optimizing the use of the chatbot.
  2. Reduced cost: In services such as OpenAI, reducing the number of tokens loaded into the model can reduce the costs associated with large requests.
  3. Fast response: By processing less information, the chatbot can provide answers in a more agile and efficient manner.
from memory_library import ConversationBufferWindowMemory
 # Create an instance of window memory window_memory = ConversationBufferWindowMemory(K=3) # For example, remember the 3 most recent interactions.

How to integrate a window memory with a language model?

Once the window buffer is configured, the next step is to integrate it with the language model. We are going to use a pre-instantiated chat model, such as OpenAI's GPT-3.5 Turbo. The process is straightforward, and ensuring that the model is correctly configured to take advantage of the memory is crucial.

  1. Create a new conversation string: This involves instantiating a ConversationChain where you specify the language model and the memory to use.
  2. Configure verbose: Setting verbose to true can be useful in development phases to verify how the model processes messages and how memory stores interactions.
  3. Initiate the conversation: Using the predict method, you can converse with the chatbot, verifying how it responds and how it enriches the stored interactions.
# Create a conversation chain with the model and memoryconversation = ConversationChain(language_model=gpt_3_5_turbo, verbose=True,  memory=window_memory)
 # Start interactionresponse = conversation.predict(input="What's up? How are you? I'm Omar and I write very colloquial.")print(response)

What to keep in mind when setting up a window memory?

When working with window memories, it is fundamental to consider the balance between memory depth and cost:

  • Short memory: ideal for transient and direct interactions.
  • Long memory: Can provide a richer conversation, but carries a higher processing cost.

It is essential to define the type of conversation desired with the chatbot. This will influence both the design and the resources required for its operation. Considering these aspects from the beginning will facilitate a more efficient and effective development.

Contributions 3

Questions 1

Sort by:

Want to see more contributions, questions and answers from the community?

Todos estos métodos están deprecados, ahora se usa LangGraph, a partir de este punto solo pude ser oyente, tomar notas pero no pude seguir el curso con código.
Tener en cuenta que el `K`, o la `window`, son sólo de las interacciones del **user**, **no** cuenta las de **ia**.

📝 Sesgo de Memorización

ℹ️ Summary

Esta guía proporciona un caso de uso sobre “Conversation Buffer Window Memory”, para tener conversaciones sesgadas en interacciones.
.

🗂️ Background

En LangChain, se puede implementa:
.

  • Un sistema de memoria muy simple que podría devolver los mensajes más recientes en cada ejecución.
  • Un sistema de memoria algo más complejo que podría devolver un resumen sucinto de los últimos K mensajes.
  • Un sistema aún más sofisticado podría extraer entidades de los mensajes almacenados y sólo devolver información sobre las entidades referenciadas en la ejecución actual.

.
Cada aplicación puede tener diferentes requisitos en cuanto a la forma de consultar la memoria. El módulo de memoria debería facilitar tanto la puesta en marcha de sistemas de memoria sencillos como la escritura de sistemas personalizados si fuera necesario.
.
Enlaces auxiliares:

.

🚧 Solution

ConversationBufferWindowMemory mantiene una lista de las interacciones de la conversación a lo largo del tiempo. Sólo utiliza las últimas k interacciones.

import { BufferWindowMemory, ChatMessageHistory } from 'langchain/memory'
import { HumanMessage, AIMessage } from 'langchain/schema'

const chatHistory = new ChatMessageHistory([
	new HumanMessage("Hi! I'm Jim."),
	new AIMessage(
		"Hi Jim! It's nice to meet you. My name is AI. What would you like to talk about?",
	),
])

const memory = new BufferWindowMemory({
	k: 1,
	chatHistory,
})

Con lo anterior, se agregó de un historial de conversación al LLM. Para darle de contexto y efectuar correctamente la siguiente tarea o pregunta. Sin embargo, después de algunas interacciones, debido a los k o mensajes pasados, olvide su contexto.

import { ConversationChain } from 'langchain/chains'

const chain = new ConversationChain({
	llm,
	memory,
})

let response = await chain.call({ input: 'What is it a LLM?' })
console.log(response)

response = await chain.call({ input: 'What is it OpenAI?' })
console.log(response)

response = await chain.call({ input: "What's my name?" })
console.log(response)

🍻 Discussion

ConversationBufferWindowMemory es un tipo de memorización útil para mantener una ventana deslizante de las interacciones más recientes, para que el buffer no se haga demasiado grande.

import { ConversationChain } from 'langchain/chains'
import { ChatOpenAI } from 'langchain/chat_models/openai'
import { BufferWindowMemory, ChatMessageHistory } from 'langchain/memory'
import { HumanMessage, AIMessage } from 'langchain/schema'

const API_TOKEN = // 👈 Enter the API Token from OpenAI

const llm = new ChatOpenAI({
	maxTokens: -1,
	modelName: 'gpt-4',
	temperature: 0,
	openAIApiKey: API_TOKEN,
})

const chatHistory = new ChatMessageHistory([
	new HumanMessage("Hi! I'm Jim."),
	new AIMessage(
		"Hi Jim! It's nice to meet you. My name is AI. What would you like to talk about?",
	),
])

const memory = new BufferWindowMemory({
	k: 1,
	chatHistory,
})

const chain = new ConversationChain({
	llm,
	memory,
	prompt,
})

let response = await chain.call({ input: 'What is it a LLM?' })
console.log(response)

response = await chain.call({ input: 'What is it OpenAI?' })
console.log(response)

response = await chain.call({ input: "What's my name?" })
console.log(response)