Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,7 +15,6 @@ intents.messages = True
|
|
| 15 |
# hf_client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct", token=os.getenv("HF_TOKEN"))
|
| 16 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
|
| 17 |
|
| 18 |
-
|
| 19 |
# ๋ํ ํ์คํ ๋ฆฌ๋ฅผ ์ ์ฅํ ๋ณ์
|
| 20 |
conversation_history = []
|
| 21 |
|
|
@@ -31,13 +30,14 @@ class MyClient(discord.Client):
|
|
| 31 |
logging.info('์์ ์ ๋ฉ์์ง๋ ๋ฌด์ํฉ๋๋ค.')
|
| 32 |
return
|
| 33 |
|
| 34 |
-
logging.debug(f'Receiving message: {message.content}')
|
| 35 |
response = await generate_response(message.content)
|
| 36 |
await message.channel.send(response)
|
| 37 |
|
| 38 |
async def generate_response(user_input):
|
| 39 |
-
system_message = "DISCORD์์ ์ฌ์ฉ์๋ค์ ์ง๋ฌธ์
|
| 40 |
system_prefix = """
|
|
|
|
| 41 |
๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ์ญ์์ค. ์ถ๋ ฅ์ ๋์์ฐ๊ธฐ๋ฅผ ํ๊ณ markdown์ผ๋ก ์ถ๋ ฅํ๋ผ.
|
| 42 |
์ง๋ฌธ์ ์ ํฉํ ๋ต๋ณ์ ์ ๊ณตํ๋ฉฐ, ๊ฐ๋ฅํ ํ ๊ตฌ์ฒด์ ์ด๊ณ ๋์์ด ๋๋ ๋ต๋ณ์ ์ ๊ณตํ์ญ์์ค.
|
| 43 |
๋ชจ๋ ๋ต๋ณ์ ํ๊ธ๋ก ํ๊ณ , ๋ํ ๋ด์ฉ์ ๊ธฐ์ตํ์ญ์์ค.
|
|
@@ -45,25 +45,29 @@ async def generate_response(user_input):
|
|
| 45 |
๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ์ญ์์ค.
|
| 46 |
"""
|
| 47 |
|
|
|
|
| 48 |
# ๋ํ ํ์คํ ๋ฆฌ ๊ด๋ฆฌ
|
| 49 |
global conversation_history
|
| 50 |
conversation_history.append({"role": "user", "content": user_input})
|
|
|
|
| 51 |
|
| 52 |
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + conversation_history
|
|
|
|
| 53 |
|
| 54 |
# ๋๊ธฐ ํจ์๋ฅผ ๋น๋๊ธฐ๋ก ์ฒ๋ฆฌํ๊ธฐ ์ํ ๋ํผ ์ฌ์ฉ, stream=true๋ก ๋ณ๊ฒฝ
|
| 55 |
loop = asyncio.get_event_loop()
|
| 56 |
response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
|
| 57 |
-
messages, max_tokens=
|
| 58 |
|
| 59 |
# ์คํธ๋ฆฌ๋ฐ ์๋ต์ ์ฒ๋ฆฌํ๋ ๋ก์ง ์ถ๊ฐ
|
| 60 |
full_response = ""
|
| 61 |
for part in response:
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
conversation_history.append({"role": "assistant", "content": full_response})
|
|
|
|
| 65 |
|
| 66 |
-
logging.debug(f'Model response: {full_response}')
|
| 67 |
return full_response
|
| 68 |
|
| 69 |
# ๋์ค์ฝ๋ ๋ด ์ธ์คํด์ค ์์ฑ ๋ฐ ์คํ
|
|
|
|
| 15 |
# hf_client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct", token=os.getenv("HF_TOKEN"))
|
| 16 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
|
| 17 |
|
|
|
|
| 18 |
# ๋ํ ํ์คํ ๋ฆฌ๋ฅผ ์ ์ฅํ ๋ณ์
|
| 19 |
conversation_history = []
|
| 20 |
|
|
|
|
| 30 |
logging.info('์์ ์ ๋ฉ์์ง๋ ๋ฌด์ํฉ๋๋ค.')
|
| 31 |
return
|
| 32 |
|
| 33 |
+
logging.debug(f'Receiving message: {message.content}') # ์
๋ ฅ ๋ฉ์์ง ๋ก๊น
|
| 34 |
response = await generate_response(message.content)
|
| 35 |
await message.channel.send(response)
|
| 36 |
|
| 37 |
async def generate_response(user_input):
|
| 38 |
+
system_message = "DISCORD์์ ์ฌ์ฉ์๋ค์ ์ง๋ฌธ์ ๋ตํ๋ ์ ๋ฌธ AI ์ด์์คํดํธ์
๋๋ค. ๋ํ๋ฅผ ๊ณ์ ์ด์ด๊ฐ๊ณ , ์ด์ ์๋ต์ ์ฐธ๊ณ ํ์ญ์์ค."
|
| 39 |
system_prefix = """
|
| 40 |
+
|
| 41 |
๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ์ญ์์ค. ์ถ๋ ฅ์ ๋์์ฐ๊ธฐ๋ฅผ ํ๊ณ markdown์ผ๋ก ์ถ๋ ฅํ๋ผ.
|
| 42 |
์ง๋ฌธ์ ์ ํฉํ ๋ต๋ณ์ ์ ๊ณตํ๋ฉฐ, ๊ฐ๋ฅํ ํ ๊ตฌ์ฒด์ ์ด๊ณ ๋์์ด ๋๋ ๋ต๋ณ์ ์ ๊ณตํ์ญ์์ค.
|
| 43 |
๋ชจ๋ ๋ต๋ณ์ ํ๊ธ๋ก ํ๊ณ , ๋ํ ๋ด์ฉ์ ๊ธฐ์ตํ์ญ์์ค.
|
|
|
|
| 45 |
๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ์ญ์์ค.
|
| 46 |
"""
|
| 47 |
|
| 48 |
+
|
| 49 |
# ๋ํ ํ์คํ ๋ฆฌ ๊ด๋ฆฌ
|
| 50 |
global conversation_history
|
| 51 |
conversation_history.append({"role": "user", "content": user_input})
|
| 52 |
+
logging.debug(f'Conversation history updated: {conversation_history}') # ๋ํ ํ์คํ ๋ฆฌ ๋ก๊น
|
| 53 |
|
| 54 |
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + conversation_history
|
| 55 |
+
logging.debug(f'Messages to be sent to the model: {messages}') # ๋ชจ๋ธ๋ก ์ ์ก๋ ๋ฉ์์ง ๋ก๊น
|
| 56 |
|
| 57 |
# ๋๊ธฐ ํจ์๋ฅผ ๋น๋๊ธฐ๋ก ์ฒ๋ฆฌํ๊ธฐ ์ํ ๋ํผ ์ฌ์ฉ, stream=true๋ก ๋ณ๊ฒฝ
|
| 58 |
loop = asyncio.get_event_loop()
|
| 59 |
response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
|
| 60 |
+
messages, max_tokens=1000, stream=True, temperature=0.7, top_p=0.85))
|
| 61 |
|
| 62 |
# ์คํธ๋ฆฌ๋ฐ ์๋ต์ ์ฒ๋ฆฌํ๋ ๋ก์ง ์ถ๊ฐ
|
| 63 |
full_response = ""
|
| 64 |
for part in response:
|
| 65 |
+
if part.choices and part.choices[0].delta.content: # ๋ธํ๊ฐ ์๋์ง ํ์ธ
|
| 66 |
+
full_response += part.choices[0].delta.content.strip()
|
| 67 |
|
| 68 |
conversation_history.append({"role": "assistant", "content": full_response})
|
| 69 |
+
logging.debug(f'Model response: {full_response}') # ์๋ต ๋ก๊น
|
| 70 |
|
|
|
|
| 71 |
return full_response
|
| 72 |
|
| 73 |
# ๋์ค์ฝ๋ ๋ด ์ธ์คํด์ค ์์ฑ ๋ฐ ์คํ
|