Skip to content

Commit b246404

Browse files
committed
fix history impl for Pydantic v2, fixes chat
1 parent d5d5d4c commit b246404

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

packages/jupyter-ai/jupyter_ai/history.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import time
2-
from typing import List, Optional, Sequence, Set, Union
2+
from typing import List, Optional, Sequence, Set
33

44
from langchain_core.chat_history import BaseChatMessageHistory
55
from langchain_core.messages import BaseMessage
6-
from pydantic import BaseModel, PrivateAttr
76

87
from .models import HumanChatMessage
98

109
HUMAN_MSG_ID_KEY = "_jupyter_ai_human_msg_id"
1110

1211

13-
class BoundedChatHistory(BaseChatMessageHistory, BaseModel):
12+
class BoundedChatHistory(BaseChatMessageHistory):
1413
"""
1514
An in-memory implementation of `BaseChatMessageHistory` that stores up to
1615
`k` exchanges between a user and an LLM.
@@ -19,10 +18,16 @@ class BoundedChatHistory(BaseChatMessageHistory, BaseModel):
1918
messages and 2 AI messages. If `k` is set to `None` all messages are kept.
2019
"""
2120

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 = []
2631

2732
@property
2833
def messages(self) -> List[BaseMessage]: # type:ignore[override]
@@ -67,7 +72,7 @@ async def aclear(self) -> None:
6772
self.clear()
6873

6974

70-
class WrappedBoundedChatHistory(BaseChatMessageHistory, BaseModel):
75+
class WrappedBoundedChatHistory(BaseChatMessageHistory):
7176
"""
7277
Wrapper around `BoundedChatHistory` that only appends an `AgentChatMessage`
7378
if the `HumanChatMessage` it is replying to was not cleared. If a chat
@@ -88,8 +93,16 @@ class WrappedBoundedChatHistory(BaseChatMessageHistory, BaseModel):
8893
Reference: https://python.langchain.com/v0.1/docs/expression_language/how_to/message_history/
8994
"""
9095

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)
93106

94107
@property
95108
def messages(self) -> List[BaseMessage]: # type:ignore[override]

0 commit comments

Comments
 (0)