MCPcopy Create free account
hub / github.com/AsyncFuncAI/deepwiki-open / DashScopeToEmbeddings

Class DashScopeToEmbeddings

api/dashscope_client.py:834–929  ·  view source on GitHub ↗

Component that converts document sequences to embedding vector sequences, specifically optimized for DashScope API

Source from the content-addressed store, hash-verified

832
833
834class DashScopeToEmbeddings(DataComponent):
835 """Component that converts document sequences to embedding vector sequences, specifically optimized for DashScope API"""
836
837 def __init__(self, embedder, batch_size: int = 100, force_recreate_db: bool = False, embedding_cache_file_name: str = "default") -> None:
838 super().__init__(batch_size=batch_size)
839 self.embedder = embedder
840 self.batch_size = batch_size
841 self.batch_embedder = DashScopeBatchEmbedder(embedder=embedder, batch_size=batch_size, embedding_cache_file_name=embedding_cache_file_name)
842 self.force_recreate_db = force_recreate_db
843
844 def __call__(self, input: List[Document]) -> List[Document]:
845 """
846 Process list of documents, generating embedding vectors for each document
847
848 Args:
849 input: List of input documents
850
851 Returns:
852 List of documents containing embedding vectors
853 """
854 output = deepcopy(input)
855
856 # Convert to text list
857 embedder_input: List[str] = [chunk.text for chunk in output]
858
859 log.info(f"Starting to process embeddings for {len(embedder_input)} documents")
860
861 # Batch process embeddings
862 outputs: List[EmbedderOutput] = self.batch_embedder(
863 input=embedder_input,
864 force_recreate=self.force_recreate_db
865 )
866
867 # Validate output
868 total_embeddings = 0
869 error_batches = 0
870
871 for batch_output in outputs:
872 if batch_output.error:
873 error_batches += 1
874 log.error(f"Found error batch: {batch_output.error}")
875 elif batch_output.data:
876 total_embeddings += len(batch_output.data)
877
878 log.info(f"Embedding statistics: total {total_embeddings} valid embeddings, {error_batches} error batches")
879
880 # Assign embedding vectors back to documents
881 doc_idx = 0
882 for batch_idx, batch_output in tqdm(
883 enumerate(outputs),
884 desc="Assigning embedding vectors to documents",
885 disable=False
886 ):
887 if batch_output.error:
888 # Create empty vectors for documents in error batches
889 batch_size_actual = min(self.batch_size, len(output) - doc_idx)
890 log.warning(f"Creating empty vectors for {batch_size_actual} documents in batch {batch_idx}")
891

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected