mirror of
https://github.com/RYDE-WORK/Langchain-Chatchat.git
synced 2026-01-24 15:53:21 +08:00
* 优化在线 API ,支持 completion 和 embedding,简化在线 API 开发方式 新功能 - 智谱AI、Minimax、千帆、千问 4 个在线模型支持 embeddings(不通过Fastchat,后续会单独提供相关api接口) - 在线模型自动检测传入参数,在传入非 messages 格式的 prompt 时,自动转换为 completion 形式,以支持 completion 接口 开发者: - 重构ApiModelWorker: - 所有在线 API 请求封装到 do_chat 方法:自动传入参数 ApiChatParams,简化参数与配置项的获取;自动处理与fastchat的接口 - 加强 API 请求错误处理,返回更有意义的信息 - 改用 qianfan sdk 重写 qianfan-api - 将所有在线模型的测试用例统一在一起,简化测试用例编写 * Delete requirements_langflow.txt
69 lines
2.3 KiB
Python
69 lines
2.3 KiB
Python
import sys
|
|
from pathlib import Path
|
|
root_path = Path(__file__).parent.parent
|
|
sys.path.append(str(root_path))
|
|
|
|
from configs import ONLINE_LLM_MODEL
|
|
from server.model_workers.base import *
|
|
from server.utils import get_model_worker_config, list_config_llm_models
|
|
from pprint import pprint
|
|
import pytest
|
|
|
|
|
|
workers = []
|
|
for x in list_config_llm_models()["online"]:
|
|
if x in ONLINE_LLM_MODEL and x not in workers:
|
|
workers.append(x)
|
|
print(f"all workers to test: {workers}")
|
|
|
|
|
|
@pytest.mark.parametrize("worker", workers)
|
|
def test_chat(worker):
|
|
params = ApiChatParams(
|
|
messages = [
|
|
{"role": "user", "content": "你是谁"},
|
|
],
|
|
)
|
|
print(f"\nchat with {worker} \n")
|
|
|
|
worker_class = get_model_worker_config(worker)["worker_class"]
|
|
for x in worker_class().do_chat(params):
|
|
pprint(x)
|
|
assert isinstance(x, dict)
|
|
assert x["error_code"] == 0
|
|
|
|
|
|
@pytest.mark.parametrize("worker", workers)
|
|
def test_embeddings(worker):
|
|
params = ApiEmbeddingsParams(
|
|
texts = [
|
|
"LangChain-Chatchat (原 Langchain-ChatGLM): 基于 Langchain 与 ChatGLM 等大语言模型的本地知识库问答应用实现。",
|
|
"一种利用 langchain 思想实现的基于本地知识库的问答应用,目标期望建立一套对中文场景与开源模型支持友好、可离线运行的知识库问答解决方案。",
|
|
]
|
|
)
|
|
|
|
worker_class = get_model_worker_config(worker)["worker_class"]
|
|
if worker_class.can_embedding():
|
|
print(f"\embeddings with {worker} \n")
|
|
resp = worker_class().do_embeddings(params)
|
|
|
|
pprint(resp, depth=2)
|
|
assert resp["code"] == 200
|
|
assert "embeddings" in resp
|
|
embeddings = resp["embeddings"]
|
|
assert isinstance(embeddings, list) and len(embeddings) > 0
|
|
assert isinstance(embeddings[0], list) and len(embeddings[0]) > 0
|
|
assert isinstance(embeddings[0][0], float)
|
|
print("向量长度:", len(embeddings[0]))
|
|
|
|
|
|
# @pytest.mark.parametrize("worker", workers)
|
|
# def test_completion(worker):
|
|
# params = ApiCompletionParams(prompt="五十六个民族")
|
|
|
|
# print(f"\completion with {worker} \n")
|
|
|
|
# worker_class = get_model_worker_config(worker)["worker_class"]
|
|
# resp = worker_class().do_completion(params)
|
|
# pprint(resp)
|