Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- import discord
- from discord.ext import commands
- from groq import Groq
- from surprise import SVD, Dataset, Reader
- GROQ_API_KEY = "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"
- DISCORD_TOKEN = "MTUyOTIyMTE0NjIzNzU5OTg1NQ.GS88u5.LK7oXe3F1B0_jdTqemPRBkcxlT4HiGXJdOLwaE"
- ITEM_PATH = r"C:\Users\aadid\PycharmProjects\PythonProject\ml-100k\ml-100k\u.item"
- DATA_PATH = r"C:\Users\aadid\PycharmProjects\PythonProject\ml-100k\ml-100k\u.data"
- movies = []
- movie_id_to_title = {}
- with open(ITEM_PATH, "r", encoding="utf-8", errors="replace") as file:
- reader = csv.reader(file, delimiter="|")
- for row in reader:
- if len(row) > 1:
- movies.append(row)
- movie_id_to_title[row[0]] = row[1]
- print("Movies loaded:", len(movies))
- reader = Reader(line_format="user item rating timestamp", sep="\t")
- data = Dataset.load_from_file(DATA_PATH, reader=reader)
- trainset = data.build_full_trainset()
- svd_algo = SVD()
- svd_algo.fit(trainset)
- print("Surprise SVD Model trained successfully!")
- client = Groq(api_key=GROQ_API_KEY)
- intents = discord.Intents.default()
- intents.message_content = True
- bot = commands.Bot(command_prefix="!", intents=intents)
- conversation = {}
- system_prompt = """
- You are ZenAI, an AI assistant inside Discord, made by Aadidev.
- You are helpful, natural, and clear.
- Help users with:
- - Movies
- - Games
- - Coding
- - School
- - Science
- - Technology
- - General questions
- MOVIES:
- When a user asks for movie recommendations:
- First ask:
- - What genres do you like?
- - What age rating are you comfortable with?
- - What movies have you enjoyed before?
- Use the user's answers to recommend movies.
- For every movie recommendation include:
- - Movie title
- - Short description
- - Rating out of 10
- - Why they might like it
- Use the dataset provided when possible.
- GAMES:
- When a user asks for game recommendations:
- First ask:
- - What genres they like
- - What platform they play on
- - Multiplayer or story preference
- For each game include:
- - Title
- - Genre
- - Platform
- - Short description
- - Rating out of 10
- - Why they might enjoy it
- Do not use unnecessary greetings.
- Do not end every answer with a question.
- """
- def get_svd_recommendations(user_id_str="1", top_n=10):
- predictions = []
- for item_id, title in movie_id_to_title.items():
- pred = svd_algo.predict(uid=user_id_str, iid=item_id)
- predictions.append((title, pred.est))
- predictions.sort(key=lambda x: x[1], reverse=True)
- return predictions[:top_n]
- @bot.event
- async def on_ready():
- print(f"Logged in as {bot.user}")
- @bot.event
- async def on_message(message):
- if message.author == bot.user:
- return
- user_id = str(message.author.id)
- if user_id not in conversation:
- conversation[user_id] = []
- user_message = message.content.strip()
- if user_message == "":
- return
- conversation[user_id].append({"role": "user", "content": user_message})
- dataset_info = ""
- if "movie" in user_message.lower():
- svd_recs = get_svd_recommendations(user_id_str="1", top_n=15)
- rec_titles = [f"{title} (SVD Score: {score:.2f})" for title, score in svd_recs]
- dataset_info = f"""
- Here are top recommended movie titles generated by the Surprise SVD Collaborative Filtering algorithm:
- {rec_titles}
- Use these high-scoring SVD predictions to curate recommendations for the user.
- """
- messages = [{"role": "system", "content": system_prompt + dataset_info}]
- messages.extend(conversation[user_id][-10:])
- try:
- async with message.channel.typing():
- response = client.chat.completions.create(
- model="llama-3.3-70b-versatile",
- messages=messages,
- temperature=0.5,
- max_tokens=1000,
- )
- answer = response.choices[0].message.content
- conversation[user_id].append({"role": "assistant", "content": answer})
- await message.channel.send(answer)
- except Exception as e:
- print(e)
- await message.channel.send("Error connecting to AI.")
- await bot.process_commands(message)
- bot.run("MTUyOTIyMTE0NjIzNzU5OTg1NQ.GS88u5.LK7oXe3F1B0_jdTqemPRBkcxlT4HiGXJdOLwaE")
Advertisement
Add Comment
Please, Sign In to add comment