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.

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