您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #
  2. # Copyright 2024 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, API4Conversation, APIToken, Dialog
  19. from api.db.services.common_service import CommonService
  20. from api.utils import current_timestamp, datetime_format
  21. class APITokenService(CommonService):
  22. model = APIToken
  23. @classmethod
  24. @DB.connection_context()
  25. def used(cls, token):
  26. return cls.model.update({
  27. "update_time": current_timestamp(),
  28. "update_date": datetime_format(datetime.now()),
  29. }).where(
  30. cls.model.token == token
  31. )
  32. class API4ConversationService(CommonService):
  33. model = API4Conversation
  34. @classmethod
  35. @DB.connection_context()
  36. def get_list(cls, dialog_id, tenant_id,
  37. page_number, items_per_page,
  38. orderby, desc, id, user_id=None, include_dsl=True, keywords="",
  39. from_date=None, to_date=None
  40. ):
  41. if include_dsl:
  42. sessions = cls.model.select().where(cls.model.dialog_id == dialog_id)
  43. else:
  44. fields = [field for field in cls.model._meta.fields.values() if field.name != 'dsl']
  45. sessions = cls.model.select(*fields).where(cls.model.dialog_id == dialog_id)
  46. if id:
  47. sessions = sessions.where(cls.model.id == id)
  48. if user_id:
  49. sessions = sessions.where(cls.model.user_id == user_id)
  50. if keywords:
  51. sessions = sessions.where(peewee.fn.LOWER(cls.model.message).contains(keywords.lower()))
  52. if from_date:
  53. sessions = sessions.where(cls.model.create_date >= from_date)
  54. if to_date:
  55. sessions = sessions.where(cls.model.create_date <= to_date)
  56. if desc:
  57. sessions = sessions.order_by(cls.model.getter_by(orderby).desc())
  58. else:
  59. sessions = sessions.order_by(cls.model.getter_by(orderby).asc())
  60. count = sessions.count()
  61. sessions = sessions.paginate(page_number, items_per_page)
  62. return count, list(sessions.dicts())
  63. @classmethod
  64. @DB.connection_context()
  65. def append_message(cls, id, conversation):
  66. cls.update_by_id(id, conversation)
  67. return cls.model.update(round=cls.model.round + 1).where(cls.model.id == id).execute()
  68. @classmethod
  69. @DB.connection_context()
  70. def stats(cls, tenant_id, from_date, to_date, source=None):
  71. if len(to_date) == 10:
  72. to_date += " 23:59:59"
  73. return cls.model.select(
  74. cls.model.create_date.truncate("day").alias("dt"),
  75. peewee.fn.COUNT(
  76. cls.model.id).alias("pv"),
  77. peewee.fn.COUNT(
  78. cls.model.user_id.distinct()).alias("uv"),
  79. peewee.fn.SUM(
  80. cls.model.tokens).alias("tokens"),
  81. peewee.fn.SUM(
  82. cls.model.duration).alias("duration"),
  83. peewee.fn.AVG(
  84. cls.model.round).alias("round"),
  85. peewee.fn.SUM(
  86. cls.model.thumb_up).alias("thumb_up")
  87. ).join(Dialog, on=((cls.model.dialog_id == Dialog.id) & (Dialog.tenant_id == tenant_id))).where(
  88. cls.model.create_date >= from_date,
  89. cls.model.create_date <= to_date,
  90. cls.model.source == source
  91. ).group_by(cls.model.create_date.truncate("day")).dicts()