Spaces:
Runtime error
Runtime error
Download responses.py from Dixith/Insider-AI: direct link, hf CLI and curl.
- Browser
- Download file 2.91 kB
-
https://huggingface.co/spaces/Dixith/Insider-AI/resolve/main/responses.py
- Command line
-
hf download hf://spaces/Dixith/Insider-AI/responses.py
-
curl -L -o responses.py https://huggingface.co/spaces/Dixith/Insider-AI/resolve/main/responses.py
2.91 kB
| 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 | |
| def home(): | |
| return render_template('index.html') | |
| 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() |