LangChain에서 메모리와 스토리지를 활용하는 LangChain 부분: 종합 가이드

WBOY
풀어 주다: 2024-08-21 22:14:25
원래의
937명이 탐색했습니다.

LangChain Part  Leveraging Memory and Storage in LangChain: A Comprehensive Guide

LangChain Part 4 - Leveraging Memory and Storage in LangChain: A Comprehensive Guide

Code can be found here: GitHub - jamesbmour/blog_tutorials:

In the ever-evolving world of conversational AI and language models, maintaining context and efficiently managing information flow are critical components of building intelligent applications. LangChain, a powerful framework designed for working with large language models (LLMs), offers robust tools for memory management and data persistence, enabling the creation of context-aware systems.

In this guide, we'll delve into the nuances of leveraging memory and storage in LangChain to build smarter, more responsive applications.

1. Working with Memory in LangChain

Memory management in LangChain allows applications to retain context, making interactions more coherent and contextually relevant. Let’s explore the different memory types and their use cases.

1.1. Types of Memory

LangChain provides various memory types to address different scenarios. Here, we’ll focus on two key types:

ConversationBufferMemory

This memory type is ideal for short-term context retention, capturing and recalling recent interactions in a conversation.

from langchain.memory import ConversationBufferMemory

memory = ConversationBufferMemory()
memory.save_context({"input": "Hi, I'm Alice"}, {"output": "Hello Alice, how can I help you today?"})
memory.save_context({"input": "What's the weather like?"}, {"output": "I'm sorry, I don't have real-time weather information. Is there anything else I can help you with?"})

print(memory.load_memory_variables({}))

로그인 후 복사

ConversationSummaryMemory

For longer conversations, ConversationSummaryMemory is a great choice. It summarizes key points, maintaining context without overwhelming detail.

from langchain.memory import ConversationSummaryMemory
from langchain.llms import Ollama 

llm = Ollama(model='phi3',temperature=0)
memory = ConversationSummaryMemory(llm=llm)
memory.save_context({"input": "Hi, I'm Alice"}, {"output": "Hello Alice, how can I help you today?"})
memory.save_context({"input": "I'm looking for a good Italian restaurant"}, {"output": "Great! I'd be happy to help you find a good Italian restaurant. Do you have any specific preferences or requirements, such as location, price range, or specific dishes you're interested in?"})

print(memory.load_memory_variables({}))

로그인 후 복사

1.2. Choosing the Right Memory Type for Your Use Case

Selecting the appropriate memory type depends on several factors:

  • Duration and Complexity: Short sessions benefit from detailed context retention with ConversationBufferMemory, while long-term interactions may require summarization via ConversationSummaryMemory.
  • Detail vs. Overview: Determine whether detailed interaction history or high-level summaries are more valuable for your application.
  • Performance: Consider the trade-offs between the memory size and retrieval speed.

Use Cases:

  • ConversationBufferMemory: Ideal for quick customer support or FAQ-style interactions.
  • ConversationSummaryMemory: Best suited for long-term engagements like project management or ongoing customer interactions.

1.3. Integrating Memory into Chains and Agents

Memory can be seamlessly integrated into LangChain chains and agents to enhance conversational capabilities.

from langchain.chains import ConversationChain  
from langchain.memory import ConversationBufferMemory
# llm = OpenAI(temperature=0)
memory = ConversationBufferMemory()
conversation = ConversationChain(
    llm=llm,
    memory=memory,
    verbose=True
)

conversation.predict(input="Hi, I'm Alice")
conversation.predict(input="What's my name?")

로그인 후 복사

This example illustrates how ConversationBufferMemory can be used to remember previous interactions, enabling more natural conversations.

2. Persisting and Retrieving Data

Persistent storage ensures that conversation history and context are maintained across sessions, enabling continuity in interactions.

2.1. Storing Conversation History and State

For basic persistence, you can use file-based storage with JSON:

import json

