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

conftest.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. from libs.auth import RAGFlowHttpApiAuth
  20. HOST_ADDRESS = os.getenv("HOST_ADDRESS", "http://127.0.0.1:9380")
  21. # def generate_random_email():
  22. # return 'user_' + ''.join(random.choices(string.ascii_lowercase + string.digits, k=8))+'@1.com'
  23. def generate_email():
  24. return "user_123@1.com"
  25. EMAIL = generate_email()
  26. # password is "123"
  27. PASSWORD = """ctAseGvejiaSWWZ88T/m4FQVOpQyUvP+x7sXtdv3feqZACiQleuewkUi35E16wSd5C5QcnkkcV9cYc8TKPTRZlxappDuirxghxoOvFcJxFU4ixLsD
  28. fN33jCHRoDUW81IH9zjij/vaw8IbVyb6vuwg6MX6inOEBRRzVbRYxXOu1wkWY6SsI8X70oF9aeLFp/PzQpjoe/YbSqpTq8qqrmHzn9vO+yvyYyvmDsphXe
  29. X8f7fp9c7vUsfOCkM+gHY3PadG+QHa7KI7mzTKgUTZImK6BZtfRBATDTthEUbbaTewY4H0MnWiCeeDhcbeQao6cFy1To8pE3RpmxnGnS8BsBn8w=="""
  30. def register():
  31. url = HOST_ADDRESS + "/v1/user/register"
  32. name = "user"
  33. register_data = {"email": EMAIL, "nickname": name, "password": PASSWORD}
  34. res = requests.post(url=url, json=register_data)
  35. res = res.json()
  36. if res.get("code") != 0:
  37. raise Exception(res.get("message"))
  38. def login():
  39. url = HOST_ADDRESS + "/v1/user/login"
  40. login_data = {"email": EMAIL, "password": PASSWORD}
  41. response = requests.post(url=url, json=login_data)
  42. res = response.json()
  43. if res.get("code") != 0:
  44. raise Exception(res.get("message"))
  45. auth = response.headers["Authorization"]
  46. return auth
  47. @pytest.fixture(scope="session")
  48. def get_api_key_fixture():
  49. try:
  50. register()
  51. except Exception as e:
  52. print(e)
  53. auth = login()
  54. url = HOST_ADDRESS + "/v1/system/new_token"
  55. auth = {"Authorization": auth}
  56. response = requests.post(url=url, headers=auth)
  57. res = response.json()
  58. if res.get("code") != 0:
  59. raise Exception(res.get("message"))
  60. return res["data"].get("token")
  61. @pytest.fixture(scope="session")
  62. def get_auth():
  63. try:
  64. register()
  65. except Exception as e:
  66. print(e)
  67. auth = login()
  68. return auth
  69. @pytest.fixture(scope="session")
  70. def get_email():
  71. return EMAIL
  72. @pytest.fixture(scope="session")
  73. def get_http_api_auth(get_api_key_fixture):
  74. return RAGFlowHttpApiAuth(get_api_key_fixture)
  75. def get_my_llms(auth, name):
  76. url = HOST_ADDRESS + "/v1/llm/my_llms"
  77. authorization = {"Authorization": auth}
  78. response = requests.get(url=url, headers=authorization)
  79. res = response.json()
  80. if res.get("code") != 0:
  81. raise Exception(res.get("message"))
  82. if name in res.get("data"):
  83. return True
  84. return False
  85. def add_models(auth):
  86. url = HOST_ADDRESS + "/v1/llm/set_api_key"
  87. authorization = {"Authorization": auth}
  88. models_info = {
  89. "ZHIPU-AI": {"llm_factory": "ZHIPU-AI", "api_key": "d06253dacd404180aa8afb096fcb6c30.KatwBIUpvCSml9sU"},
  90. }
  91. for name, model_info in models_info.items():
  92. if not get_my_llms(auth, name):
  93. response = requests.post(url=url, headers=authorization, json=model_info)
  94. res = response.json()
  95. if res.get("code") != 0:
  96. raise Exception(res.get("message"))
  97. def get_tenant_info(auth):
  98. url = HOST_ADDRESS + "/v1/user/tenant_info"
  99. authorization = {"Authorization": auth}
  100. response = requests.get(url=url, headers=authorization)
  101. res = response.json()
  102. if res.get("code") != 0:
  103. raise Exception(res.get("message"))
  104. return res["data"].get("tenant_id")
  105. @pytest.fixture(scope="session", autouse=True)
  106. def set_tenant_info(get_auth):
  107. auth = get_auth
  108. try:
  109. add_models(auth)
  110. tenant_id = get_tenant_info(auth)
  111. except Exception as e:
  112. raise Exception(e)
  113. url = HOST_ADDRESS + "/v1/user/set_tenant_info"
  114. authorization = {"Authorization": get_auth}
  115. tenant_info = {
  116. "tenant_id": tenant_id,
  117. "llm_id": "glm-4-flash@ZHIPU-AI",
  118. "embd_id": "embedding-3@ZHIPU-AI",
  119. "img2txt_id": "glm-4v@ZHIPU-AI",
  120. "asr_id": "",
  121. "tts_id": None,
  122. }
  123. response = requests.post(url=url, headers=authorization, json=tenant_info)
  124. res = response.json()
  125. if res.get("code") != 0:
  126. raise Exception(res.get("message"))