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