class PersistentMemory:
    def __init__(self, file_path):
        self.file_path = file_path
        self.load_memory()

    def load_memory(self):
        try:
            with open(self.file_path, 'r') as f:
                self.chat_memory = json.load(f)
        except FileNotFoundError:
            self.chat_memory = {'messages': []}

    def save_memory(self):
        with open(self.file_path, 'w') as f:
            json.dump({'messages': self.chat_memory['messages']}, f)

# Usage
memory = PersistentMemory(file_path='conversation_history.json')
print(memory.chat_memory)
로그인 후 복사

This method allows you to persist conversation history in a simple, human-readable format.

2.2. Integrating with Databases and Storage Systems

For more scalable and efficient storage, integrating with databases like SQLite is recommended:

import sqlite3

class SQLiteMemory:
    def __init__(self, db_path):
        self.db_path = db_path
        self.conn = sqlite3.connect(db_path)
        self.create_table()

    def create_table(self):
        cursor = self.conn.cursor()
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS conversations
            (id INTEGER PRIMARY KEY, input TEXT, output TEXT)
        ''')
        self.conn.commit()

    def save_context(self, inputs, outputs):
        cursor = self.conn.cursor()
        cursor.execute('INSERT INTO conversations (input, output) VALUES (?, ?)',
                       (inputs['input'], outputs['output']))
        self.conn.commit()

    def load_memory_variables(self, inputs):
        cursor = self.conn.cursor()
        cursor.execute('SELECT input, output FROM conversations ORDER BY id DESC LIMIT 10')
        rows = cursor.fetchall()
        history = "\\n".join([f"Human: {row[0]}\\nAI: {row[1]}" for row in reversed(rows)])
        return {"history": history }

# Usage
memory = SQLiteMemory('conversation_history.db')

print(memory.load_memory_variables({}))
로그인 후 복사

3 Optimizing Memory Usage and Performance

To ensure your application remains responsive, consider these optimization strategies:

  • Efficient Data Structures: Use structures like deque for managing fixed-size buffers.
  • Caching Strategies: Reduce database queries by implementing caching for frequently accessed data.
  • Data Pruning: Regularly prune or summarize old data to maintain a manageable memory size.

Here’s an example of a memory class with basic caching:

import time

class CachedSQLiteMemory(SQLiteMemory):
    def __init__(self, db_path, cache_ttl=60):
        super().__init__(db_path)
        self.cache = None
        self.cache_time = 0
        self.cache_ttl = cache_ttl

    def load_memory_variables(self, inputs):
        current_time = time.time()
        if self.cache is None or (current_time - self.cache_time) > self.cache_ttl:
            var = self.cache
            self.cache = super().load_memory_variables(inputs)
            self.cache_time = current_time
            return self.cache

memory = CachedSQLiteMemory('conversation_history.db', cache_ttl=30)
로그인 후 복사

This implementation caches the results of database queries for a specified time, reducing the load on the database and improving performance for applications that frequently access memory data.

Conclusion

Effective memory management is a cornerstone of building intelligent, context-aware conversational AI applications. LangChain provides a flexible and powerful framework for managing memory, allowing developers to tailor memory types to specific use cases, implement persistent storage solutions, and optimize performance for large-scale applications.

Dengan memilih jenis memori yang betul, menyepadukan storan berterusan dan memanfaatkan teknik lanjutan seperti kelas memori tersuai dan strategi caching, anda boleh membina sistem AI canggih yang mengekalkan konteks, meningkatkan pengalaman pengguna dan beroperasi dengan cekap walaupun mengikut skala dan kerumitan. interaksi berkembang.

Dengan alatan dan teknik ini yang anda boleh gunakan, anda dilengkapi dengan baik untuk memanfaatkan potensi penuh LangChain dalam mencipta aplikasi AI yang responsif, pintar dan peka terhadap konteks. Sama ada anda sedang membangunkan bot sokongan pelanggan, pembantu maya atau sistem perbualan yang kompleks, menguasai memori dan storan dalam LangChain akan menjadi faktor utama kejayaan anda.

Jika anda ingin menyokong penulisan saya atau belikan saya bir:
https://buymeacoffee.com/bmours

위 내용은 LangChain에서 메모리와 스토리지를 활용하는 LangChain 부분: 종합 가이드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