Skip to content

Commit

Permalink
Fix chinese character bug in streaming json & fix pickle load bug for…
Browse files Browse the repository at this point in the history
… bm25 index (#239)

* Fix bug

* Fix linter
  • Loading branch information
moria97 authored Oct 9, 2024
1 parent 733613b commit f3a620b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/pai_rag/core/rag_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def event_generator_async(
if token and token != DEFAULT_EMPTY_RESPONSE_GEN:
chunk = {"delta": token, "is_finished": False}
content += token
yield json.dumps(chunk) + "\n"
yield json.dumps(chunk, ensure_ascii=False) + "\n"

if chat_store:
chat_store.add_message(
Expand All @@ -56,7 +56,7 @@ async def event_generator_async(
else:
last_chunk = {"delta": "", "is_finished": True}

yield json.dumps(last_chunk, default=lambda x: x.dict())
yield json.dumps(last_chunk, default=lambda x: x.dict(), ensure_ascii=False)


class RagApplication:
Expand Down
16 changes: 15 additions & 1 deletion src/pai_rag/integrations/index/pai/local/local_bm25_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@
DEFAULT_INDEX_MATRIX_FILE = "bm25.index.matrix.pkl"


# Handle module mismatch issue.
class RenameUnpickler(pickle.Unpickler):
def find_class(self, module, name):
renamed_module = module
if module == "pai_rag.modules.index.pai_bm25_index":
renamed_module = "pai_rag.integrations.index.pai.local.local_bm25_index"

return super(RenameUnpickler, self).find_class(renamed_module, name)


def renamed_load(file_obj):
return RenameUnpickler(file_obj).load()


class LocalBm25Index:
def __init__(self):
self.doc_count: int = 0
Expand Down Expand Up @@ -76,7 +90,7 @@ def __init__(
def reload(self):
if os.path.exists(self.parts_path):
with open(self.index_file, "rb") as f:
self.index: LocalBm25Index = pickle.load(f)
self.index: LocalBm25Index = renamed_load(f)
with open(self.index_matrix_file, "rb") as f:
self.index_matrix = pickle.load(f)
else:
Expand Down

0 comments on commit f3a620b

Please # to comment.