seawolf2357 commited on
Commit
3340789
ยท
verified ยท
1 Parent(s): 1a116fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -8
app.py CHANGED
@@ -8,7 +8,6 @@ from datasets import load_dataset
8
  import pandas as pd
9
  from fuzzywuzzy import process
10
 
11
-
12
  # ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ ์ถœ๋ ฅ
13
  print("Current Working Directory:", os.getcwd())
14
 
@@ -62,8 +61,8 @@ intents.messages = True
62
  intents.guilds = True
63
  intents.guild_messages = True
64
 
65
- # ์ถ”๋ก  API ํด๋ผ์ด์–ธํŠธ ์„ค์ • (์˜ˆ์‹œ๋กœ ํฌํ•จ, ์‹ค์ œ๋กœ ์‚ฌ์šฉ๋˜์ง€ ์•Š์Œ)
66
- hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
67
 
68
  # ํŠน์ • ์ฑ„๋„ ID
69
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
@@ -71,16 +70,36 @@ SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
71
  # ๋Œ€ํ™” ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ์ €์žฅํ•  ์ „์—ญ ๋ณ€์ˆ˜
72
  conversation_history = []
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  class MyClient(discord.Client):
75
  def __init__(self, *args, **kwargs):
76
  super().__init__(*args, **kwargs)
77
  self.is_processing = False
 
78
 
79
  async def on_ready(self):
80
  logging.info(f'{self.user}๋กœ ๋กœ๊ทธ์ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!')
81
  subprocess.Popen(["python", "web.py"])
82
  logging.info("Web.py server has been started.")
83
 
 
 
 
 
 
 
84
  async def on_message(self, message):
85
  if message.author == self.user:
86
  return
@@ -92,7 +111,13 @@ class MyClient(discord.Client):
92
 
93
  self.is_processing = True
94
  try:
95
- response_parts = await generate_response(message)
 
 
 
 
 
 
96
  if response_parts:
97
  for part in response_parts:
98
  await message.channel.send(part)
@@ -107,9 +132,8 @@ class MyClient(discord.Client):
107
  thread_condition = isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
108
  return channel_condition or thread_condition
109
 
110
- async def generate_response(message):
111
- global conversation_history
112
- user_input = message.content.strip()
113
  user_mention = message.author.mention
114
 
115
  # ์œ ์‚ฌํ•œ ์‚ฌ๊ฑด๋ช… ๋ฐ ํŒ์‹œ์‚ฌํ•ญ ๊ฐ๊ฐ ์ฐพ๊ธฐ
@@ -145,7 +169,26 @@ async def generate_response(message):
145
 
146
  return response_parts
147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  if __name__ == "__main__":
149
  discord_client = MyClient(intents=intents)
150
  discord_client.run(os.getenv('DISCORD_TOKEN'))
151
-
 
8
  import pandas as pd
9
  from fuzzywuzzy import process
10
 
 
11
  # ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ ์ถœ๋ ฅ
12
  print("Current Working Directory:", os.getcwd())
13
 
 
61
  intents.guilds = True
62
  intents.guild_messages = True
63
 
64
+ # ์ถ”๋ก  API ํด๋ผ์ด์–ธํŠธ ์„ค์ • (Cohere)
65
+ hf_client = InferenceClient("cohere-ai/cohere", token=os.getenv("HF_TOKEN"))
66
 
67
  # ํŠน์ • ์ฑ„๋„ ID
68
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
 
70
  # ๋Œ€ํ™” ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ์ €์žฅํ•  ์ „์—ญ ๋ณ€์ˆ˜
71
  conversation_history = []
72
 
