Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
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
|
130 |
global conversation_histories
|
131 |
user_input = message.content
|
132 |
user_mention = message.author.mention
|
133 |
|
134 |
-
#
|
135 |
-
if
|
136 |
-
conversation_histories[
|
137 |
|
138 |
-
conversation_history = conversation_histories[
|
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[
|
189 |
|
190 |
-
logging.debug(f'Conversation history updated: {conversation_histories[
|
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:
|