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.

57 lines
1.8 KiB

  1. from __future__ import annotations
  2. import random
  3. from datetime import datetime
  4. from enum import Enum
  5. from eloquent import DatabaseManager, Model
  6. from models import Answer, Query, User
  7. from talk import Talk
  8. def main (
  9. ) -> None:
  10. queries: list[Query] = Query.where ('answered', False).get ()
  11. query: Query = random.choice (queries)
  12. message: str | list[dict[str, str | dict[str, str]]]
  13. if query.image_url is None:
  14. message = query.content
  15. else:
  16. message = [{ 'type': 'text', 'text': query.content },
  17. { 'type': 'image_url', 'image_url': query.image_url }]
  18. user: User | None = query.user
  19. user_name: str | None = None
  20. if user is not None:
  21. user_name = user.name
  22. histories: list[dict[str, str]] = []
  23. for history in query.answer_histories:
  24. if history.query is not None:
  25. histories.append ({ 'role': 'user', 'content': history.query.content })
  26. histories.append ({ 'role': 'assistant', 'content': history.content })
  27. if query.target_character & Character.DEERJIKA.value:
  28. answer = Answer ()
  29. answer.query_id = query.id
  30. answer.character = Character.DEERJIKA.value
  31. answer.content = Talk.main (query.content, user_name, histories)
  32. answer.answer_type = query.query_type
  33. answer.sent_at = datetime.now ()
  34. answer.save ()
  35. if query.target_character & Character.GOATOH.value:
  36. answer = Answer ()
  37. answer.query_id = query.id
  38. answer.character = Character.GOATOH.value
  39. answer.content = Talk.main (query.content, user_name, histories, True)
  40. answer.answer_type = query.query_type
  41. answer.sent_at = datetime.now ()
  42. answer.save ()
  43. class Character (Enum):
  44. DEERJIKA = 1
  45. GOATOH = 2
  46. if __name__ == '__main__':
  47. main ()