@@ -75,6 +75,16 @@ def __init__(
75
75
self .timeout = timeout
76
76
self .embedding_type = embedding_type or EmbeddingTypes .FLOAT
77
77
78
+ def _prepare_input (self , text : str ) -> str :
79
+ if not isinstance (text , str ):
80
+ msg = (
81
+ "CohereTextEmbedder expects a string as input."
82
+ "In case you want to embed a list of Documents, please use the CohereDocumentEmbedder."
83
+ )
84
+ raise TypeError (msg )
85
+
86
+ return text
87
+
78
88
def to_dict (self ) -> Dict [str , Any ]:
79
89
"""
80
90
Serializes the component to a dictionary.
@@ -114,20 +124,19 @@ def from_dict(cls, data: Dict[str, Any]) -> "CohereTextEmbedder":
114
124
115
125
@component .output_types (embedding = List [float ], meta = Dict [str , Any ])
116
126
def run (self , text : str ):
117
- """Embed text.
127
+ """
128
+ Embed text.
118
129
119
- :param text: the text to embed.
120
- :returns: A dictionary with the following keys:
121
- - `embedding`: the embedding of the text.
122
- - `meta`: metadata about the request.
123
- :raises TypeError: If the input is not a string.
130
+ :param text:
131
+ the text to embed.
132
+ :returns:
133
+ A dictionary with the following keys:
134
+ - `embedding`: the embedding of the text.
135
+ - `meta`: metadata about the request.
136
+ :raises TypeError:
137
+ If the input is not a string.
124
138
"""
125
- if not isinstance (text , str ):
126
- msg = (
127
- "CohereTextEmbedder expects a string as input."
128
- "In case you want to embed a list of Documents, please use the CohereDocumentEmbedder."
129
- )
130
- raise TypeError (msg )
139
+ text = self ._prepare_input (text = text )
131
140
132
141
# Establish connection to API
133
142
@@ -158,3 +167,40 @@ def run(self, text: str):
158
167
)
159
168
160
169
return {"embedding" : embedding [0 ], "meta" : metadata }
170
+
171
+ @component .output_types (embedding = List [float ], meta = Dict [str , Any ])
172
+ async def run_async (self , text : str ):
173
+ """
174
+ Asynchronously embed text.
175
+
176
+ This is the asynchronous version of the `run` method. It has the same parameters and return values
177
+ but can be used with `await` in async code.
178
+
179
+ :param text:
180
+ Text to embed.
181
+
182
+ :returns:
183
+ A dictionary with the following keys:
184
+ - `embedding`: the embedding of the text.
185
+ - `meta`: metadata about the request.
186
+
187
+ :raises TypeError:
188
+ If the input is not a string.
189
+ """
190
+ text = self ._prepare_input (text = text )
191
+
192
+ api_key = self .api_key .resolve_value ()
193
+ assert api_key is not None
194
+
195
+ cohere_client = AsyncClientV2 (
196
+ api_key ,
197
+ base_url = self .api_base_url ,
198
+ timeout = self .timeout ,
199
+ client_name = "haystack" ,
200
+ )
201
+
202
+ embedding , metadata = await get_async_response (
203
+ cohere_client , [text ], self .model , self .input_type , self .truncate , self .embedding_type
204
+ )
205
+
206
+ return {"embedding" : embedding [0 ], "meta" : metadata }
0 commit comments