1
+ from typing import Any
2
+
1
3
import litellm
4
+ from typing_extensions import Self
2
5
3
6
from ragbits .core .audit import trace
4
7
from ragbits .core .embeddings import Embedder
@@ -38,6 +41,7 @@ def __init__(
38
41
api_base : str | None = None ,
39
42
api_key : str | None = None ,
40
43
api_version : str | None = None ,
44
+ router : litellm .Router | None = None ,
41
45
) -> None :
42
46
"""
43
47
Constructs the LiteLLMEmbeddingClient.
@@ -51,12 +55,14 @@ def __init__(
51
55
for more information, follow the instructions for your specific vendor in the\
52
56
[LiteLLM documentation](https://docs.litellm.ai/docs/embedding/supported_embedding).
53
57
api_version: The API version for the call.
58
+ router: Router to be used to [route requests](https://docs.litellm.ai/docs/routing) to different models.
54
59
"""
55
60
super ().__init__ (default_options = default_options )
56
61
self .model = model
57
62
self .api_base = api_base
58
63
self .api_key = api_key
59
64
self .api_version = api_version
65
+ self .router = router
60
66
61
67
async def embed_text (self , data : list [str ], options : LiteLLMEmbedderOptions | None = None ) -> list [list [float ]]:
62
68
"""
@@ -85,7 +91,8 @@ async def embed_text(self, data: list[str], options: LiteLLMEmbedderOptions | No
85
91
options = merged_options .dict (),
86
92
) as outputs :
87
93
try :
88
- response = await litellm .aembedding (
94
+ entrypoint = self .router or litellm
95
+ response = await entrypoint .aembedding (
89
96
input = data ,
90
97
model = self .model ,
91
98
api_base = self .api_base ,
@@ -110,3 +117,19 @@ async def embed_text(self, data: list[str], options: LiteLLMEmbedderOptions | No
110
117
outputs .total_tokens = response .usage .total_tokens
111
118
112
119
return outputs .embeddings
120
+
121
+ @classmethod
122
+ def from_config (cls , config : dict [str , Any ]) -> Self :
123
+ """
124
+ Creates and returns a LiteLLMEmbedder instance.
125
+
126
+ Args:
127
+ config: A configuration object containing the configuration for initializing the LiteLLMEmbedder instance.
128
+
129
+ Returns:
130
+ LiteLLMEmbedder: An initialized LiteLLMEmbedder instance.
131
+ """
132
+ if "router" in config :
133
+ router = litellm .router .Router (model_list = config ["router" ])
134
+ config ["router" ] = router
135
+ return super ().from_config (config )
0 commit comments