-
Notifications
You must be signed in to change notification settings - Fork 19
Add decode stream #64
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f74228e
Add rust part for decode stream
Virviil b7bb0f6
Glue elixir with rust
Virviil 7f00aa5
Add tests
Virviil f11d515
Fix error
Virviil 8041e12
Fix rustler back
Virviil c4f45f8
Fix format
Virviil d2a91ce
Fix rust format
Virviil 7ba0b5d
Fix clippy
Virviil f22a2ef
Add tests for info, temoving Char
Virviil 2677724
Move to :out_of_range
Virviil 6d718ac
Update lib/tokenizers/native.ex
Virviil 703f0e7
Remove tokenizer cloning
Virviil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| defmodule Tokenizers.DecodeStream do | ||
| @moduledoc """ | ||
| Implements streaming decoding functionality for tokenizers. | ||
| """ | ||
|
|
||
| @enforce_keys [:resource] | ||
| defstruct [:resource] | ||
|
|
||
| @type t :: %__MODULE__{ | ||
| resource: reference() | ||
| } | ||
|
|
||
| @doc """ | ||
| Creates a new decode stream. | ||
|
|
||
| The `skip_special_tokens` option determines whether special tokens should be skipped during decoding. | ||
| By default, it is set to `false`. | ||
| """ | ||
| @spec new(boolean()) :: t() | ||
| def new(skip_special_tokens \\ false) do | ||
| Tokenizers.Native.decoder_stream_new(skip_special_tokens) | ||
| end | ||
|
|
||
| @doc """ | ||
| Steps through the decode stream with the given tokenizer and token ID. | ||
|
|
||
| Returns `{:ok, String.t()}` if there's a decoded string, or `{:ok, :out_ofr_range}` if the token ID is out of range. | ||
| Returns `{:error, reason}` if an error occurs during decoding. | ||
| """ | ||
| def step(%__MODULE__{} = decode_stream, tokenizer, id) when is_integer(id) do | ||
| case Tokenizers.Native.decoder_stream_step(decode_stream, tokenizer, id) do | ||
| {:ok, decoded} when is_binary(decoded) -> | ||
| {:ok, decoded} | ||
|
|
||
| {:ok, nil} -> | ||
| {:ok, :out_of_range} | ||
|
|
||
| {:error, reason} -> | ||
| {:error, reason} | ||
| end | ||
| end | ||
|
|
||
| @doc """ | ||
| Returns information about the decode stream state. | ||
| """ | ||
| defdelegate info(decode_stream), to: Tokenizers.Native, as: :decoder_stream_info | ||
|
|
||
| defimpl Inspect do | ||
| import Inspect.Algebra | ||
| alias Tokenizers.DecodeStream | ||
|
|
||
| def inspect(decode_stream, opts) do | ||
| "#Tokenizers.DecodeStream<#{to_doc(DecodeStream.info(decode_stream), opts)}>" | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.