2023-08-19 15:19:01 +08:00
|
|
|
|
import requests
|
|
|
|
|
|
import json
|
|
|
|
|
|
import sys
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
sys.path.append(str(Path(__file__).parent.parent.parent))
|
2023-09-15 17:52:22 +08:00
|
|
|
|
from configs import BING_SUBSCRIPTION_KEY
|
2023-08-29 10:06:09 +08:00
|
|
|
|
from server.utils import api_address
|
2023-08-19 15:19:01 +08:00
|
|
|
|
|
|
|
|
|
|
from pprint import pprint
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api_base_url = api_address()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dump_input(d, title):
|
|
|
|
|
|
print("\n")
|
|
|
|
|
|
print("=" * 30 + title + " input " + "="*30)
|
|
|
|
|
|
pprint(d)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dump_output(r, title):
|
|
|
|
|
|
print("\n")
|
|
|
|
|
|
print("=" * 30 + title + " output" + "="*30)
|
|
|
|
|
|
for line in r.iter_content(None, decode_unicode=True):
|
|
|
|
|
|
print(line, end="", flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
|
|
'accept': 'application/json',
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
"query": "请用100字左右的文字介绍自己",
|
|
|
|
|
|
"history": [
|
|
|
|
|
|
{
|
|
|
|
|
|
"role": "user",
|
|
|
|
|
|
"content": "你好"
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
"role": "assistant",
|
2023-08-25 10:58:40 +08:00
|
|
|
|
"content": "你好,我是人工智能大模型"
|
2023-08-19 15:19:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
],
|
2023-09-13 10:00:54 +08:00
|
|
|
|
"stream": True,
|
|
|
|
|
|
"temperature": 0.7,
|
2023-08-19 15:19:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_chat_fastchat(api="/chat/fastchat"):
|
|
|
|
|
|
url = f"{api_base_url}{api}"
|
|
|
|
|
|
data2 = {
|
|
|
|
|
|
"stream": True,
|
|
|
|
|
|
"messages": data["history"] + [{"role": "user", "content": "推荐一部科幻电影"}]
|
|
|
|
|
|
}
|
|
|
|
|
|
dump_input(data2, api)
|
|
|
|
|
|
response = requests.post(url, headers=headers, json=data2, stream=True)
|
|
|
|
|
|
dump_output(response, api)
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_chat_chat(api="/chat/chat"):
|
|
|
|
|
|
url = f"{api_base_url}{api}"
|
|
|
|
|
|
dump_input(data, api)
|
|
|
|
|
|
response = requests.post(url, headers=headers, json=data, stream=True)
|
|
|
|
|
|
dump_output(response, api)
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_knowledge_chat(api="/chat/knowledge_base_chat"):
|
|
|
|
|
|
url = f"{api_base_url}{api}"
|
|
|
|
|
|
data = {
|
|
|
|
|
|
"query": "如何提问以获得高质量答案",
|
|
|
|
|
|
"knowledge_base_name": "samples",
|
|
|
|
|
|
"history": [
|
|
|
|
|
|
{
|
|
|
|
|
|
"role": "user",
|
|
|
|
|
|
"content": "你好"
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
|
"content": "你好,我是 ChatGLM"
|
|
|
|
|
|
}
|
|
|
|
|
|
],
|
|
|
|
|
|
"stream": True
|
|
|
|
|
|
}
|
|
|
|
|
|
dump_input(data, api)
|
|
|
|
|
|
response = requests.post(url, headers=headers, json=data, stream=True)
|
|
|
|
|
|
print("\n")
|
|
|
|
|
|
print("=" * 30 + api + " output" + "="*30)
|
|
|
|
|
|
for line in response.iter_content(None, decode_unicode=True):
|
|
|
|
|
|
data = json.loads(line)
|
2023-09-28 15:12:03 +08:00
|
|
|
|
if "answer" in data:
|
2023-09-13 10:00:54 +08:00
|
|
|
|
print(data["answer"], end="", flush=True)
|
2023-10-25 08:58:31 +08:00
|
|
|
|
pprint(data)
|
2023-09-13 10:00:54 +08:00
|
|
|
|
assert "docs" in data and len(data["docs"]) > 0
|
2023-08-19 15:19:01 +08:00
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_search_engine_chat(api="/chat/search_engine_chat"):
|
2023-08-25 10:58:40 +08:00
|
|
|
|
global data
|
|
|
|
|
|
|
|
|
|
|
|
data["query"] = "室温超导最新进展是什么样?"
|
|
|
|
|
|
|
2023-08-19 15:19:01 +08:00
|
|
|
|
url = f"{api_base_url}{api}"
|
|
|
|
|
|
for se in ["bing", "duckduckgo"]:
|
2023-08-25 10:58:40 +08:00
|
|
|
|
data["search_engine_name"] = se
|
|
|
|
|
|
dump_input(data, api + f" by {se}")
|
2023-08-19 15:19:01 +08:00
|
|
|
|
response = requests.post(url, json=data, stream=True)
|
2023-08-25 10:58:40 +08:00
|
|
|
|
if se == "bing" and not BING_SUBSCRIPTION_KEY:
|
|
|
|
|
|
data = response.json()
|
|
|
|
|
|
assert data["code"] == 404
|
|
|
|
|
|
assert data["msg"] == f"要使用Bing搜索引擎,需要设置 `BING_SUBSCRIPTION_KEY`"
|
|
|
|
|
|
|
|
|
|
|
|
print("\n")
|
2023-09-17 13:27:11 +08:00
|
|
|
|
print("=" * 30 + api + f" by {se} output" + "="*30)
|
2023-08-25 10:58:40 +08:00
|
|
|
|
for line in response.iter_content(None, decode_unicode=True):
|
|
|
|
|
|
data = json.loads(line)
|
2023-09-13 10:00:54 +08:00
|
|
|
|
if "answer" in data:
|
|
|
|
|
|
print(data["answer"], end="", flush=True)
|
|
|
|
|
|
assert "docs" in data and len(data["docs"]) > 0
|
|
|
|
|
|
pprint(data["docs"])
|
2023-08-19 15:19:01 +08:00
|
|
|
|
assert response.status_code == 200
|
2023-08-25 10:58:40 +08:00
|
|
|
|
|