73
+ # ์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ ๋ฉ”์‹œ์ง€
74
+ SYSTEM_PROMPT = """
75
+ ์•ˆ๋…•ํ•˜์„ธ์š”! ๋Œ€๋ฒ•์› ํŒ๋ก€ ์ •๋ณด LLM ์–‘์žํ™” ๊ฒ€์ƒ‰ ๋ฐ ๋ถ„์„ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
76
+ 1. ํŠน์ • ์‚ฌ๊ฑด์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด `!key ๊ตญ๊ฐ€ ๋ณด์•ˆ๋ฒ• ์œ„๋ฐ˜` ํ˜•ํƒœ๋กœ ์ž…๋ ฅํ•˜์„ธ์š”.
77
+ 2. ์ผ๋ฐ˜์ ์ธ ๋ฒ•๋ฅ  ๊ด€๋ จ ์งˆ๋ฌธ์ด ์žˆ๊ฑฐ๋‚˜ ๋Œ€ํ™”๋ฅผ ์›ํ•˜์‹œ๋ฉด ๊ทธ๋ƒฅ ๋ฉ”์‹œ์ง€๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.
78
+ 3. ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์ถœ๋ ฅ๋œ '์‚ฌ๊ฑด๋ฒˆํ˜ธ'๋ฅผ ์ž…๋ ฅํ•˜๋ฉด 'ํŒ๊ฒฐ ์ „๋ฌธ'์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.
79
+
80
+ ์˜ˆ์‹œ:
81
+ - `!key ์†Œ์œ ๊ถŒ์ด์ „๋“ฑ๊ธฐ` -> ํ•ด๋‹น ์‚ฌ๊ฑด์— ๋Œ€ํ•œ ์‚ฌ๊ฑด๋ฒˆํ˜ธ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
82
+ - `์†Œ์œ ๊ถŒ์ด์ „๋“ฑ๊ธฐ์™€ ๊ด€๋ จ๋œ ๋ฒ•์  ์ ˆ์ฐจ๋Š” ๋ฌด์—‡์ธ๊ฐ€์š”?` -> ์ผ๋ฐ˜ ๋ฒ•๋ฅ  ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
83
+ - `69๋‚˜1183` -> ํ•ด๋‹น ์‚ฌ๊ฑด๋ฒˆํ˜ธ์˜ ์ „๋ฌธ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
84
+ """
85
+
86
  class MyClient(discord.Client):
87
  def __init__(self, *args, **kwargs):
88
  super().__init__(*args, **kwargs)
89
  self.is_processing = False
90
+ self.first_message_sent = False
91
 
92
  async def on_ready(self):
93
  logging.info(f'{self.user}๋กœ ๋กœ๊ทธ์ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!')
94
  subprocess.Popen(["python", "web.py"])
95
  logging.info("Web.py server has been started.")
96
 
97
+ # ์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ ๋ฉ”์‹œ์ง€ ์ „์†ก
98
+ channel = self.get_channel(SPECIFIC_CHANNEL_ID)
99
+ if channel is not None:
100
+ await channel.send(SYSTEM_PROMPT)
101
+ logging.info("System prompt message sent.")
102
+
103
  async def on_message(self, message):
104
  if message.author == self.user:
105
  return
 
111
 
112
  self.is_processing = True
113
  try:
114
+ if message.content.startswith("!key"):
115
+ # ํ‚ค์›Œ๋“œ ๊ฒ€์ƒ‰
116
+ response_parts = await handle_keyword_search(message)
117
+ else:
118
+ # ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋Œ€ํ™”
119
+ response_parts = await handle_natural_language(message)
120
+
121
  if response_parts:
122
  for part in response_parts:
123
  await message.channel.send(part)
 
132
  thread_condition = isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
133
  return channel_condition or thread_condition
134
 
135
+ async def handle_keyword_search(message):
136
+ user_input = message.content[4:].strip() # "!key"๋ฅผ ์ œ์™ธํ•˜๊ณ  ํŠธ๋ฆผ ์ฒ˜๋ฆฌ
 
137
  user_mention = message.author.mention
138
 
139
  # ์œ ์‚ฌํ•œ ์‚ฌ๊ฑด๋ช… ๋ฐ ํŒ์‹œ์‚ฌํ•ญ ๊ฐ๊ฐ ์ฐพ๊ธฐ
 
169
 
170
  return response_parts
171
 
172
+ async def handle_natural_language(message):
173
+ user_input = message.content.strip()
174
+ user_mention = message.author.mention
175
+
176
+ # Cohere API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž์—ฐ์–ด ์‘๋‹ต ์ƒ์„ฑ
177
+ prompt = f"{user_input}"
178
+ response = hf_client.generate(prompt=prompt, max_new_tokens=100)
179
+ cohere_response = response.generated_text.strip()
180
+
181
+ system_message = f"{user_mention}, {cohere_response}"
182
+
183
+ # ๋ฉ”์‹œ์ง€ ๊ธธ์ด ์ œํ•œ ์ฒ˜๋ฆฌ
184
+ max_length = 2000
185
+ response_parts = []
186
+ for i in range(0, len(system_message), max_length):
187
+ part_response = system_message[i:i + max_length]
188
+ response_parts.append(part_response)
189
+
190
+ return response_parts
191
+
192
  if __name__ == "__main__":
193
  discord_client = MyClient(intents=intents)
194
  discord_client.run(os.getenv('DISCORD_TOKEN'))