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.

65 lines
2.0 KiB

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