新推荐接口first commit
All checks were successful
git commit AiDA python develop 分支构建部署 / scheduled_deploy (push) Has been skipped
All checks were successful
git commit AiDA python develop 分支构建部署 / scheduled_deploy (push) Has been skipped
This commit is contained in:
@@ -2,12 +2,7 @@
|
||||
推荐系统配置
|
||||
"""
|
||||
import os
|
||||
from app.core.config import (
|
||||
DB_CONFIG, DB_HOST, DB_PORT, DB_USERNAME, DB_PASSWORD, DB_NAME,
|
||||
REDIS_HOST, REDIS_PORT, REDIS_DB,
|
||||
MILVUS_URL, MILVUS_TOKEN, MILVUS_ALIAS,
|
||||
MINIO_URL, MINIO_ACCESS, MINIO_SECRET, MINIO_SECURE
|
||||
)
|
||||
from app.core.config import settings
|
||||
|
||||
# Milvus 集合名称
|
||||
MILVUS_COLLECTION_SKETCH_VECTORS = "sketch_vectors_norm"
|
||||
@@ -63,11 +58,10 @@ TABLE_SYS_FILE = "t_sys_file"
|
||||
|
||||
# MySQL 连接配置(用于推荐系统)
|
||||
MYSQL_CONFIG = {
|
||||
"host": DB_HOST,
|
||||
"port": DB_PORT,
|
||||
"user": DB_USERNAME,
|
||||
"password": DB_PASSWORD,
|
||||
"database": DB_NAME,
|
||||
"host": settings.MYSQL_HOST,
|
||||
"port": settings.MYSQL_PORT,
|
||||
"user": settings.MYSQL_USER,
|
||||
"password": settings.MYSQL_PASSWORD,
|
||||
"database": settings.MYSQL_DB,
|
||||
"charset": "utf8mb4"
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ from typing import List, Dict, Optional, Any
|
||||
import numpy as np
|
||||
from pymilvus import MilvusClient, FieldSchema, CollectionSchema, DataType, connections, Collection
|
||||
|
||||
from app.core.config import MILVUS_URL, MILVUS_TOKEN, MILVUS_ALIAS
|
||||
from app.core.config import settings
|
||||
from app.service.recommendation_system.config import MILVUS_COLLECTION_SKETCH_VECTORS, RECOMMENDATION_CONFIG
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -21,9 +21,9 @@ def get_milvus_client() -> MilvusClient:
|
||||
if _milvus_client is None:
|
||||
try:
|
||||
_milvus_client = MilvusClient(
|
||||
uri=MILVUS_URL,
|
||||
token=MILVUS_TOKEN,
|
||||
db_name=MILVUS_ALIAS
|
||||
uri=settings.MILVUS_URL,
|
||||
token=settings.MILVUS_TOKEN,
|
||||
db_name=settings.MILVUS_DB,
|
||||
)
|
||||
logger.info("Milvus 客户端连接成功")
|
||||
except Exception as e:
|
||||
@@ -56,7 +56,7 @@ def create_collection():
|
||||
try:
|
||||
# 解析 Milvus URL
|
||||
# 处理 http://host.docker.internal:19530 格式
|
||||
url_clean = MILVUS_URL.replace("http://", "").replace("https://", "")
|
||||
url_clean = settings.MILVUS_URL.replace("http://", "").replace("https://", "")
|
||||
if ":" in url_clean:
|
||||
host, port_str = url_clean.split(":", 1)
|
||||
port = int(port_str)
|
||||
@@ -68,10 +68,10 @@ def create_collection():
|
||||
# 连接到 Milvus(如果未连接)
|
||||
try:
|
||||
connections.connect(
|
||||
alias=MILVUS_ALIAS,
|
||||
alias=settings.MILVUS_ALIAS,
|
||||
host=host,
|
||||
port=port,
|
||||
token=MILVUS_TOKEN if MILVUS_TOKEN else None
|
||||
token=settings.MILVUS_TOKEN if settings.MILVUS_TOKEN else None
|
||||
)
|
||||
logger.info(f"已连接到 Milvus: {host}:{port}")
|
||||
except Exception as conn_e:
|
||||
@@ -106,7 +106,7 @@ def create_collection():
|
||||
collection = Collection(
|
||||
name=MILVUS_COLLECTION_SKETCH_VECTORS,
|
||||
schema=schema,
|
||||
using=MILVUS_ALIAS
|
||||
using=settings.MILVUS_ALIAS
|
||||
)
|
||||
|
||||
# 创建索引
|
||||
@@ -200,10 +200,10 @@ def query_vectors_by_paths(paths: List[str]) -> Dict[str, Dict]:
|
||||
|
||||
|
||||
def search_similar_vectors(
|
||||
query_vector: np.ndarray,
|
||||
category: str,
|
||||
topk: int = 500,
|
||||
style: Optional[str] = None
|
||||
query_vector: np.ndarray,
|
||||
category: str,
|
||||
topk: int = 500,
|
||||
style: Optional[str] = None
|
||||
) -> List[Dict]:
|
||||
"""
|
||||
向量相似度检索
|
||||
@@ -292,4 +292,3 @@ def query_random_candidates(category: str, style: Optional[str] = None, limit: i
|
||||
except Exception as e:
|
||||
logger.error(f"随机查询候选失败: {e}", exc_info=True)
|
||||
return []
|
||||
|
||||
|
||||
Reference in New Issue
Block a user