您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

conftest.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import os
  17. import pytest
  18. import requests
  19. HOST_ADDRESS = os.getenv("HOST_ADDRESS", "http://127.0.0.1:9380")
  20. ZHIPU_AI_API_KEY = os.getenv("ZHIPU_AI_API_KEY", "ca148e43209c40109e2bc2f56281dd11.BltyA2N1B043B7Ra")
  21. if ZHIPU_AI_API_KEY is None:
  22. pytest.exit("Error: Environment variable ZHIPU_AI_API_KEY must be set")
  23. # def generate_random_email():
  24. # return 'user_' + ''.join(random.choices(string.ascii_lowercase + string.digits, k=8))+'@1.com'
  25. def generate_email():
  26. return "user_123@1.com"
  27. EMAIL = generate_email()
  28. # password is "123"
  29. PASSWORD = """ctAseGvejiaSWWZ88T/m4FQVOpQyUvP+x7sXtdv3feqZACiQleuewkUi35E16wSd5C5QcnkkcV9cYc8TKPTRZlxappDuirxghxoOvFcJxFU4ixLsD
  30. fN33jCHRoDUW81IH9zjij/vaw8IbVyb6vuwg6MX6inOEBRRzVbRYxXOu1wkWY6SsI8X70oF9aeLFp/PzQpjoe/YbSqpTq8qqrmHzn9vO+yvyYyvmDsphXe
  31. X8f7fp9c7vUsfOCkM+gHY3PadG+QHa7KI7mzTKgUTZImK6BZtfRBATDTthEUbbaTewY4H0MnWiCeeDhcbeQao6cFy1To8pE3RpmxnGnS8BsBn8w=="""
  32. def register():
  33. url = HOST_ADDRESS + "/v1/user/register"
  34. name = "user"
  35. register_data = {"email": EMAIL, "nickname": name, "password": PASSWORD}
  36. res = requests.post(url=url, json=register_data)
  37. res = res.json()
  38. if res.get("code") != 0:
  39. raise Exception(res.get("message"))
  40. def login():
  41. url = HOST_ADDRESS + "/v1/user/login"
  42. login_data = {"email": EMAIL, "password": PASSWORD}
  43. response = requests.post(url=url, json=login_data)
  44. res = response.json()
  45. if res.get("code") != 0:
  46. raise Exception(res.get("message"))
  47. auth = response.headers["Authorization"]
  48. return auth
  49. @pytest.fixture(scope="session")
  50. def get_api_key_fixture():
  51. try:
  52. register()
  53. except Exception as e:
  54. print(e)
  55. auth = login()
  56. url = HOST_ADDRESS + "/v1/system/new_token"
  57. auth = {"Authorization": auth}
  58. response = requests.post(url=url, headers=auth)
  59. res = response.json()
  60. if res.get("code") != 0:
  61. raise Exception(res.get("message"))
  62. return res["data"].get("token")
  63. @pytest.fixture(scope="session")
  64. def get_auth():
  65. try:
  66. register()
  67. except Exception as e:
  68. print(e)
  69. auth = login()
  70. return auth
  71. @pytest.fixture(scope="session")
  72. def get_email():
  73. return EMAIL
  74. def get_my_llms(auth, name):
  75. url = HOST_ADDRESS + "/v1/llm/my_llms"
  76. authorization = {"Authorization": auth}
  77. response = requests.get(url=url, headers=authorization)
  78. res = response.json()
  79. if res.get("code") != 0:
  80. raise Exception(res.get("message"))
  81. if name in res.get("data"):
  82. return True
  83. return False
  84. def add_models(auth):
  85. url = HOST_ADDRESS + "/v1/llm/set_api_key"
  86. authorization = {"Authorization": auth}
  87. models_info = {
  88. "ZHIPU-AI": {"llm_factory": "ZHIPU-AI", "api_key": ZHIPU_AI_API_KEY},
  89. }
  90. for name, model_info in models_info.items():
  91. if not get_my_llms(auth, name):
  92. response = requests.post(url=url, headers=authorization, json=model_info)
  93. res = response.json()
  94. if res.get("code") != 0:
  95. pytest.exit(f"Critical error in add_models: {res.get('message')}")
  96. def get_tenant_info(auth):
  97. url = HOST_ADDRESS + "/v1/user/tenant_info"
  98. authorization = {"Authorization": auth}
  99. response = requests.get(url=url, headers=authorization)
  100. res = response.json()
  101. if res.get("code") != 0:
  102. raise Exception(res.get("message"))
  103. return res["data"].get("tenant_id")
  104. @pytest.fixture(scope="session", autouse=True)
  105. def set_tenant_info(get_auth):
  106. auth = get_auth
  107. try:
  108. add_models(auth)
  109. tenant_id = get_tenant_info(auth)
  110. except Exception as e:
  111. pytest.exit(f"Error in set_tenant_info: {str(e)}")
  112. url = HOST_ADDRESS + "/v1/user/set_tenant_info"
  113. authorization = {"Authorization": get_auth}
  114. tenant_info = {
  115. "tenant_id": tenant_id,
  116. "llm_id": "glm-4-flash@ZHIPU-AI",
  117. "embd_id": "BAAI/bge-large-zh-v1.5@BAAI",
  118. "img2txt_id": "glm-4v@ZHIPU-AI",
  119. "asr_id": "",
  120. "tts_id": None,
  121. }
  122. response = requests.post(url=url, headers=authorization, json=tenant_info)
  123. res = response.json()
  124. if res.get("code") != 0:
  125. raise Exception(res.get("message"))