UnisKB/apps/models_provider/impl/ollama_model_provider/model/embedding.py

71 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# coding=utf-8
"""
@project: MaxKB
@Author:虎
@file: embedding.py
@date:2024/7/12 15:02
@desc:
"""
from typing import Dict, List
from langchain_community.embeddings import OllamaEmbeddings
from models_provider.base_model_provider import MaxKBBaseModel
class OllamaEmbedding(MaxKBBaseModel, OllamaEmbeddings):
model_resource_id: str | None = None
runtime_keep_alive: int = 300
batch_size: int = 10
@property
def _default_params(self):
return {**super()._default_params, 'keep_alive': self.runtime_keep_alive}
@staticmethod
def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs):
try:
batch_size = int(model_credential.get('batch_size') or 10)
except (TypeError, ValueError):
batch_size = 10
instance = OllamaEmbedding(
model=model_name,
base_url=model_credential.get('api_base'),
model_resource_id=str(model_kwargs['model_id']) if model_kwargs.get('model_id') else None,
)
instance.batch_size = batch_size
return instance
def embed_documents(self, texts: List[str]) -> List[List[float]]:
"""Embed documents using an Ollama deployed embedding model.
Args:
texts: The list of texts to embed.
Returns:
List of embeddings, one for each text.
"""
instruction_pairs = [f"{text}" for text in texts]
if not texts:
return []
from common.model_runtime_client import inference_lease
with inference_lease(self.model_resource_id) as state:
if state:
self.runtime_keep_alive = -1 if state['strategy'] == 'PRELOAD' else state['idle_seconds']
return self._embed(instruction_pairs)
def embed_query(self, text: str) -> List[float]:
"""Embed a query using a Ollama deployed embedding model.
Args:
text: The text to embed.
Returns:
Embeddings for the text.
"""
instruction_pair = f"{text}"
from common.model_runtime_client import inference_lease
with inference_lease(self.model_resource_id) as state:
if state:
self.runtime_keep_alive = -1 if state['strategy'] == 'PRELOAD' else state['idle_seconds']
return self._embed([instruction_pair])[0]