diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0ec4766e0..54c1bb977 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -101,6 +101,8 @@ jobs: cd sdk/python && uv sync --python 3.10 --group test --frozen && source .venv/bin/activate && cd test/test_frontend_api && pytest -s --tb=short get_email.py test_dataset.py - name: Run http api tests against Elasticsearch + env: + ZHIPU_AI_API_KEY: ${{ secrets.ZHIPU_AI_API_KEY }} run: | export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY="" export HOST_ADDRESS=http://host.docker.internal:9380 @@ -145,6 +147,8 @@ jobs: cd sdk/python && uv sync --python 3.10 --group test --frozen && source .venv/bin/activate && cd test/test_frontend_api && pytest -s --tb=short get_email.py test_dataset.py - name: Run http api tests against Infinity + env: + ZHIPU_AI_API_KEY: ${{ secrets.ZHIPU_AI_API_KEY }} run: | export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY="" export HOST_ADDRESS=http://host.docker.internal:9380 diff --git a/sdk/python/test/conftest.py b/sdk/python/test/conftest.py index c8b11845b..9cb09bad2 100644 --- a/sdk/python/test/conftest.py +++ b/sdk/python/test/conftest.py @@ -18,7 +18,6 @@ import os import pytest import requests -from libs.auth import RAGFlowHttpApiAuth HOST_ADDRESS = os.getenv("HOST_ADDRESS", "http://127.0.0.1:9380") @@ -88,69 +87,3 @@ def get_auth(): @pytest.fixture(scope="session") def get_email(): return EMAIL - - -@pytest.fixture(scope="session") -def get_http_api_auth(get_api_key_fixture): - return RAGFlowHttpApiAuth(get_api_key_fixture) - - -def get_my_llms(auth, name): - url = HOST_ADDRESS + "/v1/llm/my_llms" - authorization = {"Authorization": auth} - response = requests.get(url=url, headers=authorization) - res = response.json() - if res.get("code") != 0: - raise Exception(res.get("message")) - if name in res.get("data"): - return True - return False - - -def add_models(auth): - url = HOST_ADDRESS + "/v1/llm/set_api_key" - authorization = {"Authorization": auth} - models_info = { - "ZHIPU-AI": {"llm_factory": "ZHIPU-AI", "api_key": "d06253dacd404180aa8afb096fcb6c30.KatwBIUpvCSml9sU"}, - } - - for name, model_info in models_info.items(): - if not get_my_llms(auth, name): - response = requests.post(url=url, headers=authorization, json=model_info) - res = response.json() - if res.get("code") != 0: - raise Exception(res.get("message")) - - -def get_tenant_info(auth): - url = HOST_ADDRESS + "/v1/user/tenant_info" - authorization = {"Authorization": auth} - response = requests.get(url=url, headers=authorization) - res = response.json() - if res.get("code") != 0: - raise Exception(res.get("message")) - return res["data"].get("tenant_id") - - -@pytest.fixture(scope="session", autouse=True) -def set_tenant_info(get_auth): - auth = get_auth - try: - add_models(auth) - tenant_id = get_tenant_info(auth) - except Exception as e: - raise Exception(e) - url = HOST_ADDRESS + "/v1/user/set_tenant_info" - authorization = {"Authorization": get_auth} - tenant_info = { - "tenant_id": tenant_id, - "llm_id": "glm-4-flash@ZHIPU-AI", - "embd_id": "BAAI/bge-large-zh-v1.5@BAAI", - "img2txt_id": "glm-4v@ZHIPU-AI", - "asr_id": "", - "tts_id": None, - } - response = requests.post(url=url, headers=authorization, json=tenant_info) - res = response.json() - if res.get("code") != 0: - raise Exception(res.get("message")) diff --git a/sdk/python/test/test_http_api/conftest.py b/sdk/python/test/test_http_api/conftest.py index 1fbfe9527..2c13c323f 100644 --- a/sdk/python/test/test_http_api/conftest.py +++ b/sdk/python/test/test_http_api/conftest.py @@ -13,8 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import os import pytest +import requests from common import ( add_chunk, batch_create_datasets, @@ -26,6 +28,7 @@ from common import ( list_documnets, parse_documnets, ) +from libs.auth import RAGFlowHttpApiAuth from libs.utils import wait_for from libs.utils.file_utils import ( create_docx_file, @@ -45,6 +48,11 @@ MARKER_EXPRESSIONS = { "p2": "p1 or p2", "p3": "p1 or p2 or p3", } +HOST_ADDRESS = os.getenv("HOST_ADDRESS", "http://127.0.0.1:9380") +ZHIPU_AI_API_KEY = os.getenv("ZHIPU_AI_API_KEY") +print(f"{ZHIPU_AI_API_KEY=}") +if ZHIPU_AI_API_KEY is None: + pytest.exit("Error: Environment variable ZHIPU_AI_API_KEY must be set") def pytest_addoption(parser: pytest.Parser) -> None: @@ -73,6 +81,72 @@ def condition(_auth, _dataset_id): return True +@pytest.fixture(scope="session") +def get_http_api_auth(get_api_key_fixture): + return RAGFlowHttpApiAuth(get_api_key_fixture) + + +def get_my_llms(auth, name): + url = HOST_ADDRESS + "/v1/llm/my_llms" + authorization = {"Authorization": auth} + response = requests.get(url=url, headers=authorization) + res = response.json() + if res.get("code") != 0: + raise Exception(res.get("message")) + if name in res.get("data"): + return True + return False + + +def add_models(auth): + url = HOST_ADDRESS + "/v1/llm/set_api_key" + authorization = {"Authorization": auth} + models_info = { + "ZHIPU-AI": {"llm_factory": "ZHIPU-AI", "api_key": ZHIPU_AI_API_KEY}, + } + + for name, model_info in models_info.items(): + if not get_my_llms(auth, name): + response = requests.post(url=url, headers=authorization, json=model_info) + res = response.json() + if res.get("code") != 0: + pytest.exit(f"Critical error in add_models: {res.get('message')}") + + +def get_tenant_info(auth): + url = HOST_ADDRESS + "/v1/user/tenant_info" + authorization = {"Authorization": auth} + response = requests.get(url=url, headers=authorization) + res = response.json() + if res.get("code") != 0: + raise Exception(res.get("message")) + return res["data"].get("tenant_id") + + +@pytest.fixture(scope="session", autouse=True) +def set_tenant_info(get_auth): + auth = get_auth + try: + add_models(auth) + tenant_id = get_tenant_info(auth) + except Exception as e: + pytest.exit(f"Error in set_tenant_info: {str(e)}") + url = HOST_ADDRESS + "/v1/user/set_tenant_info" + authorization = {"Authorization": get_auth} + tenant_info = { + "tenant_id": tenant_id, + "llm_id": "glm-4-flash@ZHIPU-AI", + "embd_id": "BAAI/bge-large-zh-v1.5@BAAI", + "img2txt_id": "glm-4v@ZHIPU-AI", + "asr_id": "", + "tts_id": None, + } + response = requests.post(url=url, headers=authorization, json=tenant_info) + res = response.json() + if res.get("code") != 0: + raise Exception(res.get("message")) + + @pytest.fixture(scope="function") def clear_datasets(request, get_http_api_auth): def cleanup():