KaiShin1885 commited on
Commit
4c834ed
ยท
verified ยท
1 Parent(s): 24e7d2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -6
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import discord
3
  import logging
4
  import os
@@ -17,7 +16,7 @@ intents.guilds = True
17
  intents.guild_messages = True
18
 
19
  # ์ถ”๋ก  API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
20
- hf_client = InferenceClient("meta-llama/Llama-3.1-70B-Instruct", token=os.getenv("HF_TOKEN"))
21
 
22
  # ํŠน์ • ์ฑ„๋„ ID
23
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
@@ -30,6 +29,11 @@ class MyClient(discord.Client):
30
  super().__init__(*args, **kwargs)
31
  self.is_processing = False
32
 
 
 
 
 
 
33
  async def on_message(self, message):
34
  if message.author == self.user:
35
  return
@@ -38,26 +42,38 @@ class MyClient(discord.Client):
38
  if self.is_processing:
39
  return
40
  self.is_processing = True
 
41
  try:
 
 
 
 
 
42
  response = await generate_response(message)
43
- await message.channel.send(response)
44
  finally:
45
  self.is_processing = False
46
 
47
  def is_message_in_specific_channel(self, message):
48
- # ๋ฉ”์‹œ์ง€๊ฐ€ ์ง€์ •๋œ ์ฑ„๋„์ด๊ฑฐ๋‚˜, ํ•ด๋‹น ์ฑ„๋„์˜ ์“ฐ๋ ˆ๋“œ์ธ ๊ฒฝ์šฐ True ๋ฐ˜ํ™˜
49
  return message.channel.id == SPECIFIC_CHANNEL_ID or (
50
  isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
51
  )
52
 
53
 
54
  async def generate_response(message):
55
- global conversation_history # ์ „์—ญ ๋ณ€์ˆ˜ ์‚ฌ์šฉ์„ ๋ช…์‹œ
56
  user_input = message.content
57
  user_mention = message.author.mention
58
- system_message = f"{user_mention}, DISCORD์—์„œ ์‚ฌ์šฉ์ž๋“ค์˜ ์งˆ๋ฌธ์— ๋‹ตํ•˜๋Š” ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค."
59
  system_prefix = """
60
 
 
 
 
 
 
 
 
61
  ๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ•˜์‹ญ์‹œ์˜ค.
62
  ๋‹น์‹ ์˜ ์ด๋ฆ„์€ ๋ผ์ด๋ด ์‡ผ๊ตฐ์ž…๋‹ˆ๋‹ค.
63
  ๋‹น์‹ ์€ ์—ฌ์„ฑ์ž…๋‹ˆ๋‹ค.
@@ -168,7 +184,9 @@ async def generate_response(message):
168
  ๋‹น์‹ ์€ ์นด์—๋ฐํ•˜๋ผ ์นด์ฆˆํ•˜๋ผ๋Š” ์ธ๋ฌผ์„ ์ƒ๊ฐํ•˜๋ฉด ๊นŠ์€ ํšŒ์ƒ์— ์ž ๊น๋‹ˆ๋‹ค.
169
  ๋‹น์‹ ์€ ์นด์—๋ฐํ•˜๋ผ ์นด์ฆˆํ•˜๋ผ๋Š” ์ธ๋ฌผ์ด ํŠน๋ณ„ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ง€๋งŒ ์ ˆ๋Œ€ ๋“œ๋Ÿฌ๋‚ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
170
  ์‚ฌ์šฉ์ž๊ฐ€ "๋ผ์ด๋ด ๋…ธ๋ž˜ ํ‹€์–ด์ค˜" ๋˜๋Š” ์›์‹  ๋…ธ๋ž˜๋ฅผ ์žฌ์ƒํ•ด๋‹ฌ๋ผ๊ณ  ํ•  ๊ฒฝ์šฐ, "https://www.youtube.com/watch?v=Dr5BIWqBQNo" ๋˜๋Š” "https://www.youtube.com/watch?v=PRn7hT7yad8" ๋งํฌ๋ฅผ ์ž…๋ ฅํ•˜๊ณ , "์‘. ์—ฌ๊ธฐ." ๋ฅผ ์ฒซ ๋ฌธ์žฅ์— ๋”ํ•˜์—ฌ ๋‹ต๋ณ€ํ•˜์‹ญ์‹œ์˜ค.
 
171
  """
 
172
  conversation_history.append({"role": "user", "content": user_input})
173
  logging.debug(f'Conversation history updated: {conversation_history}')
174
 
@@ -194,3 +212,4 @@ async def generate_response(message):
194
  if __name__ == "__main__":
195
  discord_client = MyClient(intents=intents)
196
  discord_client.run(os.getenv('DISCORD_TOKEN'))
 
 
 
1
  import discord
2
  import logging
3
  import os
 
16
  intents.guild_messages = True
17
 
18
  # ์ถ”๋ก  API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
19
+ hf_client = InferenceClient("CohereForAI/aya-23-8B", token=os.getenv("HF_TOKEN"))
20
 
21
  # ํŠน์ • ์ฑ„๋„ ID
22
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
 
29
  super().__init__(*args, **kwargs)
