From 18cfb4c067d268449f8014798c9413a75d169927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9A=8F=E9=A3=8E=E6=9E=AB=E5=8F=B6?= Date: Wed, 30 Oct 2024 22:34:33 +0800 Subject: [PATCH] community: Add token_usage and model_name metadata to ChatZhipuAI stream() and astream() response (#27677) Thank you for contributing to LangChain! - **Description:** Add token_usage and model_name metadata to ChatZhipuAI stream() and astream() response - **Issue:** None - **Dependencies:** None - **Twitter handle:** None - [ ] **Add tests and docs**: If you're adding a new integration, please include 1. a test for the integration, preferably unit tests that do not rely on network access, 2. an example notebook showing its use. It lives in `docs/docs/integrations` directory. - [ ] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ Additional guidelines: - Make sure optional dependencies are imported within a function. - Please do not add dependencies to pyproject.toml files (even optional ones) unless they are required for unit tests. - Most PRs should not touch more than one package. - Changes should be backwards compatible. - If you are adding something to community, do not re-import it in langchain. If no one reviews your PR within a few days, please @-mention one of baskaryan, efriis, eyurtsev, ccurme, vbarda, hwchase17. Co-authored-by: jianfehuang --- .../langchain_community/chat_models/zhipuai.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/libs/community/langchain_community/chat_models/zhipuai.py b/libs/community/langchain_community/chat_models/zhipuai.py index 06f299e010e..99b58697f1a 100644 --- a/libs/community/langchain_community/chat_models/zhipuai.py +++ b/libs/community/langchain_community/chat_models/zhipuai.py @@ -591,13 +591,19 @@ class ChatZhipuAI(BaseChatModel): if len(chunk["choices"]) == 0: continue choice = chunk["choices"][0] + usage = chunk.get("usage", None) + model_name = chunk.get("model", "") chunk = _convert_delta_to_message_chunk( choice["delta"], default_chunk_class ) finish_reason = choice.get("finish_reason", None) generation_info = ( - {"finish_reason": finish_reason} + { + "finish_reason": finish_reason, + "token_usage": usage, + "model_name": model_name, + } if finish_reason is not None else None ) @@ -678,13 +684,19 @@ class ChatZhipuAI(BaseChatModel): if len(chunk["choices"]) == 0: continue choice = chunk["choices"][0] + usage = chunk.get("usage", None) + model_name = chunk.get("model", "") chunk = _convert_delta_to_message_chunk( choice["delta"], default_chunk_class ) finish_reason = choice.get("finish_reason", None) generation_info = ( - {"finish_reason": finish_reason} + { + "finish_reason": finish_reason, + "token_usage": usage, + "model_name": model_name, + } if finish_reason is not None else None )