seawolf2357 commited on
Commit
9358a00
Β·
verified Β·
1 Parent(s): f321444

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -23
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 loop.run_in_executor(None, lambda: hf_client.chat_completion(
145
- messages, max_tokens=1000, stream=True, temperature=0.7, top_p=0.85))
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 = ''.join(full_response)
154
  logging.debug(f'Full model response: {full_response_text}')
155
 
156
- # λŒ€ν™” νžˆμŠ€ν† λ¦¬ μ—…λ°μ΄νŠΈ
157
- conversation_history.append(user_input)
158
- conversation_history.append(full_response_text)
159
 
160
- # λŒ€ν™” νžˆμŠ€ν† λ¦¬ 길이 μ œν•œ (졜근 8개 λ©”μ‹œμ§€λ§Œ μœ μ§€)
161
- conversation_histories[message.author.id] = conversation_history[-8:]
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()