Skip to content
This repository was archived by the owner on Sep 10, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion torchchat/usages/openai_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ def callback(x, *, done_generating=False):

device_sync(device=self.builder_args.device)

buffer = []
ILLEGAL_CHAR = '\ufffd'
# Process each token, metrics tuple yielded by Generator.generate.
for y, _ in self.generate(
model=self.model,
Expand All @@ -413,10 +415,15 @@ def callback(x, *, done_generating=False):
break

y = y.view(-1)
buffer.append(y.item())
# Decode the torch.Tensor token to a string and append to the buffer. Separate the sequences with a period token.
content = "".join(
self.tokenizer.decode([self.tokenizer.encode(".")[0]] + y.tolist())[1:]
self.tokenizer.decode([self.tokenizer.encode(".")[0]] + buffer)[1:]
)
# Skip content while illegal characters appear.
if ILLEGAL_CHAR in content:
continue
buffer.clear()

# Package the sequence into a CompletionChunkResponse and yield it.
chunk_delta = ChunkDelta(
Expand Down
Loading