فهرست منبع

model service

丁云鹏 5 ماه پیش
والد
کامیت
73a630c717

+ 33 - 36
recommend-server-client/src/main/java/com/tzld/piaoquan/recommend/server/gen/recommend/Recommend.java

@@ -54,45 +54,43 @@ public final class Recommend {
   static {
     java.lang.String[] descriptorData = {
       "\n2com/tzld/piaoquan/recommend/server/rec" +
-      "ommend.proto\032\031google/protobuf/any.proto\032" +
-      "/com/tzld/piaoquan/recommend/server/comm" +
-      "on.proto\"\256\004\n\020RecommendRequest\022\022\n\nrequest" +
-      "_id\030\001 \001(\t\022\013\n\003mid\030\002 \001(\t\022\013\n\003uid\030\003 \001(\t\022\014\n\004s" +
-      "ize\030\004 \001(\005\022\020\n\010app_type\030\005 \001(\005\022\021\n\tcity_code" +
-      "\030\006 \001(\t\022\025\n\rprovince_code\030\007 \001(\t\022\023\n\013ab_exp_" +
-      "code\030\010 \003(\t\0220\n\010event_id\030\t \003(\0132\036.Recommend" +
-      "Request.EventIdEntry\022\034\n\024version_audit_st" +
-      "atus\030\n \001(\005\022\032\n\022recommend_trace_id\030\013 \001(\t\022\020" +
-      "\n\010video_id\030\014 \001(\003\022\014\n\004city\030\r \001(\t\022\020\n\010provin" +
-      "ce\030\016 \001(\t\022\'\n\014machine_info\030\017 \001(\0132\021.Machine" +
-      "InfoProto\022\025\n\rnew_exp_group\030\020 \001(\t\022\022\n\nsess" +
-      "ion_id\030\021 \001(\t\022\026\n\016sub_session_id\030\022 \001(\t\022\023\n\013" +
-      "page_source\030\023 \001(\t\022\023\n\013category_id\030\024 \001(\t\022\026" +
-      "\n\016hot_scene_type\030\025 \001(\003\022\021\n\tclient_ip\030\026 \001(" +
-      "\t\032.\n\014EventIdEntry\022\013\n\003key\030\001 \001(\t\022\r\n\005value\030" +
-      "\002 \001(\t:\0028\001\"\177\n\020MachineInfoProto\022\r\n\005brand\030\001" +
-      " \001(\t\022\r\n\005model\030\002 \001(\t\022\020\n\010platform\030\003 \001(\t\022\023\n" +
-      "\013sdk_version\030\004 \001(\t\022\016\n\006system\030\005 \001(\t\022\026\n\016we" +
-      "chat_version\030\006 \001(\t\"H\n\021RecommendResponse\022" +
-      "\027\n\006result\030\001 \001(\0132\007.Result\022\032\n\005video\030\002 \003(\0132" +
-      "\013.VideoProto\"\336\001\n\nVideoProto\022\020\n\010video_id\030" +
-      "\001 \001(\003\022\021\n\trov_score\030\002 \001(\001\022\021\n\tpush_from\030\003 " +
-      "\001(\t\022\017\n\007ab_code\030\004 \001(\t\022\022\n\nsort_score\030\005 \001(\001" +
-      "\022\020\n\010position\030\006 \001(\005\022\021\n\tflow_pool\030\007 \001(\t\022\027\n" +
-      "\017is_in_flow_pool\030\010 \001(\005\022\014\n\004rand\030\t \001(\001\022\'\n\017" +
-      "push_from_index\030\n \003(\0132\016.PushFromIndex\"1\n" +
-      "\rPushFromIndex\022\021\n\tpush_from\030\001 \001(\t\022\r\n\005ind" +
-      "ex\030\002 \003(\t2\212\001\n\020RecommendService\022:\n\021Homepag" +
-      "eRecommend\022\021.RecommendRequest\032\022.Recommen" +
-      "dResponse\022:\n\021RelevantRecommend\022\021.Recomme" +
-      "ndRequest\032\022.RecommendResponseB7\n0com.tzl" +
-      "d.piaoquan.recommend.server.gen.recommen" +
-      "dP\001\210\001\001b\006proto3"
+      "ommend.proto\032/com/tzld/piaoquan/recommen" +
+      "d/server/common.proto\"\256\004\n\020RecommendReque" +
+      "st\022\022\n\nrequest_id\030\001 \001(\t\022\013\n\003mid\030\002 \001(\t\022\013\n\003u" +
+      "id\030\003 \001(\t\022\014\n\004size\030\004 \001(\005\022\020\n\010app_type\030\005 \001(\005" +
+      "\022\021\n\tcity_code\030\006 \001(\t\022\025\n\rprovince_code\030\007 \001" +
+      "(\t\022\023\n\013ab_exp_code\030\010 \003(\t\0220\n\010event_id\030\t \003(" +
+      "\0132\036.RecommendRequest.EventIdEntry\022\034\n\024ver" +
+      "sion_audit_status\030\n \001(\005\022\032\n\022recommend_tra" +
+      "ce_id\030\013 \001(\t\022\020\n\010video_id\030\014 \001(\003\022\014\n\004city\030\r " +
+      "\001(\t\022\020\n\010province\030\016 \001(\t\022\'\n\014machine_info\030\017 " +
+      "\001(\0132\021.MachineInfoProto\022\025\n\rnew_exp_group\030" +
+      "\020 \001(\t\022\022\n\nsession_id\030\021 \001(\t\022\026\n\016sub_session" +
+      "_id\030\022 \001(\t\022\023\n\013page_source\030\023 \001(\t\022\023\n\013catego" +
+      "ry_id\030\024 \001(\t\022\026\n\016hot_scene_type\030\025 \001(\003\022\021\n\tc" +
+      "lient_ip\030\026 \001(\t\032.\n\014EventIdEntry\022\013\n\003key\030\001 " +
+      "\001(\t\022\r\n\005value\030\002 \001(\t:\0028\001\"\177\n\020MachineInfoPro" +
+      "to\022\r\n\005brand\030\001 \001(\t\022\r\n\005model\030\002 \001(\t\022\020\n\010plat" +
+      "form\030\003 \001(\t\022\023\n\013sdk_version\030\004 \001(\t\022\016\n\006syste" +
+      "m\030\005 \001(\t\022\026\n\016wechat_version\030\006 \001(\t\"H\n\021Recom" +
+      "mendResponse\022\027\n\006result\030\001 \001(\0132\007.Result\022\032\n" +
+      "\005video\030\002 \003(\0132\013.VideoProto\"\336\001\n\nVideoProto" +
+      "\022\020\n\010video_id\030\001 \001(\003\022\021\n\trov_score\030\002 \001(\001\022\021\n" +
+      "\tpush_from\030\003 \001(\t\022\017\n\007ab_code\030\004 \001(\t\022\022\n\nsor" +
+      "t_score\030\005 \001(\001\022\020\n\010position\030\006 \001(\005\022\021\n\tflow_" +
+      "pool\030\007 \001(\t\022\027\n\017is_in_flow_pool\030\010 \001(\005\022\014\n\004r" +
+      "and\030\t \001(\001\022\'\n\017push_from_index\030\n \003(\0132\016.Pus" +
+      "hFromIndex\"1\n\rPushFromIndex\022\021\n\tpush_from" +
+      "\030\001 \001(\t\022\r\n\005index\030\002 \003(\t2\212\001\n\020RecommendServi" +
+      "ce\022:\n\021HomepageRecommend\022\021.RecommendReque" +
+      "st\032\022.RecommendResponse\022:\n\021RelevantRecomm" +
+      "end\022\021.RecommendRequest\032\022.RecommendRespon" +
+      "seB7\n0com.tzld.piaoquan.recommend.server" +
+      ".gen.recommendP\001\210\001\001b\006proto3"
     };
     descriptor = com.google.protobuf.Descriptors.FileDescriptor
       .internalBuildGeneratedFileFrom(descriptorData,
         new com.google.protobuf.Descriptors.FileDescriptor[] {
-          com.google.protobuf.AnyProto.getDescriptor(),
           com.tzld.piaoquan.recommend.server.gen.common.Common.getDescriptor(),
         });
     internal_static_RecommendRequest_descriptor =
@@ -131,7 +129,6 @@ public final class Recommend {
       com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
         internal_static_PushFromIndex_descriptor,
         new java.lang.String[] { "PushFrom", "Index", });
-    com.google.protobuf.AnyProto.getDescriptor();
     com.tzld.piaoquan.recommend.server.gen.common.Common.getDescriptor();
   }
 

+ 0 - 1
recommend-server-client/src/main/proto/com/tzld/piaoquan/recommend/server/recommend.proto

@@ -1,6 +1,5 @@
 syntax = "proto3";
 
-import "google/protobuf/any.proto";
 import "com/tzld/piaoquan/recommend/server/common.proto";
 
 option java_multiple_files = true;

+ 22 - 0
recommend-server-service/pom.xml

@@ -158,6 +158,12 @@
             <artifactId>aliyun-log</artifactId>
             <version>0.6.35</version>
             <classifier>jar-with-dependencies</classifier>
+            <exclusions>
+                <exclusion>
+                    <artifactId>commons-lang</artifactId>
+                    <groupId>commons-lang</groupId>
+                </exclusion>
+            </exclusions>
         </dependency>
         <dependency>
             <groupId>org.apache.httpcomponents</groupId>
@@ -270,6 +276,22 @@
             <artifactId>recommend-similarity</artifactId>
             <version>1.0.0</version>
         </dependency>
+        <dependency>
+            <groupId>io.paddle.serving.client</groupId>
+            <artifactId>paddle-serving-sdk-java</artifactId>
+            <version>0.0.1</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>io.grpc</groupId>
+                    <artifactId>grpc-core</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>io.milvus</groupId>
+            <artifactId>milvus-sdk-java</artifactId>
+            <version>2.4.5</version>
+        </dependency>
     </dependencies>
 
 

+ 71 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/remote/MilvusRemoteService.java

@@ -0,0 +1,71 @@
+package com.tzld.piaoquan.recommend.server.remote;
+
+import com.tzld.piaoquan.recommend.server.util.JSONUtils;
+import io.milvus.client.MilvusClient;
+import io.milvus.grpc.SearchResultData;
+import io.milvus.grpc.SearchResults;
+import io.milvus.param.ConnectParam;
+import io.milvus.param.R;
+import io.milvus.param.dml.SearchParam;
+import io.milvus.pool.MilvusClientV1Pool;
+import io.milvus.pool.PoolConfig;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.time.Duration;
+
+/**
+ * https://github.com/milvus-io/milvus-sdk-java/blob/2.4/examples/main/java/io/milvus/v2/ClientPoolExample.java
+ *
+ * @author dyp
+ */
+@Component
+@Slf4j
+public class MilvusRemoteService {
+
+    private MilvusClientV1Pool pool;
+    private String poolKey = "milvus";
+    @Value("${milvusHost:}")
+    private String milvusHost;
+    @Value("${milvusPort:}")
+    private int milvusPort;
+
+
+    @PostConstruct
+    public void init() {
+        try {
+            ConnectParam connectConfig = ConnectParam.newBuilder()
+                    .withHost(milvusHost)
+                    .withPort(milvusPort)
+                    .build();
+            PoolConfig poolConfig = PoolConfig.builder()
+                    .maxIdlePerKey(1000) // max idle clients per key
+                    .maxTotalPerKey(1000) // max total(idle + active) clients per key
+                    .maxTotal(1000) // max total clients for all keys
+                    .maxBlockWaitDuration(Duration.ofMillis(200)) // getClient() will wait 5 seconds if no idle
+                    // client available
+                    .minEvictableIdleDuration(Duration.ofSeconds(10L)) // if number of idle clients is larger than maxIdlePerKey, redundant idle clients will be evicted after 10 seconds
+                    .build();
+            pool = new MilvusClientV1Pool(poolConfig, connectConfig);
+        } catch (Exception e) {
+            log.error("MilvusClientV2Pool init error ", e);
+            throw new RuntimeException();
+        }
+    }
+
+    public SearchResultData search(SearchParam param) {
+        MilvusClient client = pool.getClient(poolKey);
+
+        // Construct a vector to search top5 similar records, return the book title for us.
+        // This vector is equal to the No.3 record, we suppose the No.3 record is the most similar.
+        R<SearchResults> searchRet = client.search(param);
+        if (searchRet.getStatus() != R.Status.Success.getCode()) {
+            throw new RuntimeException("Failed to search! Error: " + searchRet.getMessage());
+        }
+        System.out.println(JSONUtils.toJson(searchRet));
+        return searchRet.getData().getResults();
+    }
+
+}

+ 32 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/remote/ModelRemoteService.java

@@ -0,0 +1,32 @@
+package com.tzld.piaoquan.recommend.server.remote;
+
+import com.tzld.piaoquan.recommend.feature.client.FeatureClient;
+import com.tzld.piaoquan.recommend.server.util.JSONUtils;
+import io.milvus.client.MilvusClient;
+import io.milvus.grpc.SearchResultData;
+import io.milvus.grpc.SearchResults;
+import io.milvus.param.ConnectParam;
+import io.milvus.param.R;
+import io.milvus.param.dml.SearchParam;
+import io.milvus.pool.MilvusClientV1Pool;
+import io.milvus.pool.PoolConfig;
+import io.paddle.serving.client.Client;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.time.Duration;
+
+/**
+ * @author dyp
+ */
+@Component
+@Slf4j
+public class ModelRemoteService {
+
+    @Autowired
+    private FeatureClient client;
+
+}

+ 37 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/VecSearchService.java

@@ -0,0 +1,37 @@
+package com.tzld.piaoquan.recommend.server.service;
+
+import com.tzld.piaoquan.recommend.server.remote.MilvusRemoteService;
+import io.milvus.grpc.SearchResultData;
+import io.milvus.param.MetricType;
+import io.milvus.param.dml.SearchParam;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * @author dyp
+ */
+@Service
+public class VecSearchService {
+    @Autowired
+    private MilvusRemoteService milvusRemoteService;
+
+    private String videoCollectionName = "video";
+
+    public List<String> searchVideo(List<Float> vector, int topK) {
+        SearchParam param = SearchParam.newBuilder()
+                .withCollectionName(videoCollectionName)
+                .withMetricType(MetricType.L2)
+                .withTopK(topK)
+                .withFloatVectors(Arrays.asList(vector))
+                .withVectorFieldName("video_vec")
+                .withParams("{}")
+                .addOutField("video_vec")
+                .addOutField("vid")
+                .build();
+        SearchResultData data = milvusRemoteService.search(param);
+        return null;
+    }
+}