Skip to content

Commit 07715f8

Browse files
yabooungeyurtsev
andauthored
community[minor]: Add ability to specify file encoding and json encoding for FileChatMessageHistory (#24258)
Description: Add UTF-8 encoding support Issue: Inability to properly handle characters from certain languages (e.g., Korean) Fix: Implement UTF-8 encoding in FileChatMessageHistory --------- Co-authored-by: Eugene Yurtsev <eugene@langchain.dev> Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
1 parent 020cc1c commit 07715f8

File tree

1 file changed

+23
-7
lines changed
  • libs/community/langchain_community/chat_message_histories

1 file changed

+23
-7
lines changed
Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
from pathlib import Path
3-
from typing import List
3+
from typing import List, Optional
44

55
from langchain_core.chat_history import (
66
BaseChatMessageHistory,
@@ -11,30 +11,46 @@
1111
class FileChatMessageHistory(BaseChatMessageHistory):
1212
"""Chat message history that stores history in a local file."""
1313

14-
def __init__(self, file_path: str) -> None:
14+
def __init__(
15+
self,
16+
file_path: str,
17+
*,
18+
encoding: Optional[str] = None,
19+
ensure_ascii: bool = True,
20+
) -> None:
1521
"""Initialize the file path for the chat history.
16-
1722
Args:
1823
file_path: The path to the local file to store the chat history.
24+
encoding: The encoding to use for file operations. Defaults to None.
25+
ensure_ascii: If True, escape non-ASCII in JSON. Defaults to True.
1926
"""
2027
self.file_path = Path(file_path)
28+
self.encoding = encoding
29+
self.ensure_ascii = ensure_ascii
30+
2131
if not self.file_path.exists():
2232
self.file_path.touch()
23-
self.file_path.write_text(json.dumps([]))
33+
self.file_path.write_text(
34+
json.dumps([], ensure_ascii=self.ensure_ascii), encoding=self.encoding
35+
)
2436

2537
@property
2638
def messages(self) -> List[BaseMessage]: # type: ignore
2739
"""Retrieve the messages from the local file"""
28-
items = json.loads(self.file_path.read_text())
40+
items = json.loads(self.file_path.read_text(encoding=self.encoding))
2941
messages = messages_from_dict(items)
3042
return messages
3143

3244
def add_message(self, message: BaseMessage) -> None:
3345
"""Append the message to the record in the local file"""
3446
messages = messages_to_dict(self.messages)
3547
messages.append(messages_to_dict([message])[0])
36-
self.file_path.write_text(json.dumps(messages))
48+
self.file_path.write_text(
49+
json.dumps(messages, ensure_ascii=self.ensure_ascii), encoding=self.encoding
50+
)
3751

3852
def clear(self) -> None:
3953
"""Clear session memory from the local file"""
40-
self.file_path.write_text(json.dumps([]))
54+
self.file_path.write_text(
55+
json.dumps([], ensure_ascii=self.ensure_ascii), encoding=self.encoding
56+
)

0 commit comments

Comments
 (0)