123Aadidev

Untitled

Jul 28th, 2026
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. import csv
  2. import discord
  3. from discord.ext import commands
  4. from groq import Groq
  5. from surprise import SVD, Dataset, Reader
  6.  
  7. GROQ_API_KEY = "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"
  8. DISCORD_TOKEN = "MTUyOTIyMTE0NjIzNzU5OTg1NQ.GS88u5.LK7oXe3F1B0_jdTqemPRBkcxlT4HiGXJdOLwaE"
  9.  
  10. ITEM_PATH = r"C:\Users\aadid\PycharmProjects\PythonProject\ml-100k\ml-100k\u.item"
  11. DATA_PATH = r"C:\Users\aadid\PycharmProjects\PythonProject\ml-100k\ml-100k\u.data"
  12.  
  13.  
  14. movies = []
  15. movie_id_to_title = {}
  16.  
  17. with open(ITEM_PATH, "r", encoding="utf-8", errors="replace") as file:
  18. reader = csv.reader(file, delimiter="|")
  19. for row in reader:
  20. if len(row) > 1:
  21. movies.append(row)
  22. movie_id_to_title[row[0]] = row[1]
  23.  
  24. print("Movies loaded:", len(movies))
  25. reader = Reader(line_format="user item rating timestamp", sep="\t")
  26. data = Dataset.load_from_file(DATA_PATH, reader=reader)
  27.  
  28. trainset = data.build_full_trainset()
  29. svd_algo = SVD()
  30. svd_algo.fit(trainset)
  31. print("Surprise SVD Model trained successfully!")
  32.  
  33. client = Groq(api_key=GROQ_API_KEY)
  34.  
  35. intents = discord.Intents.default()
  36. intents.message_content = True
  37.  
  38. bot = commands.Bot(command_prefix="!", intents=intents)
  39.  
  40. conversation = {}
  41.  
  42. system_prompt = """
  43. You are ZenAI, an AI assistant inside Discord, made by Aadidev.
  44.  
  45. You are helpful, natural, and clear.
  46.  
  47. Help users with:
  48. - Movies
  49. - Games
  50. - Coding
  51. - School
  52. - Science
  53. - Technology
  54. - General questions
  55.  
  56. MOVIES:
  57. When a user asks for movie recommendations:
  58. First ask:
  59. - What genres do you like?
  60. - What age rating are you comfortable with?
  61. - What movies have you enjoyed before?
  62.  
  63. Use the user's answers to recommend movies.
  64.  
  65. For every movie recommendation include:
  66. - Movie title
  67. - Short description
  68. - Rating out of 10
  69. - Why they might like it
  70.  
  71. Use the dataset provided when possible.
  72.  
  73. GAMES:
  74. When a user asks for game recommendations:
  75. First ask:
  76. - What genres they like
  77. - What platform they play on
  78. - Multiplayer or story preference
  79.  
  80. For each game include:
  81. - Title
  82. - Genre
  83. - Platform
  84. - Short description
  85. - Rating out of 10
  86. - Why they might enjoy it
  87.  
  88. Do not use unnecessary greetings.
  89. Do not end every answer with a question.
  90. """
  91.  
  92. def get_svd_recommendations(user_id_str="1", top_n=10):
  93. predictions = []
  94. for item_id, title in movie_id_to_title.items():
  95. pred = svd_algo.predict(uid=user_id_str, iid=item_id)
  96. predictions.append((title, pred.est))
  97.  
  98. predictions.sort(key=lambda x: x[1], reverse=True)
  99. return predictions[:top_n]
  100.  
  101. @bot.event
  102. async def on_ready():
  103. print(f"Logged in as {bot.user}")
  104. @bot.event
  105. async def on_message(message):
  106. if message.author == bot.user:
  107. return
  108. user_id = str(message.author.id)
  109. if user_id not in conversation:
  110. conversation[user_id] = []
  111.  
  112. user_message = message.content.strip()
  113. if user_message == "":
  114. return
  115.  
  116. conversation[user_id].append({"role": "user", "content": user_message})
  117.  
  118. dataset_info = ""
  119. if "movie" in user_message.lower():
  120. svd_recs = get_svd_recommendations(user_id_str="1", top_n=15)
  121. rec_titles = [f"{title} (SVD Score: {score:.2f})" for title, score in svd_recs]
  122. dataset_info = f"""
  123. Here are top recommended movie titles generated by the Surprise SVD Collaborative Filtering algorithm:
  124. {rec_titles}
  125. Use these high-scoring SVD predictions to curate recommendations for the user.
  126. """
  127. messages = [{"role": "system", "content": system_prompt + dataset_info}]
  128. messages.extend(conversation[user_id][-10:])
  129. try:
  130. async with message.channel.typing():
  131. response = client.chat.completions.create(
  132. model="llama-3.3-70b-versatile",
  133. messages=messages,
  134. temperature=0.5,
  135. max_tokens=1000,
  136. )
  137. answer = response.choices[0].message.content
  138. conversation[user_id].append({"role": "assistant", "content": answer})
  139. await message.channel.send(answer)
  140. except Exception as e:
  141. print(e)
  142. await message.channel.send("Error connecting to AI.")
  143. await bot.process_commands(message)
  144. bot.run("MTUyOTIyMTE0NjIzNzU5OTg1NQ.GS88u5.LK7oXe3F1B0_jdTqemPRBkcxlT4HiGXJdOLwaE")
Advertisement
Add Comment
Please, Sign In to add comment