robinroy03 commited on
Commit
c74400e
β€’
1 Parent(s): cdcece5

updated app.py, initial rag demo ready. pip requirements also updated.

Browse files
Files changed (2) hide show
  1. app.py +176 -20
  2. requirements.txt +66 -0
app.py CHANGED
@@ -1,14 +1,17 @@
1
  import discord
2
 
3
  import os
 
4
  import requests
5
  import threading
6
 
 
7
  intents = discord.Intents.default()
8
  intents.message_content = True
9
  bot = discord.Bot(intents = intents)
10
  token = os.environ.get('TOKEN_DISCORD')
11
 
 
12
  class Like_Dislike(discord.ui.View):
13
  @discord.ui.button(style=discord.ButtonStyle.primary, emoji="πŸ‘")
14
  async def like_button(self, button, interaction):
@@ -18,44 +21,197 @@ class Like_Dislike(discord.ui.View):
18
  async def dislike_button(self, button, interaction):
19
  await interaction.response.send_message("You disliked the response")
20
 
 
21
  @bot.event
22
  async def on_ready():
23
  print(f"{bot.user} is ready and online!")
24
 
 
25
  @bot.slash_command(name="help", description="list of commands and other info.")
26
  async def help(ctx: discord.ApplicationContext):
27
  await ctx.respond("Hello! FURY Bot responds to all your messages\
28
  \n1)Inside Forum channel and\
29
  \n2)Those that tag the bot.")
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  @bot.event
32
  async def on_message(message):
33
- url = 'http://127.0.0.1:8000/completion'
34
- obj = {'user': message.author.id,
35
- 'text': message.content.replace("<@1243428204124045385>", "")}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- if (message.author != bot.user) and (bot.user.mentioned_in(message)):
38
- await message.reply(content="Your message was received, it'll take around 10 seconds for FURY to process an answer.")
39
 
40
- try:
41
- return_obj = requests.post(url, json=obj)
42
- print(return_obj.text)
43
- await message.reply(content=return_obj.text, view=Like_Dislike())
44
- except requests.exceptions.RequestException as e:
45
- print(e)
46
- await message.reply(content="Sorry something internally went wrong. Retry again.")
47
 
48
- def run_bot():
49
- bot.run(token)
50
- threading.Thread(target=run_bot).start()
51
 
52
 
53
  # ------------------------------------------------------------------------------------------------------------------------------
54
 
55
- import gradio as gr
56
 
57
- demo = gr.Blocks()
58
- with demo:
59
- gr.HTML("The bot is working..")
60
 
61
- demo.queue().launch()
 
1
  import discord
2
 
3
  import os
4
+ import json
5
  import requests
6
  import threading
7
 
8
+
9
  intents = discord.Intents.default()
10
  intents.message_content = True
11
  bot = discord.Bot(intents = intents)
12
  token = os.environ.get('TOKEN_DISCORD')
13
 
14
+
15
  class Like_Dislike(discord.ui.View):
16
  @discord.ui.button(style=discord.ButtonStyle.primary, emoji="πŸ‘")
17
  async def like_button(self, button, interaction):
 
21
  async def dislike_button(self, button, interaction):
22
  await interaction.response.send_message("You disliked the response")
23
 
24
+
25
  @bot.event
26
  async def on_ready():
27
  print(f"{bot.user} is ready and online!")
28
 
29
+
30
  @bot.slash_command(name="help", description="list of commands and other info.")
31
  async def help(ctx: discord.ApplicationContext):
32
  await ctx.respond("Hello! FURY Bot responds to all your messages\
33
  \n1)Inside Forum channel and\
34
  \n2)Those that tag the bot.")
35
 
