Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -105,7 +105,6 @@ async def generate_response(message):
|
|
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 |
|
@@ -127,38 +126,29 @@ async def generate_response(message):
|
|
127 |
# Google Custom Search μ€ν
|
128 |
search_results = google_search(user_input)
|
129 |
|
|
|
|
|
|
|
|
|
130 |
messages = [
|
131 |
{"role": "system", "content": f"{system_prefix} {system_message}"},
|
132 |
-
{"role": "user", "content": f"μ¬μ©μ μ§λ¬Έ: {user_input}\n\nκ²μ κ²°κ³Ό:\n{search_results}"}
|
133 |
]
|
134 |
-
|
135 |
-
# μ΄μ λν νμ€ν 리 μΆκ° (μ΅λ 4κ°μ ν΄, μ¦ 8κ°μ λ©μμ§λ§ ν¬ν¨)
|
136 |
-
for i in range(min(len(conversation_history), 8), 0, -2):
|
137 |
-
messages.append({"role": "user", "content": conversation_history[-i]})
|
138 |
-
messages.append({"role": "assistant", "content": conversation_history[-i+1]})
|
139 |
|
140 |
logging.debug(f'Messages to be sent to the model: {messages}')
|
141 |
|
142 |
-
loop = asyncio.get_event_loop()
|
143 |
try:
|
144 |
-
response = await
|
145 |
-
messages, max_tokens=1000,
|
146 |
-
|
147 |
-
full_response = []
|
148 |
-
for part in response:
|
149 |
-
logging.debug(f'Part received from stream: {part}')
|
150 |
-
if part.choices and part.choices[0].delta and part.choices[0].delta.content:
|
151 |
-
full_response.append(part.choices[0].delta.content)
|
152 |
|
153 |
-
full_response_text =
|
154 |
logging.debug(f'Full model response: {full_response_text}')
|
155 |
|
156 |
-
# λν
|
157 |
-
conversation_history.append(
|
158 |
-
conversation_history.append(full_response_text)
|
159 |
|
160 |
-
# λν νμ€ν 리 κΈΈμ΄ μ ν (μ΅κ·Ό
|
161 |
-
conversation_histories[message.author.id] = conversation_history[-
|
162 |
|
163 |
logging.debug(f'Conversation history updated: {conversation_histories[message.author.id]}')
|
164 |
|
@@ -166,7 +156,7 @@ async def generate_response(message):
|
|
166 |
except Exception as e:
|
167 |
logging.error(f"Error in generate_response: {e}")
|
168 |
return f"{user_mention}, μ£μ‘ν©λλ€. μλ΅μ μμ±νλ μ€ μ€λ₯κ° λ°μνμ΅λλ€. λ€μ μλν΄ μ£ΌμΈμ."
|
169 |
-
|
170 |
if __name__ == "__main__":
|
171 |
# Discord ν΄λΌμ΄μΈνΈ μ€ν
|
172 |
bot = MyClient()
|
|
|
105 |
user_input = message.content
|
106 |
user_mention = message.author.mention
|
107 |
|
|
|
108 |
if message.author.id not in conversation_histories:
|
109 |
conversation_histories[message.author.id] = []
|
110 |
|
|
|
126 |
# Google Custom Search μ€ν
|
127 |
search_results = google_search(user_input)
|
128 |
|
129 |
+
# λν νμ€ν 리μ μ¬μ©μ μ
λ ₯ μΆκ°
|
130 |
+
conversation_history.append({"role": "user", "content": f"μ¬μ©μ μ§λ¬Έ: {user_input}\n\nκ²μ κ²°κ³Ό:\n{search_results}"})
|
131 |
+
|
132 |
+
# API μμ²μ μν λ©μμ§ κ΅¬μ±
|
133 |
messages = [
|
134 |
{"role": "system", "content": f"{system_prefix} {system_message}"},
|
|
|
135 |
]
|
136 |
+
messages.extend(conversation_history[-10:]) # μ΅κ·Ό 10κ°μ λ©μμ§λ§ ν¬ν¨
|
|
|
|
|
|
|
|
|
137 |
|
138 |
logging.debug(f'Messages to be sent to the model: {messages}')
|
139 |
|
|
|
140 |
try:
|
141 |
+
response = await asyncio.to_thread(hf_client.chat_completion,
|
142 |
+
messages, max_tokens=1000, temperature=0.7, top_p=0.85)
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
+
full_response_text = response.choices[0].message.content
|
145 |
logging.debug(f'Full model response: {full_response_text}')
|
146 |
|
147 |
+
# λν νμ€ν 리μ μ΄μμ€ν΄νΈ μλ΅ μΆκ°
|
148 |
+
conversation_history.append({"role": "assistant", "content": full_response_text})
|
|
|
149 |
|
150 |
+
# λν νμ€ν 리 κΈΈμ΄ μ ν (μ΅κ·Ό 20κ° λ©μμ§λ§ μ μ§)
|
151 |
+
conversation_histories[message.author.id] = conversation_history[-20:]
|
152 |
|
153 |
logging.debug(f'Conversation history updated: {conversation_histories[message.author.id]}')
|
154 |
|
|
|
156 |
except Exception as e:
|
157 |
logging.error(f"Error in generate_response: {e}")
|
158 |
return f"{user_mention}, μ£μ‘ν©λλ€. μλ΅μ μμ±νλ μ€ μ€λ₯κ° λ°μνμ΅λλ€. λ€μ μλν΄ μ£ΌμΈμ."
|
159 |
+
|
160 |
if __name__ == "__main__":
|
161 |
# Discord ν΄λΌμ΄μΈνΈ μ€ν
|
162 |
bot = MyClient()
|