Skip to content

Commit cd32b14

Browse files
authored
知识库管理相关功能 (#34)
* 定义不太文件类型解析类 * 文件管理相关接口 * 知识库管理业务 * 知识库管理集成测试 * 知识库管理集成测试 * 修改embedding_model * pythonsdk,上传知识库文件接口缺少参数custom_separator pythonsdk,查询知识库接口多了一个data参数,参考文档修改 pythonsdk,文件上传接口,response对象DocumentSuccessinfo中多余参数,参考文档修改 请求knowledge.document.list报错 查询文件详情接口参照文档修改,多了几个参数 * pydantic-core = ">=2.14.6"
1 parent 3006bb1 commit cd32b14

38 files changed

+1584
-275
lines changed

pyproject.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ readme = "README.md"
99
python = ">=3.7.1,<4.0,!=3.9.7 "
1010
httpx = ">=0.23.0"
1111
pydantic = ">=1.9.0,<3.0"
12+
pydantic-core = ">=2.14.6"
1213
cachetools = ">=4.2.2"
1314
pyjwt = "~=2.8.0"
15+
pandas = {version = ">=1.3.0", python = ">=3.9,<3.9.7 || >3.9.7,<4.0"}
1416

1517

1618
[tool.poetry.group.test.dependencies]
@@ -86,10 +88,6 @@ markers = [
8688
]
8789
asyncio_mode = "auto"
8890

89-
[tool.poetry.plugins.dotenv]
90-
ignore = "false"
91-
location = ".env"
92-
9391

9492
# https://python-poetry.org/docs/repositories/
9593
[[tool.poetry.source]]

tests/integration_tests/file.xlsx

8.75 KB
Binary file not shown.

tests/integration_tests/test_file.py

Lines changed: 82 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,88 @@
1-
from zhipuai import ZhipuAI
2-
import zhipuai
1+
2+
from __future__ import annotations
3+
4+
import unittest
35
import os
46

57
import logging
68
import logging.config
79

10+
import zhipuai
11+
12+
import httpx
13+
import pytest
14+
from respx import MockRouter
15+
16+
from zhipuai import ZhipuAI
17+
from zhipuai.api_resource import FilesWithRawResponse
18+
19+
@pytest.fixture(scope='class')
20+
def test_server():
21+
class SharedData:
22+
client = ZhipuAI()
23+
file_id1 = None
24+
file_id2 = None
25+
26+
return SharedData()
27+
28+
29+
class TestZhipuAIFileServer:
30+
31+
def test_logs(self, logging_conf):
32+
logging.config.dictConfig(logging_conf) # type: ignore
33+
34+
def test_files(self,test_server, test_file_path):
35+
36+
try:
37+
result = test_server.client.files.create(
38+
file=open(os.path.join(test_file_path,"demo.jsonl"), "rb"),
39+
purpose="fine-tune"
40+
)
41+
print(result)
42+
test_server.file_id1 = result.id
43+
44+
45+
except zhipuai.core._errors.APIRequestFailedError as err:
46+
print(err)
47+
except zhipuai.core._errors.APIInternalError as err:
48+
print(err)
49+
except zhipuai.core._errors.APIStatusError as err:
50+
print(err)
51+
52+
53+
def test_files_validation(self,test_server, test_file_path):
54+
try:
55+
result = test_server.client.files.create(
56+
file=open(os.path.join(test_file_path,"demo.jsonl"), "rb"),
57+
purpose="fine-tune"
58+
)
59+
print(result)
60+
61+
test_server.file_id2 = result.id
62+
63+
64+
65+
except zhipuai.core._errors.APIRequestFailedError as err:
66+
print(err)
67+
except zhipuai.core._errors.APIInternalError as err:
68+
print(err)
69+
except zhipuai.core._errors.APIStatusError as err:
70+
print(err)
71+
72+
73+
def test_files_list(self,test_server):
74+
try:
75+
list = test_server.client.files.list()
76+
print(list)
77+
78+
79+
80+
except zhipuai.core._errors.APIRequestFailedError as err:
81+
print(err)
82+
except zhipuai.core._errors.APIInternalError as err:
83+
print(err)
84+
except zhipuai.core._errors.APIStatusError as err:
85+
print(err)
86+
87+
888

9-
def test_files(test_file_path, logging_conf):
10-
logging.config.dictConfig(logging_conf) # type: ignore
11-
client = ZhipuAI()
12-
try:
13-
result = client.files.create(
14-
file=open(os.path.join(test_file_path,"demo.jsonl"), "rb"),
15-
purpose="fine-tune"
16-
)
17-
print(result)
18-
# "file-20240418025911536-6dqgr"
19-
20-
21-
except zhipuai.core._errors.APIRequestFailedError as err:
22-
print(err)
23-
except zhipuai.core._errors.APIInternalError as err:
24-
print(err)
25-
except zhipuai.core._errors.APIStatusError as err:
26-
print(err)
27-
28-
29-
def test_files_validation(test_file_path, logging_conf):
30-
logging.config.dictConfig(logging_conf) # type: ignore
31-
client = ZhipuAI()
32-
try:
33-
result = client.files.create(
34-
file=open(os.path.join(test_file_path,"demo.jsonl"), "rb"),
35-
purpose="fine-tune"
36-
)
37-
print(result)
38-
# "file-20240418025931214-c87tj"
39-
40-
41-
42-
except zhipuai.core._errors.APIRequestFailedError as err:
43-
print(err)
44-
except zhipuai.core._errors.APIInternalError as err:
45-
print(err)
46-
except zhipuai.core._errors.APIStatusError as err:
47-
print(err)
48-
49-
def test_files_list(logging_conf):
50-
logging.config.dictConfig(logging_conf) # type: ignore
51-
client = ZhipuAI()
52-
try:
53-
list = client.files.list()
54-
print(list)
55-
56-
57-
58-
except zhipuai.core._errors.APIRequestFailedError as err:
59-
print(err)
60-
except zhipuai.core._errors.APIInternalError as err:
61-
print(err)
62-
except zhipuai.core._errors.APIStatusError as err:
63-
print(err)
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
from __future__ import annotations
2+
3+
import unittest
4+
import os
5+
6+
import logging
7+
import logging.config
8+
9+
import zhipuai
10+
11+
import httpx
12+
import pytest
13+
from respx import MockRouter
14+
15+
from zhipuai import ZhipuAI
16+
17+
18+
@pytest.fixture(scope='class')
19+
def test_server():
20+
class SharedData:
21+
client = ZhipuAI()
22+
test_knowledge_document_id = None
23+
test_knowledge_id = None
24+
25+
return SharedData()
26+
27+
28+
class TestZhipuAIKnowledgeServer:
29+
30+
def test_logs(self, logging_conf):
31+
logging.config.dictConfig(logging_conf) # type: ignore
32+
33+
def test_knowledge_create(self, test_server):
34+
35+
try:
36+
result = test_server.client.knowledge.create(
37+
embedding_id=1,
38+
name="test",
39+
description="测试",
40+
background="blue",
41+
icon="question",
42+
43+
)
44+
print(result)
45+
test_server.test_knowledge_id = result.id
46+
47+
48+
except zhipuai.core._errors.APIRequestFailedError as err:
49+
print(err)
50+
except zhipuai.core._errors.APIInternalError as err:
51+
print(err)
52+
except zhipuai.core._errors.APIStatusError as err:
53+
print(err)
54+
55+
def test_knowledge_document_create(self, test_server, test_file_path):
56+
57+
try:
58+
result = test_server.client.knowledge.document.create(
59+
file=open(os.path.join(test_file_path, "file.xlsx"), "rb"),
60+
purpose="retrieval",
61+
knowledge_id=test_server.test_knowledge_id,
62+
sentence_size=202
63+
)
64+
print(result)
65+
test_server.test_knowledge_document_id = result.successInfos[0].documentId
66+
67+
68+
except zhipuai.core._errors.APIRequestFailedError as err:
69+
print(err)
70+
except zhipuai.core._errors.APIInternalError as err:
71+
print(err)
72+
except zhipuai.core._errors.APIStatusError as err:
73+
print(err)
74+
75+
def test_knowledge_modify(self, test_server):
76+
77+
try:
78+
result = test_server.client.knowledge.modify(
79+
knowledge_id=test_server.test_knowledge_id,
80+
embedding_id=1,
81+
name="测试1",
82+
background="red",
83+
icon="book",
84+
85+
)
86+
print(result)
87+
88+
89+
except zhipuai.core._errors.APIRequestFailedError as err:
90+
print(err)
91+
except zhipuai.core._errors.APIInternalError as err:
92+
print(err)
93+
except zhipuai.core._errors.APIStatusError as err:
94+
print(err)
95+
96+
def test_knowledge_query(self, test_server):
97+
98+
try:
99+
result = test_server.client.knowledge.query(
100+
)
101+
print(result)
102+
103+
104+
except zhipuai.core._errors.APIRequestFailedError as err:
105+
print(err)
106+
except zhipuai.core._errors.APIInternalError as err:
107+
print(err)
108+
except zhipuai.core._errors.APIStatusError as err:
109+
print(err)
110+
111+
def test_knowledge_used(self, test_server):
112+
113+
try:
114+
result = test_server.client.knowledge.used(
115+
)
116+
print(result)
117+
118+
119+
except zhipuai.core._errors.APIRequestFailedError as err:
120+
print(err)
121+
except zhipuai.core._errors.APIInternalError as err:
122+
print(err)
123+
except zhipuai.core._errors.APIStatusError as err:
124+
print(err)
125+
126+
def test_knowledge_document_retrieve(self, test_server, test_file_path):
127+
try:
128+
result = test_server.client.knowledge.document.retrieve(
129+
test_server.test_knowledge_document_id
130+
)
131+
print(result)
132+
133+
except zhipuai.core._errors.APIRequestFailedError as err:
134+
print(err)
135+
except zhipuai.core._errors.APIInternalError as err:
136+
print(err)
137+
except zhipuai.core._errors.APIStatusError as err:
138+
print(err)
139+
140+
def test_knowledge_document_edit(self, test_server):
141+
try:
142+
result = test_server.client.knowledge.document.edit(
143+
document_id=test_server.test_knowledge_document_id,
144+
knowledge_type="1",
145+
sentence_size=204,
146+
)
147+
print(result)
148+
149+
except zhipuai.core._errors.APIRequestFailedError as err:
150+
print(err)
151+
except zhipuai.core._errors.APIInternalError as err:
152+
print(err)
153+
except zhipuai.core._errors.APIStatusError as err:
154+
print(err)
155+
156+
def test_knowledge_document_list(self, test_server):
157+
try:
158+
result = test_server.client.knowledge.document.list(
159+
test_server.test_knowledge_id,
160+
purpose="retrieval"
161+
)
162+
print(result)
163+
164+
except zhipuai.core._errors.APIRequestFailedError as err:
165+
print(err)
166+
except zhipuai.core._errors.APIInternalError as err:
167+
print(err)
168+
except zhipuai.core._errors.APIStatusError as err:
169+
print(err)
170+
171+
def test_knowledge_document_delete(self, test_server):
172+
try:
173+
file1 = test_server.client.knowledge.document.delete(test_server.test_knowledge_document_id)
174+
print(file1)
175+
176+
except zhipuai.core._errors.APIRequestFailedError as err:
177+
print(err)
178+
except zhipuai.core._errors.APIInternalError as err:
179+
print(err)
180+
except zhipuai.core._errors.APIStatusError as err:
181+
print(err)
182+
183+
def test_knowledge_delete(self, test_server):
184+
185+
try:
186+
result = test_server.client.knowledge.delete(
187+
knowledge_id=test_server.test_knowledge_id
188+
)
189+
print(result)
190+
191+
192+
except zhipuai.core._errors.APIRequestFailedError as err:
193+
print(err)
194+
except zhipuai.core._errors.APIInternalError as err:
195+
print(err)
196+
except zhipuai.core._errors.APIStatusError as err:
197+
print(err)

tests/unit_tests/batchinput.jsonl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"custom_id": "request-1", "method": "POST", "url": "/v4/chat/completions", "body": {"model": "glm-4", "messages": [{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "Hello world!"}],"max_tokens": 1000}}

