Files
hgaol 22c2a5c276 feat: implement vector search plugin and syncer for question/answer embeddings
- Added a new vector search syncer to aggregate questions and answers with comments for vector embedding.
- Introduced a new VectorSearch interface and related structures for managing vector storage and similarity search.
- Refactored embedding service to delegate semantic search to the new vector search plugin.
- Removed embedding-related fields from SiteAIProvider and UI forms as part of the transition to the new vector search architecture.
- Updated plugin registration to include vector search capabilities.
- Cleaned up embedding service methods and removed unused dependencies.
2026-05-08 19:57:19 +08:00

61 lines
1.9 KiB
Go

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package embedding
import (
"context"
"fmt"
"github.com/apache/answer/plugin"
)
// EmbeddingService is a thin facade that delegates semantic search to a VectorSearch plugin.
// If no plugin is enabled, semantic search is unavailable.
type EmbeddingService struct{}
// NewEmbeddingService creates a new EmbeddingService.
func NewEmbeddingService() *EmbeddingService {
return &EmbeddingService{}
}
// SearchSimilar delegates to the VectorSearch plugin.
// Returns an error if no plugin is enabled.
func (s *EmbeddingService) SearchSimilar(ctx context.Context, query string, topK int) ([]plugin.VectorSearchResult, error) {
var results []plugin.VectorSearchResult
var searchErr error
found := false
err := plugin.CallVectorSearch(func(vs plugin.VectorSearch) error {
found = true
results, searchErr = vs.SearchSimilar(ctx, query, topK)
return nil
})
if err != nil {
return nil, fmt.Errorf("call vector search plugin failed: %w", err)
}
if !found {
return nil, fmt.Errorf("semantic search is not available: no vector search plugin is enabled")
}
if searchErr != nil {
return nil, searchErr
}
return results, nil
}