feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed 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 knowledge
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/knowledge"
|
||||
)
|
||||
|
||||
type DeleterConfig struct {
|
||||
KnowledgeID int64
|
||||
KnowledgeDeleter knowledge.KnowledgeOperator
|
||||
}
|
||||
|
||||
type KnowledgeDeleter struct {
|
||||
config *DeleterConfig
|
||||
}
|
||||
|
||||
func NewKnowledgeDeleter(_ context.Context, cfg *DeleterConfig) (*KnowledgeDeleter, error) {
|
||||
if cfg.KnowledgeDeleter == nil {
|
||||
return nil, errors.New("knowledge deleter is required")
|
||||
}
|
||||
return &KnowledgeDeleter{
|
||||
config: cfg,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (k *KnowledgeDeleter) Delete(ctx context.Context, input map[string]any) (map[string]any, error) {
|
||||
documentID, ok := input["documentID"].(string)
|
||||
if !ok {
|
||||
return nil, errors.New("documentID is required and must be a string")
|
||||
}
|
||||
|
||||
req := &knowledge.DeleteDocumentRequest{
|
||||
DocumentID: documentID,
|
||||
}
|
||||
|
||||
response, err := k.config.KnowledgeDeleter.Delete(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make(map[string]any)
|
||||
result["isSuccess"] = response.IsSuccess
|
||||
|
||||
return result, nil
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed 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 knowledge
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/knowledge"
|
||||
"github.com/coze-dev/coze-studio/backend/infra/contract/document/parser"
|
||||
)
|
||||
|
||||
type IndexerConfig struct {
|
||||
KnowledgeID int64
|
||||
ParsingStrategy *knowledge.ParsingStrategy
|
||||
ChunkingStrategy *knowledge.ChunkingStrategy
|
||||
KnowledgeIndexer knowledge.KnowledgeOperator
|
||||
}
|
||||
|
||||
type KnowledgeIndexer struct {
|
||||
config *IndexerConfig
|
||||
}
|
||||
|
||||
func NewKnowledgeIndexer(_ context.Context, cfg *IndexerConfig) (*KnowledgeIndexer, error) {
|
||||
if cfg.ParsingStrategy == nil {
|
||||
return nil, errors.New("parsing strategy is required")
|
||||
}
|
||||
if cfg.ChunkingStrategy == nil {
|
||||
return nil, errors.New("chunking strategy is required")
|
||||
}
|
||||
if cfg.KnowledgeIndexer == nil {
|
||||
return nil, errors.New("knowledge indexer is required")
|
||||
}
|
||||
return &KnowledgeIndexer{
|
||||
config: cfg,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (k *KnowledgeIndexer) Store(ctx context.Context, input map[string]any) (map[string]any, error) {
|
||||
|
||||
fileURL, ok := input["knowledge"].(string)
|
||||
if !ok {
|
||||
return nil, errors.New("knowledge is required")
|
||||
}
|
||||
|
||||
fileName, ext, err := parseToFileNameAndFileExtension(fileURL)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req := &knowledge.CreateDocumentRequest{
|
||||
KnowledgeID: k.config.KnowledgeID,
|
||||
ParsingStrategy: k.config.ParsingStrategy,
|
||||
ChunkingStrategy: k.config.ChunkingStrategy,
|
||||
FileURL: fileURL,
|
||||
FileName: fileName,
|
||||
FileExtension: ext,
|
||||
}
|
||||
|
||||
response, err := k.config.KnowledgeIndexer.Store(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make(map[string]any)
|
||||
result["documentId"] = response.DocumentID
|
||||
result["fileName"] = response.FileName
|
||||
result["fileUrl"] = response.FileURL
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func parseToFileNameAndFileExtension(fileURL string) (string, parser.FileExtension, error) {
|
||||
|
||||
u, err := url.Parse(fileURL)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
fileName := u.Query().Get("x-wf-file_name")
|
||||
if len(fileName) == 0 {
|
||||
return "", "", errors.New("file name is required")
|
||||
}
|
||||
|
||||
fileExt := strings.ToLower(strings.TrimPrefix(filepath.Ext(fileName), "."))
|
||||
|
||||
ext, support := parser.ValidateFileExtension(fileExt)
|
||||
if !support {
|
||||
return "", "", fmt.Errorf("unsupported file type: %s", fileExt)
|
||||
}
|
||||
return fileName, ext, nil
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed 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 knowledge
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/knowledge"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/slices"
|
||||
)
|
||||
|
||||
const outputList = "outputList"
|
||||
|
||||
type RetrieveConfig struct {
|
||||
KnowledgeIDs []int64
|
||||
RetrievalStrategy *knowledge.RetrievalStrategy
|
||||
Retriever knowledge.KnowledgeOperator
|
||||
}
|
||||
|
||||
type KnowledgeRetrieve struct {
|
||||
config *RetrieveConfig
|
||||
}
|
||||
|
||||
func NewKnowledgeRetrieve(_ context.Context, cfg *RetrieveConfig) (*KnowledgeRetrieve, error) {
|
||||
if cfg == nil {
|
||||
return nil, errors.New("cfg is required")
|
||||
}
|
||||
|
||||
if cfg.Retriever == nil {
|
||||
return nil, errors.New("retriever is required")
|
||||
}
|
||||
|
||||
if len(cfg.KnowledgeIDs) == 0 {
|
||||
return nil, errors.New("knowledgeI ids is required")
|
||||
}
|
||||
|
||||
if cfg.RetrievalStrategy == nil {
|
||||
return nil, errors.New("retrieval strategy is required")
|
||||
}
|
||||
|
||||
return &KnowledgeRetrieve{
|
||||
config: cfg,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (kr *KnowledgeRetrieve) Retrieve(ctx context.Context, input map[string]any) (map[string]any, error) {
|
||||
|
||||
query, ok := input["Query"].(string)
|
||||
if !ok {
|
||||
return nil, errors.New("capital query key is required")
|
||||
}
|
||||
|
||||
req := &knowledge.RetrieveRequest{
|
||||
Query: query,
|
||||
KnowledgeIDs: kr.config.KnowledgeIDs,
|
||||
RetrievalStrategy: kr.config.RetrievalStrategy,
|
||||
}
|
||||
|
||||
response, err := kr.config.Retriever.Retrieve(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make(map[string]any)
|
||||
result[outputList] = slices.Transform(response.Slices, func(m *knowledge.Slice) any {
|
||||
return map[string]any{
|
||||
"documentId": m.DocumentID,
|
||||
"output": m.Output,
|
||||
}
|
||||
})
|
||||
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user