SemanticSlicer は、意味のある境界でテキストを分割しながら、テキスト データをより小さな断片にスライスするための C# ライブラリです。
GitHub: https://github.com/drittich/SemanticSlicer
このライブラリはテキストを受け入れ、それを小さなチャンクに分割します。通常、LLM AI 埋め込みを作成する場合に役立ちます。
パッケージ名はdrittich.SemanticSlicer
です。コマンドラインを介して Nuget からこれをインストールできます。
dotnet add package drittich.SemanticSlicer
またはパッケージ マネージャー コンソールから:
NuGetInstall-Package drittich.SemanticSlicer
単純なテキストドキュメント:
// The default options uses text separators, a max chunk size of 1,000, and
// cl100k_base encoding to count tokens.
var slicer = new Slicer ( ) ;
var text = File . ReadAllText ( " MyDocument.txt " ) ;
var documentChunks = slicer . GetDocumentChunks ( text ) ;
マークダウンドキュメント:
// Let's use Markdown separators and reduce the chunk size
var options = new SlicerOptions { MaxChunkTokenCount = 600 , Separators = Separators . Markdown } ;
var slicer = new Slicer ( options ) ;
var text = File . ReadAllText ( " MyDocument.md " ) ;
var documentChunks = slicer . GetDocumentChunks ( text ) ;
HTMLドキュメント:
var options = new SlicerOptions { Separators = Separators . Html } ;
var slicer = new Slicer ( options ) ;
var text = File . ReadAllText ( " MyDocument.html " ) ;
var documentChunks = slicer . GetDocumentChunks ( text ) ;
HTML タグの削除:
どのコンテンツでも、チャンクから HTML タグを削除してトークンの数を最小限に抑えることができます。内部テキストは保持され、 <Title>
タグがある場合は、タイトルが結果の先頭に追加されます。
// Let's remove the HTML tags as they just consume a lot of tokens without adding much value
var options = new SlicerOptions { Separators = Separators . Html , StripHtml = true } ;
var slicer = new Slicer ( options ) ;
var text = File . ReadAllText ( " MyDocument.html " ) ;
var documentChunks = slicer . GetDocumentChunks ( text ) ;
カスタム区切り文字:
必要に応じて、たとえば他のドキュメントのサポートを追加したい場合など、区切り文字の場合は独自のリストを渡すことができます。
チャンクはドキュメント内で見つかった順序で返され、必要に応じて順序を戻すために使用できる Index プロパティが含まれています。
必要な追加のメタデータを辞書として渡すことができ、各ドキュメント チャンクとともに返されるため、永続化が簡単です。メタデータを使用して、ドキュメント ID、タイトル、または最終変更日を保存できます。
var slicer = new Slicer ( ) ;
var text = File . ReadAllText ( " MyDocument.txt " ) ;
var metadata = new Dictionary < string , object ? > ( ) ;
metadata [ " Id " ] = 123 ;
metadata [ " FileName " ] = " MyDocument.txt " ;
var documentChunks = slicer . GetDocumentChunks ( text , metadata ) ;
// All chunks returned will have a Metadata property with the data you passed in.
必要に応じて、各チャンクの先頭にヘッダーを含めることができます。ユースケースの例としては、コンテキストを維持しやすくするために、チャンク コンテンツの一部としてドキュメントのタイトルまたはタグを含めることが挙げられます。
var slicer = new Slicer ( ) ;
var fileName = " MyDocument.txt " ;
var text = File . ReadAllText ( fileName ) ;
var header = $" FileName: { fileName } " ;
var documentChunks = slicer . GetDocumentChunks ( text , null , header ) ;
このプロジェクトは MIT ライセンスに基づいてライセンスされています。詳細については、LICENSE ファイルを参照してください。
ご質問やフィードバックがある場合は、このリポジトリで問題を開いてください。