Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing files in Windows crashes on certain unicode characters in cp1252 files. Specify utf-8 encoding when opening files. #68

Open
chipfox opened this issue Jan 11, 2025 · 0 comments

Comments

@chipfox
Copy link

chipfox commented Jan 11, 2025

The SaveLangchain function in PrintUtils.py writes MarkdownVersion to a file. The default encoding for file writing on Windows is cp1252, which doesn't support many Unicode characters and will sometimes cause a crash.

All lines that open files need to be specified as utf-8 like this:
with open(ThisLogPathJSON, "w", encoding="utf-8") as f:
with open(ThisLogPathMD, "w", encoding="utf-8") as f:
with open(f"{self.LogDirPrefix}/Story.md", "w", encoding="utf-8") as f:

with open(ThisLogPathJSON, "w") as f:
f.write(json.dumps(_LangChain, indent=4, sort_keys=True))
# Now, Save Markdown Version
with open(ThisLogPathMD, "w") as f:
MarkdownVersion:str = f"# Debug LangChain {LangChainDebugTitle}\n**Note: '```' tags have been removed in this version.**\n"
for Message in _LangChain:
MarkdownVersion += f"\n\n\n# Role: {Message['role']}\n"
MarkdownVersion += f"```{Message['content'].replace('```', '')}```"
f.write(MarkdownVersion)
self.Log(f"Wrote This Language Chain ({LangChainDebugTitle}) To Debug File {ThisLogPathMD}", 5)
# Saves the given story to disk
def SaveStory(self, _StoryContent:str):
with open(f"{self.LogDirPrefix}/Story.md", "w") as f:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant