33
44from __future__ import annotations
55
6- from typing import Any , Sequence
7-
6+ from typing import Any , Sequence , Optional , TYPE_CHECKING
87import numpy as np
98from haystack import Document , component
109
10+ if TYPE_CHECKING :
11+ from fastembed import LateInteractionTextEmbedding # type: ignore
12+
1113_TWO_D = 2
1214_VALID_SIMS = {"cosine" , "dot" }
1315
@@ -87,7 +89,7 @@ def __init__(
8789 msg = f"batch_size must be > 0, got { batch_size } "
8890 raise ValueError (msg )
8991
90- self ._encoder = None # LateInteractionTextEmbedding
92+ self ._encoder : Optional [ "LateInteractionTextEmbedding" ] = None # LateInteractionTextEmbedding
9193 self ._ready = False
9294
9395 def warm_up (self ):
@@ -96,18 +98,14 @@ def warm_up(self):
9698 try :
9799 from fastembed import LateInteractionTextEmbedding # type: ignore # noqa: PLC0415
98100
99- kwargs = {"model_name" : self .model , "threads" : self .threads }
100- for k , v in {
101- "max_tokens_query" : self .max_query_tokens ,
102- "max_tokens_document" : self .max_doc_tokens ,
103- }.items ():
104- if v is not None :
105- kwargs [k ] = v
106-
107- # Only include keys that are not None
108- init_kwargs = {k : v for k , v in kwargs .items () if v is not None }
109- self ._encoder = LateInteractionTextEmbedding (** init_kwargs )
110-
101+ # Call constructor with explicit named arguments so the type checker can verify them.
102+ # Passing None for optional args is OK if the fastembed constructor accepts Optional values.
103+ self ._encoder = LateInteractionTextEmbedding (
104+ model_name = self .model ,
105+ threads = self .threads ,
106+ max_tokens_query = self .max_query_tokens ,
107+ max_tokens_document = self .max_doc_tokens ,
108+ )
111109 gen_q = self ._encoder .query_embed (["warmup" ])
112110 next (gen_q , None )
113111 gen_d = self ._encoder .embed (["warmup" ])
0 commit comments