Update app.py
Browse files
app.py
CHANGED
|
@@ -14,13 +14,16 @@ groq_key = os.getenv("groq_key")
|
|
| 14 |
if not groq_key:
|
| 15 |
raise ValueError("請設定環境變數 'groq_key',包含您的 API 金鑰")
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
notion_api_key = os.getenv("NOTION_API_KEY")
|
| 18 |
notion_db_id = os.getenv("NOTION_DB_ID")
|
| 19 |
|
| 20 |
if not notion_api_key or not notion_db_id:
|
| 21 |
raise ValueError("請設定環境變數 'NOTION_API_KEY' 和 'NOTION_DB_ID',以連結 Notion 資料庫")
|
| 22 |
|
| 23 |
-
client = Groq(api_key=groq_key)
|
| 24 |
notion = Client(auth=notion_api_key)
|
| 25 |
|
| 26 |
# 定義函數,將對話記錄寫入 Notion
|
|
@@ -53,8 +56,10 @@ def chat_with_groq(name, history):
|
|
| 53 |
:param history: List of tuples (user_input, bot_response)
|
| 54 |
:return: Updated history with new bot response
|
| 55 |
"""
|
|
|
|
| 56 |
user_message = history[-1][0] if history else "你好!"
|
| 57 |
|
|
|
|
| 58 |
messages = [
|
| 59 |
{
|
| 60 |
"role": "system",
|
|
@@ -62,12 +67,15 @@ def chat_with_groq(name, history):
|
|
| 62 |
}
|
| 63 |
]
|
| 64 |
|
| 65 |
-
|
|
|
|
| 66 |
messages.append({"role": "user", "content": user_msg})
|
| 67 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 68 |
-
|
|
|
|
| 69 |
messages.append({"role": "user", "content": user_message})
|
| 70 |
|
|
|
|
| 71 |
try:
|
| 72 |
completion = client.chat.completions.create(
|
| 73 |
model="llama3-8b-8192",
|
|
@@ -75,17 +83,19 @@ def chat_with_groq(name, history):
|
|
| 75 |
temperature=1,
|
| 76 |
max_tokens=1024,
|
| 77 |
top_p=1,
|
| 78 |
-
stream=False,
|
| 79 |
stop=None,
|
| 80 |
)
|
|
|
|
| 81 |
response = completion.choices[0].message.content.strip()
|
| 82 |
except Exception as e:
|
| 83 |
response = f"抱歉,發生了一個錯誤:{str(e)}"
|
| 84 |
-
|
| 85 |
-
# 記錄
|
| 86 |
-
timestamp = datetime.now().isoformat()
|
| 87 |
log_to_notion(name, timestamp, user_message, response)
|
| 88 |
|
|
|
|
| 89 |
history[-1][1] = response
|
| 90 |
return history
|
| 91 |
|
|
@@ -108,9 +118,11 @@ with gr.Blocks() as demo:
|
|
| 108 |
chat_history.append((user_message, None)) # 暫時加入用戶輸入,等待回應
|
| 109 |
return "", chat_history
|
| 110 |
|
|
|
|
| 111 |
msg.submit(user_input, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 112 |
lambda history: chat_with_groq(name_input.value, history), chatbot, chatbot
|
| 113 |
)
|
|
|
|
| 114 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 115 |
|
| 116 |
# 啟動應用程式
|
|
|
|
| 14 |
if not groq_key:
|
| 15 |
raise ValueError("請設定環境變數 'groq_key',包含您的 API 金鑰")
|
| 16 |
|
| 17 |
+
# 初始化 Groq 客戶端
|
| 18 |
+
client = Groq(api_key=groq_key)
|
| 19 |
+
|
| 20 |
+
# 初始化 Notion 客戶端
|
| 21 |
notion_api_key = os.getenv("NOTION_API_KEY")
|
| 22 |
notion_db_id = os.getenv("NOTION_DB_ID")
|
| 23 |
|
| 24 |
if not notion_api_key or not notion_db_id:
|
| 25 |
raise ValueError("請設定環境變數 'NOTION_API_KEY' 和 'NOTION_DB_ID',以連結 Notion 資料庫")
|
| 26 |
|
|
|
|
| 27 |
notion = Client(auth=notion_api_key)
|
| 28 |
|
| 29 |
# 定義函數,將對話記錄寫入 Notion
|
|
|
|
| 56 |
:param history: List of tuples (user_input, bot_response)
|
| 57 |
:return: Updated history with new bot response
|
| 58 |
"""
|
| 59 |
+
# 提取最新的用戶輸入
|
| 60 |
user_message = history[-1][0] if history else "你好!"
|
| 61 |
|
| 62 |
+
# 初始化對話,帶有 role: system 的設定
|
| 63 |
messages = [
|
| 64 |
{
|
| 65 |
"role": "system",
|
|
|
|
| 67 |
}
|
| 68 |
]
|
| 69 |
|
| 70 |
+
# 添加歷史記錄作為上下文
|
| 71 |
+
for user_msg, bot_msg in history[:-1]: # 忽略最後一條,因為它是新的輸入
|
| 72 |
messages.append({"role": "user", "content": user_msg})
|
| 73 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 74 |
+
|
| 75 |
+
# 添加最新的用戶訊息
|
| 76 |
messages.append({"role": "user", "content": user_message})
|
| 77 |
|
| 78 |
+
# 呼叫 Groq API 生成回應
|
| 79 |
try:
|
| 80 |
completion = client.chat.completions.create(
|
| 81 |
model="llama3-8b-8192",
|
|
|
|
| 83 |
temperature=1,
|
| 84 |
max_tokens=1024,
|
| 85 |
top_p=1,
|
| 86 |
+
stream=False, # Gradio 不支援流式輸出,設為 False
|
| 87 |
stop=None,
|
| 88 |
)
|
| 89 |
+
# 提取 AI 回應
|
| 90 |
response = completion.choices[0].message.content.strip()
|
| 91 |
except Exception as e:
|
| 92 |
response = f"抱歉,發生了一個錯誤:{str(e)}"
|
| 93 |
+
|
| 94 |
+
# 將聊天記錄寫入 Notion
|
| 95 |
+
timestamp = datetime.now().isoformat() # 取得當前時間戳
|
| 96 |
log_to_notion(name, timestamp, user_message, response)
|
| 97 |
|
| 98 |
+
# 將新回應加入歷史記錄
|
| 99 |
history[-1][1] = response
|
| 100 |
return history
|
| 101 |
|
|
|
|
| 118 |
chat_history.append((user_message, None)) # 暫時加入用戶輸入,等待回應
|
| 119 |
return "", chat_history
|
| 120 |
|
| 121 |
+
# 提交訊息並處理對話
|
| 122 |
msg.submit(user_input, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 123 |
lambda history: chat_with_groq(name_input.value, history), chatbot, chatbot
|
| 124 |
)
|
| 125 |
+
# 清除對話按鈕
|
| 126 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 127 |
|
| 128 |
# 啟動應用程式
|