AI ニジカ綜合
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.

queries_to_answers.py 2.5 KiB

3 weeks ago
1 month ago
1 month ago
1 month ago
3 weeks ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from __future__ import annotations
  2. import random
  3. from datetime import datetime
  4. from typing import TypedDict
  5. from .config import DB
  6. from .consts import AnswerType, Character, Platform
  7. from .models import Answer, AnsweredFlag, Query, User
  8. from .talk import Talk
  9. def main (
  10. ) -> None:
  11. DB.begin_transaction ()
  12. queries: list[Query] = Query.where ('answered', False).get ()
  13. if not queries:
  14. return
  15. query: Query = random.choice (queries)
  16. user: User = query.user
  17. user_name: str | None = None
  18. if query.user_id is not None:
  19. user_name = user.name
  20. histories: list[History] = []
  21. for history in query.answer_histories:
  22. if history.query is not None:
  23. histories.append ({ 'role': 'user', 'content': history.query.content })
  24. histories.append ({ 'role': 'assistant', 'content': history.content })
  25. for character in [Character.DEERJIKA, Character.GOATOH]:
  26. if query.target_character & character.value:
  27. add_answer (query, character, user_name, histories)
  28. query.answered = True
  29. query.save ()
  30. DB.commit ()
  31. def add_answer (
  32. query: Query,
  33. character: Character,
  34. user_name: str | None,
  35. histories: list[History],
  36. ) -> None:
  37. message: str | list[dict[str, str | dict[str, str]]]
  38. if query.image_url is None:
  39. message = query.content
  40. else:
  41. message = [{ 'type': 'text', 'text': query.content },
  42. { 'type': 'image_url', 'image_url': query.image_url }]
  43. answer = Answer ()
  44. answer.query_id = query.id
  45. answer.character = character.value
  46. answer.content = Talk.main (message, user_name, histories,
  47. goatoh_mode = character == Character.GOATOH)
  48. answer.answer_type = query.query_type
  49. answer.sent_at = datetime.now ()
  50. answer.save ()
  51. add_answered_flags (answer)
  52. def add_answered_flags (
  53. answer: Answer,
  54. ) -> None:
  55. print (answer.answer_type)
  56. if answer.answer_type in [AnswerType.YOUTUBE_REPLY]:
  57. add_answered_flag (answer, Platform.YOUTUBE)
  58. if answer.answer_type in [AnswerType.BLUESKY_REPLY]:
  59. add_answered_flag (answer, Platform.BLUESKY)
  60. def add_answered_flag (
  61. answer: Answer,
  62. platform: Platform,
  63. ) -> None:
  64. answered_flag = AnsweredFlag ()
  65. answered_flag.answer_id = answer.id
  66. answered_flag.platform = platform.value
  67. answered_flag.answered = False
  68. answered_flag.save ()
  69. class History (TypedDict):
  70. role: str
  71. content: str
  72. if __name__ == '__main__':
  73. main ()