Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

configure_openai_plugin.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 config_helper
  7. from common import Logger
  8. def configure_openai_plugin() -> None:
  9. """Configure OpenAI plugin with mock server credentials."""
  10. log = Logger("ConfigPlugin")
  11. log.header("Configuring OpenAI Plugin")
  12. # Read token from config
  13. access_token = config_helper.get_token()
  14. if not access_token:
  15. log.error("No access token found in config")
  16. log.info("Please run login_admin.py first to get access token")
  17. return
  18. log.step("Configuring OpenAI plugin with mock server...")
  19. # API endpoint for plugin configuration
  20. base_url = "http://localhost:5001"
  21. config_endpoint = f"{base_url}/console/api/workspaces/current/model-providers/langgenius/openai/openai/credentials"
  22. # Configuration payload with mock server
  23. config_payload = {
  24. "credentials": {
  25. "openai_api_key": "apikey",
  26. "openai_organization": None,
  27. "openai_api_base": "http://host.docker.internal:5004",
  28. }
  29. }
  30. headers = {
  31. "Accept": "*/*",
  32. "Accept-Language": "en-US,en;q=0.9",
  33. "Cache-Control": "no-cache",
  34. "Connection": "keep-alive",
  35. "DNT": "1",
  36. "Origin": "http://localhost:3000",
  37. "Pragma": "no-cache",
  38. "Referer": "http://localhost:3000/",
  39. "Sec-Fetch-Dest": "empty",
  40. "Sec-Fetch-Mode": "cors",
  41. "Sec-Fetch-Site": "same-site",
  42. "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
  43. "authorization": f"Bearer {access_token}",
  44. "content-type": "application/json",
  45. "sec-ch-ua": '"Not;A=Brand";v="99", "Google Chrome";v="139", "Chromium";v="139"',
  46. "sec-ch-ua-mobile": "?0",
  47. "sec-ch-ua-platform": '"macOS"',
  48. }
  49. cookies = {"locale": "en-US"}
  50. try:
  51. # Make the configuration request
  52. with httpx.Client() as client:
  53. response = client.post(
  54. config_endpoint,
  55. json=config_payload,
  56. headers=headers,
  57. cookies=cookies,
  58. )
  59. if response.status_code == 200:
  60. log.success("OpenAI plugin configured successfully!")
  61. log.key_value(
  62. "API Base", config_payload["credentials"]["openai_api_base"]
  63. )
  64. log.key_value(
  65. "API Key", config_payload["credentials"]["openai_api_key"]
  66. )
  67. elif response.status_code == 201:
  68. log.success("OpenAI plugin credentials created successfully!")
  69. log.key_value(
  70. "API Base", config_payload["credentials"]["openai_api_base"]
  71. )
  72. log.key_value(
  73. "API Key", config_payload["credentials"]["openai_api_key"]
  74. )
  75. elif response.status_code == 401:
  76. log.error("Configuration failed: Unauthorized")
  77. log.info("Token may have expired. Please run login_admin.py again")
  78. else:
  79. log.error(
  80. f"Configuration failed with status code: {response.status_code}"
  81. )
  82. log.debug(f"Response: {response.text}")
  83. except httpx.ConnectError:
  84. log.error("Could not connect to Dify API at http://localhost:5001")
  85. log.info("Make sure the API server is running with: ./dev/start-api")
  86. except Exception as e:
  87. log.error(f"An error occurred: {e}")
  88. if __name__ == "__main__":
  89. configure_openai_plugin()