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ů.

langfuse_service.py 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. from datetime import datetime
  17. import peewee
  18. from api.db.db_models import DB, TenantLangfuse
  19. from api.db.services.common_service import CommonService
  20. from api.utils import current_timestamp, datetime_format
  21. class TenantLangfuseService(CommonService):
  22. """
  23. All methods that modify the status should be enclosed within a DB.atomic() context to ensure atomicity
  24. and maintain data integrity in case of errors during execution.
  25. """
  26. model = TenantLangfuse
  27. @classmethod
  28. @DB.connection_context()
  29. def filter_by_tenant(cls, tenant_id):
  30. fields = [cls.model.tenant_id, cls.model.host, cls.model.secret_key, cls.model.public_key]
  31. try:
  32. keys = cls.model.select(*fields).where(cls.model.tenant_id == tenant_id).first()
  33. return keys
  34. except peewee.DoesNotExist:
  35. return None
  36. @classmethod
  37. @DB.connection_context()
  38. def filter_by_tenant_with_info(cls, tenant_id):
  39. fields = [cls.model.tenant_id, cls.model.host, cls.model.secret_key, cls.model.public_key]
  40. try:
  41. keys = cls.model.select(*fields).where(cls.model.tenant_id == tenant_id).dicts().first()
  42. return keys
  43. except peewee.DoesNotExist:
  44. return None
  45. @classmethod
  46. def update_by_tenant(cls, tenant_id, langfuse_keys):
  47. langfuse_keys["update_time"] = current_timestamp()
  48. langfuse_keys["update_date"] = datetime_format(datetime.now())
  49. return cls.model.update(**langfuse_keys).where(cls.model.tenant_id == tenant_id).execute()
  50. @classmethod
  51. def save(cls, **kwargs):
  52. kwargs["create_time"] = current_timestamp()
  53. kwargs["create_date"] = datetime_format(datetime.now())
  54. kwargs["update_time"] = current_timestamp()
  55. kwargs["update_date"] = datetime_format(datetime.now())
  56. obj = cls.model.create(**kwargs)
  57. return obj
  58. @classmethod
  59. def delete_model(cls, langfuse_model):
  60. langfuse_model.delete_instance()