36
+
37
+ def llm_output(question: str, context: str) -> str:
38
+ """
39
+ Returns output from the LLM using the given user-question and retrived context
40
+ """
41
+
42
+ URL_LLM = 'https://robinroy03-fury-bot.hf.space'
43
+ # URL_LLM = 'http://localhost:11434' # NOTE: FOR TESTING
44
+
45
+ prompt = f"""
46
+ You are a senior developer. Answer the users question based on the context provided.
47
+
48
+ Question: {question}
49
+
50
+ Context: {context}
51
+ """
52
+ obj = {
53
+ 'model': 'phi3',
54
+ 'prompt': prompt,
55
+ 'stream': False
56
+ }
57
+
58
+ response = requests.post(URL_LLM + "/api/generate", json=obj)
59
+ response_json = json.loads(response.text)
60
+
61
+ return response_json['response']
62
+
63
+
64
+ def embedding_output(message: str) -> list:
65
+ """
66
+ Returns embeddings for the given message
67
+
68
+ rtype: list of embeddings. Length depends on the model.
69
+ """
70
+
71
+ URL_EMBEDDING = 'https://robinroy03-fury-embeddings-endpoint.hf.space'
72
+
73
+ response = requests.post(URL_EMBEDDING + "/embedding", json={"text": message})
74
+ response_json = json.loads(response.text)
75
+
76
+ return response_json['output']
77
+
78
+
79
+ def db_output(embedding: list) -> dict:
80
+ """
81
+ Returns the KNN results.
82
+
83
+ rtype: JSON
84
+ """
85
+
86
+ URL_DB = 'https://robinroy03-fury-db-endpoint.hf.space'
87
+
88
+ response = requests.post(URL_DB + "/query", json={"embeddings": embedding})
89
+ response_json = json.loads(response.text)
90
+
91
+ return response_json
92
+
93
+
94
  @bot.event
95
  async def on_message(message):
96
+ """
97
+ Returns llm answer with the relevant context.
98
+ """
99
+
100
+ if (message.author == bot.user) or not(bot.user.mentioned_in(message)):
101
+ return
102
+
103
+ print(message.content)
104
+ await message.reply(content="Your message was received, it'll take around 30 seconds for FURY to process an answer.")
105
+
106
+ question = message.content.replace("<@1243428204124045385>", "")
107
+ embedding: list = embedding_output(question)
108
+ db_knn: dict = db_output(embedding)
109
+ 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
110
+
111
+ try:
112
+ await message.reply(content=llm_answer, view=Like_Dislike())
113
+ await message.reply(content=db_knn['matches'][0]['metadata']['text'])
114
+ except Exception as e: # TODO: make exception handling better
115
+ print(e)
116
+ await message.reply("An error occurred. Retry again.")
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
+ # @bot.event
147
+ # async def on_message(message):
148
+ # url_llm = 'https://robinroy03-fury-bot.hf.space'
149
+ # url_embedding = 'https://robinroy03-fury-embeddings-endpoint.hf.space'
150
+ # url_db = 'https://robinroy03-fury-db-endpoint.hf.space'
151
+
152
+ # PROMPT = """
153
+ # You are a senior developer. Answer the users question based on the context provided.
154
+
155
+ # Question: {question}
156
+
157
+ # Context: {context}
158
+ # """
159
+
160
+ # user_question = message.content.replace("<@1243428204124045385>", "")
161
+
162
+ # user_question_embedding = requests.post(url_embedding + "/embedding", json={"text": user_question})
163
+ # user_question_embedding = json.loads(user_question_embedding.text)['output']
164
+ # print(user_question_embedding)
165
+
166
+ # user_question_retrived_db_context = requests.post(url_db + "/query", json={"embeddings": user_question_embedding})
167
+ # user_question_retrived_db_context = json.loads(user_question_retrived_db_context.text)['matches'][0]['metadata']['text']
168
+ # print(user_question_retrived_db_context)
169
+
170
+ # PROMPT = PROMPT.format(question=user_question, context=user_question_retrived_db_context)
171
+ # print(PROMPT)
172
+
173
+ # obj = {
174
+ # # 'user': message.author.id,
175
+ # 'model': 'phi3',
176
+ # 'prompt': PROMPT,
177
+ # 'stream': False
178
+ # }
179
+
180
+ # if (message.author != bot.user) and (bot.user.mentioned_in(message)):
181
+ # await message.reply(content="Your message was received, it'll take around 10 seconds for FURY to process an answer.")
182
+
183
+ # try:
184
+ # return_obj = requests.post(url_llm + "/api/generate", json=obj)
185
+ # return_json = json.loads(return_obj.text)
186
+ # await message.reply(content=return_json['response'] + "\n\n" + user_question_retrived_db_context, view=Like_Dislike())
187
+ # except requests.exceptions.RequestException as e:
188
+ # print(e)
189
+ # await message.reply(content="Sorry something internally went wrong. Retry again.")
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+
198
+
199
+
200
 
 
 
201
 
202
+ bot.run(token)
 
 
 
 
 
 
203
 
204
+ # def run_bot():
205
+ # bot.run(token)
206
+ # threading.Thread(target=run_bot).start()
207
 
