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.

llm_app.py 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #
  2. # Copyright 2019 The FATE 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. from flask import request
  17. from flask_login import login_required, current_user
  18. from web_server.db.services import duplicate_name
  19. from web_server.db.services.llm_service import LLMFactoriesService, TenantLLMService, LLMService
  20. from web_server.db.services.user_service import TenantService, UserTenantService
  21. from web_server.utils.api_utils import server_error_response, get_data_error_result, validate_request
  22. from web_server.utils import get_uuid, get_format_time
  23. from web_server.db import StatusEnum, UserTenantRole
  24. from web_server.db.services.kb_service import KnowledgebaseService
  25. from web_server.db.db_models import Knowledgebase, TenantLLM
  26. from web_server.settings import stat_logger, RetCode
  27. from web_server.utils.api_utils import get_json_result
  28. @manager.route('/factories', methods=['GET'])
  29. @login_required
  30. def factories():
  31. try:
  32. fac = LLMFactoriesService.get_all()
  33. return get_json_result(data=fac.to_json())
  34. except Exception as e:
  35. return server_error_response(e)
  36. @manager.route('/set_api_key', methods=['POST'])
  37. @login_required
  38. @validate_request("llm_factory", "api_key")
  39. def set_api_key():
  40. req = request.json
  41. llm = {
  42. "tenant_id": current_user.id,
  43. "llm_factory": req["llm_factory"],
  44. "api_key": req["api_key"]
  45. }
  46. # TODO: Test api_key
  47. for n in ["model_type", "llm_name"]:
  48. if n in req: llm[n] = req[n]
  49. TenantLLM.insert(**llm).on_conflict("replace").execute()
  50. return get_json_result(data=True)
  51. @manager.route('/my_llms', methods=['GET'])
  52. @login_required
  53. def my_llms():
  54. try:
  55. objs = TenantLLMService.query(tenant_id=current_user.id)
  56. objs = [o.to_dict() for o in objs]
  57. for o in objs: del o["api_key"]
  58. return get_json_result(data=objs)
  59. except Exception as e:
  60. return server_error_response(e)
  61. @manager.route('/list', methods=['GET'])
  62. @login_required
  63. def list():
  64. try:
  65. objs = TenantLLMService.query(tenant_id=current_user.id)
  66. objs = [o.to_dict() for o in objs if o.api_key]
  67. fct = {}
  68. for o in objs:
  69. if o["llm_factory"] not in fct: fct[o["llm_factory"]] = []
  70. if o["llm_name"]: fct[o["llm_factory"]].append(o["llm_name"])
  71. llms = LLMService.get_all()
  72. llms = [m.to_dict() for m in llms if m.status == StatusEnum.VALID.value]
  73. for m in llms:
  74. m["available"] = False
  75. if m["fid"] in fct and (not fct[m["fid"]] or m["llm_name"] in fct[m["fid"]]):
  76. m["available"] = True
  77. res = {}
  78. for m in llms:
  79. if m["fid"] not in res: res[m["fid"]] = []
  80. res[m["fid"]].append(m)
  81. return get_json_result(data=res)
  82. except Exception as e:
  83. return server_error_response(e)