async def astream_chat(
        self, messages: Sequence[ChatMessage], **kwargs: Any
    ) -> ChatResponseGen:
        """Asynchronously stream chat results from DashScope."""
        parameters = self._get_default_parameters()
        tools = kwargs.pop("tools", None)

        parameters.update(kwargs)
        parameters["incremental_output"] = True
        parameters["result_format"] = "message"
        parameters["stream"] = True

        dashscope_messages = chat_message_to_dashscope_messages(messages)

        async_responses = astream_call_with_messages(
            model=self.model_name,
            messages=dashscope_messages,
            format=format,
            tools=tools,
            api_key=self.api_key,
            parameters=parameters,
        )

        async def gen() -> AsyncGenerator[ChatResponse, None]:
            content = ""
            all_tool_calls = {}
            async for response in async_responses:
                if response.status_code == HTTPStatus.OK:
                    top_choice = response.output.choices[0]
                    role = top_choice["message"]["role"]
                    incremental_output = top_choice["message"].get("content", "")
                    tool_calls = top_choice["message"].get("tool_calls", [])
                    content += incremental_output

                    for tool_call in tool_calls:
                        index = tool_call["index"]
                        if index is None:
                            continue
                        if index not in all_tool_calls:
                            all_tool_calls[index] = tool_call
                        else:
                            function_args = str(
                                tool_call["function"].get("arguments", "").strip()
                            )
                            if function_args:
                                all_tool_calls[index]["function"]["arguments"] += (
                                    function_args
                                )
                    yield ChatResponse(
                        message=ChatMessage(
                            role=role,
                            content=content,
                            additional_kwargs={
                                "tool_calls": list(all_tool_calls.values())
                            },
                        ),
                        delta=incremental_output,
                        raw=response,
                    )
                else:
                    yield ChatResponse(message=ChatMessage(), raw=response)
                    return

        return gen()
Asynchronously stream chat results from DashScope.
llama-index-integrations/llms/llama-index-llms-dashscope/llama_index/llms/dashscope/base.py