fix: Tokens cannot be obtained from the model dialogue#2326
fix: Tokens cannot be obtained from the model dialogue#2326shaohuzhang1 merged 1 commit intomainfrom
Conversation
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
| self.usage_metadata = generation_chunk.message.usage_metadata | ||
| yield generation_chunk | ||
|
|
||
| def _create_chat_result(self, |
There was a problem hiding this comment.
The provided code snippet appears to be a part of an implementation for handling streaming responses from a chat model, specifically within a class called Stream. Here's a breakdown with notes on potential improvements:
Key Issues Identified:
- Repeated Stream Option Setting: The stream option (
streamattribute) is set multiple times without considering whether it was already defined. - Redundant Usage Metadata Handling: There seems to be redundant logic around usage metadata retrieval.
Potential Improvements:
- Single Stream Option Check:
Ensure that thestreamoption is only set once, ideally during initialization or just before initiating the stream process. This reduces ambiguity and potential bugs.
# Single check for stream option
if not kwargs.get('stream', False):
kwargs["stream"] = True- Avoid Redundant Usage Metadata Retrieval:
Remove unnecessary checks and assignments related to retrieving usage metadata because it might lead to overwriting values if they are fetched more than once.
# Custom code removed for clarity- Enhance Error Handling (Optional):
While not directly addressed in this snippet, consider adding error handling mechanisms to manage edge cases such as invalid inputs or timeouts when fetching response chunks.
Suggested Changes:
Here’s how you could refactor the function based on these guidelines:
def _stream(
messages: list[base_message.BaseMessage],
stop: Optional[list[str]] = None,
callbacks: Optional[List[CallbackHandler]] = None,
verbose: bool = False,
use_cache: bool = True, # Assuming there's a need for caching
llm_backend=None,
run_manager: Optional[ CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> Iterator[ChatGenerationChunk]:
"""
Set default stream_options and initiate streaming response.
"""
if llm_backend == "azure":
del kwargs["stream"]
# Ensure stream option is set correctly
kwargs["stream"] = kwargs.get("stream", False)
if kwargs.get("stream"):
# Additional setup for streaming can go here
payload = self._get_request_payload(messages, stop=stop, use_cache=use_cache, **kwargs)
default_chunk_class: Type[BaseMessageChunk] = AIMessageChunk
base_generation_info = {}
# Rest of the code remains mostly unchanged
# Example usage
async def main():
async for chunk in client.stream(["Hello"], use_stream=True):
print(chunk.text)Conclusion:
By ensuring the stream option is consistently managed and avoiding redundant operations concerning usage metadata, we improve both readability and robustness of the _stream method. These changes also enhance efficiency and reliability while maintaining consistency throughout the implementation.
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
fix: Tokens cannot be obtained from the model dialogue