diff --git a/go/appbuilder/config.go b/go/appbuilder/config.go index 7b74ac91e..3bad2bc01 100644 --- a/go/appbuilder/config.go +++ b/go/appbuilder/config.go @@ -20,6 +20,7 @@ import ( "net/http" "net/url" "os" + "path" "strings" "github.com/google/uuid" @@ -143,28 +144,33 @@ func (t *SDKConfig) authHeader() http.Header { } func (t *SDKConfig) ServiceURL(suffix string) (*url.URL, error) { - absolutePath, err := url.JoinPath(t.GatewayURL, suffix) + absolutePath, err := url.JoinPath(t.GatewayURL) if err != nil { return nil, err } - return t.formatURL(absolutePath) + return t.formatURL(absolutePath, suffix) } // ServiceURLV2 适配OpenAPI,当前仅AppbuilderClient使用 func (t *SDKConfig) ServiceURLV2(suffix string) (*url.URL, error) { - absolutePath, err := url.JoinPath(t.GatewayURLV2, t.ConsoleOpenAPIPrefix, t.ConsoleOpenAPIVersion, suffix) - if err != nil { - return nil, err - } - return t.formatURL(absolutePath) + suffix = path.Join(t.ConsoleOpenAPIPrefix, t.ConsoleOpenAPIVersion, suffix) + return t.formatURL(t.GatewayURLV2, suffix) } -func (t *SDKConfig) formatURL(absolutePath string) (*url.URL, error) { +func (t *SDKConfig) formatURL(absolutePath, suffix string) (*url.URL, error) { t.logger.Debug().Msgf("Service URL %s", absolutePath) url, err := url.Parse(absolutePath) if err != nil { return nil, err } + + endpoint, err := url.Parse(suffix) + if err != nil { + return nil, err + } + + url = url.ResolveReference(endpoint) + return url, nil } diff --git a/go/appbuilder/knowledge_base.go b/go/appbuilder/knowledge_base.go index 73178687d..d5f72ad42 100644 --- a/go/appbuilder/knowledge_base.go +++ b/go/appbuilder/knowledge_base.go @@ -226,3 +226,445 @@ func (t *KnowledgeBase) UploadFile(localFilePath string) (string, error) { fileID := rsp.FileID return fileID, nil } + +func (t *KnowledgeBase) CreateKnowledgeBase(req KnowledgeBaseDetail) (KnowledgeBaseDetail, error) { + request := http.Request{} + header := t.sdkConfig.AuthHeaderV2() + serviceURL, err := t.sdkConfig.ServiceURLV2("/knowledgeBase?Action=CreateKnowledgeBase") + if err != nil { + return KnowledgeBaseDetail{}, err + } + request.URL = serviceURL + request.Method = "POST" + header.Set("Content-Type", "application/json") + request.Header = header + data, _ := json.Marshal(req) + request.Body = io.NopCloser(bytes.NewReader(data)) + t.sdkConfig.BuildCurlCommand(&request) + resp, err := t.client.Do(&request) + if err != nil { + return KnowledgeBaseDetail{}, err + } + defer resp.Body.Close() + requestID, err := checkHTTPResponse(resp) + if err != nil { + return KnowledgeBaseDetail{}, fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + data, err = io.ReadAll(resp.Body) + if err != nil { + return KnowledgeBaseDetail{}, fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + rsp := KnowledgeBaseDetail{} + if err := json.Unmarshal(data, &rsp); err != nil { + return KnowledgeBaseDetail{}, fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + return rsp, nil +} + +func (t *KnowledgeBase) GetKnowledgeBaseDetail(knowledgeBaseID string) (KnowledgeBaseDetail, error) { + request := http.Request{} + header := t.sdkConfig.AuthHeaderV2() + serviceURL, err := t.sdkConfig.ServiceURLV2("/knowledgeBase?Action=DescribeKnowledgeBase") + if err != nil { + return KnowledgeBaseDetail{}, err + } + req := KnowledgeBaseDetail{} + req.ID = knowledgeBaseID + request.URL = serviceURL + request.Method = "POST" + header.Set("Content-Type", "application/json") + request.Header = header + data, _ := json.Marshal(req) + request.Body = io.NopCloser(bytes.NewReader(data)) + t.sdkConfig.BuildCurlCommand(&request) + resp, err := t.client.Do(&request) + if err != nil { + return KnowledgeBaseDetail{}, err + } + defer resp.Body.Close() + requestID, err := checkHTTPResponse(resp) + if err != nil { + return KnowledgeBaseDetail{}, fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + data, err = io.ReadAll(resp.Body) + if err != nil { + return KnowledgeBaseDetail{}, fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + rsp := KnowledgeBaseDetail{} + if err := json.Unmarshal(data, &rsp); err != nil { + return KnowledgeBaseDetail{}, fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + return rsp, nil +} + +func (t *KnowledgeBase) GetKnowledgeBaseList(req GetKnowledgeBaseListRequest) (GetKnowledgeBaseListResponse, error) { + request := http.Request{} + header := t.sdkConfig.AuthHeaderV2() + serviceURL, err := t.sdkConfig.ServiceURLV2("/knowledgeBase?Action=DescribeKnowledgeBases") + if err != nil { + return GetKnowledgeBaseListResponse{}, err + } + request.URL = serviceURL + request.Method = "POST" + header.Set("Content-Type", "application/json") + request.Header = header + data, _ := json.Marshal(req) + request.Body = io.NopCloser(bytes.NewReader(data)) + t.sdkConfig.BuildCurlCommand(&request) + resp, err := t.client.Do(&request) + if err != nil { + return GetKnowledgeBaseListResponse{}, err + } + defer resp.Body.Close() + requestID, err := checkHTTPResponse(resp) + if err != nil { + return GetKnowledgeBaseListResponse{}, fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + data, err = io.ReadAll(resp.Body) + if err != nil { + return GetKnowledgeBaseListResponse{}, fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + rsp := GetKnowledgeBaseListResponse{} + if err := json.Unmarshal(data, &rsp); err != nil { + return GetKnowledgeBaseListResponse{}, fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + return rsp, nil +} + +func (t *KnowledgeBase) ModifyKnowledgeBase(req ModifyKnowlegeBaseRequest) error { + request := http.Request{} + header := t.sdkConfig.AuthHeaderV2() + serviceURL, err := t.sdkConfig.ServiceURLV2("/knowledgeBase?Action=ModifyKnowledgeBase") + if err != nil { + return err + } + request.URL = serviceURL + request.Method = "POST" + header.Set("Content-Type", "application/json") + request.Header = header + data, _ := json.Marshal(req) + request.Body = io.NopCloser(bytes.NewReader(data)) + t.sdkConfig.BuildCurlCommand(&request) + resp, err := t.client.Do(&request) + if err != nil { + return err + } + defer resp.Body.Close() + requestID, err := checkHTTPResponse(resp) + if err != nil { + return fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + _, err = io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + return nil +} + +func (t *KnowledgeBase) DeleteKnowledgeBase(knowledgeBaseID string) error { + request := http.Request{} + header := t.sdkConfig.AuthHeaderV2() + serviceURL, err := t.sdkConfig.ServiceURLV2("/knowledgeBase?Action=DeleteKnowledgeBase") + if err != nil { + return err + } + req := KnowledgeBaseDetail{} + req.ID = knowledgeBaseID + request.URL = serviceURL + request.Method = "POST" + header.Set("Content-Type", "application/json") + request.Header = header + data, _ := json.Marshal(req) + request.Body = io.NopCloser(bytes.NewReader(data)) + t.sdkConfig.BuildCurlCommand(&request) + resp, err := t.client.Do(&request) + if err != nil { + return err + } + defer resp.Body.Close() + requestID, err := checkHTTPResponse(resp) + if err != nil { + return fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + _, err = io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + return nil +} + +func (t *KnowledgeBase) CreateDocuments(req CreateDocumentsRequest) error { + request := http.Request{} + header := t.sdkConfig.AuthHeaderV2() + serviceURL, err := t.sdkConfig.ServiceURLV2("/knowledgeBase?Action=CreateDocuments") + if err != nil { + return err + } + request.URL = serviceURL + request.Method = "POST" + header.Set("Content-Type", "application/json") + request.Header = header + data, _ := json.Marshal(req) + request.Body = io.NopCloser(bytes.NewReader(data)) + t.sdkConfig.BuildCurlCommand(&request) + resp, err := t.client.Do(&request) + if err != nil { + return err + } + defer resp.Body.Close() + requestID, err := checkHTTPResponse(resp) + if err != nil { + return fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + _, err = io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + return nil +} + +func (t *KnowledgeBase) UploadDocuments(localFilePath string, req CreateDocumentsRequest) error { + var data bytes.Buffer + w := multipart.NewWriter(&data) + file, err := os.Open(localFilePath) + if err != nil { + return err + } + defer file.Close() + filePart, _ := w.CreateFormFile("file", filepath.Base(file.Name())) + if _, err := io.Copy(filePart, file); err != nil { + return err + } + + jsonData, err := json.Marshal(req) + if err != nil { + return fmt.Errorf("failed to marshal request: %w", err) + } + jsonPart, err := w.CreateFormField("payload") + if err != nil { + return fmt.Errorf("failed to create form field: %w", err) + } + if _, err := jsonPart.Write(jsonData); err != nil { + return fmt.Errorf("failed to write JSON data: %w", err) + } + w.Close() + + request := http.Request{} + header := t.sdkConfig.AuthHeaderV2() + serviceURL, err := t.sdkConfig.ServiceURLV2("/knowledgeBase?Action=UploadDocuments") + if err != nil { + return err + } + request.URL = serviceURL + request.Method = "POST" + request.Header = header + header.Set("Content-Type", w.FormDataContentType()) + request.Body = io.NopCloser(bytes.NewReader(data.Bytes())) + resp, err := t.client.Do(&request) + if err != nil { + return err + } + defer resp.Body.Close() + requestID, err := checkHTTPResponse(resp) + if err != nil { + return fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + _, err = io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + return nil +} + +func (t *KnowledgeBase) CreateChunk(req CreateChunkRequest) (string, error) { + request := http.Request{} + header := t.sdkConfig.AuthHeaderV2() + serviceURL, err := t.sdkConfig.ServiceURLV2("/knowledgeBase?Action=CreateChunk") + if err != nil { + return "", err + } + request.URL = serviceURL + request.Method = "POST" + header.Set("Content-Type", "application/json") + request.Header = header + data, _ := json.Marshal(req) + request.Body = io.NopCloser(bytes.NewReader(data)) + t.sdkConfig.BuildCurlCommand(&request) + resp, err := t.client.Do(&request) + if err != nil { + return "", err + } + defer resp.Body.Close() + requestID, err := checkHTTPResponse(resp) + if err != nil { + return "", fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + data, err = io.ReadAll(resp.Body) + if err != nil { + return "", fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + rsp := CreateChunkResponse{} + if err := json.Unmarshal(data, &rsp); err != nil { + return "", fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + return rsp.ID, nil +} + +func (t *KnowledgeBase) ModifyChunk(req ModifyChunkRequest) error { + request := http.Request{} + header := t.sdkConfig.AuthHeaderV2() + serviceURL, err := t.sdkConfig.ServiceURLV2("/knowledgeBase?Action=ModifyChunk") + if err != nil { + return err + } + request.URL = serviceURL + request.Method = "POST" + header.Set("Content-Type", "application/json") + request.Header = header + data, _ := json.Marshal(req) + request.Body = io.NopCloser(bytes.NewReader(data)) + t.sdkConfig.BuildCurlCommand(&request) + resp, err := t.client.Do(&request) + if err != nil { + return err + } + defer resp.Body.Close() + requestID, err := checkHTTPResponse(resp) + if err != nil { + return fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + data, err = io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + rsp := CreateChunkResponse{} + if err := json.Unmarshal(data, &rsp); err != nil { + return fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + return nil +} + +func (t *KnowledgeBase) DeleteChunk(chunkID string) error { + request := http.Request{} + header := t.sdkConfig.AuthHeaderV2() + serviceURL, err := t.sdkConfig.ServiceURLV2("/knowledgeBase?Action=DeleteChunk") + if err != nil { + return err + } + request.URL = serviceURL + request.Method = "POST" + header.Set("Content-Type", "application/json") + request.Header = header + req := DeleteChunkRequest{ + ChunkID: chunkID, + } + data, _ := json.Marshal(req) + request.Body = io.NopCloser(bytes.NewReader(data)) + t.sdkConfig.BuildCurlCommand(&request) + resp, err := t.client.Do(&request) + if err != nil { + return err + } + defer resp.Body.Close() + requestID, err := checkHTTPResponse(resp) + if err != nil { + return fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + data, err = io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + rsp := CreateChunkResponse{} + if err := json.Unmarshal(data, &rsp); err != nil { + return fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + return nil +} + +func (t *KnowledgeBase) DescribeChunk(chunkID string) (DescribeChunkResponse, error) { + request := http.Request{} + header := t.sdkConfig.AuthHeaderV2() + serviceURL, err := t.sdkConfig.ServiceURLV2("/knowledgeBase?Action=DescribeChunk") + if err != nil { + return DescribeChunkResponse{}, err + } + request.URL = serviceURL + request.Method = "POST" + header.Set("Content-Type", "application/json") + request.Header = header + req := DescribeChunkRequest{ + ChunkID: chunkID, + } + data, _ := json.Marshal(req) + request.Body = io.NopCloser(bytes.NewReader(data)) + t.sdkConfig.BuildCurlCommand(&request) + resp, err := t.client.Do(&request) + if err != nil { + return DescribeChunkResponse{}, err + } + defer resp.Body.Close() + requestID, err := checkHTTPResponse(resp) + if err != nil { + return DescribeChunkResponse{}, fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + data, err = io.ReadAll(resp.Body) + if err != nil { + return DescribeChunkResponse{}, fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + rsp := DescribeChunkResponse{} + if err := json.Unmarshal(data, &rsp); err != nil { + return DescribeChunkResponse{}, fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + return rsp, nil +} + +func (t *KnowledgeBase) DescribeChunks(req DescribeChunksRequest) (DescribeChunksResponse, error) { + request := http.Request{} + header := t.sdkConfig.AuthHeaderV2() + serviceURL, err := t.sdkConfig.ServiceURLV2("/knowledgeBase?Action=DescribeChunks") + if err != nil { + return DescribeChunksResponse{}, err + } + request.URL = serviceURL + request.Method = "POST" + header.Set("Content-Type", "application/json") + request.Header = header + data, _ := json.Marshal(req) + request.Body = io.NopCloser(bytes.NewReader(data)) + t.sdkConfig.BuildCurlCommand(&request) + resp, err := t.client.Do(&request) + if err != nil { + return DescribeChunksResponse{}, err + } + defer resp.Body.Close() + requestID, err := checkHTTPResponse(resp) + if err != nil { + return DescribeChunksResponse{}, fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + data, err = io.ReadAll(resp.Body) + if err != nil { + return DescribeChunksResponse{}, fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + rsp := DescribeChunksResponse{} + if err := json.Unmarshal(data, &rsp); err != nil { + return DescribeChunksResponse{}, fmt.Errorf("requestID=%s, err=%v", requestID, err) + } + + return rsp, nil +} diff --git a/go/appbuilder/knowledge_base_data.go b/go/appbuilder/knowledge_base_data.go index d450722b0..d0eb0174b 100644 --- a/go/appbuilder/knowledge_base_data.go +++ b/go/appbuilder/knowledge_base_data.go @@ -81,3 +81,143 @@ type UploadFileResponse struct { Code string `json:"code"` Message string `json:"message"` } + +type KnowlegeBaseConfig struct { + Index KnowledgeBaseConfigIndex `json:"index"` +} + +type KnowledgeBaseConfigIndex struct { + Type string `json:"type"` + EsUrl string `json:"esUrl"` + Username string `json:"username"` + Password string `json:"password"` +} + +type KnowledgeBaseDetail struct { + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` + Description string `json:"description,omitempty"` + Config *KnowlegeBaseConfig `json:"config,omitempty"` +} + +type ModifyKnowlegeBaseRequest struct { + ID string `json:"id"` + Name *string `json:"name,omitempty"` + Description *string `json:"description,omitempty"` +} + +type GetKnowledgeBaseListRequest struct { + Marker string `json:"marker,omitempty"` + Keyword string `json:"keyword,omitempty"` + MaxKeys int `json:"maxKeys,omitempty"` +} + +type GetKnowledgeBaseListResponse struct { + RequestID string `json:"requestId"` + Data []KnowledgeBaseDetail `json:"data"` + Marker string `json:"marker"` + IsTruncated bool `json:"isTruncated"` + NextMarker string `json:"nextMarker"` + MaxKeys int `json:"maxKeys"` +} + +type DocumentsSource struct { + Type string `json:"type"` + Urls []string `json:"urls,omitempty"` + UrlDepth int `json:"url_depth,omitempty"` +} + +type DocumentsProcessOptionParser struct { + Choices []string `json:"choices"` +} + +type DocumentsProcessOptionChunkerSeparator struct { + Separators []string `json:"separators"` + TargetLength int64 `json:"targetLength"` + OverlapRate float64 `json:"overlapRate"` +} + +type DocumentsProcessOptionChunkerPattern struct { + MarkPosition string `json:"markPosition"` + Regex string `json:"regex"` + TargetLength int64 `json:"targetLength"` + OverlapRate float64 `json:"overlapRate"` +} + +type DocumentsProcessOptionChunker struct { + Choices []string `json:"choices"` + PrependInfo []string `json:"prependInfo"` + Separator *DocumentsProcessOptionChunkerSeparator `json:"separator,omitempty"` + Pattern *DocumentsProcessOptionChunkerPattern `json:"pattern,omitempty"` +} + +type DocumentsProcessOptionKnowledgeAugmentation struct { + Choices []string `json:"choices"` +} + +type DocumentsProcessOption struct { + Template string `json:"template"` + Parser *DocumentsProcessOptionParser `json:"parser,omitempty"` + Chunker *DocumentsProcessOptionChunker `json:"chunker,omitempty"` + KnowledgeAugmentation *DocumentsProcessOptionKnowledgeAugmentation `json:"knowledgeAugmentation,omitempty"` +} + +type CreateDocumentsRequest struct { + ID string `json:"id"` + ContentFormat string `json:"contentFormat"` + Source DocumentsSource `json:"source"` + ProcessOption *DocumentsProcessOption `json:"processOption,omitempty"` +} + +type CreateChunkRequest struct { + DocumentID string `json:"documentId"` + Content string `json:"content"` +} + +type CreateChunkResponse struct { + ID string `json:"id"` +} + +type ModifyChunkRequest struct { + ChunkID string `json:"chunkId"` + Content string `json:"content"` + Enable bool `json:"enable"` +} + +type DeleteChunkRequest struct { + ChunkID string `json:"chunkId"` +} + +type DescribeChunkRequest struct { + ChunkID string `json:"chunkId"` +} + +type DescribeChunkResponse struct { + ID string `json:"id"` + Type string `json:"type"` + KnowledgeBaseID string `json:"knowledgeBaseId"` + DocumentID string `json:"documentId"` + Content string `json:"content"` + WordCount int64 `json:"wordCount"` + TokenCount int64 `json:"tokenCount"` + Enabled bool `json:"enabled"` + Status string `json:"status"` + StatusMessage string `json:"statusMessage"` + CreatedAt int64 `json:"createTime"` + UpdatedAt int64 `json:"updateTime"` +} + +type DescribeChunksRequest struct { + DocumnetID string `json:"documentId"` + Marker string `json:"marker,omitempty"` + MaxKeys int `json:"maxKeys,omitempty"` + Type string `json:"type,omitempty"` +} + +type DescribeChunksResponse struct { + Data []DescribeChunkResponse `json:"data"` + Marker string `json:"marker"` + IsTruncated bool `json:"isTruncated"` + NextMarker string `json:"nextMarker"` + MaxKeys int `json:"maxKeys"` +} diff --git a/go/appbuilder/knowledge_base_test.go b/go/appbuilder/knowledge_base_test.go index 8eebe8e19..516320166 100644 --- a/go/appbuilder/knowledge_base_test.go +++ b/go/appbuilder/knowledge_base_test.go @@ -20,7 +20,7 @@ import ( "testing" ) -func TestKnowledgeBase(t *testing.T) { +func TestAddDocument(t *testing.T) { os.Setenv("APPBUILDER_LOGLEVEL", "DEBUG") os.Setenv("APPBUILDER_LOGFILE", "") knowledgeBaseID := "" @@ -68,3 +68,193 @@ func TestKnowledgeBase(t *testing.T) { t.Fatalf("delete document failed: %v", err) } } + +func TestKnowledgeBase(t *testing.T) { + os.Setenv("APPBUILDER_LOGLEVEL", "DEBUG") + os.Setenv("APPBUILDER_TOKEN", "") + config, err := NewSDKConfig("", "") + if err != nil { + t.Fatalf("new http client config failed: %v", err) + } + + client, err := NewKnowledgeBase(config) + if err != nil { + t.Fatalf("new Knowledge base instance failed") + } + + // 创建知识库 + createKnowledgeBaseRes, err := client.CreateKnowledgeBase(KnowledgeBaseDetail{ + Name: "test-go", + Description: "test-go", + Config: &KnowlegeBaseConfig{ + Index: KnowledgeBaseConfigIndex{ + Type: "public", + EsUrl: "http://localhost:9200", + Password: "elastic", + Username: "elastic", + }, + }, + }) + if err != nil { + t.Fatalf("create knowledge base failed: %v", err) + } + knowledgeBaseID := createKnowledgeBaseRes.ID + fmt.Println(knowledgeBaseID) + + // 获取知识库详情 + getKnowledgeBaseRes, err := client.GetKnowledgeBaseDetail(knowledgeBaseID) + if err != nil { + t.Fatalf("get knowledge base failed: %v", err) + } + fmt.Println(getKnowledgeBaseRes) + + // 获取知识库列表 + knowledgeBaseListRes, err := client.GetKnowledgeBaseList( + GetKnowledgeBaseListRequest{ + Marker: knowledgeBaseID, + }, + ) + if err != nil { + t.Fatalf("get knowledge base list failed: %v", err) + } + fmt.Println(knowledgeBaseListRes) + + // 导入知识库 + err = client.CreateDocuments(CreateDocumentsRequest{ + ID: knowledgeBaseID, + ContentFormat: "rawText", + Source: DocumentsSource{ + Type: "web", + Urls: []string{"https://baijiahao.baidu.com/s?id=1802527379394162441"}, + UrlDepth: 1, + }, + ProcessOption: &DocumentsProcessOption{ + Template: "custom", + Parser: &DocumentsProcessOptionParser{ + Choices: []string{"layoutAnalysis", "ocr"}, + }, + Chunker: &DocumentsProcessOptionChunker{ + Choices: []string{"separator"}, + Separator: &DocumentsProcessOptionChunkerSeparator{ + Separators: []string{"。"}, + TargetLength: 300, + OverlapRate: 0.25, + }, + PrependInfo: []string{"title", "filename"}, + }, + KnowledgeAugmentation: &DocumentsProcessOptionKnowledgeAugmentation{ + Choices: []string{"faq"}, + }, + }, + }) + if err != nil { + t.Fatalf("create documents failed: %v", err) + } + + // 上传知识库文档 + err = client.UploadDocuments("./files/test.pdf", CreateDocumentsRequest{ + ID: knowledgeBaseID, + ContentFormat: "rawText", + Source: DocumentsSource{ + Type: "file", + }, + ProcessOption: &DocumentsProcessOption{ + Template: "custom", + Parser: &DocumentsProcessOptionParser{ + Choices: []string{"layoutAnalysis", "ocr"}, + }, + Chunker: &DocumentsProcessOptionChunker{ + Choices: []string{"separator"}, + Separator: &DocumentsProcessOptionChunkerSeparator{ + Separators: []string{"。"}, + TargetLength: 300, + OverlapRate: 0.25, + }, + PrependInfo: []string{"title", "filename"}, + }, + KnowledgeAugmentation: &DocumentsProcessOptionKnowledgeAugmentation{ + Choices: []string{"faq"}, + }, + }, + }) + if err != nil { + t.Fatalf("upload documents failed: %v", err) + } + + // 修改知识库 + name := "test-go" + description := "22" + err = client.ModifyKnowledgeBase(ModifyKnowlegeBaseRequest{ + ID: knowledgeBaseID, + Name: &name, + Description: &description, + }) + if err != nil { + t.Fatalf("modify knowledge base failed: %v", err) + } + + // 删除知识库 + err = client.DeleteKnowledgeBase(knowledgeBaseID) + if err != nil { + t.Fatalf("delete knowledge base failed: %v", err) + } +} + +func TestChunk(t *testing.T) { + os.Setenv("APPBUILDER_LOGLEVEL", "DEBUG") + os.Setenv("APPBUILDER_TOKEN", "") + documentID := "" + + config, err := NewSDKConfig("", "") + if err != nil { + t.Fatalf("new http client config failed: %v", err) + } + + client, err := NewKnowledgeBase(config) + if err != nil { + t.Fatalf("new Knowledge base instance failed") + } + // 创建切片 + chunkID, err := client.CreateChunk(CreateChunkRequest{ + DocumentID: documentID, + Content: "test", + }) + if err != nil { + t.Fatalf("create chunk failed: %v", err) + } + fmt.Println(chunkID) + + // 修改切片 + err = client.ModifyChunk(ModifyChunkRequest{ + ChunkID: chunkID, + Content: "new test", + Enable: true, + }) + if err != nil { + t.Fatalf("modify chunk failed: %v", err) + } + + // 获取切片详情 + describeChunkRes, err := client.DescribeChunk(chunkID) + if err != nil { + t.Fatalf("describe chunk failed: %v", err) + } + fmt.Println(describeChunkRes) + + // 获取切片列表 + describeChunksRes, err := client.DescribeChunks(DescribeChunksRequest{ + DocumnetID: documentID, + Marker: chunkID, + MaxKeys: 10, + }) + if err != nil { + t.Fatalf("describe chunks failed: %v", err) + } + fmt.Println(describeChunksRes) + + // 删除切片 + err = client.DeleteChunk(chunkID) + if err != nil { + t.Fatalf("delete chunk failed: %v", err) + } +}