2121import com .alibaba .langengine .core .vectorstore .VectorStore ;
2222import com .alibaba .langengine .faiss .FaissConfiguration ;
2323import com .alibaba .langengine .faiss .exception .FaissException ;
24- import com .alibaba .langengine .faiss .model .FaissIndex ;
2524import com .alibaba .langengine .faiss .model .FaissSearchResult ;
2625import com .alibaba .langengine .faiss .service .FaissService ;
2726import lombok .Data ;
2827import lombok .extern .slf4j .Slf4j ;
2928import org .apache .commons .collections4 .CollectionUtils ;
30- import org .apache .commons .lang3 .StringUtils ;
3129
3230import java .io .File ;
3331import java .util .ArrayList ;
@@ -87,74 +85,75 @@ public void init() {
8785 throw new FaissException ("Failed to initialize FAISS vector store" , e );
8886 }
8987 }
90-
88+
89+
9190 @ Override
9291 public void addDocuments (List <Document > documents ) {
9392 if (CollectionUtils .isEmpty (documents )) {
9493 log .warn ("No documents to add" );
9594 return ;
9695 }
97-
96+
9897 try {
9998 log .info ("Adding {} documents to FAISS vector store" , documents .size ());
100-
99+
101100 // 生成嵌入向量
102101 List <Document > embeddedDocuments = embedding .embedDocument (documents );
103-
102+
104103 // 批量添加到FAISS索引
105104 List <float []> vectors = new ArrayList <>();
106105 List <String > documentIds = new ArrayList <>();
107-
106+
108107 for (Document document : embeddedDocuments ) {
109108 if (document .getEmbedding () != null && !document .getEmbedding ().isEmpty ()) {
110- // 转换为float数组
111- float [] vector = document .getEmbedding ().stream ()
112- .mapToDouble (Double ::doubleValue )
113- .mapToObj (d -> (float ) d )
114- .collect (java .util .stream .Collectors .toList ())
115- .stream ()
116- .mapToFloat (Float ::floatValue )
117- .toArray ();
118-
109+ // 修复:使用传统循环方式转换float数组
110+ List <Double > embeddingList = document .getEmbedding ();
111+ float [] vector = new float [embeddingList .size ()];
112+ for (int i = 0 ; i < embeddingList .size (); i ++) {
113+ vector [i ] = embeddingList .get (i ).floatValue ();
114+ }
115+
119116 vectors .add (vector );
120117 documentIds .add (document .getUniqueId ());
121-
118+
122119 // 更新文档缓存
123120 documentCache .put (document .getUniqueId (), document );
124121 }
125122 }
126-
123+
127124 if (!vectors .isEmpty ()) {
128125 faissService .addVectors (vectors , documentIds );
129126 log .info ("Successfully added {} vectors to FAISS index" , vectors .size ());
130127 }
131-
128+
132129 } catch (Exception e ) {
133130 log .error ("Failed to add documents to FAISS vector store" , e );
134131 throw new FaissException ("Failed to add documents" , e );
135132 }
136133 }
137-
134+
138135 @ Override
139136 public List <Document > similaritySearch (String query , int k , Double maxDistanceValue , Integer type ) {
140137 try {
141138 log .info ("Performing similarity search for query: {}, k: {}" , query , k );
142-
139+
143140 // 生成查询向量
144141 List <String > embeddingStrings = embedding .embedQuery (query , 1 );
145142 if (CollectionUtils .isEmpty (embeddingStrings ) || !embeddingStrings .get (0 ).startsWith ("[" )) {
146143 log .warn ("Failed to generate embedding for query: {}" , query );
147144 return new ArrayList <>();
148145 }
149-
146+
150147 List <Float > queryVectorList = JSON .parseArray (embeddingStrings .get (0 ), Float .class );
151- float [] queryVector = queryVectorList .stream ()
152- .mapToFloat (Float ::floatValue )
153- .toArray ();
154-
148+ // 修复:使用传统循环方式转换float数组
149+ float [] queryVector = new float [queryVectorList .size ()];
150+ for (int i = 0 ; i < queryVectorList .size (); i ++) {
151+ queryVector [i ] = queryVectorList .get (i );
152+ }
153+
155154 // 执行相似性搜索
156155 List <FaissSearchResult > searchResults = faissService .search (queryVector , k , maxDistanceValue );
157-
156+
158157 // 转换为Document对象
159158 List <Document > documents = new ArrayList <>();
160159 for (FaissSearchResult result : searchResults ) {
@@ -166,16 +165,15 @@ public List<Document> similaritySearch(String query, int k, Double maxDistanceVa
166165 documents .add (document );
167166 }
168167 }
169-
168+
170169 log .info ("Found {} similar documents" , documents .size ());
171170 return documents ;
172-
171+
173172 } catch (Exception e ) {
174173 log .error ("Failed to perform similarity search" , e );
175174 throw new FaissException ("Failed to perform similarity search" , e );
176175 }
177176 }
178-
179177 /**
180178 * 批量相似性搜索
181179 */
0 commit comments