-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #331 from NVIDIA/feature/huggingface-pipeline-stre…
…aming Feature/huggingface pipeline streaming
- Loading branch information
Showing
11 changed files
with
246 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,20 +1,27 @@ | ||
define user express greeting | ||
"Hello" | ||
"Hi" | ||
# Uncomment these to test dialog rails | ||
|
||
define user ask capabilities | ||
"What can you do?" | ||
"What can you help me with?" | ||
"tell me what you can do" | ||
"tell me about you" | ||
|
||
define flow | ||
user express greeting | ||
bot express greeting | ||
|
||
define flow | ||
user ask capabilities | ||
bot inform capabilities | ||
|
||
define bot inform capabilities | ||
"I am an AI assistant and I'm here to help." | ||
#define user express greeting | ||
# "Hello" | ||
# "Hi" | ||
# | ||
#define user ask capabilities | ||
# "What can you do?" | ||
# "What can you help me with?" | ||
# "tell me what you can do" | ||
# "tell me about you" | ||
# | ||
#define user ask general question | ||
# "How is the weather tomorrow?" | ||
# "Can you tell me which is the best movie this week" | ||
# "i would like to know the best scifi books of all time." | ||
# | ||
#define flow | ||
# user express greeting | ||
# bot express greeting | ||
# | ||
#define flow | ||
# user ask capabilities | ||
# bot inform capabilities | ||
# | ||
#define bot inform capabilities | ||
# "I am an AI assistant and I'm here to help." |
This file contains 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
This file contains 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
This file contains 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,16 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from .streamers import AsyncTextIteratorStreamer |
This file contains 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,53 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import asyncio | ||
|
||
from transformers.generation.streamers import TextStreamer | ||
|
||
|
||
class AsyncTextIteratorStreamer(TextStreamer): | ||
""" | ||
Simple async implementation for HuggingFace Transformers streamers. | ||
This follows closely how transformers.generation.streamers.TextIteratorStreamer works, | ||
with minor modifications to make it async. | ||
""" | ||
|
||
def __init__( | ||
self, tokenizer: "AutoTokenizer", skip_prompt: bool = False, **decode_kwargs | ||
): | ||
super().__init__(tokenizer, skip_prompt, **decode_kwargs) | ||
self.text_queue = asyncio.Queue() | ||
self.stop_signal = None | ||
self.loop = None | ||
|
||
def on_finalized_text(self, text: str, stream_end: bool = False): | ||
"""Put the new text in the queue. If the stream is ending, also put a stop signal in the queue.""" | ||
if len(text) > 0: | ||
asyncio.run_coroutine_threadsafe(self.text_queue.put(text), self.loop) | ||
|
||
if stream_end: | ||
asyncio.run_coroutine_threadsafe(self.text_queue.put(text), self.loop) | ||
|
||
def __aiter__(self): | ||
return self | ||
|
||
async def __anext__(self): | ||
value = await self.text_queue.get() | ||
if value == self.stop_signal: | ||
raise StopAsyncIteration() | ||
else: | ||
return value |
Oops, something went wrong.