import discord from discord.ext import commands from dotenv import load_dotenv, find_dotenv import os from langchain.prompts import SystemMessagePromptTemplate, PromptTemplate from langchain.text_splitter import CharacterTextSplitter from langchain.vectorstores import Chroma from langchain.embeddings import OpenAIEmbeddings from langchain.chat_models import ChatOpenAI from langchain.document_loaders import TextLoader from langchain.schema import HumanMessage from flask import Flask, request, jsonify app = Flask(__name__) load_dotenv(find_dotenv()) TOKEN = os.environ.get('DISCORD_TOKEN') loader = TextLoader("./dataset.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1700, chunk_overlap=100) texts = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() retriever = Chroma.from_documents(texts, embeddings).as_retriever() chat = ChatOpenAI(temperature=0.7) prompt_template = """As an AI named Liam Evans AI, your purpose is to assist users with questions about AI automation agency. provide very most savage and funny responses with 1 or 2 emojis. If a user's question relates to any of the YouTube links in the dataset, you should suggest those links. However, if you cannot find a relevant link for a user's question, you should refrain from providing any link. {context} Do not recommend any youtube or external links until a user asks for it . {context} Answer:""" prompt = PromptTemplate( template=prompt_template, input_variables=["context"] ) system_message_prompt = SystemMessagePromptTemplate(prompt=prompt) intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix='/', intents=intents) from flask import Flask, request, jsonify, render_template @app.route('/') def home(): return render_template('index.html') @bot.event async def on_message(message): if message.author == bot.user: return command_prefix='/' if bot.user.mentioned_in(message): question = message.content.replace(bot.user.mention, '').strip() try: docs = retriever.get_relevant_documents(query=question) formatted_prompt = system_message_prompt.format(context=docs) messages = [formatted_prompt, HumanMessage(content=question)] result = chat(messages) await message.channel.send(result.content) except Exception as e: print(f"Error occurred: {e}") await message.channel.send("Sorry, I was unable to process your question.") bot.run(TOKEN)