Skip to content

Commit

Permalink
知识库创建接口对齐\upload_file接口隐藏
Browse files Browse the repository at this point in the history
  • Loading branch information
yinjiaqi authored and yinjiaqi committed Jan 23, 2025
1 parent b38dd18 commit 5d667d9
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 64 deletions.
63 changes: 9 additions & 54 deletions docs/BasisModule/Platform/KnowledgeBase/knowledgebase.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,41 +441,7 @@ knowledge.upload_documents(
)
```

### 9、上传通用文档 `KnowledgeBase().upload_file(file_path: str)->KnowledgeBaseUploadFileResponse`

#### 方法参数
| 参数名称 | 参数类型 | 是否必传 | 描述 | 示例值 |
| --------- | -------- | -------- | -------------- | -------------- |
| file_path | string || 本地的文件路径 | "/home/my_doc" |

#### 方法返回值
`KnowledgeBaseUploadFileResponse` 类定义如下

```python
class KnowledgeBaseUploadFileResponse(BaseModel):
request_id: str = Field(..., description="请求ID")
id: str = Field(..., description="文件ID")
name: str = Field(..., description="文件名称")
```


#### 方法示例
```python
import os
import appbuilder
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)

upload_res = my_knowledge.upload_file("./test.txt")
print(upload_res)

# 知识库ID: da51a988-cbe7-4b24-aa5b-768985e8xxxx
# request_id='255eec22-ec87-4564-bdeb-3e5623eaxxxx' id='ef12119b-d5be-492a-997c-77f8e84axxxx' name='test.txt'
```
### 10、向知识库添加文档 `KnowledgeBase().add_document()->KnowledgeBaseAddDocumentResponse`
### 9、向知识库添加文档 `KnowledgeBase().add_document()->KnowledgeBaseAddDocumentResponse`

#### 方法参数
| 参数名称 | 参数类型 | 是否必传 | 描述 | 示例值 |
Expand Down Expand Up @@ -525,7 +491,7 @@ print("添加文档结果: ",add_res)
# 添加文档结果: request_id='412e1630-b570-47c9-a042-caf3cd9dxxxx' knowledge_base_id='da51a988-cbe7-4b24-aa5b-768985e8xxxx' document_ids=['5e0eb279-7688-4100-95d1-241f3d19xxxx']
```

### 11、从知识库删除文档 `KnowledgeBase().delete_document()->KnowledgeBaseDeleteDocumentResponse`
### 10、从知识库删除文档 `KnowledgeBase().delete_document()->KnowledgeBaseDeleteDocumentResponse`

#### 方法参数
| 参数名称 | 参数类型 | 是否必传 | 描述 | 示例值 |
Expand Down Expand Up @@ -566,7 +532,7 @@ print("删除文档结果: ",delete_res)
# 删除文档结果: request_id='ba0e8bc0-b799-45b5-bdac-0d4c50e2xxxx'
```

### 12、获取知识库的文档列表`KnowledgeBase().get_documents_list()->KnowledgeBaseGetDocumentsListResponse`
### 11、获取知识库的文档列表`KnowledgeBase().get_documents_list()->KnowledgeBaseGetDocumentsListResponse`

#### 方法参数
| 参数名称 | 参数类型 | 是否必传 | 描述 | 示例值 |
Expand Down Expand Up @@ -617,7 +583,7 @@ print("文档列表: ", list_res)
# 文档列表: request_id='f66c2193-6035-4022-811b-c4cd7743xxxx' data=[{'id': '8f388b10-5e6a-423f-8acc-dd5fdc2fxxxx', 'name': 'test.txt', 'created_at': 1719988868, 'word_count': 16886, 'enabled': True, 'meta': {'source': 'upload_file', 'file_id': '0ebb03fb-ea48-4c49-b494-cf0cec11xxxx'}}, {'id': '5e0eb279-7688-4100-95d1-241f3d19xxxx', 'name': 'test.txt', 'created_at': 1719987921, 'word_count': 16886, 'enabled': True, 'meta': {'source': 'upload_file', 'file_id': '059e2ae2-1e3c-43ea-8b42-5d988f93xxxx'}}]
```

### 13、获取知识库全部文档`KnowledgeBase().get_all_documents()->list`
### 12、获取知识库全部文档`KnowledgeBase().get_all_documents()->list`

#### 方法参数
| 参数名称 | 参数类型 | 是否必传 | 描述 | 示例值 |
Expand Down Expand Up @@ -658,7 +624,7 @@ for message in doc_list:
print(message)
```

### 14. 创建切片`create_chunk(documentId: str, content: str) -> CreateChunkResponse`
### 13. 创建切片`create_chunk(documentId: str, content: str) -> CreateChunkResponse`

#### 方法参数

Expand Down Expand Up @@ -692,7 +658,7 @@ print("切片ID: ", resp.id)
chunk_id = resp.id
```

### 15. 修改切片信息`modify_chunk(chunkId: str, content: str, enable: bool)`
### 14. 修改切片信息`modify_chunk(chunkId: str, content: str, enable: bool)`

#### 方法参数

Expand All @@ -716,7 +682,7 @@ print("知识库ID: ", my_knowledge.knowledge_id)
my_knowledge.modify_chunk("your_chunk_id", "content", True, knowledgebase_id=my_knowledge_base_id)
```

### 16. 删除切片`delete_chunk(chunkId: str)`
### 15. 删除切片`delete_chunk(chunkId: str)`

#### 方法参数

Expand All @@ -738,7 +704,7 @@ print("知识库ID: ", my_knowledge.knowledge_id)
my_knowledge.delete_chunk("your_chunk_id", knowledgebase_id=my_knowledge_base_id)
```

