Skip to content

Commit

Permalink
知识库管理相关功能 (#34)
Browse files Browse the repository at this point in the history
* 定义不太文件类型解析类

* 文件管理相关接口

* 知识库管理业务

* 知识库管理集成测试

* 知识库管理集成测试

* 修改embedding_model

* pythonsdk,上传知识库文件接口缺少参数custom_separator
pythonsdk,查询知识库接口多了一个data参数,参考文档修改
pythonsdk,文件上传接口,response对象DocumentSuccessinfo中多余参数,参考文档修改
请求knowledge.document.list报错
查询文件详情接口参照文档修改,多了几个参数

* pydantic-core = ">=2.14.6"
  • Loading branch information
glide-the authored Jun 20, 2024
1 parent 3006bb1 commit cd32b14
Show file tree
Hide file tree
Showing 38 changed files with 1,584 additions and 275 deletions.
6 changes: 2 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ readme = "README.md"
python = ">=3.7.1,<4.0,!=3.9.7 "
httpx = ">=0.23.0"
pydantic = ">=1.9.0,<3.0"
pydantic-core = ">=2.14.6"
cachetools = ">=4.2.2"
pyjwt = "~=2.8.0"
pandas = {version = ">=1.3.0", python = ">=3.9,<3.9.7 || >3.9.7,<4.0"}


[tool.poetry.group.test.dependencies]
Expand Down Expand Up @@ -86,10 +88,6 @@ markers = [
]
asyncio_mode = "auto"

[tool.poetry.plugins.dotenv]
ignore = "false"
location = ".env"


# https://python-poetry.org/docs/repositories/
[[tool.poetry.source]]
Expand Down
Binary file added tests/integration_tests/file.xlsx
Binary file not shown.
139 changes: 82 additions & 57 deletions tests/integration_tests/test_file.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,88 @@
from zhipuai import ZhipuAI
import zhipuai

from __future__ import annotations

import unittest
import os

import logging
import logging.config

import zhipuai

import httpx
import pytest
from respx import MockRouter

from zhipuai import ZhipuAI
from zhipuai.api_resource import FilesWithRawResponse

@pytest.fixture(scope='class')
def test_server():
class SharedData:
client = ZhipuAI()
file_id1 = None
file_id2 = None

return SharedData()


class TestZhipuAIFileServer:

def test_logs(self, logging_conf):
logging.config.dictConfig(logging_conf) # type: ignore

def test_files(self,test_server, test_file_path):

try:
result = test_server.client.files.create(
file=open(os.path.join(test_file_path,"demo.jsonl"), "rb"),
purpose="fine-tune"
)
print(result)
test_server.file_id1 = result.id


except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)


def test_files_validation(self,test_server, test_file_path):
try:
result = test_server.client.files.create(
file=open(os.path.join(test_file_path,"demo.jsonl"), "rb"),
purpose="fine-tune"
)
print(result)

test_server.file_id2 = result.id



except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)


def test_files_list(self,test_server):
try:
list = test_server.client.files.list()
print(list)



except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)



def test_files(test_file_path, logging_conf):
logging.config.dictConfig(logging_conf) # type: ignore
client = ZhipuAI()
try:
result = client.files.create(
file=open(os.path.join(test_file_path,"demo.jsonl"), "rb"),
purpose="fine-tune"
)
print(result)
# "file-20240418025911536-6dqgr"


except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)


def test_files_validation(test_file_path, logging_conf):
logging.config.dictConfig(logging_conf) # type: ignore
client = ZhipuAI()
try:
result = client.files.create(
file=open(os.path.join(test_file_path,"demo.jsonl"), "rb"),
purpose="fine-tune"
)
print(result)
# "file-20240418025931214-c87tj"



except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)

def test_files_list(logging_conf):
logging.config.dictConfig(logging_conf) # type: ignore
client = ZhipuAI()
try:
list = client.files.list()
print(list)



except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)
197 changes: 197 additions & 0 deletions tests/integration_tests/test_knowledge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
from __future__ import annotations

import unittest
import os

import logging
import logging.config

import zhipuai

import httpx
import pytest
from respx import MockRouter

from zhipuai import ZhipuAI


@pytest.fixture(scope='class')
def test_server():
class SharedData:
client = ZhipuAI()
test_knowledge_document_id = None
test_knowledge_id = None

