def stream_chat_response_to_completion_response(
    chat_response_gen: ChatResponseGen,
) -> CompletionResponseGen:
    """Convert a stream chat response to a completion response."""

    def gen() -> CompletionResponseGen:
        for response in chat_response_gen:
            additional_kwargs = response.message.additional_kwargs
            additional_kwargs.update(response.additional_kwargs)

            yield CompletionResponse(
                text=response.message.content or "",
                additional_kwargs=additional_kwargs,
                delta=response.delta,
                raw=response.raw,
            )

    return gen()
Convert a stream chat response to a completion response.
llama-index-core/llama_index/core/base/llms/generic_utils.py