-
Notifications
You must be signed in to change notification settings - Fork 15.8k
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
Streaming final agent response callbacks #5937
Streaming final agent response callbacks #5937
Conversation
ee75e0a
to
4ff2718
Compare
Dear @hwchase17 and @agola11, I have fixed the linting and improve the usability, please check it! |
1828015
to
aea3734
Compare
@thaiminhpv is attempting to deploy a commit to the LangChain Team on Vercel. A member of the Team first needs to authorize it. |
@thaiminhpv cool! Especially love the postprocessing on the fly. |
@thaiminhpv Also a suggestion: Consider renaming the class to make this PR backward-compatible. If people are already using |
@UmerHA thank you!
It is just a convenient shorthand for We can use this class by custom defining stream = StreamingLastResponseCallbackHandler(answer_prefix_phrases=["Now I know the final answer:"]) As in here:
@UmerHA IMO, I prefer to keep both classes as separate files, as each classes serve different purposes. |
"This is needed in order to calculate detection_windows_size for StreamingLastResponseCallbackHandler" | ||
"Please install it with `pip install tiktoken`." | ||
) | ||
self._enc = tiktoken.get_encoding(tiktoken_encoding) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it worth letting folks pass in whatever tokenize they want (can still default to tiktoken "cl100k_base")?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it worth letting folks pass in whatever tokenize they want (can still default to tiktoken "cl100k_base")?
@baskaryan Thank you for the suggestion, I have implemented it, please check it!
langchain/libs/langchain/langchain/callbacks/streaming_last_response_callback.py
Lines 106 to 135 in 4c8bdce
super().__init__() | |
if isinstance(tokenizer, str): | |
try: | |
import tiktoken | |
except ImportError: | |
raise ImportError( | |
"Could not import tiktoken python package. " | |
"This is needed in order to calculate detection_windows_size for StreamingLastResponseCallbackHandler" | |
"Please install it with `pip install tiktoken`." | |
) | |
tokenizer = tiktoken.get_encoding(tokenizer) | |
else: | |
try: | |
from transformers import PreTrainedTokenizerBase | |
if not isinstance(tokenizer, PreTrainedTokenizerBase): | |
raise ValueError( | |
"Tokenizer received was neither a string nor a PreTrainedTokenizerBase from transformers." | |
) | |
except ImportError: | |
raise ValueError( | |
"Could not import transformers python package. " | |
"Please install it with `pip install transformers`." | |
) | |
def _huggingface_tokenizer_length(text: str) -> int: | |
return len(tokenizer.encode(text)) | |
self._get_length_in_tokens = _huggingface_tokenizer_length |
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for pr @thaiminhpv! could we add a not explaining the difference between this and FinalStreamingStdOutCallbackHandler? imagine it might otherwise confuse a lot of folks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@baskaryan
I think the differences are as follows:
The FinalStreamingStdOutCallbackHandler
, as the name suggests, it just output the final response of the agent to stdout. User can implement anything such as postprocess or complex logic on top of that.
The StreamingLastResponseCallbackHandler
, is like the battery included version of FinalStreamingStdOutCallbackHandler
, with additional features that I mentioned earlier in this PR.
In other words:
-
If the user needs to custom some complex logic themselves -> go for
FinalStreamingStdOutCallbackHandler
-
If user want to post-process on-the-fly, or use multiple answer-prefix-phrase, or use abnormal detection feature -> go for
FinalStreamingStdOutCallbackHandler
.
I'm not sure where to document the explanation of the difference. Do you have any idea?
946a9e2
to
f18dafe
Compare
BTW, I've rebased my branch (manually) to be ahead of the This means we can merge my branch without any conflict, for now. |
@baskaryan Could you, please, review it? |
This PR is so helpful, please merge it. I want this in the new version of langchain. |
+2 would love to see this! |
Is there a way to make this work for openai functions? |
@baskaryan FYI |
This pull request was created prior to the introduction of OpenAI functions and the LangChain Expression Language (LCEL). I think this pull request is now obsolete and outdated. |
Isn't it possible to adapt it to LCEL and OpenAI functions? It would be usefult to have it |
+1, this would be very useful. |
This is a comprehensive version of callbacks when we want to streaming the only the last response of the agent.
This fix issue #2483 but in a more comprehensive way. This is NOT duplicate of #4630 .
I know there is a great work has been done by @UmerHA for FinalStreamingStdOutCallbackHandler class, and this PR work is inspired by him, but I want that class to be a simple use case for final streaming. What this PR does is a comprehensive version of final streaming with the following additional feature:
Quick setup
Usage 1 - use as callback function for last response new token
Usage 2 - use as iterator
Usage 3 - Post process on-the-fly
Who can review?
Hi @hwchase17 and @agola11, please review my PR, I do have an usage example in the docstring of this StreamingLastResponseCallbackHandler class.
I appreciate any feedback, please review my PR soon.
P/s: Updated using decorator approach.