1
1
import time
2
- from typing import List , Optional , Sequence , Set , Union
2
+ from typing import List , Optional , Sequence , Set
3
3
4
4
from langchain_core .chat_history import BaseChatMessageHistory
5
5
from langchain_core .messages import BaseMessage
6
- from pydantic import BaseModel , PrivateAttr
7
6
8
7
from .models import HumanChatMessage
9
8
10
9
HUMAN_MSG_ID_KEY = "_jupyter_ai_human_msg_id"
11
10
12
11
13
- class BoundedChatHistory (BaseChatMessageHistory , BaseModel ):
12
+ class BoundedChatHistory (BaseChatMessageHistory ):
14
13
"""
15
14
An in-memory implementation of `BaseChatMessageHistory` that stores up to
16
15
`k` exchanges between a user and an LLM.
@@ -19,10 +18,16 @@ class BoundedChatHistory(BaseChatMessageHistory, BaseModel):
19
18
messages and 2 AI messages. If `k` is set to `None` all messages are kept.
20
19
"""
21
20
22
- k : Union [int , None ]
23
- clear_time : float = 0.0
24
- cleared_msgs : Set [str ] = set ()
25
- _all_messages : List [BaseMessage ] = PrivateAttr (default_factory = list )
21
+ def __init__ (
22
+ self ,
23
+ k : Optional [int ] = None ,
24
+ clear_time : float = 0.0 ,
25
+ cleared_msgs : Set [str ] = set (),
26
+ ):
27
+ self .k = k
28
+ self .clear_time = clear_time
29
+ self .cleared_msgs = cleared_msgs
30
+ self ._all_messages = []
26
31
27
32
@property
28
33
def messages (self ) -> List [BaseMessage ]: # type:ignore[override]
@@ -67,7 +72,7 @@ async def aclear(self) -> None:
67
72
self .clear ()
68
73
69
74
70
- class WrappedBoundedChatHistory (BaseChatMessageHistory , BaseModel ):
75
+ class WrappedBoundedChatHistory (BaseChatMessageHistory ):
71
76
"""
72
77
Wrapper around `BoundedChatHistory` that only appends an `AgentChatMessage`
73
78
if the `HumanChatMessage` it is replying to was not cleared. If a chat
@@ -88,8 +93,16 @@ class WrappedBoundedChatHistory(BaseChatMessageHistory, BaseModel):
88
93
Reference: https://python.langchain.com/v0.1/docs/expression_language/how_to/message_history/
89
94
"""
90
95
91
- history : BoundedChatHistory
92
- last_human_msg : HumanChatMessage
96
+ def __init__ (
97
+ self ,
98
+ history : BoundedChatHistory ,
99
+ last_human_msg : HumanChatMessage ,
100
+ * args ,
101
+ ** kwargs ,
102
+ ):
103
+ self .history = history
104
+ self .last_human_msg = last_human_msg
105
+ super ().__init__ (* args , ** kwargs )
93
106
94
107
@property
95
108
def messages (self ) -> List [BaseMessage ]: # type:ignore[override]
0 commit comments