File size: 5,879 Bytes
1b31a45
 
 
c74400e
1b31a45
cdcece5
1b31a45
c74400e
1b31a45
 
 
 
 
c74400e
1b31a45
 
 
 
 
 
 
 
 
c74400e
1b31a45
 
 
 
c74400e
1b31a45
 
 
 
 
 
c74400e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b31a45
 
c74400e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b31a45
 
c74400e
1b31a45
c74400e
 
 
1b31a45
a749a60
cdcece5
a749a60
c74400e
cdcece5
c74400e
 
 
cdcece5
c74400e
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import discord

import os
import json
import requests
import threading


intents = discord.Intents.default()
intents.message_content = True
bot = discord.Bot(intents = intents)
token = os.environ.get('TOKEN_DISCORD')


class Like_Dislike(discord.ui.View):
    @discord.ui.button(style=discord.ButtonStyle.primary, emoji="πŸ‘")
    async def like_button(self, button, interaction):
        await interaction.response.send_message("You liked the response")

    @discord.ui.button(style=discord.ButtonStyle.primary, emoji="πŸ‘Ž")
    async def dislike_button(self, button, interaction):
        await interaction.response.send_message("You disliked the response")


@bot.event
async def on_ready():
    print(f"{bot.user} is ready and online!")


@bot.slash_command(name="help", description="list of commands and other info.")
async def help(ctx: discord.ApplicationContext):
    await ctx.respond("Hello! FURY Bot responds to all your messages\
                      \n1)Inside Forum channel and\
                      \n2)Those that tag the bot.")


def llm_output(question: str, context: str) -> str:
    """
    Returns output from the LLM using the given user-question and retrived context
    """

    URL_LLM = 'https://robinroy03-fury-bot.hf.space'
    # URL_LLM = 'http://localhost:11434'    # NOTE: FOR TESTING

    prompt = f"""
    You are a senior developer. Answer the users question based on the context provided.

    Question: {question}

    Context: {context}
    """
    obj = {
            'model': 'phi3',
            'prompt': prompt,
            'stream': False
        }

    response = requests.post(URL_LLM + "/api/generate", json=obj)
    response_json = json.loads(response.text)

    return response_json['response']


def embedding_output(message: str) -> list:
    """
    Returns embeddings for the given message

    rtype: list of embeddings. Length depends on the model.
    """

    URL_EMBEDDING = 'https://robinroy03-fury-embeddings-endpoint.hf.space'

    response = requests.post(URL_EMBEDDING + "/embedding", json={"text": message})
    response_json = json.loads(response.text)

    return response_json['output']


def db_output(embedding: list) -> dict:
    """
    Returns the KNN results.

    rtype: JSON
    """

    URL_DB = 'https://robinroy03-fury-db-endpoint.hf.space'
    
    response = requests.post(URL_DB + "/query", json={"embeddings": embedding})
    response_json = json.loads(response.text)

    return response_json


@bot.event
async def on_message(message):
    """
    Returns llm answer with the relevant context.
    """

    if (message.author == bot.user) or not(bot.user.mentioned_in(message)):
        return

    print(message.content)
    await message.reply(content="Your message was received, it'll take around 30 seconds for FURY to process an answer.")

    question = message.content.replace("<@1243428204124045385>", "")
    embedding: list = embedding_output(question)
    db_knn: dict = db_output(embedding)
    llm_answer: str = llm_output(question, db_knn['matches'][0]['metadata']['text'])      # for the highest knn result (for the test only right now)  TODO: make this better

    try:
        await message.reply(content=llm_answer, view=Like_Dislike())
        await message.reply(content=db_knn['matches'][0]['metadata']['text'])
    except Exception as e:                                                           # TODO: make exception handling better
        print(e)
        await message.reply("An error occurred. Retry again.")





























# @bot.event
# async def on_message(message):
#     url_llm = 'https://robinroy03-fury-bot.hf.space'
#     url_embedding = 'https://robinroy03-fury-embeddings-endpoint.hf.space'
#     url_db = 'https://robinroy03-fury-db-endpoint.hf.space'

#     PROMPT = """
#     You are a senior developer. Answer the users question based on the context provided.

#     Question: {question}

#     Context: {context}
#     """

#     user_question = message.content.replace("<@1243428204124045385>", "")
    
#     user_question_embedding = requests.post(url_embedding + "/embedding", json={"text": user_question})
#     user_question_embedding = json.loads(user_question_embedding.text)['output']
#     print(user_question_embedding)

#     user_question_retrived_db_context = requests.post(url_db + "/query", json={"embeddings": user_question_embedding})
#     user_question_retrived_db_context = json.loads(user_question_retrived_db_context.text)['matches'][0]['metadata']['text']
#     print(user_question_retrived_db_context)

#     PROMPT = PROMPT.format(question=user_question, context=user_question_retrived_db_context)
#     print(PROMPT)

#     obj = {
#             # 'user': message.author.id,
#             'model': 'phi3',
#             'prompt': PROMPT,
#             'stream': False
#         }

#     if (message.author != bot.user) and (bot.user.mentioned_in(message)):
#         await message.reply(content="Your message was received, it'll take around 10 seconds for FURY to process an answer.")

#         try:
#             return_obj = requests.post(url_llm + "/api/generate", json=obj)
#             return_json = json.loads(return_obj.text)
#             await message.reply(content=return_json['response'] + "\n\n" + user_question_retrived_db_context, view=Like_Dislike())
#         except requests.exceptions.RequestException as e:
#             print(e)
#             await message.reply(content="Sorry something internally went wrong. Retry again.")












bot.run(token)

# def run_bot():
#     bot.run(token)
# threading.Thread(target=run_bot).start()


# ------------------------------------------------------------------------------------------------------------------------------

# import gradio as gr

# demo = gr.Blocks()
# with demo:
#     gr.HTML("The bot is working..")

# demo.queue().launch()