seawolf2357 commited on
Commit
8f9044c
Β·
verified Β·
1 Parent(s): c4359cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -15
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import discord
 
2
  import logging
3
  import os
4
  from huggingface_hub import InferenceClient
@@ -27,8 +28,11 @@ SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
27
  API_KEY = os.getenv("JSONKEY")
28
  CX = "c01abc75e1b95483d" # μ‚¬μš©μž μ»€μŠ€ν…€ 검색 μ—”μ§„ ID
29
 
30
- # λŒ€ν™” νžˆμŠ€ν† λ¦¬λ₯Ό μ €μž₯ν•  μ „μ—­ λ³€μˆ˜
31
- conversation_history = []
 
 
 
32
 
33
  def google_search(query):
34
  logger.info(f"Searching for query: {query}")
@@ -63,9 +67,9 @@ def google_search(query):
63
  logger.error(f"Request failed: {e}")
64
  return f"An error occurred: {e}"
65
 
66
- class MyClient(discord.Client):
67
- def __init__(self, *args, **kwargs):
68
- super().__init__(*args, **kwargs)
69
  self.is_processing = False
70
 
71
  async def on_ready(self):
@@ -83,20 +87,56 @@ class MyClient(discord.Client):
83
 
84
  self.is_processing = True
85
  try:
86
- response = await generate_response(message)
87
- await message.channel.send(response)
 
 
 
 
88
  finally:
89
  self.is_processing = False
90
 
91
  def is_message_in_specific_channel(self, message):
92
- return message.channel.id == SPECIFIC_CHANNEL_ID or (
93
- isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  )
 
 
95
 
96
- async def generate_response(message):
97
- global conversation_history
 
 
 
 
 
 
 
 
98
  user_input = message.content
99
  user_mention = message.author.mention
 
 
 
 
 
 
 
100
  system_message = f"{user_mention}, DISCORDμ—μ„œ μ‚¬μš©μžλ“€μ˜ μ§ˆλ¬Έμ— λ‹΅ν•˜λŠ” μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€."
