gemini_search.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import json
  2. import google
  3. import google.genai
  4. class gemini_search:
  5. def __init__(self, apikey: str) -> None:
  6. self.__apikey = apikey
  7. @property
  8. def __client(self) -> google.genai.Client:
  9. return google.genai.Client(api_key = self.__apikey)
  10. @property
  11. def __model(self) -> str:
  12. return "gemini-2.5-flash-preview-05-20"
  13. def __user(self, barcode: str) -> google.genai.types.Content:
  14. return google.genai.types.Content(
  15. role = "user",
  16. parts = [
  17. google.genai.types.Part.from_text(
  18. text = barcode
  19. )
  20. ]
  21. )
  22. @property
  23. def __instructions(self) -> str:
  24. return (
  25. "Find a product with the given EAN code. Write a short " \
  26. + "description of this product (about 200 characters) in " \
  27. + "the native language of the country where the product " \
  28. + "comes from. Write only the truth."
  29. )
  30. @property
  31. def __research(self) -> google.genai.types.GenerateContentConfig:
  32. return google.genai.types.GenerateContentConfig(
  33. response_mime_type = "text/plain",
  34. thinking_config = google.genai.types.ThinkingConfig(
  35. thinking_budget = 0,
  36. ),
  37. tools = [
  38. google.genai.types.Tool(
  39. google_search = google.genai.types.GoogleSearch()
  40. )
  41. ],
  42. system_instruction = [
  43. google.genai.types.Part.from_text(
  44. text = self.__instructions
  45. )
  46. ]
  47. )
  48. @property
  49. def __process(self) -> google.genai.types.GenerateContentConfig:
  50. return google.genai.types.GenerateContentConfig(
  51. thinking_config = google.genai.types.ThinkingConfig(
  52. thinking_budget = 0,
  53. ),
  54. response_mime_type = "application/json",
  55. response_schema = google.genai.types.Schema(
  56. type = google.genai.types.Type.OBJECT,
  57. required = ["title", "description", "author"],
  58. properties = {
  59. "title": google.genai.types.Schema(
  60. type = google.genai.types.Type.STRING,
  61. ),
  62. "description": google.genai.types.Schema(
  63. type = google.genai.types.Type.STRING,
  64. ),
  65. "author": google.genai.types.Schema(
  66. type = google.genai.types.Type.STRING,
  67. ),
  68. },
  69. ),
  70. system_instruction = [
  71. google.genai.types.Part.from_text(
  72. text = self.__instructions
  73. )
  74. ],
  75. )
  76. def __request(
  77. self,
  78. barcode: str,
  79. conf: google.genai.types.GenerateContentConfig,
  80. addon: str | None = None
  81. ) -> str:
  82. content = ""
  83. contents = [
  84. self.__user(barcode)
  85. ]
  86. if addon is not None:
  87. contents.append(
  88. google.genai.types.Content(
  89. role = "model",
  90. parts = [
  91. google.genai.types.Part.from_text(
  92. text = addon
  93. )
  94. ]
  95. )
  96. )
  97. contents.append(
  98. self.__user(barcode)
  99. )
  100. for count in self.__client.models.generate_content_stream(
  101. model = self.__model,
  102. contents = contents,
  103. config = conf
  104. ):
  105. content = content + count.text
  106. return content
  107. def search(self, barcode: str) -> dict:
  108. research = self.__request(barcode, self.__research)
  109. model = self.__request(barcode, self.__process, research)
  110. return json.loads(model)