diff --git a/docs/BasisModule/Platform/KnowledgeBase/knowledgebase.md b/docs/BasisModule/Platform/KnowledgeBase/knowledgebase.md index 27cd0569..fa2a35d8 100644 --- a/docs/BasisModule/Platform/KnowledgeBase/knowledgebase.md +++ b/docs/BasisModule/Platform/KnowledgeBase/knowledgebase.md @@ -288,7 +288,7 @@ class DocumentProcessOption(BaseModel): description="模板类型,ppt:模版配置—ppt幻灯片, resume:模版配置—简历文档, paper:模版配置—论文文档, custom:自定义配置—自定义切片, default:自定义配置—默认切分", enum=["ppt", "paper", "qaPair", "resume", " custom", "default"], ) - parser: Optional[DocumentChoices] = Field(None, description="解析方法(文字提取默认启动,参数不体现,layoutAnalysis版面分析,ocr按需增加)") + parser: Optional[DocumentChoices] = Field(None, description="解析方法(文字提取默认启动,参数不体现,layoutAnalysis版面分析,ocr光学字符识别,pageImageAnalysis文档图片解析,chartAnalysis图表解析,tableAnalysis表格深度解析,按需增加)") knowledgeAugmentation: Optional[DocumentChoices] = Field( None, description="知识增强,faq、spokenQuery、spo、shortSummary按需增加。问题生成:faq、spokenQuery,段落摘要:shortSummary,三元组知识抽取:spo" ) @@ -662,10 +662,11 @@ for message in doc_list: #### 方法参数 -| 参数名称 | 参数类型 | 是否必传 | 描述 | 示例值 | -| ---------- | -------- | -------- | -------- | -------------- | -| documentId | string | 是 | 文档ID | "正确的文档ID" | -| content | string | 是 | 切片内容 | "内容" | +| 参数名称 | 参数类型 | 是否必传 | 描述 | 示例值 | +| --------------- | -------- | -------- | -------- | -------------- | +| knowledgeBaseId | string | 是 | 知识库ID | | +| documentId | string | 是 | 文档ID | "正确的文档ID" | +| content | string | 是 | 切片内容 | "内容" | #### 方法返回值 @@ -686,7 +687,7 @@ os.environ["APPBUILDER_TOKEN"] = "your_appbuilder_token" my_knowledge_base_id = "your_knowledge_base_id" my_knowledge = appbuilder.KnowledgeBase(my_knowledge_base_id) print("知识库ID: ", my_knowledge.knowledge_id) -resp = my_knowledge.create_chunk("your_document_id", "content") +resp = my_knowledge.create_chunk("your_document_id", "content", knowledgebase_id=knowledge_base_id) print("切片ID: ", resp.id) chunk_id = resp.id ``` @@ -697,6 +698,7 @@ chunk_id = resp.id | 参数名称 | 参数类型 | 是否必传 | 描述 | 示例值 | | ---------- | -------- | ------------ | -------------- | -------------- | +| knowledgeBaseId | string | 是 | 知识库ID | | | chunkId | string | 是 | 文档ID | "正确的切片ID" | | content | string | 是 | 切片内容 | "内容" | | enable | bool | 是 | 是否用该切片 | True | @@ -711,16 +713,17 @@ os.environ["APPBUILDER_TOKEN"] = "your_appbuilder_token" my_knowledge_base_id = "your_knowledge_base_id" my_knowledge = appbuilder.KnowledgeBase(my_knowledge_base_id) print("知识库ID: ", my_knowledge.knowledge_id) -my_knowledge.modify_chunk("your_chunk_id", "content", True) +my_knowledge.modify_chunk("your_chunk_id", "content", True, knowledgebase_id=my_knowledge_base_id) ``` ### 16. 删除切片`delete_chunk(chunkId: str)` #### 方法参数 -| 参数名称 | 参数类型 | 是否必传 | 描述 | 示例值 | -| -------- | -------- | -------- | ------ | -------------- | -| chunkId | string | 是 | 文档ID | "正确的切片ID" | +| 参数名称 | 参数类型 | 是否必传 | 描述 | 示例值 | +| --------------- | -------- | -------- | -------- | -------------- | +| knowledgeBaseId | string | 是 | 知识库ID | | +| chunkId | string | 是 | 文档ID | "正确的切片ID" | #### 方法示例 @@ -732,16 +735,17 @@ os.environ["APPBUILDER_TOKEN"] = "your_appbuilder_token" my_knowledge_base_id = "your_knowledge_base_id" my_knowledge = appbuilder.KnowledgeBase(my_knowledge_base_id) print("知识库ID: ", my_knowledge.knowledge_id) -my_knowledge.delete_chunk("your_chunk_id") +my_knowledge.delete_chunk("your_chunk_id", knowledgebase_id=my_knowledge_base_id) ``` ### 17. 获取切片信息`describe_chunk(chunkId: str)` #### 方法参数 -| 参数名称 | 参数类型 | 是否必传 | 描述 | 示例值 | -| -------- | -------- | -------- | ------ | -------------- | -| chunkId | string | 是 | 文档ID | "正确的切片ID" | +| 参数名称 | 参数类型 | 是否必传 | 描述 | 示例值 | +| --------------- | -------- | -------- | -------- | -------------- | +| knowledgeBaseId | string | 是 | 知识库ID | | +| chunkId | string | 是 | 文档ID | "正确的切片ID" | #### 方法返回值 @@ -774,7 +778,7 @@ os.environ["APPBUILDER_TOKEN"] = "your_appbuilder_token" my_knowledge_base_id = "your_knowledge_base_id" my_knowledge = appbuilder.KnowledgeBase(my_knowledge_base_id) print("知识库ID: ", my_knowledge.knowledge_id) -resp = my_knowledge.describe_chunk("your_chunk_id") +resp = my_knowledge.describe_chunk("your_chunk_id", knowledgebase_id=my_knowledge_base_id) print("切片详情:") print(resp) ``` @@ -783,12 +787,13 @@ print(resp) #### 方法参数 -| 参数名称 | 参数类型 | 是否必传 | 描述 | 示例值 | -| ---------- | -------- | -------- | ------------------------------------------------------------ | -------------- | -| documentId | string | 是 | 文档ID | "正确的文档ID" | -| marker | string | 否 | 起始位置,切片ID | "正确的切片ID" | -| maxKeys | string | 否 | 返回文档数量大小,默认10,最大值100 | 10 | -| type | string | 否 | 根据类型获取切片列表(RAW、NEW、COPY),RAW:原文切片,NEW:新增切片,COPY:复制切片 | "RAW" | +| 参数名称 | 参数类型 | 是否必传 | 描述 | 示例值 | +| --------------- | -------- | -------- | ------------------------------------------------------------ | -------------- | +| knowledgeBaseId | string | 是 | 知识库ID | | +| documentId | string | 是 | 文档ID | "正确的文档ID" | +| marker | string | 否 | 起始位置,切片ID | "正确的切片ID" | +| maxKeys | string | 否 | 返回文档数量大小,默认10,最大值100 | 10 | +| type | string | 否 | 根据类型获取切片列表(RAW、NEW、COPY),RAW:原文切片,NEW:新增切片,COPY:复制切片 | "RAW" | #### 方法返回值 @@ -834,7 +839,7 @@ os.environ["APPBUILDER_TOKEN"] = "your_appbuilder_token" my_knowledge_base_id = "your_knowledge_base_id" my_knowledge = appbuilder.KnowledgeBase(my_knowledge_base_id) print("知识库ID: ", my_knowledge.knowledge_id) -resp = my_knowledge.describe_chunks("your_document_id") +resp = my_knowledge.describe_chunks("your_document_id", knowledgebase_id=my_knowledge_base_id) print("切片列表:") print(resp) ``` @@ -964,7 +969,11 @@ public class KnowledgebaseTest { @Test public void testCreateChunk() throws IOException, AppBuilderServerException { String documentId = ""; - Knowledgebase knowledgebase = new Knowledgebase(); + // 知识库ID + String knowledgeBaseId = ""; + // Appbuilder Token + String secretKey = ""; + Knowledgebase knowledgebase = new Knowledgebase(knowledgeBaseID, secretKey); // 创建切片 String chunkId = knowledgebase.createChunk(documentId, "test"); // 修改切片 @@ -1189,13 +1198,13 @@ func TestChunk(t *testing.T) { os.Setenv("APPBUILDER_LOGLEVEL", "DEBUG") os.Setenv("APPBUILDER_TOKEN", "") documentID := "" - + knowledgeBaseID := ""; config, err := NewSDKConfig("", "") if err != nil { t.Fatalf("new http client config failed: %v", err) } - client, err := NewKnowledgeBase(config) + client, err := NewKnowledgeBaseWithKnowledgeBaseID(knowledgeBaseID, config) if err != nil { t.Fatalf("new Knowledge base instance failed") } diff --git a/go/appbuilder/knowledge_base.go b/go/appbuilder/knowledge_base.go index 55b0280e..7c434bde 100644 --- a/go/appbuilder/knowledge_base.go +++ b/go/appbuilder/knowledge_base.go @@ -42,9 +42,21 @@ func NewKnowledgeBase(config *SDKConfig) (*KnowledgeBase, error) { return &KnowledgeBase{sdkConfig: config, client: client}, nil } +func NewKnowledgeBaseWithKnowledgeBaseID(knowledgeBaseID string, config *SDKConfig) (*KnowledgeBase, error) { + if config == nil { + return nil, errors.New("invalid config") + } + client := config.HTTPClient + if client == nil { + client = &http.Client{Timeout: 60 * time.Second} + } + return &KnowledgeBase{knowledgeBaseID: knowledgeBaseID, sdkConfig: config, client: client}, nil +} + type KnowledgeBase struct { - sdkConfig *SDKConfig - client HTTPClient + knowledgeBaseID string + sdkConfig *SDKConfig + client HTTPClient } func (t *KnowledgeBase) CreateDocument(req CreateDocumentRequest) (CreateDocumentResponse, error) { @@ -618,6 +630,10 @@ func (t *KnowledgeBase) CreateChunk(req CreateChunkRequest) (string, error) { if req.ClientToken == "" { req.ClientToken = uuid.New().String() } + + if req.KnowledgeBaseID == "" && t.knowledgeBaseID != "" { + req.KnowledgeBaseID = t.knowledgeBaseID + } serviceURL, err := t.sdkConfig.ServiceURLV2("/knowledgeBase?Action=CreateChunk&clientToken=" + req.ClientToken) if err != nil { return "", err @@ -657,6 +673,10 @@ func (t *KnowledgeBase) ModifyChunk(req ModifyChunkRequest) error { if req.ClientToken == "" { req.ClientToken = uuid.New().String() } + + if req.KnowledgeBaseID == "" && t.knowledgeBaseID != "" { + req.KnowledgeBaseID = t.knowledgeBaseID + } serviceURL, err := t.sdkConfig.ServiceURLV2("/knowledgeBase?Action=ModifyChunk&clientToken=" + req.ClientToken) if err != nil { return err @@ -715,6 +735,10 @@ func (t *KnowledgeBase) deleteChunk(chunkID string, clientToken string) error { req := DeleteChunkRequest{ ChunkID: chunkID, } + + if t.knowledgeBaseID != "" { + req.KnowledgeBaseID = t.knowledgeBaseID + } data, _ := json.Marshal(req) request.Body = NopCloser(bytes.NewReader(data)) t.sdkConfig.BuildCurlCommand(&request) @@ -754,6 +778,10 @@ func (t *KnowledgeBase) DescribeChunk(chunkID string) (DescribeChunkResponse, er req := DescribeChunkRequest{ ChunkID: chunkID, } + + if t.knowledgeBaseID != "" { + req.KnowledgeBaseID = t.knowledgeBaseID + } data, _ := json.Marshal(req) request.Body = NopCloser(bytes.NewReader(data)) t.sdkConfig.BuildCurlCommand(&request) @@ -786,6 +814,10 @@ func (t *KnowledgeBase) DescribeChunks(req DescribeChunksRequest) (DescribeChunk if err != nil { return DescribeChunksResponse{}, err } + + if req.KnowledgeBaseID == "" && t.knowledgeBaseID != "" { + req.KnowledgeBaseID = t.knowledgeBaseID + } request.URL = serviceURL request.Method = "POST" header.Set("Content-Type", "application/json") diff --git a/go/appbuilder/knowledge_base_data.go b/go/appbuilder/knowledge_base_data.go index a6a1f3db..2c36cfef 100644 --- a/go/appbuilder/knowledge_base_data.go +++ b/go/appbuilder/knowledge_base_data.go @@ -190,9 +190,10 @@ type UploadDocumentsResponse struct { } type CreateChunkRequest struct { - DocumentID string `json:"documentId"` - Content string `json:"content"` - ClientToken string `json:"client_token,omitempty"` + KnowledgeBaseID string `json:"knowledgeBaseId"` + DocumentID string `json:"documentId"` + Content string `json:"content"` + ClientToken string `json:"client_token,omitempty"` } type CreateChunkResponse struct { @@ -200,19 +201,22 @@ type CreateChunkResponse struct { } type ModifyChunkRequest struct { - ChunkID string `json:"chunkId"` - Content string `json:"content"` - Enable bool `json:"enable"` - ClientToken string `json:"client_token,omitempty"` + KnowledgeBaseID string `json:"knowledgeBaseId"` + ChunkID string `json:"chunkId"` + Content string `json:"content"` + Enable bool `json:"enable"` + ClientToken string `json:"client_token,omitempty"` } type DeleteChunkRequest struct { - ChunkID string `json:"chunkId"` - ClientToken string `json:"client_token,omitempty"` + KnowledgeBaseID string `json:"knowledgeBaseId"` + ChunkID string `json:"chunkId"` + ClientToken string `json:"client_token,omitempty"` } type DescribeChunkRequest struct { - ChunkID string `json:"chunkId"` + KnowledgeBaseID string `json:"knowledgeBaseId"` + ChunkID string `json:"chunkId"` } type DescribeChunkResponse struct { @@ -232,10 +236,11 @@ type DescribeChunkResponse struct { } type DescribeChunksRequest struct { - DocumnetID string `json:"documentId"` - Marker string `json:"marker,omitempty"` - MaxKeys int `json:"maxKeys,omitempty"` - Type string `json:"type,omitempty"` + KnowledgeBaseID string `json:"knowledgeBaseId"` + DocumnetID string `json:"documentId"` + Marker string `json:"marker,omitempty"` + MaxKeys int `json:"maxKeys,omitempty"` + Type string `json:"type,omitempty"` } type DescribeChunksResponse struct { diff --git a/go/appbuilder/knowledge_base_test.go b/go/appbuilder/knowledge_base_test.go index 5a6ef52b..1a62ca2b 100644 --- a/go/appbuilder/knowledge_base_test.go +++ b/go/appbuilder/knowledge_base_test.go @@ -1405,22 +1405,33 @@ func TestChunk(t *testing.T) { fmt.Fprintf(&logBuffer, format+"\n", args...) } - documentID := os.Getenv(DocumentIDV3) - config, err := NewSDKConfig("", os.Getenv(SecretKeyV3)) + knowledgeBaseID := os.Getenv(DatasetID) + config, err := NewSDKConfig("", os.Getenv(SecretKey)) if err != nil { t.Logf("%s========== FAIL: %s ==========%s", "\033[31m", t.Name(), "\033[0m") t.Fatalf("new http client config failed: %v", err) } - client, err := NewKnowledgeBase(config) + client, err := NewKnowledgeBaseWithKnowledgeBaseID(knowledgeBaseID, config) if err != nil { t.Logf("%s========== FAIL: %s ==========%s", "\033[31m", t.Name(), "\033[0m") t.Fatalf("new Knowledge base instance failed") } + + documentsRes, err := client.GetDocumentList(GetDocumentListRequest{ + KnowledgeBaseID: knowledgeBaseID, + }) + if err != nil { + t.Logf("%s========== FAIL: %s ==========%s", "\033[31m", t.Name(), "\033[0m") + t.Fatalf("get document list failed: %v", err) + } + log("Documents retrieved: %+v", documentsRes) + documentID := documentsRes.Data[0].ID // 创建切片 chunkID, err := client.CreateChunk(CreateChunkRequest{ - DocumentID: documentID, - Content: "test", + KnowledgeBaseID: knowledgeBaseID, + DocumentID: documentID, + Content: "test", }) if err != nil { t.Logf("%s========== FAIL: %s ==========%s", "\033[31m", t.Name(), "\033[0m") @@ -1430,9 +1441,10 @@ func TestChunk(t *testing.T) { // 修改切片 err = client.ModifyChunk(ModifyChunkRequest{ - ChunkID: chunkID, - Content: "new test", - Enable: true, + KnowledgeBaseID: knowledgeBaseID, + ChunkID: chunkID, + Content: "new test", + Enable: true, }) if err != nil { t.Logf("%s========== FAIL: %s ==========%s", "\033[31m", t.Name(), "\033[0m") @@ -1450,9 +1462,10 @@ func TestChunk(t *testing.T) { // 获取切片列表 describeChunksRes, err := client.DescribeChunks(DescribeChunksRequest{ - DocumnetID: documentID, - Marker: chunkID, - MaxKeys: 10, + KnowledgeBaseID: knowledgeBaseID, + DocumnetID: documentID, + Marker: chunkID, + MaxKeys: 10, }) if err != nil { t.Logf("%s========== FAIL: %s ==========%s", "\033[31m", t.Name(), "\033[0m") diff --git a/java/src/main/java/com/baidubce/appbuilder/console/knowledgebase/Knowledgebase.java b/java/src/main/java/com/baidubce/appbuilder/console/knowledgebase/Knowledgebase.java index e670f084..0af88dd0 100644 --- a/java/src/main/java/com/baidubce/appbuilder/console/knowledgebase/Knowledgebase.java +++ b/java/src/main/java/com/baidubce/appbuilder/console/knowledgebase/Knowledgebase.java @@ -17,6 +17,8 @@ import com.baidubce.appbuilder.model.knowledgebase.*; public class Knowledgebase extends Component { + private String knowledgeBaseId; + public Knowledgebase() { super(); } @@ -25,6 +27,11 @@ public Knowledgebase(String SecretKey) { super(SecretKey); } + public Knowledgebase(String knowledgeBaseId, String SecretKey) { + super(SecretKey); + this.knowledgeBaseId = knowledgeBaseId; + } + /** * 上传文档 * @@ -537,8 +544,14 @@ public String createChunk(String documentId, String content, String clientToken) private String innerCreateChunk(String documentId, String content, String clientToken) throws IOException, AppBuilderServerException { String url = AppBuilderConfig.CHUNK_CREATE_URL; - - ChunkCreateRequest request = new ChunkCreateRequest(documentId, content); + + ChunkCreateRequest request; + if(this.knowledgeBaseId.isEmpty()) { + request = new ChunkCreateRequest(documentId, content); + } else { + request = new ChunkCreateRequest(this.knowledgeBaseId, documentId, content); + } + String jsonBody = JsonUtils.serialize(request); url = url + "&clientToken=" + clientToken; ClassicHttpRequest postRequest = httpClient.createPostRequestV2(url, @@ -592,7 +605,12 @@ private void innerModifyChunk(String chunkId, String content, boolean enable, St throws IOException, AppBuilderServerException { String url = AppBuilderConfig.CHUNK_MODIFY_URL; - ChunkModifyRequest request = new ChunkModifyRequest(chunkId, content, enable); + ChunkModifyRequest request; + if (this.knowledgeBaseId.isEmpty()) { + request = new ChunkModifyRequest(chunkId, content, enable); + } else { + request = new ChunkModifyRequest(this.knowledgeBaseId, chunkId, content, enable); + } String jsonBody = JsonUtils.serialize(request); url = url + "&clientToken=" + clientToken; ClassicHttpRequest postRequest = httpClient.createPostRequestV2(url, @@ -634,8 +652,12 @@ public void deleteChunk(String chunkId, String clientToken) throws IOException, */ private void innderDeleteChunk(String chunkId, String clientToken) throws IOException, AppBuilderServerException { String url = AppBuilderConfig.CHUNK_DELETE_URL; + ChunkDeleteRequest request = new ChunkDeleteRequest(); request.setChunkId(chunkId); + if (!this.knowledgeBaseId.isEmpty()) { + request.setKnowledgeBaseId(this.knowledgeBaseId); + } String jsonBody = JsonUtils.serialize(request); url = url + "&clientToken=" + clientToken; ClassicHttpRequest postRequest = httpClient.createPostRequestV2(url, @@ -658,6 +680,9 @@ public ChunkDescribeResponse describeChunk(String chunkId) ChunkDescribeRequest request = new ChunkDescribeRequest(); request.setChunkId(chunkId); + if (!this.knowledgeBaseId.isEmpty()) { + request.setKnowledgeBaseId(this.knowledgeBaseId); + } String jsonBody = JsonUtils.serialize(request); ClassicHttpRequest postRequest = httpClient.createPostRequestV2(url, new StringEntity(jsonBody, StandardCharsets.UTF_8)); @@ -683,6 +708,9 @@ public ChunksDescribeResponse describeChunks(String documentId, String marker, I String url = AppBuilderConfig.CHUNKS_DESCRIBE_URL; ChunksDescribeRequest request = new ChunksDescribeRequest(documentId, marker, maxKeys, type); + if (!this.knowledgeBaseId.isEmpty()) { + request.setKnowledgeBaseId(this.knowledgeBaseId); + } String jsonBody = JsonUtils.serialize(request); ClassicHttpRequest postRequest = httpClient.createPostRequestV2(url, new StringEntity(jsonBody, StandardCharsets.UTF_8)); diff --git a/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunkCreateRequest.java b/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunkCreateRequest.java index 5a59fb12..a1443e35 100644 --- a/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunkCreateRequest.java +++ b/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunkCreateRequest.java @@ -1,19 +1,43 @@ package com.baidubce.appbuilder.model.knowledgebase; public class ChunkCreateRequest { + private String knowledgeBaseId; private String documentId; private String content; + public ChunkCreateRequest(String documetId, String content) { this.documentId = documetId; this.content = content; } + public ChunkCreateRequest(String knowledgeBaseId, String documetId, String content) { + this.knowledgeBaseId = knowledgeBaseId; + this.documentId = documetId; + this.content = content; + } + + public String getKnowledgeBaseId() { + return knowledgeBaseId; + } + + public void setKnowledgeBaseId(String knowledgeBaseId) { + this.knowledgeBaseId = knowledgeBaseId; + } + public String getDocumentId() { return documentId; } + public void setDocumentId(String documentId) { + this.documentId = documentId; + } + public String getContent() { return content; } + + public void setContent(String content) { + this.content = content; + } } diff --git a/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunkDeleteRequest.java b/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunkDeleteRequest.java index 58a1a79a..510f0585 100644 --- a/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunkDeleteRequest.java +++ b/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunkDeleteRequest.java @@ -1,8 +1,17 @@ package com.baidubce.appbuilder.model.knowledgebase; public class ChunkDeleteRequest { + private String knowledgeBaseId; private String chunkId; + public void setKnowledgeBaseId(String knowledgeBaseId) { + this.knowledgeBaseId = knowledgeBaseId; + } + + public String getKnowledgeBaseId() { + return knowledgeBaseId; + } + public void setChunkId(String chunkId) { this.chunkId = chunkId; } diff --git a/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunkDescribeRequest.java b/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunkDescribeRequest.java index 171dddba..1606edef 100644 --- a/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunkDescribeRequest.java +++ b/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunkDescribeRequest.java @@ -1,13 +1,22 @@ package com.baidubce.appbuilder.model.knowledgebase; public class ChunkDescribeRequest { + private String knowledgeBaseId; private String chunkId; public void setChunkId(String chunkId) { this.chunkId = chunkId; } + public void setKnowledgeBaseId(String knowledgeBaseId) { + this.knowledgeBaseId = knowledgeBaseId; + } + public String getChunkId() { return chunkId; } + + public String getKnowledgeBaseId() { + return knowledgeBaseId; + } } diff --git a/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunkModifyRequest.java b/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunkModifyRequest.java index ec3f07a4..afdfb8a7 100644 --- a/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunkModifyRequest.java +++ b/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunkModifyRequest.java @@ -1,6 +1,7 @@ package com.baidubce.appbuilder.model.knowledgebase; public class ChunkModifyRequest { + private String knowledgeBaseId; private String chunkId; private String content; private boolean enable; @@ -11,15 +12,42 @@ public ChunkModifyRequest(String chunkId, String content, boolean enable) { this.enable = enable; } + public ChunkModifyRequest(String knowledgeBaseId, String chunkId, String content, boolean enable) { + this.knowledgeBaseId = knowledgeBaseId; + this.chunkId = chunkId; + this.content = content; + this.enable = enable; + } + + public String getKnowledgeBaseId() { + return knowledgeBaseId; + } + + public void setKnowledgeBaseId(String knowledgeBaseId) { + this.knowledgeBaseId = knowledgeBaseId; + } + public String getChunkId() { return chunkId; } + public void setChunkId(String chunkId) { + this.chunkId = chunkId; + } + public String getContent() { return content; } + public void setContent(String content) { + this.content = content; + } + public boolean getEnable() { return enable; } + + public void setEnable(boolean enable) { + this.enable = enable; + } } diff --git a/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunksDescribeRequest.java b/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunksDescribeRequest.java index d0ae1e56..bd0bad74 100644 --- a/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunksDescribeRequest.java +++ b/java/src/main/java/com/baidubce/appbuilder/model/knowledgebase/ChunksDescribeRequest.java @@ -1,6 +1,7 @@ package com.baidubce.appbuilder.model.knowledgebase; public class ChunksDescribeRequest { + private String knowledgeBaseId; private String documentId; private String marker; private Integer maxKeys; @@ -13,19 +14,52 @@ public ChunksDescribeRequest(String documentId, String marker, Integer maxKeys, this.type = type; } + public ChunksDescribeRequest(String knowledgeBaseId, String documentId, String marker, Integer maxKeys, + String type) { + this.knowledgeBaseId = knowledgeBaseId; + this.documentId = documentId; + this.marker = marker; + this.maxKeys = maxKeys; + this.type = type; + } + + public String getKnowledgeBaseId() { + return knowledgeBaseId; + } + + public void setKnowledgeBaseId(String knowledgeBaseId) { + this.knowledgeBaseId = knowledgeBaseId; + } + public String getDocumentId() { return documentId; } + public void setDocumentId(String documentId) { + this.documentId = documentId; + } + public String getMarker() { return marker; } + public void setMarker(String marker) { + this.marker = marker; + } + public Integer getMaxKeys() { return maxKeys; } + public void setMaxKeys(Integer maxKeys) { + this.maxKeys = maxKeys; + } + public String getType() { return type; } + + public void setType(String type) { + this.type = type; + } } diff --git a/java/src/test/java/com/baidubce/appbuilder/KnowledgebaseTest.java b/java/src/test/java/com/baidubce/appbuilder/KnowledgebaseTest.java index f9a60678..a81b60ee 100644 --- a/java/src/test/java/com/baidubce/appbuilder/KnowledgebaseTest.java +++ b/java/src/test/java/com/baidubce/appbuilder/KnowledgebaseTest.java @@ -139,8 +139,15 @@ public void testCreateKnowledgebase() throws IOException, AppBuilderServerExcept @Test public void testCreateChunk() throws IOException, AppBuilderServerException { - String documentId = System.getenv("DOCUMENT_ID_V3"); - Knowledgebase knowledgebase = new Knowledgebase(); + String knowledgeBaseID = System.getenv("DATASET_ID"); + String secretKey = System.getenv("APPBUILDER_TOKEN"); + + Knowledgebase knowledgebase = new Knowledgebase(knowledgeBaseID, secretKey); + DocumentListRequest listRequest = new DocumentListRequest(); + listRequest.setKonwledgeBaseId(knowledgeBaseID); + listRequest.setLimit(10); + Document[] documents = knowledgebase.getDocumentList(listRequest); + String documentId = documents[0].getId(); // 创建切片 String chunkId = knowledgebase.createChunk(documentId, "test"); // 修改切片 diff --git a/python/core/console/knowledge_base/data_class.py b/python/core/console/knowledge_base/data_class.py index 13924959..ff98bf0e 100644 --- a/python/core/console/knowledge_base/data_class.py +++ b/python/core/console/knowledge_base/data_class.py @@ -198,7 +198,7 @@ class DocumentProcessOption(BaseModel): ) parser: Optional[DocumentChoices] = Field( None, - description="解析方法(文字提取默认启动,参数不体现,layoutAnalysis版面分析,ocr按需增加)", + description="解析方法(文字提取默认启动,参数不体现,layoutAnalysis版面分析,ocr光学字符识别,pageImageAnalysis文档图片解析,chartAnalysis图表解析,tableAnalysis表格深度解析,按需增加)", ) knowledgeAugmentation: Optional[DocumentChoices] = Field( None, @@ -230,6 +230,7 @@ class KnowledgeBaseUploadDocumentsResponse(BaseModel): class CreateChunkRequest(BaseModel): + knowledgeBaseId: str = Field(None, description="知识库ID") documentId: str = Field(..., description="文档ID") content: str = Field(..., description="文档内容") @@ -239,16 +240,19 @@ class CreateChunkResponse(BaseModel): class ModifyChunkRequest(BaseModel): + knowledgeBaseId: str = Field(None, description="知识库ID") chunkId: str = Field(..., description="切片ID") content: str = Field(..., description="文档内容") enable: bool = Field(..., description="是否启用") class DeleteChunkRequest(BaseModel): + knowledgeBaseId: str = Field(None, description="知识库ID") chunkId: str = Field(..., description="切片ID") class DescribeChunkRequest(BaseModel): + knowledgeBaseId: str = Field(None, description="知识库ID") chunkId: str = Field(..., description="切片ID") @@ -269,6 +273,7 @@ class DescribeChunkResponse(BaseModel): class DescribeChunksRequest(BaseModel): + knowledgeBaseId: str = Field(None, description="知识库ID") documentId: str = Field(..., description="文档ID") marker: Optional[str] = Field(None, description="起始位置") maxKeys: Optional[int] = Field( diff --git a/python/core/console/knowledge_base/knowledge_base.py b/python/core/console/knowledge_base/knowledge_base.py index c34a953e..c30b4d2c 100644 --- a/python/core/console/knowledge_base/knowledge_base.py +++ b/python/core/console/knowledge_base/knowledge_base.py @@ -646,6 +646,7 @@ def create_chunk( documentId: str, content: str, client_token: str = None, + knowledgebase_id: Optional[str] = None, ) -> data_class.CreateChunkResponse: r""" 创建文档块 @@ -670,6 +671,7 @@ def create_chunk( ) request = data_class.CreateChunkRequest( + knowledgeBaseId=knowledgebase_id or self.knowledge_id, documentId=documentId, content=content, ) @@ -690,6 +692,7 @@ def modify_chunk( chunkId: str, content: str, enable: bool, + knowledgebase_id: Optional[str] = None, client_token: str = None, ): r""" @@ -713,6 +716,7 @@ def modify_chunk( ) request = data_class.ModifyChunkRequest( + knowledgeBaseId=knowledgebase_id or self.knowledge_id, chunkId=chunkId, content=content, enable=enable, @@ -731,6 +735,7 @@ def modify_chunk( def delete_chunk( self, chunkId: str, + knowledgebase_id: Optional[str] = None, client_token: str = None, ): r""" @@ -753,6 +758,7 @@ def delete_chunk( ) request = data_class.DeleteChunkRequest( + knowledgeBaseId=knowledgebase_id or self.knowledge_id, chunkId=chunkId, ) @@ -769,6 +775,7 @@ def delete_chunk( def describe_chunk( self, chunkId: str, + knowledgebase_id: Optional[str] = None, ) -> data_class.DescribeChunkResponse: r""" 获取文档块详情 @@ -797,6 +804,7 @@ def describe_chunk( url = self.http_client.service_url_v2("/knowledgeBase?Action=DescribeChunk") request = data_class.DescribeChunkRequest( + knowledgeBaseId=knowledgebase_id or self.knowledge_id, chunkId=chunkId, ) @@ -814,6 +822,7 @@ def describe_chunk( def describe_chunks( self, documentId: str, + knowledgebase_id: Optional[str] = None, marker: str = None, maxKeys: int = None, type: str = None, @@ -841,6 +850,7 @@ def describe_chunks( url = self.http_client.service_url_v2("/knowledgeBase?Action=DescribeChunks") request = data_class.DescribeChunksRequest( + knowledgeBaseId=knowledgebase_id or self.knowledge_id, documentId=documentId, marker=marker, maxKeys=maxKeys, diff --git a/python/tests/test_knowledge_base.py b/python/tests/test_knowledge_base.py index c90ae78b..0f8e430e 100644 --- a/python/tests/test_knowledge_base.py +++ b/python/tests/test_knowledge_base.py @@ -133,13 +133,13 @@ def test_create_knowledge_base(self): list_res = knowledge.get_documents_list( knowledge_base_id=knowledge_base_id) document_id = list_res.data[-1].id - res = knowledge.describe_chunks(document_id) - resp = knowledge.create_chunk(document_id, content="test") + res = knowledge.describe_chunks(document_id, knowledgebase_id=knowledge_base_id) + resp = knowledge.create_chunk(document_id, content="test", knowledgebase_id=knowledge_base_id) chunk_id = resp.id - knowledge.modify_chunk(chunk_id, content="new test", enable=True) + knowledge.modify_chunk(chunk_id, content="new test", enable=True, knowledgebase_id=knowledge_base_id) # 目前openapi有延迟,后续openapi完善后,删除注释 - # knowledge.describe_chunk(chunk_id) - knowledge.delete_chunk(chunk_id) + # knowledge.describe_chunk(chunk_id, knowledgebase_id=knowledge_base_id) + knowledge.delete_chunk(chunk_id, knowledgebase_id=knowledge_base_id) knowledge.modify_knowledge_base( knowledge_base_id=knowledge_base_id, name="test"