-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fixed webloader bug * Added memory component and Chat Buffer memory
- Loading branch information
1 parent
a50f19b
commit 75b9f2e
Showing
4 changed files
with
93 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .chatBufferMemory import ChatBufferMemory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import Any, Dict, List, Optional | ||
from abc import ABC, abstractmethod | ||
from pydantic import BaseModel | ||
|
||
class MemoryConfig(BaseModel): | ||
"""Base configuration model for all memory components. | ||
This class can be extended to include more fields specific to certain memory components. | ||
""" | ||
pass | ||
|
||
class BaseMemory(ABC): | ||
""" | ||
Base class for memory components. | ||
""" | ||
|
||
@abstractmethod | ||
def load_memory(self, **kwargs) -> None: | ||
"""Loads the memory component.""" | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def add_to_memory(self, context: str, response: str) -> None: | ||
"""Adds the conversation context and response to memory.""" | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def get_memory(self) -> Any: | ||
"""Returns the current memory.""" | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def clear_memory(self) -> None: | ||
"""Clears the memory.""" | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from dataclasses import dataclass, field | ||
from typing import List, Optional, Dict | ||
from .base import BaseMemory, MemoryConfig | ||
import re | ||
|
||
@dataclass | ||
class ChatBufferMemory(BaseMemory): | ||
""" | ||
ChatBufferMemory stores the recent conversation history as a buffer. | ||
Args: | ||
window_size (int): The maximum number of messages to store in the buffer. | ||
memory_key (str): The key to identify the memory. | ||
max_tokens (int): The maximum number of tokens to store in the buffer. Defaults to None, which means no limit. | ||
""" | ||
|
||
window_size: int = 5 | ||
memory_key: str = "chat_buffer" | ||
_buffer: List[Dict[str, str]] = field(default_factory=list) | ||
config: MemoryConfig = field(default_factory=MemoryConfig) | ||
|
||
def load_memory(self, **kwargs) -> None: | ||
self._buffer = [] | ||
self.config = MemoryConfig(**kwargs) | ||
|
||
def add_to_memory(self, question: str, response: str) -> None: | ||
"""Adds the context and response to the buffer, maintaining the window size.""" | ||
self._buffer.append( | ||
{"question": question, "response": response} | ||
) | ||
|
||
if len(self._buffer) > self.window_size+1: | ||
self._buffer.pop(0) | ||
|
||
def get_memory(self) -> List[Dict[str, str]]: | ||
"""Returns the current buffer.""" | ||
return self._buffer | ||
|
||
def clear_memory(self) -> None: | ||
"""Clears the buffer.""" | ||
self._buffer = [] |