55from chromadb .utils .embedding_functions .schemas import validate_config_schema
66from enum import Enum
77
8+
89class ChromaCloudQwenEmbeddingModel (Enum ):
910 QWEN3_EMBEDDING_0p6B = "Qwen/Qwen3-Embedding-0.6B"
1011
11- class ChromaCloudQwenEmbeddingTask (Enum ):
12- NL_TO_CODE = "nl_to_code"
1312
1413class ChromaCloudQwenEmbeddingTarget (Enum ):
1514 DOCUMENTS = "documents"
1615 QUERY = "query"
1716
18- ChromaCloudQwenEmbeddingInstructions = Dict [ChromaCloudQwenEmbeddingTask , Dict [ChromaCloudQwenEmbeddingTarget , str ]]
17+
18+ ChromaCloudQwenEmbeddingInstructions = Dict [
19+ str , Dict [ChromaCloudQwenEmbeddingTarget , str ]
20+ ]
1921
2022CHROMA_CLOUD_QWEN_DEFAULT_INSTRUCTIONS : ChromaCloudQwenEmbeddingInstructions = {
21- ChromaCloudQwenEmbeddingTask . NL_TO_CODE : {
23+ "nl_to_code" : {
2224 ChromaCloudQwenEmbeddingTarget .DOCUMENTS : "" ,
2325 # Taken from https://github.com/QwenLM/Qwen3-Embedding/blob/main/evaluation/task_prompts.json
2426 ChromaCloudQwenEmbeddingTarget .QUERY : "Given a question about coding, retrieval code or passage that can solve user's question" ,
25- }
27+ }
2628}
2729
30+
2831class ChromaCloudQwenEmbeddingFunction (EmbeddingFunction [Documents ]):
2932 def __init__ (
3033 self ,
3134 model : ChromaCloudQwenEmbeddingModel ,
32- task : ChromaCloudQwenEmbeddingTask ,
35+ task : str ,
3336 instructions : ChromaCloudQwenEmbeddingInstructions = CHROMA_CLOUD_QWEN_DEFAULT_INSTRUCTIONS ,
3437 api_key_env_var : str = "CHROMA_API_KEY" ,
3538 ):
@@ -38,11 +41,11 @@ def __init__(
3841
3942 Args:
4043 model (ChromaCloudQwenEmbeddingModel): The specific Qwen model to use for embeddings.
41- task (ChromaCloudQwenEmbeddingTask ): The task for which embeddings are being generated.
44+ task (str ): The task for which embeddings are being generated.
4245 instructions (ChromaCloudQwenEmbeddingInstructions, optional): A dictionary containing
4346 custom instructions to use for the specified Qwen model. Defaults to CHROMA_CLOUD_QWEN_DEFAULT_INSTRUCTIONS.
4447 api_key_env_var (str, optional): Environment variable name that contains your API key.
45- Defaults to "CHROMA_API_KEY".
48+ Defaults to "CHROMA_API_KEY".
4649 """
4750 try :
4851 import httpx
@@ -63,7 +66,10 @@ def __init__(
6366 self ._api_url = "https://embed.trychroma.com"
6467 self ._session = httpx .Client ()
6568 self ._session .headers .update (
66- {"x-chroma-token" : self .api_key , "x-chroma-embedding-model" : self .model .value }
69+ {
70+ "x-chroma-token" : self .api_key ,
71+ "x-chroma-embedding-model" : self .model .value ,
72+ }
6773 )
6874
6975 def _parse_response (self , response : Any ) -> Embeddings :
@@ -83,7 +89,6 @@ def _parse_response(self, response: Any) -> Embeddings:
8389
8490 return np .array (embeddings , dtype = np .float32 )
8591
86-
8792 def __call__ (self , input : Documents ) -> Embeddings :
8893 """
8994 Generate embeddings for the given documents.
@@ -98,7 +103,9 @@ def __call__(self, input: Documents) -> Embeddings:
98103 return []
99104
100105 payload : Dict [str , Union [str , Documents ]] = {
101- "instructions" : self .instructions [self .task ][ChromaCloudQwenEmbeddingTarget .DOCUMENTS ],
106+ "instructions" : self .instructions [self .task ][
107+ ChromaCloudQwenEmbeddingTarget .DOCUMENTS
108+ ],
102109 "texts" : input ,
103110 }
104111
@@ -114,7 +121,9 @@ def embed_query(self, input: Documents) -> Embeddings:
114121 return []
115122
116123 payload : Dict [str , Union [str , Documents ]] = {
117- "instructions" : self .instructions [self .task ][ChromaCloudQwenEmbeddingTarget .QUERY ],
124+ "instructions" : self .instructions [self .task ][
125+ ChromaCloudQwenEmbeddingTarget .QUERY
126+ ],
118127 "texts" : input ,
119128 }
120129
@@ -147,34 +156,30 @@ def build_from_config(config: Dict[str, Any]) -> "EmbeddingFunction[Documents]":
147156 if instructions is not None :
148157 deserialized_instructions = {}
149158 for task_key , targets in instructions .items ():
150- # Convert string key to enum
151- task_enum = ChromaCloudQwenEmbeddingTask (task_key )
152- deserialized_instructions [task_enum ] = {}
159+ deserialized_instructions [task_key ] = {}
153160 for target_key , instruction in targets .items ():
154161 # Convert string key to enum
155162 target_enum = ChromaCloudQwenEmbeddingTarget (target_key )
156- deserialized_instructions [task_enum ][target_enum ] = instruction
163+ deserialized_instructions [task_key ][target_enum ] = instruction
164+ deserialized_instructions [task_key ][target_enum ] = instruction
157165
158166 return ChromaCloudQwenEmbeddingFunction (
159167 model = ChromaCloudQwenEmbeddingModel (model ),
160- task = ChromaCloudQwenEmbeddingTask ( task ) ,
168+ task = task ,
161169 instructions = deserialized_instructions ,
162170 api_key_env_var = api_key_env_var or "CHROMA_API_KEY" ,
163171 )
164172
165173 def get_config (self ) -> Dict [str , Any ]:
166174 # Serialize instructions dict with enum keys to string keys for JSON compatibility
167175 serialized_instructions = {
168- task .value : {
169- target .value : instruction
170- for target , instruction in targets .items ()
171- }
176+ task : {target .value : instruction for target , instruction in targets .items ()}
172177 for task , targets in self .instructions .items ()
173178 }
174179 return {
175180 "api_key_env_var" : self .api_key_env_var ,
176181 "model" : self .model .value ,
177- "task" : self .task . value ,
182+ "task" : self .task ,
178183 "instructions" : serialized_instructions ,
179184 }
180185
@@ -192,7 +197,7 @@ def validate_config_update(
192197 elif "instructions" in new_config :
193198 raise ValueError (
194199 "The instructions cannot be changed after the embedding function has been initialized."
195- )
200+ )
196201
197202 @staticmethod
198203 def validate_config (config : Dict [str , Any ]) -> None :
@@ -205,4 +210,4 @@ def validate_config(config: Dict[str, Any]) -> None:
205210 Raises:
206211 ValidationError: If the configuration does not match the schema
207212 """
208- validate_config_schema (config , "chroma-cloud-qwen" )
213+ validate_config_schema (config , "chroma-cloud-qwen" )
0 commit comments