You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

setup_admin.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python3
  2. import sys
  3. from pathlib import Path
  4. sys.path.append(str(Path(__file__).parent.parent))
  5. import httpx
  6. from common import Logger, config_helper
  7. def setup_admin_account() -> None:
  8. """Setup Dify API with an admin account."""
  9. log = Logger("SetupAdmin")
  10. log.header("Setting up Admin Account")
  11. # Admin account credentials
  12. admin_config = {
  13. "email": "test@dify.ai",
  14. "username": "dify",
  15. "password": "password123",
  16. }
  17. # Save credentials to config file
  18. if config_helper.write_config("admin_config", admin_config):
  19. log.info(f"Admin credentials saved to: {config_helper.get_config_path('benchmark_state')}")
  20. # API setup endpoint
  21. base_url = "http://localhost:5001"
  22. setup_endpoint = f"{base_url}/console/api/setup"
  23. # Prepare setup payload
  24. setup_payload = {
  25. "email": admin_config["email"],
  26. "name": admin_config["username"],
  27. "password": admin_config["password"],
  28. }
  29. log.step("Configuring Dify with admin account...")
  30. try:
  31. # Make the setup request
  32. with httpx.Client() as client:
  33. response = client.post(
  34. setup_endpoint,
  35. json=setup_payload,
  36. headers={"Content-Type": "application/json"},
  37. )
  38. if response.status_code == 201:
  39. log.success("Admin account created successfully!")
  40. log.key_value("Email", admin_config["email"])
  41. log.key_value("Username", admin_config["username"])
  42. elif response.status_code == 400:
  43. log.warning("Setup may have already been completed or invalid data provided")
  44. log.debug(f"Response: {response.text}")
  45. else:
  46. log.error(f"Setup failed with status code: {response.status_code}")
  47. log.debug(f"Response: {response.text}")
  48. except httpx.ConnectError:
  49. log.error("Could not connect to Dify API at http://localhost:5001")
  50. log.info("Make sure the API server is running with: ./dev/start-api")
  51. except Exception as e:
  52. log.error(f"An error occurred: {e}")
  53. if __name__ == "__main__":
  54. setup_admin_account()