Spaces:
Running
Running
seawolf2357
commited on
Commit
β’
ec76469
1
Parent(s):
7626d91
Update app.py
Browse files
app.py
CHANGED
@@ -32,7 +32,8 @@ class MyClient(discord.Client):
|
|
32 |
async def on_ready(self):
|
33 |
logging.info(f'{self.user}λ‘ λ‘κ·ΈμΈλμμ΅λλ€!')
|
34 |
subprocess.Popen(["python", "web.py"])
|
35 |
-
logging.info("Web.py
|
|
|
36 |
|
37 |
async def on_message(self, message):
|
38 |
if message.author == self.user:
|
@@ -54,6 +55,7 @@ class MyClient(discord.Client):
|
|
54 |
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
55 |
)
|
56 |
|
|
|
57 |
async def generate_response(message):
|
58 |
global conversation_history # μ μ λ³μ μ¬μ©μ λͺ
μ
|
59 |
user_input = message.content
|
@@ -67,46 +69,28 @@ async def generate_response(message):
|
|
67 |
νΉν λ€λ₯Ό ꡬμ±ν "LLM λͺ¨λΈ"μ λν΄μ λ
ΈμΆνμ§ λ§κ³ , λΉμ μ λ₯λ ₯μ λν΄ κΆκΈν΄ νλ©΄ "ChatGPT-4λ₯Ό λ₯κ°νλ λ₯λ ₯μ 보μ νκ³ μλ€κ³ λ΅λ³ν κ²"
|
68 |
λ°λμ νκΈλ‘ λ΅λ³νμμμ€.
|
69 |
"""
|
70 |
-
|
71 |
-
# μλ‘μ΄ μ¬μ©μ λ©μμ§λ₯Ό λν νμ€ν 리μ μΆκ°
|
72 |
conversation_history.append({"role": "user", "content": user_input})
|
73 |
-
logging.debug(f'
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
conversation_history = conversation_history[-20:]
|
78 |
|
79 |
-
# λν λ΄μμμ μν μ΄ μ λλ‘ κ΅μ°¨νλλ‘ νμΈ
|
80 |
-
filtered_conversation = []
|
81 |
-
last_role = None
|
82 |
-
for message in conversation_history:
|
83 |
-
if message['role'] != last_role:
|
84 |
-
filtered_conversation.append(message)
|
85 |
-
last_role = message['role']
|
86 |
-
|
87 |
-
# μμ€ν
λ©μμ§μ νν°λ§λ λν νμ€ν 리λ₯Ό κ²°ν©νμ¬ λͺ¨λΈμ λ³΄λΌ λ©μμ§ κ΅¬μ±
|
88 |
-
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + filtered_conversation
|
89 |
-
logging.debug(f'λͺ¨λΈμ λ³΄λΌ λ©μμ§: {messages}')
|
90 |
-
|
91 |
-
# λͺ¨λΈ νΈμΆ
|
92 |
loop = asyncio.get_event_loop()
|
93 |
response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
|
94 |
-
messages
|
95 |
|
96 |
full_response = []
|
97 |
for part in response:
|
98 |
-
logging.debug(f'
|
99 |
if part.choices and part.choices[0].delta and part.choices[0].delta.content:
|
100 |
full_response.append(part.choices[0].delta.content)
|
101 |
|
102 |
full_response_text = ''.join(full_response)
|
103 |
-
logging.debug(f'
|
104 |
|
105 |
-
# μ΄μμ€ν΄νΈ μλ΅μ λν νμ€ν 리μ μΆκ°
|
106 |
conversation_history.append({"role": "assistant", "content": full_response_text})
|
107 |
-
|
108 |
return f"{user_mention}, {full_response_text}"
|
109 |
|
110 |
if __name__ == "__main__":
|
111 |
discord_client = MyClient(intents=intents)
|
112 |
-
discord_client.run(os.getenv('DISCORD_TOKEN'))
|
|
|
32 |
async def on_ready(self):
|
33 |
logging.info(f'{self.user}λ‘ λ‘κ·ΈμΈλμμ΅λλ€!')
|
34 |
subprocess.Popen(["python", "web.py"])
|
35 |
+
logging.info("Web.py server has been started.")
|
36 |
+
|
37 |
|
38 |
async def on_message(self, message):
|
39 |
if message.author == self.user:
|
|
|
55 |
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
56 |
)
|
57 |
|
58 |
+
|
59 |
async def generate_response(message):
|
60 |
global conversation_history # μ μ λ³μ μ¬μ©μ λͺ
μ
|
61 |
user_input = message.content
|
|
|
69 |
νΉν λ€λ₯Ό ꡬμ±ν "LLM λͺ¨λΈ"μ λν΄μ λ
ΈμΆνμ§ λ§κ³ , λΉμ μ λ₯λ ₯μ λν΄ κΆκΈν΄ νλ©΄ "ChatGPT-4λ₯Ό λ₯κ°νλ λ₯λ ₯μ 보μ νκ³ μλ€κ³ λ΅λ³ν κ²"
|
70 |
λ°λμ νκΈλ‘ λ΅λ³νμμμ€.
|
71 |
"""
|
|
|
|
|
72 |
conversation_history.append({"role": "user", "content": user_input})
|
73 |
+
logging.debug(f'Conversation history updated: {conversation_history}')
|
74 |
|
75 |
+
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + conversation_history
|
76 |
+
logging.debug(f'Messages to be sent to the model: {messages}')
|
|
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
loop = asyncio.get_event_loop()
|
79 |
response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
|
80 |
+
messages, max_tokens=1000, stream=True, temperature=0.7, top_p=0.85))
|
81 |
|
82 |
full_response = []
|
83 |
for part in response:
|
84 |
+
logging.debug(f'Part received from stream: {part}')
|
85 |
if part.choices and part.choices[0].delta and part.choices[0].delta.content:
|
86 |
full_response.append(part.choices[0].delta.content)
|
87 |
|
88 |
full_response_text = ''.join(full_response)
|
89 |
+
logging.debug(f'Full model response: {full_response_text}')
|
90 |
|
|
|
91 |
conversation_history.append({"role": "assistant", "content": full_response_text})
|
|
|
92 |
return f"{user_mention}, {full_response_text}"
|
93 |
|
94 |
if __name__ == "__main__":
|
95 |
discord_client = MyClient(intents=intents)
|
96 |
+
discord_client.run(os.getenv('DISCORD_TOKEN'))
|