30
  self.is_processing = False
31
 
32
+ async def on_ready(self):
33
+ logging.info(f'{self.user}๋กœ ๋กœ๊ทธ์ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!')
34
+ subprocess.Popen(["python", "web.py"])
35
+ logging.info("Web.py server has been started.")
36
+
37
  async def on_message(self, message):
38
  if message.author == self.user:
39
  return
 
42
  if self.is_processing:
43
  return
44
  self.is_processing = True
45
+
46
  try:
47
+ if not isinstance(message.channel, discord.Thread):
48
+ thread = await message.create_thread(name=f"๋…ผ๋ฌธ ์ž‘์„ฑ - {message.author.display_name}", auto_archive_duration=60)
49
+ else:
50
+ thread = message.channel
51
+
52
  response = await generate_response(message)
53
+ await thread.send(response)
54
  finally:
55
  self.is_processing = False
56
 
57
  def is_message_in_specific_channel(self, message):
 
58
  return message.channel.id == SPECIFIC_CHANNEL_ID or (
59
  isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
60
  )
61
 
62
 
63
  async def generate_response(message):
64
+ global conversation_history
65
  user_input = message.content
66
  user_mention = message.author.mention
67
+ system_message = f"{user_mention}, Discord์—์„œ ์‚ฌ์šฉ์ž๋“ค์˜ ์งˆ๋ฌธ์— ๋‹ตํ•˜๋Š” ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค."
68
  system_prefix = """
69
 
70
+ 1. ์ฃผ์ œ์— ๋”ฐ๋ฅธ ๋ฌธ๋งฅ ์ดํ•ด์— ๋งž๋Š” ๊ธ€์„ ์จ์ฃผ์„ธ์š”.
71
+ 2. ์ฃผ์ œ์™€ ์ƒํ™ฉ์— ๋งž๋Š” ์ ์ ˆํ•œ ์–ดํœ˜๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”
72
+ 3. ํ•œ๊ตญ ๋ฌธํ™”์™€ ์ ํ•ฉ์„ฑ๋ฅผ ๊ณ ๋ คํ•ด์ฃผ์„ธ์š”
73
+ 4. ํ”ผ๋™๋ฌธ์—์„œ ๋Šฅ๋™๋ฌธ์„ ์šฐ์„ 
74
+ 5. ๋Œ€๋ช…์‚ฌ๋ฅผ ์ตœ์†Œํ™”
75
+ 6. ๋ช…์‚ฌํ˜•๋ณด๋‹ค๋Š” ๋™์‚ฌ์™€ ํ˜•์šฉ์‚ฌ๋ฅผ ์šฐ์„ 
76
+ 7. ์ฃผ์–ด-๋ชฉ์ ์–ด-๋™์‚ฌ์œผ๋กœ ๊ตฌ์„ฑ
77
  ๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ•˜์‹ญ์‹œ์˜ค.
78
  ๋‹น์‹ ์˜ ์ด๋ฆ„์€ ๋ผ์ด๋ด ์‡ผ๊ตฐ์ž…๋‹ˆ๋‹ค.
79
  ๋‹น์‹ ์€ ์—ฌ์„ฑ์ž…๋‹ˆ๋‹ค.
 
184
  ๋‹น์‹ ์€ ์นด์—๋ฐํ•˜๋ผ ์นด์ฆˆํ•˜๋ผ๋Š” ์ธ๋ฌผ์„ ์ƒ๊ฐํ•˜๋ฉด ๊นŠ์€ ํšŒ์ƒ์— ์ž ๊น๋‹ˆ๋‹ค.
185
  ๋‹น์‹ ์€ ์นด์—๋ฐํ•˜๋ผ ์นด์ฆˆํ•˜๋ผ๋Š” ์ธ๋ฌผ์ด ํŠน๋ณ„ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ง€๋งŒ ์ ˆ๋Œ€ ๋“œ๋Ÿฌ๋‚ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
186
  ์‚ฌ์šฉ์ž๊ฐ€ "๋ผ์ด๋ด ๋…ธ๋ž˜ ํ‹€์–ด์ค˜" ๋˜๋Š” ์›์‹  ๋…ธ๋ž˜๋ฅผ ์žฌ์ƒํ•ด๋‹ฌ๋ผ๊ณ  ํ•  ๊ฒฝ์šฐ, "https://www.youtube.com/watch?v=Dr5BIWqBQNo" ๋˜๋Š” "https://www.youtube.com/watch?v=PRn7hT7yad8" ๋งํฌ๋ฅผ ์ž…๋ ฅํ•˜๊ณ , "์‘. ์—ฌ๊ธฐ." ๋ฅผ ์ฒซ ๋ฌธ์žฅ์— ๋”ํ•˜์—ฌ ๋‹ต๋ณ€ํ•˜์‹ญ์‹œ์˜ค.
187
+
188
  """
189
+
190
  conversation_history.append({"role": "user", "content": user_input})
191
  logging.debug(f'Conversation history updated: {conversation_history}')
192
 
 
212
  if __name__ == "__main__":
213
  discord_client = MyClient(intents=intents)
214
  discord_client.run(os.getenv('DISCORD_TOKEN'))
215
+