101
  system_prefix = """
102
  λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ λ‹΅λ³€ν•˜μ‹­μ‹œμ˜€. 좜λ ₯μ‹œ markdown ν˜•μ‹μœΌλ‘œ 좜λ ₯ν•˜λΌ.
@@ -145,9 +185,9 @@ async def generate_response(message):
145
  conversation_history.append({"role": "assistant", "content": full_response_text})
146
 
147
  # λŒ€ν™” νžˆμŠ€ν† λ¦¬ 길이 μ œν•œ (졜근 10개 λ©”μ‹œμ§€λ§Œ μœ μ§€)
148
- conversation_history = conversation_history[-10:]
149
 
150
- logging.debug(f'Conversation history updated: {conversation_history}')
151
 
152
  return f"{user_mention}, {full_response_text}"
153
  except Exception as e:
@@ -156,7 +196,7 @@ async def generate_response(message):
156
 
157
  if __name__ == "__main__":
158
  # Discord ν΄λΌμ΄μ–ΈνŠΈ μ‹€ν–‰
159
- discord_client = MyClient(intents=intents)
160
 
161
  # Discord 봇 μ‹€ν–‰
162
- discord_client.run(os.getenv('DISCORD_TOKEN'))
 
1
  import discord
2
+ from discord.ext import commands
3
  import logging
4
  import os
5
  from huggingface_hub import InferenceClient
 
28
  API_KEY = os.getenv("JSONKEY")
29
  CX = "c01abc75e1b95483d" # μ‚¬μš©μž μ»€μŠ€ν…€ 검색 μ—”μ§„ ID
30
 
31
+ # λŒ€ν™” νžˆμŠ€ν† λ¦¬λ₯Ό μ €μž₯ν•  λ”•μ…”λ„ˆλ¦¬
32
+ conversation_histories = {}
33
+
34
+ # μŠ€λ ˆλ“œ IDλ₯Ό μ €μž₯ν•  λ”•μ…”λ„ˆλ¦¬
35
+ user_threads = {}
36
 
37
  def google_search(query):
38
  logger.info(f"Searching for query: {query}")
 
67
  logger.error(f"Request failed: {e}")
68
  return f"An error occurred: {e}"
69
 
70
+ class MyClient(commands.Bot):
71
+ def __init__(self):
72
+ super().__init__(command_prefix="!", intents=intents)
73
  self.is_processing = False
74
 
75
  async def on_ready(self):
 
87
 
88
  self.is_processing = True
89
  try:
90
+ # μŠ€λ ˆλ“œ 생성 λ˜λŠ” κΈ°μ‘΄ μŠ€λ ˆλ“œ μ°ΎκΈ°
91
+ thread = await self.get_or_create_thread(message)
92
+
93
+ # μŠ€λ ˆλ“œμ—μ„œ 응닡 생성
94
+ response = await generate_response(message, thread.id)
95
+ await self.send_long_message(thread, response)
96
  finally:
97
  self.is_processing = False
98
 
99
  def is_message_in_specific_channel(self, message):
100
+ return message.channel.id == SPECIFIC_CHANNEL_ID
101
+
102
+ async def get_or_create_thread(self, message):
103
+ if message.author.id in user_threads:
104
+ thread_id = user_threads[message.author.id]
105
+ thread = self.get_channel(thread_id)
106
+ if thread is None:
107
+ # μŠ€λ ˆλ“œκ°€ μ‚­μ œλ˜μ—ˆλ‹€λ©΄ μƒˆλ‘œ 생성
108
+ thread = await self.create_thread(message)
109
+ else:
110
+ thread = await self.create_thread(message)
111
+ return thread
112
+
113
+ async def create_thread(self, message):
114
+ thread = await message.channel.create_thread(
115
+ name=f"λŒ€ν™” - {message.author.name}",
116
+ auto_archive_duration=60
117
  )
118
+ user_threads[message.author.id] = thread.id
119
+ return thread
120
 
121
+ async def send_long_message(self, channel, message):
122
+ if len(message) <= 2000:
123
+ await channel.send(message)
124
+ else:
125
+ parts = [message[i:i+2000] for i in range(0, len(message), 2000)]
126
+ for part in parts:
127
+ await channel.send(part)
128
+
129
+ async def generate_response(message, thread_id):
130
+ global conversation_histories
131
  user_input = message.content
132
  user_mention = message.author.mention
133
+
134
+ # μŠ€λ ˆλ“œλ³„ λŒ€ν™” νžˆμŠ€ν† λ¦¬ 관리
135
+ if thread_id not in conversation_histories:
136
+ conversation_histories[thread_id] = []
137
+
138
+ conversation_history = conversation_histories[thread_id]
139
+
140
  system_message = f"{user_mention}, DISCORDμ—μ„œ μ‚¬μš©μžλ“€μ˜ μ§ˆλ¬Έμ— λ‹΅ν•˜λŠ” μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€."
141
  system_prefix = """
142
  λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ λ‹΅λ³€ν•˜μ‹­μ‹œμ˜€. 좜λ ₯μ‹œ markdown ν˜•μ‹μœΌλ‘œ 좜λ ₯ν•˜λΌ.
 
185
  conversation_history.append({"role": "assistant", "content": full_response_text})
186
 
187
  # λŒ€ν™” νžˆμŠ€ν† λ¦¬ 길이 μ œν•œ (졜근 10개 λ©”μ‹œμ§€λ§Œ μœ μ§€)
188
+ conversation_histories[thread_id] = conversation_history[-10:]
189
 
190
+ logging.debug(f'Conversation history updated: {conversation_histories[thread_id]}')
191
 
192
  return f"{user_mention}, {full_response_text}"
193
  except Exception as e:
 
196
 
197
  if __name__ == "__main__":
198
  # Discord ν΄λΌμ΄μ–ΈνŠΈ μ‹€ν–‰
199
+ bot = MyClient()
200
 
201
  # Discord 봇 μ‹€ν–‰
202
+ bot.run(os.getenv('DISCORD_TOKEN'))