Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

api_service.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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):
  39. sessions = cls.model.select().where(cls.model.dialog_id == dialog_id)
  40. if id:
  41. sessions = sessions.where(cls.model.id == id)
  42. if user_id:
  43. sessions = sessions.where(cls.model.user_id == user_id)
  44. if desc:
  45. sessions = sessions.order_by(cls.model.getter_by(orderby).desc())
  46. else:
  47. sessions = sessions.order_by(cls.model.getter_by(orderby).asc())
  48. sessions = sessions.where(cls.model.user_id == tenant_id)
  49. sessions = sessions.paginate(page_number, items_per_page)
  50. return list(sessions.dicts())
  51. @classmethod
  52. @DB.connection_context()
  53. def append_message(cls, id, conversation):
  54. cls.update_by_id(id, conversation)
  55. return cls.model.update(round=cls.model.round + 1).where(cls.model.id == id).execute()
  56. @classmethod
  57. @DB.connection_context()
  58. def stats(cls, tenant_id, from_date, to_date, source=None):
  59. if len(to_date) == 10:
  60. to_date += " 23:59:59"
  61. return cls.model.select(
  62. cls.model.create_date.truncate("day").alias("dt"),
  63. peewee.fn.COUNT(
  64. cls.model.id).alias("pv"),
  65. peewee.fn.COUNT(
  66. cls.model.user_id.distinct()).alias("uv"),
  67. peewee.fn.SUM(
  68. cls.model.tokens).alias("tokens"),
  69. peewee.fn.SUM(
  70. cls.model.duration).alias("duration"),
  71. peewee.fn.AVG(
  72. cls.model.round).alias("round"),
  73. peewee.fn.SUM(
  74. cls.model.thumb_up).alias("thumb_up")
  75. ).join(Dialog, on=((cls.model.dialog_id == Dialog.id) & (Dialog.tenant_id == tenant_id))).where(
  76. cls.model.create_date >= from_date,
  77. cls.model.create_date <= to_date,
  78. cls.model.source == source
  79. ).group_by(cls.model.create_date.truncate("day")).dicts()