return SharedData()


class TestZhipuAIKnowledgeServer:

def test_logs(self, logging_conf):
logging.config.dictConfig(logging_conf) # type: ignore

def test_knowledge_create(self, test_server):

try:
result = test_server.client.knowledge.create(
embedding_id=1,
name="test",
description="测试",
background="blue",
icon="question",

)
print(result)
test_server.test_knowledge_id = result.id


except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)

def test_knowledge_document_create(self, test_server, test_file_path):

try:
result = test_server.client.knowledge.document.create(
file=open(os.path.join(test_file_path, "file.xlsx"), "rb"),
purpose="retrieval",
knowledge_id=test_server.test_knowledge_id,
sentence_size=202
)
print(result)
test_server.test_knowledge_document_id = result.successInfos[0].documentId


except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)

def test_knowledge_modify(self, test_server):

try:
result = test_server.client.knowledge.modify(
knowledge_id=test_server.test_knowledge_id,
embedding_id=1,
name="测试1",
background="red",
icon="book",

)
print(result)


except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)

def test_knowledge_query(self, test_server):

try:
result = test_server.client.knowledge.query(
)
print(result)


except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)

def test_knowledge_used(self, test_server):

try:
result = test_server.client.knowledge.used(
)
print(result)


except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)

def test_knowledge_document_retrieve(self, test_server, test_file_path):
try:
result = test_server.client.knowledge.document.retrieve(
test_server.test_knowledge_document_id
)
print(result)

except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)

def test_knowledge_document_edit(self, test_server):
try:
result = test_server.client.knowledge.document.edit(
document_id=test_server.test_knowledge_document_id,
knowledge_type="1",
sentence_size=204,
)
print(result)

except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)

def test_knowledge_document_list(self, test_server):
try:
result = test_server.client.knowledge.document.list(
test_server.test_knowledge_id,
purpose="retrieval"
)
print(result)

except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)

def test_knowledge_document_delete(self, test_server):
try:
file1 = test_server.client.knowledge.document.delete(test_server.test_knowledge_document_id)
print(file1)

except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)

def test_knowledge_delete(self, test_server):

try:
result = test_server.client.knowledge.delete(
knowledge_id=test_server.test_knowledge_id
)
print(result)


except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)
1 change: 1 addition & 0 deletions tests/unit_tests/batchinput.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +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}}
Binary file added tests/unit_tests/batchinput.xlsx
Binary file not shown.
14 changes: 5 additions & 9 deletions tests/unit_tests/response_model/test_response.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# -*- coding: utf-8 -*-
import unittest
from typing import Type, cast, Iterable
from typing import Type
import pytest
import httpx
import inspect

import pydantic
from zhipuai.core import BaseModel, StreamResponse, get_args, HttpClient, construct_type
from zhipuai.core._base_type import ResponseT, ModelBuilderProtocol
from zhipuai.core import BaseModel, HttpClient
from zhipuai.core._base_type import ResponseT
from zhipuai.core._request_opt import FinalRequestOptions
from zhipuai.core._response import APIResponse
from zhipuai.types.chat.async_chat_completion import AsyncTaskStatus, AsyncCompletion
Expand All @@ -17,11 +14,10 @@
CompletionUsage as ChatCompletionUsage)

from zhipuai.types.embeddings import Embedding, EmbeddingsResponded
from zhipuai.types.file_object import FileObject, ListOfFileObject
from zhipuai.types.files.file_object import FileObject, ListOfFileObject
from zhipuai.types.fine_tuning import FineTuningJobEvent
from zhipuai.types.fine_tuning.fine_tuning_job import FineTuningJob, ListOfFineTuningJob, Error
from zhipuai.types.fine_tuning.fine_tuning_job import FineTuningJob, Error
from zhipuai.types.fine_tuning.fine_tuning_job_event import Metric, JobEvent
from zhipuai.types.fine_tuning.job_create_params import Hyperparameters
from zhipuai.types.fine_tuning.fine_tuning_job import Hyperparameters as FineTuningHyperparameters
from zhipuai.types.fine_tuning.models import FineTunedModelsStatus
from zhipuai.types.image import GeneratedImage, ImagesResponded
Expand Down
Loading

0 comments on commit cd32b14

Please sign in to comment.