Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

configure_openai_plugin.py 3.4KB

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