Second-Me/lpm_kernel/common/strategy/strategy_huggingface.py
doubleBlack2 2483288348
Feature/embedding strategy (#235)
* Optimization of files Local Chat API.md and Public Chat API.md

* MCP sever

* Embedding strategy

* change name

* drop mcp files

* llm.py change
2025-04-16 19:40:11 +08:00

21 lines
No EOL
718 B
Python

from sentence_transformers import SentenceTransformer
from lpm_kernel.api.dto.user_llm_config_dto import (
UserLLMConfigDTO,
)
from typing import Optional
import os
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
def huggingface_strategy(user_llm_config: Optional[UserLLMConfigDTO], chunked_texts):
parts = user_llm_config.embedding_endpoint.strip("/").split("/")
if len(parts) >= 2:
model_name = "/".join(parts[-2:])
else:
raise ValueError("Endpoint error")
try:
model = SentenceTransformer(model_name)
embeddings = model.encode(chunked_texts)
return embeddings
except Exception as e:
raise Exception(f"Failed to get embeddings: {str(e)}")