unionllm 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- unionllm-0.1.0/LICENSE +3 -0
- unionllm-0.1.0/PKG-INFO +16 -0
- unionllm-0.1.0/README.md +3 -0
- unionllm-0.1.0/examples/example_usage.py +3 -0
- unionllm-0.1.0/pyproject.toml +17 -0
- unionllm-0.1.0/requirements.txt +8 -0
- unionllm-0.1.0/setup.cfg +4 -0
- unionllm-0.1.0/setup.py +21 -0
- unionllm-0.1.0/tests/__init__.py +0 -0
- unionllm-0.1.0/tests/test_api.py +4 -0
- unionllm-0.1.0/tests/test_baichuan.py +56 -0
- unionllm-0.1.0/tests/test_coze.py +33 -0
- unionllm-0.1.0/tests/test_fastgpt.py +44 -0
- unionllm-0.1.0/tests/test_litellm.py +24 -0
- unionllm-0.1.0/tests/test_minimax.py +51 -0
- unionllm-0.1.0/tests/test_moonshot.py +78 -0
- unionllm-0.1.0/tests/test_moonshot2.py +43 -0
- unionllm-0.1.0/tests/test_qwen.py +53 -0
- unionllm-0.1.0/tests/test_tiangong.py +54 -0
- unionllm-0.1.0/tests/test_wenxin.py +54 -0
- unionllm-0.1.0/tests/test_zhipu.py +47 -0
- unionllm-0.1.0/tests/test_zhipu2.py +42 -0
- unionllm-0.1.0/unionllm/__init__.py +1 -0
- unionllm-0.1.0/unionllm/exceptions.py +11 -0
- unionllm-0.1.0/unionllm/main.py +67 -0
- unionllm-0.1.0/unionllm/models.py +28 -0
- unionllm-0.1.0/unionllm/providers/__init__.py +0 -0
- unionllm-0.1.0/unionllm/providers/baichuan.py +128 -0
- unionllm-0.1.0/unionllm/providers/base_provider.py +51 -0
- unionllm-0.1.0/unionllm/providers/coze.py +230 -0
- unionllm-0.1.0/unionllm/providers/dify.py +192 -0
- unionllm-0.1.0/unionllm/providers/fastgpt.py +171 -0
- unionllm-0.1.0/unionllm/providers/litellm.py +63 -0
- unionllm-0.1.0/unionllm/providers/minimax.py +122 -0
- unionllm-0.1.0/unionllm/providers/moonshot.py +69 -0
- unionllm-0.1.0/unionllm/providers/qwen.py +168 -0
- unionllm-0.1.0/unionllm/providers/tiangong.py +145 -0
- unionllm-0.1.0/unionllm/providers/wenxin.py +161 -0
- unionllm-0.1.0/unionllm/providers/xunfei.py +245 -0
- unionllm-0.1.0/unionllm/providers/zhipu.py +63 -0
- unionllm-0.1.0/unionllm/utils.py +432 -0
- unionllm-0.1.0/unionllm.egg-info/PKG-INFO +16 -0
- unionllm-0.1.0/unionllm.egg-info/SOURCES.txt +44 -0
- unionllm-0.1.0/unionllm.egg-info/dependency_links.txt +1 -0
- unionllm-0.1.0/unionllm.egg-info/requires.txt +7 -0
- unionllm-0.1.0/unionllm.egg-info/top_level.txt +2 -0
unionllm-0.1.0/LICENSE
ADDED
unionllm-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: unionllm
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python library for unified access to Chinese domestic large language models.
|
|
5
|
+
Home-page: https://github.com/EvalsOne/UnionLLM
|
|
6
|
+
Author: everfly
|
|
7
|
+
Author-email: tagriver@gmail.com
|
|
8
|
+
License: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: openai
|
|
11
|
+
Requires-Dist: pydantic
|
|
12
|
+
Requires-Dist: zhipuai
|
|
13
|
+
Requires-Dist: tenacity
|
|
14
|
+
Requires-Dist: dashscope
|
|
15
|
+
Requires-Dist: websocket_client
|
|
16
|
+
Requires-Dist: requests
|
unionllm-0.1.0/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "unionllm"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A Python library for unified access to Chinese domestic large language models."
|
|
5
|
+
license = "MIT"
|
|
6
|
+
homepage = "https://github.com/EvalsOne/UnionLLM"
|
|
7
|
+
authors = ["everfly <tagriver@gmail.com>"]
|
|
8
|
+
|
|
9
|
+
[tool.poetry.dependencies]
|
|
10
|
+
python = "*"
|
|
11
|
+
dashscope = "*"
|
|
12
|
+
openai = "*"
|
|
13
|
+
pydantic = "*"
|
|
14
|
+
requests = "*"
|
|
15
|
+
tenacity = "*"
|
|
16
|
+
websocket-client = "*"
|
|
17
|
+
zhipuai = "*"
|
unionllm-0.1.0/setup.cfg
ADDED
unionllm-0.1.0/setup.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name='unionllm',
|
|
5
|
+
version='0.1.0',
|
|
6
|
+
packages=find_packages(),
|
|
7
|
+
license='MIT',
|
|
8
|
+
description='A Python library for unified access to Chinese domestic large language models.',
|
|
9
|
+
author='everfly',
|
|
10
|
+
author_email='tagriver@gmail.com',
|
|
11
|
+
url='https://github.com/EvalsOne/UnionLLM',
|
|
12
|
+
install_requires=[
|
|
13
|
+
'openai',
|
|
14
|
+
'pydantic',
|
|
15
|
+
'zhipuai',
|
|
16
|
+
'tenacity',
|
|
17
|
+
'dashscope',
|
|
18
|
+
'websocket_client',
|
|
19
|
+
'requests'
|
|
20
|
+
],
|
|
21
|
+
)
|
|
File without changes
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import os
|
|
3
|
+
import unittest
|
|
4
|
+
|
|
5
|
+
# 将项目根目录添加到sys.path中
|
|
6
|
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
7
|
+
|
|
8
|
+
from unionllm.providers.baichuan import BaiChuanAIProvider, BaiChuanOpenAIError
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class TestBaiChuanProvider(unittest.TestCase):
|
|
12
|
+
def setUp(self):
|
|
13
|
+
# 请将'your_api_key'替换为您的Zhipu AI API密钥
|
|
14
|
+
self.provider = BaiChuanAIProvider(
|
|
15
|
+
api_key="sk-42c0206e207bc39c2367964e76da72ff",
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
def test_completion(self):
|
|
19
|
+
model = "Baichuan2-Turbo"
|
|
20
|
+
messages = [{"content": "你好,今天天气怎么样?", "role": "user"}]
|
|
21
|
+
try:
|
|
22
|
+
response = self.provider.completion(model=model, messages=messages)
|
|
23
|
+
print("response: ", response)
|
|
24
|
+
self.assertIsNotNone(response)
|
|
25
|
+
except BaiChuanOpenAIError as e:
|
|
26
|
+
raise e
|
|
27
|
+
|
|
28
|
+
# def test_completion(self):
|
|
29
|
+
# model = "Baichuan2-Turbo"
|
|
30
|
+
# messages = [{"content": "你好,今天天气怎么样?", "role": "user"}]
|
|
31
|
+
# response = self.provider.completion(model=model, messages=messages, stream=True)
|
|
32
|
+
# for chunk in response:
|
|
33
|
+
# delta = chunk["choices"][0]["delta"]
|
|
34
|
+
# line = {
|
|
35
|
+
# "choices": [
|
|
36
|
+
# {
|
|
37
|
+
# "delta": {
|
|
38
|
+
# "role": delta["role"],
|
|
39
|
+
# "content": delta["content"],
|
|
40
|
+
# }
|
|
41
|
+
# }
|
|
42
|
+
# ]
|
|
43
|
+
# }
|
|
44
|
+
# if "usage" in chunk:
|
|
45
|
+
# usage_info = chunk["usage"]
|
|
46
|
+
# line["usage"] = {
|
|
47
|
+
# "total_tokens": usage_info["total_tokens"],
|
|
48
|
+
# "prompt_tokens": usage_info["prompt_tokens"],
|
|
49
|
+
# "completion_tokens": usage_info["completion_tokens"],
|
|
50
|
+
# }
|
|
51
|
+
# print("line: ", line)
|
|
52
|
+
# self.assertIsNotNone(response)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
if __name__ == "__main__":
|
|
56
|
+
unittest.main()
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import os
|
|
3
|
+
import unittest
|
|
4
|
+
import json
|
|
5
|
+
|
|
6
|
+
# 将项目根目录添加到sys.path中
|
|
7
|
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
8
|
+
|
|
9
|
+
from unionllm.providers.coze import CozeAIProvider, CozeAIError
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TestFastGPTProvider(unittest.TestCase):
|
|
13
|
+
def setUp(self):
|
|
14
|
+
# 请将'your_api_key'替换为您的Zhipu AI API密钥
|
|
15
|
+
self.provider = CozeAIProvider(
|
|
16
|
+
api_key="pat_hodyfcRog0CfCAfEMtKAueI1I8yD2tDuDBSrvSRoXhKm1xLCw0HDiePtEVgRFHzt"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
def test_completion_non_stream_failure(self):
|
|
20
|
+
|
|
21
|
+
# 定义模型和消息
|
|
22
|
+
model = "7355829428481769477"
|
|
23
|
+
messages = [{"content": "智商测试", "role": "user"}]
|
|
24
|
+
|
|
25
|
+
response = self.provider.completion(model=model, messages=messages, stream=False, details=True)
|
|
26
|
+
|
|
27
|
+
print("response: ", response)
|
|
28
|
+
|
|
29
|
+
# 检查捕获的异常是否符合预期
|
|
30
|
+
self.assertIsNotNone(response)
|
|
31
|
+
|
|
32
|
+
if __name__ == "__main__":
|
|
33
|
+
unittest.main()
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import os
|
|
3
|
+
import unittest
|
|
4
|
+
import json
|
|
5
|
+
|
|
6
|
+
# 将项目根目录添加到sys.path中
|
|
7
|
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
8
|
+
|
|
9
|
+
from unionllm.providers.fastgpt import FastGPTProvider
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TestFastGPTProvider(unittest.TestCase):
|
|
13
|
+
def setUp(self):
|
|
14
|
+
# 请将'your_api_key'替换为您的Zhipu AI API密钥
|
|
15
|
+
self.provider = FastGPTProvider(
|
|
16
|
+
api_key="fastgpt-iiCmNijSSN6Mvkr3LOv1tICdlVeVVhUlgSP7QUaiZMgXeXBhM4SzJPvyx4vVT"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
# def test_completion_non_stream_success(self):
|
|
20
|
+
# # Test non-stream completion
|
|
21
|
+
# model = "fastgpt"
|
|
22
|
+
# messages = [{"content": "你好,今天天气怎么样?", "role": "user"}]
|
|
23
|
+
# response = self.provider.completion(model=model, messages=messages, stream=True, details=True)
|
|
24
|
+
|
|
25
|
+
# print("stream response: ", response)
|
|
26
|
+
|
|
27
|
+
# # 检查捕获的异常是否符合预期
|
|
28
|
+
# self.assertIsNotNone(response)
|
|
29
|
+
|
|
30
|
+
def test_completion_non_stream_failure(self):
|
|
31
|
+
|
|
32
|
+
# 定义模型和消息
|
|
33
|
+
model = "fastgpt"
|
|
34
|
+
messages = [{"content": "财商测试", "role": "user"}]
|
|
35
|
+
|
|
36
|
+
response = self.provider.completion(model=model, messages=messages, stream=False, details=True)
|
|
37
|
+
|
|
38
|
+
print("response: ", response)
|
|
39
|
+
|
|
40
|
+
# 检查捕获的异常是否符合预期
|
|
41
|
+
self.assertIsNotNone(response)
|
|
42
|
+
|
|
43
|
+
if __name__ == "__main__":
|
|
44
|
+
unittest.main()
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import os
|
|
3
|
+
import unittest
|
|
4
|
+
import json
|
|
5
|
+
import litellm
|
|
6
|
+
|
|
7
|
+
# 将项目根目录添加到sys.path中
|
|
8
|
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
9
|
+
|
|
10
|
+
from unionllm.providers.base_provider import BaseProvider
|
|
11
|
+
|
|
12
|
+
class TestLiteLLMProvider(unittest.TestCase):
|
|
13
|
+
def setUp(self):
|
|
14
|
+
self.provider = BaseProvider(api_key="sk-45ad8752a48d47ffaf86f5b000eff589")
|
|
15
|
+
|
|
16
|
+
def test_completion(self):
|
|
17
|
+
model = "qwen-turbo"
|
|
18
|
+
messages = [{"content": "你好,今天天气怎么样?", "role": "user"}]
|
|
19
|
+
response = self.provider.completion(model=model, messages=messages)
|
|
20
|
+
print("response: ", response)
|
|
21
|
+
self.assertIsNotNone(response)
|
|
22
|
+
|
|
23
|
+
if __name__ == "__main__":
|
|
24
|
+
unittest.main()
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import sys
|
|
3
|
+
import os
|
|
4
|
+
import unittest
|
|
5
|
+
|
|
6
|
+
# 将项目根目录添加到sys.path中
|
|
7
|
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
8
|
+
|
|
9
|
+
from unionllm.providers.minimax import MinimaxAIProvider
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TestMinimaxProvider(unittest.TestCase):
|
|
13
|
+
def setUp(self):
|
|
14
|
+
# 请将'your_api_key'替换为您的Zhipu AI API密钥
|
|
15
|
+
self.provider = MinimaxAIProvider(
|
|
16
|
+
api_key="eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJHcm91cE5hbWUiOiJ0aWFtbyIsIlVzZXJOYW1lIjoidGlhbW8iLCJBY2NvdW50IjoiIiwiU3ViamVjdElEIjoiMTc2ODUzNjQzNzMxNTA4MDQxMCIsIlBob25lIjoiMTk1MTAzNjA5MzQiLCJHcm91cElEIjoiMTc2ODUzNjQzNzMwNjY5MTgwMiIsIlBhZ2VOYW1lIjoiIiwiTWFpbCI6IiIsIkNyZWF0ZVRpbWUiOiIyMDI0LTAzLTIxIDE1OjAyOjI0IiwiaXNzIjoibWluaW1heCJ9.LVXY3zEwzpNQ7yei--ZP7JtG7Xs_vfeADmGd_JcooTOlzUJ3PmDQFssDkY-8DKj_Pm9dthI4Dd3bI2ELdyYbmcA_VU-qNWBfqtB0Gtfb_azdzAdTbkftUxgSm6t44gHZCgymKfblIHk-hdAISJ5YeE1ETdFww9gGREnY8T54Zhe8OsCdy7pjFXktlAEFWWPNBfumcUORSHtiIX3SAJRHTafgMWuv0tAxvIO1Vzz0OoWIBdpYoslHJx1aIOCwuaohsuXmfUVdmiWZyQJKvxl5L6ix9IsNQW9e1m9gJ_1M3UnJh9i6I2UC3gkNLwm9zYFqG2v20nWLtchu9vBShEpFQA",
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
def test_completion(self):
|
|
20
|
+
model = "abab6-chat"
|
|
21
|
+
messages = [{"content": "你好,今天天气怎么样?", "role": "user"}]
|
|
22
|
+
response = self.provider.completion(model=model, messages=messages)
|
|
23
|
+
print("response: ", response)
|
|
24
|
+
self.assertIsNotNone(response)
|
|
25
|
+
|
|
26
|
+
# def test_completion(self):
|
|
27
|
+
# model = "abab6-chat"
|
|
28
|
+
# messages = [{"content": "你好,今天天气怎么样?", "role": "user"}]
|
|
29
|
+
# response = self.provider.completion(model=model, messages=messages, stream=True)
|
|
30
|
+
# for chunk in response:
|
|
31
|
+
# delta = chunk["choices"][0]["delta"]
|
|
32
|
+
# line = {
|
|
33
|
+
# "choices": [
|
|
34
|
+
# {
|
|
35
|
+
# "delta": {
|
|
36
|
+
# "role": delta["role"],
|
|
37
|
+
# "content": delta["content"],
|
|
38
|
+
# }
|
|
39
|
+
# }
|
|
40
|
+
# ]
|
|
41
|
+
# }
|
|
42
|
+
# if "usage" in chunk:
|
|
43
|
+
# line["usage"] = {
|
|
44
|
+
# "total_tokens": chunk["usage"]["total_tokens"],
|
|
45
|
+
# }
|
|
46
|
+
# print("line: ", line, flush=True)
|
|
47
|
+
# self.assertIsNotNone(response)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
if __name__ == "__main__":
|
|
51
|
+
unittest.main()
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import sys
|
|
3
|
+
import os
|
|
4
|
+
import unittest
|
|
5
|
+
|
|
6
|
+
# 将项目根目录添加到sys.path中
|
|
7
|
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
8
|
+
|
|
9
|
+
from unionllm.providers.moonshot import MoonshotAIProvider
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TestZhipuAIProvider(unittest.TestCase):
|
|
13
|
+
def setUp(self):
|
|
14
|
+
# 请将'your_api_key'替换为您的Zhipu AI API密钥
|
|
15
|
+
self.provider = MoonshotAIProvider(
|
|
16
|
+
api_key="sk-EUZowzL6zYhlBi9lbeBHnaWV2yloiooI0Hec0WpiqCC1n0Ar",
|
|
17
|
+
base_url="https://api.moonshot.cn/v1",
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
# def test_completion(self):
|
|
21
|
+
# model = "moonshot-v1-8k"
|
|
22
|
+
# messages = [{"content": "你好,今天天气怎么样?", "role": "user"}]
|
|
23
|
+
# response = self.provider.completion(model=model, messages=messages, stream=True)
|
|
24
|
+
# for chunk in response:
|
|
25
|
+
# new_chunk = json.loads(chunk)
|
|
26
|
+
# delta = new_chunk["choices"][0]["delta"]
|
|
27
|
+
# line = {
|
|
28
|
+
# "choices": [
|
|
29
|
+
# {
|
|
30
|
+
# "delta": {
|
|
31
|
+
# "role": delta["role"],
|
|
32
|
+
# "content": delta["content"],
|
|
33
|
+
# }
|
|
34
|
+
# }
|
|
35
|
+
# ]
|
|
36
|
+
# }
|
|
37
|
+
# if "usage" in new_chunk:
|
|
38
|
+
# usage_info = new_chunk["usage"]
|
|
39
|
+
# line["usage"] = {
|
|
40
|
+
# "prompt_tokens": usage_info["prompt_tokens"],
|
|
41
|
+
# "completion_tokens": usage_info["completion_tokens"],
|
|
42
|
+
# "total_tokens": usage_info["total_tokens"],
|
|
43
|
+
# }
|
|
44
|
+
# print("line: ", line)
|
|
45
|
+
# self.assertIsNotNone(response)
|
|
46
|
+
|
|
47
|
+
def test_completion(self):
|
|
48
|
+
model = "moonshot-v1-8k"
|
|
49
|
+
messages = [{"content": "你好,今天天气怎么样?", "role": "user"}]
|
|
50
|
+
response = self.provider.completion(model=model, messages=messages, stream=True)
|
|
51
|
+
for chunk in response:
|
|
52
|
+
print("chunk: ", chunk)
|
|
53
|
+
chunk_message = chunk.choices[0].delta
|
|
54
|
+
line = {
|
|
55
|
+
"choices": [
|
|
56
|
+
{
|
|
57
|
+
"delta": {
|
|
58
|
+
"role": chunk_message.role,
|
|
59
|
+
"content": chunk_message.content,
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
if (
|
|
65
|
+
hasattr(chunk.choices[0], "usage")
|
|
66
|
+
and chunk.choices[0].usage is not None
|
|
67
|
+
):
|
|
68
|
+
line["usage"] = {
|
|
69
|
+
"prompt_tokens": chunk.choices[0].usage.prompt_tokens,
|
|
70
|
+
"completion_tokens": chunk.choices[0].usage.completion_tokens,
|
|
71
|
+
"total_tokens": chunk.choices[0].usage.total_tokens,
|
|
72
|
+
}
|
|
73
|
+
print("response: ", response)
|
|
74
|
+
self.assertIsNotNone(response)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
if __name__ == "__main__":
|
|
78
|
+
unittest.main()
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
from unittest.mock import patch
|
|
3
|
+
from unionllm.providers.moonshot import MoonshotAIProvider
|
|
4
|
+
from unionllm.exceptions import UnionLLMError
|
|
5
|
+
|
|
6
|
+
class TestMoonshotAIProvider(unittest.TestCase):
|
|
7
|
+
|
|
8
|
+
def setUp(self):
|
|
9
|
+
self.provider = MoonshotAIProvider(api_key='test_api_key')
|
|
10
|
+
|
|
11
|
+
@patch('unionllm.providers.moonshot.ZhipuAI')
|
|
12
|
+
def test_completion_success(self, mock_zhipu):
|
|
13
|
+
mock_zhipu.return_value.chat.completions.create.return_value = 'Test response'
|
|
14
|
+
response = self.provider.completion(
|
|
15
|
+
model='test-model',
|
|
16
|
+
messages=['Hello, world!'],
|
|
17
|
+
max_tokens=50
|
|
18
|
+
)
|
|
19
|
+
self.assertEqual(response, 'Test response')
|
|
20
|
+
|
|
21
|
+
@patch('unionllm.providers.moonshot.ZhipuAI')
|
|
22
|
+
def test_completion_failure_missing_model(self, mock_zhipu):
|
|
23
|
+
with self.assertRaises(UnionLLMError) as context:
|
|
24
|
+
self.provider.completion(
|
|
25
|
+
model=None,
|
|
26
|
+
messages=['Hello, world!']
|
|
27
|
+
)
|
|
28
|
+
self.assertEqual(context.exception.status_code, 422)
|
|
29
|
+
self.assertEqual(context.exception.message, 'Missing model or messages')
|
|
30
|
+
|
|
31
|
+
@patch('unionllm.providers.moonshot.ZhipuAI')
|
|
32
|
+
def test_completion_api_error(self, mock_zhipu):
|
|
33
|
+
mock_zhipu.return_value.chat.completions.create.side_effect = Exception("API error")
|
|
34
|
+
with self.assertRaises(UnionLLMError) as context:
|
|
35
|
+
self.provider.completion(
|
|
36
|
+
model='test-model',
|
|
37
|
+
messages=['Hello, world!']
|
|
38
|
+
)
|
|
39
|
+
self.assertEqual(context.exception.status_code, 500)
|
|
40
|
+
self.assertEqual(context.exception.message, 'API error')
|
|
41
|
+
|
|
42
|
+
if __name__ == '__main__':
|
|
43
|
+
unittest.main()
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import os
|
|
3
|
+
import unittest
|
|
4
|
+
import json
|
|
5
|
+
import dashscope
|
|
6
|
+
|
|
7
|
+
# 将项目根目录添加到sys.path中
|
|
8
|
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
9
|
+
|
|
10
|
+
from unionllm.providers.qwen import QwenAIProvider
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class TestQwenAIProvider(unittest.TestCase):
|
|
14
|
+
def setUp(self):
|
|
15
|
+
self.provider = QwenAIProvider(api_key="sk-45ad8752a48d47ffaf86f5b000eff589")
|
|
16
|
+
|
|
17
|
+
def test_completion(self):
|
|
18
|
+
model = "qwen-turbo"
|
|
19
|
+
messages = [{"content": "你好,今天天气怎么样?", "role": "user"}]
|
|
20
|
+
response = self.provider.completion(model=model, messages=messages)
|
|
21
|
+
print("response: ", response)
|
|
22
|
+
self.assertIsNotNone(response)
|
|
23
|
+
|
|
24
|
+
# def test_completion(self):
|
|
25
|
+
# model = "qwen-turbo"
|
|
26
|
+
# messages = [{"content": "你好,今天天气怎么样?", "role": "user"}]
|
|
27
|
+
# response = self.provider.completion(model=model, messages=messages, stream=True)
|
|
28
|
+
# for chunk in response:
|
|
29
|
+
# new_chunk = json.loads(chunk)
|
|
30
|
+
# delta = new_chunk["choices"][0]["delta"]
|
|
31
|
+
# line = {
|
|
32
|
+
# "choices": [
|
|
33
|
+
# {
|
|
34
|
+
# "delta": {
|
|
35
|
+
# "role": delta["role"],
|
|
36
|
+
# "content": delta["content"],
|
|
37
|
+
# }
|
|
38
|
+
# }
|
|
39
|
+
# ]
|
|
40
|
+
# }
|
|
41
|
+
# if "usage" in new_chunk:
|
|
42
|
+
# usage_info = new_chunk["usage"]
|
|
43
|
+
# line["usage"] = {
|
|
44
|
+
# "prompt_tokens": usage_info["prompt_tokens"],
|
|
45
|
+
# "completion_tokens": usage_info["completion_tokens"],
|
|
46
|
+
# "total_tokens": usage_info["total_tokens"],
|
|
47
|
+
# }
|
|
48
|
+
# print("line:", line)
|
|
49
|
+
# self.assertIsNotNone(response)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
if __name__ == "__main__":
|
|
53
|
+
unittest.main()
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import sys
|
|
3
|
+
import os
|
|
4
|
+
import unittest
|
|
5
|
+
|
|
6
|
+
# 将项目根目录添加到sys.path中
|
|
7
|
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
8
|
+
|
|
9
|
+
from unionllm.providers.tiangong import TianGongAIProvider
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TestTianGongProvider(unittest.TestCase):
|
|
13
|
+
def setUp(self):
|
|
14
|
+
# 请将'your_api_key'替换为您的Zhipu AI API密钥
|
|
15
|
+
self.provider = TianGongAIProvider(
|
|
16
|
+
api_key="a253c3d3bb00495a6f1f6572367e20b7",
|
|
17
|
+
app_secret="df6b91ff0e400b8114122de882ff16597ab3539eb71afee5",
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
def test_completion(self):
|
|
21
|
+
model = "SkyChat-MegaVerse"
|
|
22
|
+
messages = [{"content": "你好,今天天气怎么样?", "role": "user"}]
|
|
23
|
+
response = self.provider.completion(model=model, messages=messages)
|
|
24
|
+
print("response: ", response)
|
|
25
|
+
self.assertIsNotNone(response)
|
|
26
|
+
|
|
27
|
+
# def test_completion(self):
|
|
28
|
+
# model = "SkyChat-MegaVerse"
|
|
29
|
+
# messages = [{"content": "你好,今天天气怎么样?", "role": "user"}]
|
|
30
|
+
# response = self.provider.completion(model=model, messages=messages, stream=True)
|
|
31
|
+
# for chunk in response:
|
|
32
|
+
# delta = chunk["choices"][0]["delta"]
|
|
33
|
+
# line = {
|
|
34
|
+
# "choices": [
|
|
35
|
+
# {
|
|
36
|
+
# "delta": {
|
|
37
|
+
# "role": delta["role"],
|
|
38
|
+
# "content": delta["content"],
|
|
39
|
+
# }
|
|
40
|
+
# }
|
|
41
|
+
# ]
|
|
42
|
+
# }
|
|
43
|
+
# if "usage" in chunk:
|
|
44
|
+
# line["usage"] = {
|
|
45
|
+
# "prompt_tokens": chunk["usage"]["prompt_tokens"],
|
|
46
|
+
# "completion_tokens": chunk["usage"]["completion_tokens"],
|
|
47
|
+
# "total_tokens": chunk["usage"]["total_tokens"],
|
|
48
|
+
# }
|
|
49
|
+
# print("line: ", line)
|
|
50
|
+
# self.assertIsNotNone(response)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
if __name__ == "__main__":
|
|
54
|
+
unittest.main()
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import sys
|
|
3
|
+
import os
|
|
4
|
+
import unittest
|
|
5
|
+
|
|
6
|
+
# 将项目根目录添加到sys.path中
|
|
7
|
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
8
|
+
|
|
9
|
+
from unionllm.providers.wenxin import WenXinAIProvider
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TestWenXinProvider(unittest.TestCase):
|
|
13
|
+
def setUp(self):
|
|
14
|
+
# 请将'your_api_key'替换为您的Zhipu AI API密钥
|
|
15
|
+
self.provider = WenXinAIProvider(
|
|
16
|
+
api_key="AvA4gBP5LZqKEruKGYzfYzU9",
|
|
17
|
+
secret_key="8Mg2XN3zPmskxzeYY3i7P7u24poGIHmO",
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
def test_completion(self):
|
|
21
|
+
model = "completions_pro"
|
|
22
|
+
messages = [{"content": "你好,今天天气怎么样?", "role": "user"}]
|
|
23
|
+
response = self.provider.completion(model=model, messages=messages)
|
|
24
|
+
print("response: ", response)
|
|
25
|
+
self.assertIsNotNone(response)
|
|
26
|
+
|
|
27
|
+
# def test_completion(self):
|
|
28
|
+
# model = "completions_pro"
|
|
29
|
+
# messages = [{"content": "你好,今天天气怎么样?", "role": "user"}]
|
|
30
|
+
# response = self.provider.completion(model=model, messages=messages, stream=True)
|
|
31
|
+
# for chunk in response:
|
|
32
|
+
# delta = chunk["choices"][0]["delta"]
|
|
33
|
+
# line = {
|
|
34
|
+
# "choices": [
|
|
35
|
+
# {
|
|
36
|
+
# "delta": {
|
|
37
|
+
# "role": delta["role"],
|
|
38
|
+
# "content": delta["content"],
|
|
39
|
+
# }
|
|
40
|
+
# }
|
|
41
|
+
# ]
|
|
42
|
+
# }
|
|
43
|
+
# if "usage" in chunk:
|
|
44
|
+
# line["usage"] = {
|
|
45
|
+
# "prompt_tokens": chunk["usage"]["prompt_tokens"],
|
|
46
|
+
# "completion_tokens": chunk["usage"]["completion_tokens"],
|
|
47
|
+
# "total_tokens": chunk["usage"]["total_tokens"],
|
|
48
|
+
# }
|
|
49
|
+
# print("line: ", line)
|
|
50
|
+
# self.assertIsNotNone(response)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
if __name__ == "__main__":
|
|
54
|
+
unittest.main()
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import os
|
|
3
|
+
import unittest
|
|
4
|
+
import json
|
|
5
|
+
|
|
6
|
+
# 将项目根目录添加到sys.path中
|
|
7
|
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
8
|
+
|
|
9
|
+
from unionllm.providers.zhipu import ZhipuAIProvider
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TestZhipuAIProvider(unittest.TestCase):
|
|
13
|
+
def setUp(self):
|
|
14
|
+
# 请将'your_api_key'替换为您的Zhipu AI API密钥
|
|
15
|
+
self.provider = ZhipuAIProvider(
|
|
16
|
+
api_key="38dc046c622b6ec1b9bfa0413e6ca2ee.2Yn3uFr8j74pMOvK"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
def test_completion(self):
|
|
20
|
+
model = "GLM-4"
|
|
21
|
+
messages = [{"content": "你好,今天天气怎么样?", "role": "user"}]
|
|
22
|
+
response = self.provider.completion(model=model, messages=messages)
|
|
23
|
+
for chunk in response:
|
|
24
|
+
print("chunk: ", chunk)
|
|
25
|
+
delta = chunk.choices[0].delta
|
|
26
|
+
line = {
|
|
27
|
+
"choices": [
|
|
28
|
+
{
|
|
29
|
+
"delta": {
|
|
30
|
+
"role": delta.role,
|
|
31
|
+
"content": delta.content,
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
}
|
|
36
|
+
if chunk.usage is not None:
|
|
37
|
+
line["usage"] = {
|
|
38
|
+
"prompt_tokens": chunk.usage.prompt_tokens,
|
|
39
|
+
"completion_tokens": chunk.usage.completion_tokens,
|
|
40
|
+
"total_tokens": chunk.usage.total_tokens,
|
|
41
|
+
}
|
|
42
|
+
print("line内容2: ", line)
|
|
43
|
+
print("response: ", response)
|
|
44
|
+
self.assertIsNotNone(response)
|
|
45
|
+
|
|
46
|
+
if __name__ == "__main__":
|
|
47
|
+
unittest.main()
|