# -*- coding: utf-8 -*-"""The ollama text embedding model class."""importasynciofromdatetimeimportdatetimefromtypingimportList,Anyfrom._embedding_responseimportEmbeddingResponsefrom._embedding_usageimportEmbeddingUsagefrom._cache_baseimportEmbeddingCacheBasefrom..embeddingimportEmbeddingModelBasefrom..messageimportTextBlock
[文档]classOllamaTextEmbedding(EmbeddingModelBase):"""The Ollama embedding model."""supported_modalities:list[str]=["text"]"""This class only supports text input."""
[文档]def__init__(self,model_name:str,dimensions:int,host:str|None=None,embedding_cache:EmbeddingCacheBase|None=None,**kwargs:Any,)->None:"""Initialize the Ollama text embedding model class. Args: model_name (`str`): The name of the embedding model. dimensions (`int`): The dimension of the embedding vector, the parameter should be provided according to the model used. host (`str | None`, defaults to `None`): The host URL for the Ollama API. embedding_cache (`EmbeddingCacheBase | None`, defaults to `None`): The embedding cache class instance, used to cache the embedding results to avoid repeated API calls. """importollamasuper().__init__(model_name,dimensions)self.client=ollama.AsyncClient(host=host,**kwargs)self.embedding_cache=embedding_cache
[文档]asyncdef__call__(self,text:List[str|TextBlock],**kwargs:Any,)->EmbeddingResponse:"""Call the Ollama embedding API. Args: text (`List[str | TextBlock]`): The input text to be embedded. It can be a list of strings. """gather_text=[]for_intext:ifisinstance(_,dict)and"text"in_:gather_text.append(_["text"])elifisinstance(_,str):gather_text.append(_)else:raiseValueError("Input text must be a list of strings or TextBlock dicts.",)ifself.embedding_cache:cached_embeddings=awaitself.embedding_cache.retrieve(identifier=kwargs,)ifcached_embeddings:returnEmbeddingResponse(embeddings=cached_embeddings,usage=EmbeddingUsage(tokens=0,time=0,),source="cache",)start_time=datetime.now()response=awaitasyncio.gather(*[self.client.embeddings(self.model_name,_,**kwargs)for_ingather_text],)time=(datetime.now()-start_time).total_seconds()ifself.embedding_cache:awaitself.embedding_cache.store(identifier=kwargs,embeddings=[_.embeddingfor_inresponse],)returnEmbeddingResponse(embeddings=[_.embeddingfor_inresponse],usage=EmbeddingUsage(time=time,),)