RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(llm): honor the batch size BaiduYiyanEmbed already declares (#19690)
Lukas Buck9f31a2459ea2
diff --git a/rag/llm/embedding_model.py b/rag/llm/embedding_model.pyindex f338ac32e4..4efc832164 100644--- a/rag/llm/embedding_model.py+++ b/rag/llm/embedding_model.py@@ -1187,27 +1187,20 @@ class BaiduYiyanEmbed(Base):self.client = qianfan.Embedding(access_token=key_obj)self.model_name = model_name+ def _call(self, batch):+ res = self.client.do(model=self.model_name, texts=batch).body+ return [r["embedding"] for r in res["data"]], total_token_count_from_response(res)+def encode(self, texts: list, batch_size=16):- try:- res = self.client.do(model=self.model_name, texts=texts).body- return (- np.array([r["embedding"] for r in res["data"]]),- total_token_count_from_response(res),- )- except Exception as _e:- logger.exception("BaiduYiyanEmbed: embedding request failed")- raise EmbeddingError(f"Embedding request failed for BaiduYiyanEmbed. Error: {_e}") from _e+ # `batch_size` has been part of this signature since the class was added but the+ # request went out whole, so a document with more chunks than the provider accepts+ # per call failed as a whole. Drive the shared template instead, as every other+ # OpenAI-style provider here does.+ return self._batched_encode(texts, self._call, batch_size=batch_size)def encode_queries(self, text):- try:- res = self.client.do(model=self.model_name, texts=[text]).body- return (- np.array(res["data"][0]["embedding"]),- total_token_count_from_response(res),- )- except Exception as _e:- logger.exception("BaiduYiyanEmbed: query embedding request failed")- raise EmbeddingError(f"Embedding request failed for BaiduYiyanEmbed. Error: {_e}") from _e+ vectors, token_count = self._batched_encode([text], self._call, batch_size=1)+ return vectors[0], token_countclass VoyageEmbed(Base):diff --git a/test/unit_test/rag/llm/test_embedding_model.py b/test/unit_test/rag/llm/test_embedding_model.pyindex 4b32ee8583..087efa5c72 100644--- a/test/unit_test/rag/llm/test_embedding_model.py+++ b/test/unit_test/rag/llm/test_embedding_model.py@@ -309,6 +309,49 @@ class TestBatching:vectors, _ = embed.encode(["a", "b", "c"])np.testing.assert_array_equal(vectors[:, 0], np.array([0.0, 1.0, 2.0]))+ def test_baidu_yiyan_issues_ceil_n_over_batch_calls(self):+ """`batch_size` was in the signature and unused: the whole document went in one call."""+ embed = BaiduYiyanEmbed.__new__(BaiduYiyanEmbed)+ embed.model_name = "bge-large-zh"+ embed.client = MagicMock()+ embed.client.do = MagicMock(side_effect=lambda model, texts: SimpleNamespace(body={"data": [{"embedding": [float(len(t))]} for t in texts], "usage": {"total_tokens": len(texts)}}))++ texts = [f"t{i}" for i in range(40)] # batch_size 16 -> ceil(40/16) == 3+ vectors, token_count = embed.encode(texts)++ assert embed.client.do.call_count == 3+ assert [len(call.kwargs["texts"]) for call in embed.client.do.call_args_list] == [16, 16, 8]+ assert vectors.shape == (40, 1)+ assert token_count == 40++ def test_baidu_yiyan_preserves_order_across_batches(self):+ embed = BaiduYiyanEmbed.__new__(BaiduYiyanEmbed)+ embed.model_name = "bge-large-zh"+ embed.client = MagicMock()+ embed.client.do = MagicMock(side_effect=lambda model, texts: SimpleNamespace(body={"data": [{"embedding": [float(len(t))]} for t in texts], "usage": {"total_tokens": 1}}))++ texts = ["a" * (i + 1) for i in range(20)] # spans two batches+ vectors, _ = embed.encode(texts)++ np.testing.assert_array_equal(vectors[:, 0], np.array([float(len(t)) for t in texts]))++ def test_baidu_yiyan_surfaces_a_failed_batch(self):+ embed = BaiduYiyanEmbed.__new__(BaiduYiyanEmbed)+ embed.model_name = "bge-large-zh"+ embed.client = MagicMock()+ calls = {"n": 0}++ def _do(model, texts):+ calls["n"] += 1+ if calls["n"] == 2:+ raise RuntimeError("batch too large")+ return SimpleNamespace(body={"data": [{"embedding": [0.0]} for _ in texts], "usage": {"total_tokens": 1}})++ embed.client.do = MagicMock(side_effect=_do)++ with pytest.raises(EmbeddingError, match="Embedding request failed for BaiduYiyanEmbed"):+ embed.encode([f"t{i}" for i in range(40)])+def test_ollama_issues_ceil_n_over_batch_calls(self):embed = OllamaEmbed("x", "nomic-embed-text", base_url="http://localhost:11434")embed.client = MagicMock()