dmitry-brazhenko / SharpToken

SharpToken is a C# library for tokenizing natural language text. It's based on the tiktoken Python library and designed to be fast and accurate.
https://www.nuget.org/packages/SharpToken
MIT License
214 stars 14 forks source link

GptEncoding thread-safe? #30

Closed omri-suissa-clearmash closed 9 months ago

omri-suissa-clearmash commented 9 months ago

Is the SharpToken GptEncoding thread-safe, or should we create a new instance for every request?

dmitry-brazhenko commented 9 months ago

Hello! I would say that it should be thread-safe. I will double check that.

Could you try this as well please? If you observe any problems, please let me know

omri-suissa-clearmash commented 9 months ago

I saw Dictionary usage in the code. It is ok as long as they are created and modified only in the contracture. Otherwise, it is not thread-safe.

vwilson commented 9 months ago

This library is threadsafe. I have submitted a PR with an additional unit test that would seem to prove this. https://github.com/dmitry-brazhenko/SharpToken/pull/31

dmitry-brazhenko commented 9 months ago

https://github.com/dmitry-brazhenko/SharpToken/pull/31