Files
group_xinghuo_jinrong/app/tool/embedding_tool.py
T
zhanghongyu_0626 70aa861983 feat(advisor-agent): Introduce advisor agent functionalities with compliance, KYC, and script templates
- Added new modules for advisor compliance, KYC sessions, and script templates, enhancing the advisor agent's capabilities.
- Implemented a comprehensive API structure under the `/api/advisor-agent` prefix, ensuring clear organization and access to new features.
- Established database models and repositories for compliance rules and KYC sessions, facilitating robust data management.
- Integrated exception handling and response models to improve error management and user feedback.
- Updated settings to include new configurations for compliance and KYC features, ensuring flexibility and adaptability.

This update significantly expands the advisor agent's functionality, providing essential tools for compliance and customer interaction while maintaining a structured API design.
2026-09-12 16:33:07 +08:00

66 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""向量化:Ollama bge-m3,1024 维(客服 KB 灌库 / fin_* 检索用)。
薄封装 `app.service.embedding`,与 T-21 共用同一 Ollama 配置。
"""
from __future__ import annotations
import httpx
from app.config.settings import settings
from app.service import embedding
class EmbeddingError(RuntimeError):
pass
class OllamaEmbeddingTool:
"""顾问话术向量:直连 Ollama(与 Embedder 共用 settings)。"""
def __init__(
self,
*,
base_url: str | None = None,
model: str | None = None,
expected_dim: int | None = None,
timeout_seconds: float | None = None,
) -> None:
self.base_url = (base_url or settings.ollama_base_url).rstrip("/")
self.model = model or settings.embed_model
self.expected_dim = expected_dim or settings.embed_dim
self.timeout_seconds = timeout_seconds or settings.embed_timeout_seconds
def embed_text(self, text: str) -> list[float]:
try:
response = httpx.post(
f"{self.base_url}/api/embeddings",
json={"model": self.model, "prompt": text},
timeout=self.timeout_seconds,
)
response.raise_for_status()
body = response.json()
except httpx.HTTPError as exc:
raise EmbeddingError(f"Ollama embedding request failed: {exc}") from exc
vector = body.get("embedding")
if not isinstance(vector, list):
raise EmbeddingError("Ollama embedding response missing embedding list")
if len(vector) != self.expected_dim:
raise EmbeddingError(f"Embedding dimension must be {self.expected_dim}, got {len(vector)}")
return [float(value) for value in vector]
class Embedder:
"""批量/单条 embedding(build_collections / test_search 调用面)。"""
def embed(self, text: str) -> list[float]:
return embedding.embed_text(text)
def embed_batch(self, texts: list[str]) -> list[list[float]]:
return embedding.embed_texts(texts)
def get_embedder() -> Embedder:
return Embedder()