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.

60 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. query: Query = random.choice (queries)
  15. message: str | list[dict[str, str | dict[str, str]]]
  16. if query.image_url is None:
  17. message = query.content
  18. else:
  19. message = [{ 'type': 'text', 'text': query.content },
  20. { 'type': 'image_url', 'image_url': query.image_url }]
  21. user: User | None = query.user
  22. user_name: str | None = None
  23. if user is not None:
  24. user_name = user.name
  25. histories: list[dict[str, str]] = []
  26. for history in query.answer_histories:
  27. if history.query is not None:
  28. histories.append ({ 'role': 'user', 'content': history.query.content })
  29. histories.append ({ 'role': 'assistant', 'content': history.content })
  30. if query.target_character & Character.DEERJIKA.value:
  31. answer = Answer ()
  32. answer.query_id = query.id
  33. answer.character = Character.DEERJIKA.value
  34. answer.content = Talk.main (query.content, user_name, histories)
  35. answer.answer_type = query.query_type
  36. answer.sent_at = datetime.now ()
  37. answer.save ()
  38. if query.target_character & Character.GOATOH.value:
  39. answer = Answer ()
  40. answer.query_id = query.id
  41. answer.character = Character.GOATOH.value
  42. answer.content = Talk.main (query.content, user_name, histories, True)
  43. answer.answer_type = query.query_type
  44. answer.sent_at = datetime.now ()
  45. answer.save ()
  46. class Character (Enum):
  47. DEERJIKA = 1
  48. GOATOH = 2
  49. if __name__ == '__main__':
  50. main ()