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

71 lines
2.4 KiB
Python
Raw Normal View History

2025-04-17 10:01:33 +00:00
# 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):
2026-10-21 07:48:54 +00:00
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}
2025-04-17 10:01:33 +00:00
@staticmethod
def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs):
2026-10-21 07:48:54 +00:00
try:
batch_size = int(model_credential.get('batch_size') or 10)
except (TypeError, ValueError):
batch_size = 10
instance = OllamaEmbedding(
2025-04-17 10:01:33 +00:00
model=model_name,
base_url=model_credential.get('api_base'),
2026-10-21 07:48:54 +00:00
model_resource_id=str(model_kwargs['model_id']) if model_kwargs.get('model_id') else None,
2025-04-17 10:01:33 +00:00
)
2026-10-21 07:48:54 +00:00
instance.batch_size = batch_size
return instance
2025-04-17 10:01:33 +00:00
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]
2026-10-21 07:48:54 +00:00
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)
2025-04-17 10:01:33 +00:00
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}"
2026-10-21 07:48:54 +00:00
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]