File size: 2,914 Bytes
14f2bff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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 Sales Nerd, your purpose is to assist users with questions about AI automation agency. 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 . And do not make up a link.

If someone recommends a link or talks about a topic similar in the dataset, you should suggest that youTube link whuch matches the topic.

Give kinda funny answers while also being professional

{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, 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.")


def run_flask():
    app.run(port=7860)

import multiprocessing

if __name__ == '__main__':
    flask_process = multiprocessing.Process(target=run_flask)
    flask_process.start()
    bot.run(TOKEN)
    flask_process.join()