tests/unit_tests/batchinput.xlsx

8.75 KB
Binary file not shown.

tests/unit_tests/response_model/test_response.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
# -*- coding: utf-8 -*-
2-
import unittest
3-
from typing import Type, cast, Iterable
2+
from typing import Type
43
import pytest
54
import httpx
6-
import inspect
75

8-
import pydantic
9-
from zhipuai.core import BaseModel, StreamResponse, get_args, HttpClient, construct_type
10-
from zhipuai.core._base_type import ResponseT, ModelBuilderProtocol
6+
from zhipuai.core import BaseModel, HttpClient
7+
from zhipuai.core._base_type import ResponseT
118
from zhipuai.core._request_opt import FinalRequestOptions
129
from zhipuai.core._response import APIResponse
1310
from zhipuai.types.chat.async_chat_completion import AsyncTaskStatus, AsyncCompletion
@@ -17,11 +14,10 @@
1714
CompletionUsage as ChatCompletionUsage)
1815

1916
from zhipuai.types.embeddings import Embedding, EmbeddingsResponded
20-
from zhipuai.types.file_object import FileObject, ListOfFileObject
17+
from zhipuai.types.files.file_object import FileObject, ListOfFileObject
2118
from zhipuai.types.fine_tuning import FineTuningJobEvent
22-
from zhipuai.types.fine_tuning.fine_tuning_job import FineTuningJob, ListOfFineTuningJob, Error
19+
from zhipuai.types.fine_tuning.fine_tuning_job import FineTuningJob, Error
2320
from zhipuai.types.fine_tuning.fine_tuning_job_event import Metric, JobEvent
24-
from zhipuai.types.fine_tuning.job_create_params import Hyperparameters
2521
from zhipuai.types.fine_tuning.fine_tuning_job import Hyperparameters as FineTuningHyperparameters
2622
from zhipuai.types.fine_tuning.models import FineTunedModelsStatus
2723
from zhipuai.types.image import GeneratedImage, ImagesResponded

0 commit comments

Comments
 (0)