Skip to content

Commit fa703d4

Browse files
iriditeWH-2099utsumi-fj
authored
fix(model): pass text embedding input type (#312)
Co-authored-by: WH-2099 <wh2099@pm.me> Co-authored-by: Yuichiro Utsumi <utsumi.yuichiro@fujitsu.com>
1 parent a7b4170 commit fa703d4

3 files changed

Lines changed: 26 additions & 12 deletions

File tree

src/dify_plugin/core/entities/plugin/request.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ class ModelInvokeTextEmbeddingRequest(PluginAccessModelRequest):
203203
action: ModelActions = ModelActions.InvokeTextEmbedding
204204

205205
texts: list[str]
206+
input_type: EmbeddingInputType
206207

207208

208209
class ModelInvokeMultimodalEmbeddingRequest(PluginAccessModelRequest):

src/dify_plugin/core/plugin_executor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ def invoke_text_embedding(
377377
data.model,
378378
data.credentials,
379379
data.texts,
380-
data.user_id,
380+
user=data.user_id,
381+
input_type=data.input_type,
381382
)
382383
msg = f"Model `{data.model_type}` not found for provider `{data.provider}`"
383384
raise ValueError(

tests/core/test_session_context.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
from unittest.mock import MagicMock
33

44
import dify_plugin
5+
from dify_plugin.core.entities.plugin.request import ModelInvokeTextEmbeddingRequest
56
from dify_plugin.core.plugin_executor import PluginExecutor
67
from dify_plugin.core.runtime import Session
78
from dify_plugin.core.session_context import get_current_session
9+
from dify_plugin.entities.model import EmbeddingInputType
810
from dify_plugin.interfaces.model.large_language_model import LargeLanguageModel
911
from dify_plugin.interfaces.model.text_embedding_model import TextEmbeddingModel
1012

@@ -36,15 +38,18 @@ def _llm_data() -> MagicMock:
3638
return data
3739

3840

39-
def _text_embedding_data() -> MagicMock:
40-
data = MagicMock()
41-
data.provider = "provider"
42-
data.model_type = "text-embedding"
43-
data.model = "model"
44-
data.credentials = {}
45-
data.texts = ["hello"]
46-
data.user_id = "user"
47-
return data
41+
def _text_embedding_data() -> ModelInvokeTextEmbeddingRequest:
42+
return ModelInvokeTextEmbeddingRequest.model_validate({
43+
"type": "model",
44+
"action": "invoke_text_embedding",
45+
"user_id": "user",
46+
"provider": "provider",
47+
"model_type": "text-embedding",
48+
"model": "model",
49+
"credentials": {},
50+
"texts": ["hello"],
51+
"input_type": "query",
52+
})
4853

4954

5055
def test_get_current_session_is_public() -> None:
@@ -71,10 +76,10 @@ def stream() -> Generator[str, None, None]:
7176
assert get_current_session() is None
7277

7378

74-
def test_text_embedding_exposes_session_during_invoke() -> None:
79+
def test_text_embedding_forwards_input_type_and_exposes_session() -> None:
7580
seen: list[Session | None] = []
7681

77-
def invoke(*_args: object) -> list[str]:
82+
def invoke(*_args: object, **_kwargs: object) -> list[str]:
7883
seen.append(get_current_session())
7984
return ["embedding"]
8085

@@ -87,3 +92,10 @@ def invoke(*_args: object) -> list[str]:
8792
assert seen[0] is not None
8893
assert seen[0].app_id == "app-1"
8994
assert get_current_session() is None
95+
model.invoke.assert_called_once_with(
96+
"model",
97+
{},
98+
["hello"],
99+
user="user",
100+
input_type=EmbeddingInputType.QUERY,
101+
)

0 commit comments

Comments
 (0)