208
 
209
  # ------------------------------------------------------------------------------------------------------------------------------
210
 
211
+ # import gradio as gr
212
 
213
+ # demo = gr.Blocks()
214
+ # with demo:
215
+ # gr.HTML("The bot is working..")
216
 
217
+ # demo.queue().launch()
requirements.txt CHANGED
@@ -1,18 +1,84 @@
1
  aiodns==3.2.0
 
2
  aiohttp==3.9.5
3
  aiosignal==1.3.1
 
 
 
4
  attrs==23.2.0
5
  Brotli==1.1.0
6
  certifi==2024.6.2
7
  cffi==1.16.0
8
  charset-normalizer==3.3.2
 
 
 
 
 
 
 
 
 
 
9
  frozenlist==1.4.1
 
 
 
 
 
 
 
 
10
  idna==3.7
 
 
 
 
 
 
 
 
 
11
  msgspec==0.18.6
12
  multidict==6.0.5
 
 
 
 
 
13
  py-cord==2.5.0
14
  pycares==4.4.0
15
  pycparser==2.22
 
 
 
 
 
 
 
 
 
 
 
16
  requests==2.32.3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  urllib3==2.2.1
 
 
 
 
18
  yarl==1.9.4
 
1
  aiodns==3.2.0
2
+ aiofiles==23.2.1
3
  aiohttp==3.9.5
4
  aiosignal==1.3.1
5
+ altair==5.3.0
6
+ annotated-types==0.7.0
7
+ anyio==4.4.0
8
  attrs==23.2.0
9
  Brotli==1.1.0
10
  certifi==2024.6.2
11
  cffi==1.16.0
12
  charset-normalizer==3.3.2
13
+ click==8.1.7
14
+ contourpy==1.2.1
15
+ cycler==0.12.1
16
+ dnspython==2.6.1
17
+ email_validator==2.1.1
18
+ fastapi==0.111.0
19
+ fastapi-cli==0.0.4
20
+ ffmpy==0.3.2
21
+ filelock==3.14.0
22
+ fonttools==4.53.0
23
  frozenlist==1.4.1
24
+ fsspec==2024.6.0
25
+ gradio==4.33.0
26
+ gradio_client==0.17.0
27
+ h11==0.14.0
28
+ httpcore==1.0.5
29
+ httptools==0.6.1
30
+ httpx==0.27.0
31
+ huggingface-hub==0.23.3
32
  idna==3.7
33
+ importlib_resources==6.4.0
34
+ Jinja2==3.1.4
35
+ jsonschema==4.22.0
36
+ jsonschema-specifications==2023.12.1
37
+ kiwisolver==1.4.5
38
+ markdown-it-py==3.0.0
39
+ MarkupSafe==2.1.5
40
+ matplotlib==3.9.0
41
+ mdurl==0.1.2
42
  msgspec==0.18.6
43
  multidict==6.0.5
44
+ numpy==1.26.4
45
+ orjson==3.10.3
46
+ packaging==24.0
47
+ pandas==2.2.2
48
+ pillow==10.3.0
49
  py-cord==2.5.0
50
  pycares==4.4.0
51
  pycparser==2.22
52
+ pydantic==2.7.3
53
+ pydantic_core==2.18.4
54
+ pydub==0.25.1
55
+ Pygments==2.18.0
56
+ pyparsing==3.1.2
57
+ python-dateutil==2.9.0.post0
58
+ python-dotenv==1.0.1
59
+ python-multipart==0.0.9
60
+ pytz==2024.1
61
+ PyYAML==6.0.1
62
+ referencing==0.35.1
63
  requests==2.32.3
64
+ rich==13.7.1
65
+ rpds-py==0.18.1
66
+ ruff==0.4.7
67
+ semantic-version==2.10.0
68
+ shellingham==1.5.4
69
+ six==1.16.0
70
+ sniffio==1.3.1
71
+ starlette==0.37.2
72
+ tomlkit==0.12.0
73
+ toolz==0.12.1
74
+ tqdm==4.66.4
75
+ typer==0.12.3
76
+ typing_extensions==4.12.1
77
+ tzdata==2024.1
78
+ ujson==5.10.0
79
  urllib3==2.2.1
80
+ uvicorn==0.30.1
81
+ uvloop==0.19.0
82
+ watchfiles==0.22.0
83
+ websockets==11.0.3
84
  yarl==1.9.4