### 17. 获取切片信息`describe_chunk(chunkId: str)`
### 16. 获取切片信息`describe_chunk(chunkId: str)`

#### 方法参数

Expand Down Expand Up @@ -783,7 +749,7 @@ print("切片详情:")
print(resp)
```

### 18. 获取切片列表`describe_chunks(documentId: str, marker: str = None, maxKeys: int = None, type: str = None) -> DescribeChunksResponse`
### 17. 获取切片列表`describe_chunks(documentId: str, marker: str = None, maxKeys: int = None, type: str = None) -> DescribeChunksResponse`

#### 方法参数

Expand Down Expand Up @@ -871,10 +837,6 @@ public class KnowledgebaseTest {
listRequest.setKonwledgeBaseId(knowledgeBaseId);
listRequest.setLimit(10);
Document[] documents = knowledgebase.getDocumentList(listRequest);

// 向知识库上传通用文档
String fileId = knowledgebase.uploadFile("src/test/java/com/baidubce/appbuilder/files/test.pdf");
System.out.println(fileId);

// 向知识库添加文档
DocumentAddRequest request = new DocumentAddRequest();
Expand Down Expand Up @@ -1031,13 +993,6 @@ func TestKnowledgeBase(t *testing.T) {
}
fmt.Println(documentsRes)

// 向知识库中上传通用文件
fileID, err := client.UploadFile("./files/test.pdf")
if err != nil {
t.Fatalf("upload file failed: %v", err)
}
fmt.Println(fileID)

// 向知识库中添加文档
createDocumentRes, err := client.CreateDocument(CreateDocumentRequest{
KnowledgeBaseID: knowledgeBaseID,
Expand Down
1 change: 1 addition & 0 deletions go/appbuilder/knowledge_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func (t *KnowledgeBase) GetDocumentList(req GetDocumentListRequest) (*GetDocumen
return &rsp, nil
}

// Deprecated: 此功能即将废弃
func (t *KnowledgeBase) UploadFile(localFilePath string) (string, error) {
var data bytes.Buffer
w := multipart.NewWriter(&data)
Expand Down
9 changes: 5 additions & 4 deletions go/appbuilder/knowledge_base_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ type KnowlegeBaseConfig struct {
}

type KnowledgeBaseConfigIndex struct {
Type string `json:"type"`
EsUrl string `json:"esUrl"`
Username string `json:"username"`
Password string `json:"password"`
Type string `json:"type"`
ClusterId string `json:"clusterId"`
Username string `json:"username"`
Password string `json:"password"`
Location string `json:"location"`
}

type KnowledgeBaseDetail struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public Knowledgebase(String knowledgeBaseId, String SecretKey) {
* @throws IOException 当文件上传失败时抛出IOException
* @throws AppBuilderServerException 当服务器返回错误码时抛出AppBuilderServerException
*/
@Deprecated
public String uploadFile(String filePath) throws IOException, AppBuilderServerException {
return innerUploadFile(filePath, java.util.UUID.randomUUID().toString());
}
Expand All @@ -53,6 +54,7 @@ public String uploadFile(String filePath) throws IOException, AppBuilderServerEx
* @throws IOException 如果发生I/O错误
* @throws AppBuilderServerException 如果应用构建服务器发生错误
*/
@Deprecated
public String uploadFile(String filePath, String clientToken) throws IOException, AppBuilderServerException {
return innerUploadFile(filePath, clientToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,25 @@ public Index getIndex() {

public static class Index {
private String type;
private String esUrl;
private String clusterId;
private String username;
private String password;
private String location;

public Index(String type, String esUrl, String username, String password) {
public Index(String type, String clusterId, String username, String password, String location) {
this.type = type;
this.esUrl = esUrl;
this.clusterId = clusterId;
this.username = username;
this.password = password;
this.location = location;
}

public String getType() {
return type;
}

public String getEsUrl() {
return esUrl;
public String getClusterId() {
return clusterId;
}

public String getUsername() {
Expand All @@ -39,5 +41,9 @@ public String getUsername() {
public String getPassword() {
return password;
}

public String getLocation() {
return location;
}
}
}
4 changes: 3 additions & 1 deletion python/core/console/knowledge_base/data_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ class KnowledgeBaseGetDocumentsListResponse(BaseModel):

class KnowledgeBaseConfigIndex(BaseModel):
type: str = Field(..., description="索引类型", enum=["public", "bes", "vdb"])
esUrl: Optional[str] = Field(None, description="bes地址")
clusterId: Optional[str] = Field(None, description="集群/实例 ID")
username: Optional[str] = Field(None, description="bes用户名")
password: Optional[str] = Field(None, description="bes密码")
location: Optional[str] = Field(None, description="托管资源的区域", enum=["bj", "bd", "sz", "gz"])


class KnowledgeBaseConfig(BaseModel):
Expand All @@ -117,6 +118,7 @@ class KnowledgeBaseGetDetailRequest(BaseModel):


class KnowledgeBaseDetailResponse(BaseModel):
requestId: str = Field(..., description="请求ID")
id: str = Field(..., description="知识库ID")
name: str = Field(..., description="知识库名称")
description: Optional[str] = Field(None, description="知识库描述")
Expand Down
1 change: 1 addition & 0 deletions python/core/console/knowledge_base/knowledge_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def create_knowledge(cls, knowledge_name: str) -> "KnowledgeBase":
knowledge_id=response["id"], knowledge_name=response["name"]
)

@deprecated
def upload_file(
self, file_path: str, client_token: str = None
) -> data_class.KnowledgeBaseUploadFileResponse:
Expand Down

0 comments on commit 5d667d9

Please sign in to comment.