Skip to content
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

community: tongyi multimodal response format fix to support langchain #28210

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 16 additions & 0 deletions libs/community/langchain_community/chat_models/tongyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ def _stream_completion_with_retry(**_kwargs: Any) -> Any:
if _kwargs.get("stream") and not _kwargs.get(
"incremental_output", False
):
resp = self.fix_response_text(resp)
if prev_resp is None:
delta_resp = resp
else:
Expand All @@ -557,6 +558,21 @@ def _stream_completion_with_retry(**_kwargs: Any) -> Any:
yield check_response(resp)

return _stream_completion_with_retry(**kwargs)

def fix_response_text(self, resp: Any) -> Any:
"""reponse ` {"role": "assistant", "content": [{"text": "图像"}]}}]}`
is not working for langchain
"""

resp_copy = json.loads(json.dumps(resp))
choice = resp_copy["output"]["choices"][0]
message = choice["message"]
if isinstance(message.get('content'), list):
content_text = "".join(item.get('text', '')
for item in message['content'] if isinstance(item, dict))
message['content'] = content_text

return resp_copy

def subtract_client_response(self, resp: Any, prev_resp: Any) -> Any:
"""Subtract prev response from curr response.
Expand Down
Loading