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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -35
app.py CHANGED
@@ -31,9 +31,6 @@ CX = "c01abc75e1b95483d" # μ‚¬μš©μž μ»€μŠ€ν…€ 검색 μ—”μ§„ ID
31
  # λŒ€ν™” νžˆμŠ€ν† λ¦¬λ₯Ό μ €μž₯ν•  λ”•μ…”λ„ˆλ¦¬
32
  conversation_histories = {}
33
 
34
- # μŠ€λ ˆλ“œ IDλ₯Ό μ €μž₯ν•  λ”•μ…”λ„ˆλ¦¬
35
- user_threads = {}
36
-
37
  def google_search(query):
38
  logger.info(f"Searching for query: {query}")
39
  url = f"https://www.googleapis.com/customsearch/v1?key={API_KEY}&cx={CX}&q={query}&num=10"
@@ -87,37 +84,14 @@ class MyClient(commands.Bot):
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)
@@ -126,16 +100,16 @@ class MyClient(commands.Bot):
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 = """
@@ -185,9 +159,9 @@ async def generate_response(message, thread_id):
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:
 
31
  # λŒ€ν™” νžˆμŠ€ν† λ¦¬λ₯Ό μ €μž₯ν•  λ”•μ…”λ„ˆλ¦¬
32
  conversation_histories = {}
33
 
 
 
 
34
  def google_search(query):
35
  logger.info(f"Searching for query: {query}")
36
  url = f"https://www.googleapis.com/customsearch/v1?key={API_KEY}&cx={CX}&q={query}&num=10"
 
84
 
85
  self.is_processing = True
86
  try:
87
+ response = await generate_response(message)
88
+ await self.send_long_message(message.channel, response)
 
 
 
 
89
  finally:
90
  self.is_processing = False
91
 
92
  def is_message_in_specific_channel(self, message):
93
  return message.channel.id == SPECIFIC_CHANNEL_ID
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  async def send_long_message(self, channel, message):
96
  if len(message) <= 2000:
97
  await channel.send(message)
 
100
  for part in parts:
101
  await channel.send(part)
102
 
103
+ async def generate_response(message):
104
  global conversation_histories
105
  user_input = message.content
106
  user_mention = message.author.mention
107
 
108
+ # μ‚¬μš©μžλ³„ λŒ€ν™” νžˆμŠ€ν† λ¦¬ 관리
109
+ if message.author.id not in conversation_histories:
110
+ conversation_histories[message.author.id] = []
111
 
112
+ conversation_history = conversation_histories[message.author.id]
113
 
114
  system_message = f"{user_mention}, DISCORDμ—μ„œ μ‚¬μš©μžλ“€μ˜ μ§ˆλ¬Έμ— λ‹΅ν•˜λŠ” μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€."
115
  system_prefix = """
 
159
  conversation_history.append({"role": "assistant", "content": full_response_text})
160
 
161
  # λŒ€ν™” νžˆμŠ€ν† λ¦¬ 길이 μ œν•œ (졜근 10개 λ©”μ‹œμ§€λ§Œ μœ μ§€)
162
+ conversation_histories[message.author.id] = conversation_history[-10:]
163
 
164
+ logging.debug(f'Conversation history updated: {conversation_histories[message.author.id]}')
165
 
166
  return f"{user_mention}, {full_response_text}"
167
  except Exception as e: