Repository: Cinnamon/kotaemon
File: libs/kotaemon/kotaemon/indices/vectorindex.py (Python)

Code before:
        print("Adding embeddings to vector store")
        self.vector_store.add(
            embeddings=embeddings,
            ids=[t.doc_id for t in input_],
        )
        if self.doc_store:
            print("Adding documents to doc store")
            self.doc_store.add(input_)
        # save the chunks content into markdown format
        if self.cache_dir:
            file_name = Path(input_[0].metadata["file_name"])
            for i in range(len(input_)):
                markdown_content = ""
                if "page_label" in input_[i].metadata:
                    page_label = str(input_[i].metadata["page_label"])
                    markdown_content += f"Page label: {page_label}"
                if "file_name" in input_[i].metadata:
                    filename = input_[i].metadata["file_name"]
                    markdown_content += f"\nFile name: {filename}"
                if "section" in input_[i].metadata:
                    section = input_[i].metadata["section"]
                    markdown_content += f"\nSection: {section}"
                if "type" in input_[i].metadata:
                    if input_[i].metadata["type"] == "image":
                        image_origin = input_[i].metadata["image_origin"]
                        markdown_content += f"\nImage origin: {image_origin}"
                if input_[i].text:
                    markdown_content += f"\ntext:\n{input_[i].text}"

                with open(
                    Path(self.cache_dir) / f"{file_name.stem}_{self.count_+i}.md", "w"
                ) as f:
                    f.write(markdown_content)
            self.count_ += len(input_)


class VectorRetrieval(BaseRetrieval):
    """Retrieve list of documents from vector store"""

    vector_store: BaseVectorStore
    doc_store: Optional[BaseDocumentStore] = None
    embedding: BaseEmbeddings
    rerankers: Sequence[BaseReranking] = []
    top_k: int = 5
    retrieval_mode: str = "hybrid"  # vector, text, hybrid

    def run(
        self, text: str | Document, top_k: Optional[int] = None, **kwargs
    ) -> list[RetrievedDocument]:
        """Retrieve a list of documents from vector store


Review comment (bug):
The image_origin is a base64 string. You can try adding the base64 image in markdown file like [this](https://stackoverflow.com/questions/50817518/hard-code-markdown-images).

After that, you can try with Markdown preview tool like https://markdownlivepreview.com/

If the Markdown couldn't show image, then I suggest convert the image from base64 string to PNG file.

Code after:
        print("Adding embeddings to vector store")
        self.vector_store.add(
            embeddings=embeddings,
            ids=[t.doc_id for t in input_],
        )
        if self.doc_store:
            print("Adding documents to doc store")
            self.doc_store.add(input_)
        # save the chunks content into markdown format
        if self.cache_dir:
            file_name = Path(input_[0].metadata["file_name"])
            for i in range(len(input_)):
                markdown_content = ""
                if "page_label" in input_[i].metadata:
                    page_label = str(input_[i].metadata["page_label"])
                    markdown_content += f"Page label: {page_label}"
                if "file_name" in input_[i].metadata:
                    filename = input_[i].metadata["file_name"]
                    markdown_content += f"\nFile name: {filename}"
                if "section" in input_[i].metadata:
                    section = input_[i].metadata["section"]
                    markdown_content += f"\nSection: {section}"
                if "type" in input_[i].metadata:
                    if input_[i].metadata["type"] == "image":
                        image_origin = input_[i].metadata["image_origin"]
                        image_origin = f'<p><img src="{image_origin}"></p>'
                        markdown_content += f"\nImage origin: {image_origin}"
                if input_[i].text:
                    markdown_content += f"\ntext:\n{input_[i].text}"

                with open(
                    Path(self.cache_dir) / f"{file_name.stem}_{self.count_+i}.md", "w"
                ) as f:
                    f.write(markdown_content)
            self.count_ += len(input_)


class VectorRetrieval(BaseRetrieval):
    """Retrieve list of documents from vector store"""

    vector_store: BaseVectorStore
    doc_store: Optional[BaseDocumentStore] = None
    embedding: BaseEmbeddings
    rerankers: Sequence[BaseReranking] = []
    top_k: int = 5
    retrieval_mode: str = "hybrid"  # vector, text, hybrid

    def run(
        self, text: str | Document, top_k: Optional[int] = None, **kwargs
    ) -> list[RetrievedDocument]:
        """Retrieve a list of documents from vector store