Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,29 +23,26 @@ def chat_with_groq(user_message):
|
|
| 23 |
try:
|
| 24 |
# 調用 API
|
| 25 |
completion = client.chat.completions.create(
|
| 26 |
-
model="
|
| 27 |
messages=[
|
| 28 |
{
|
| 29 |
"role": "system",
|
| 30 |
-
"content": "
|
| 31 |
},
|
| 32 |
{"role": "user", "content": user_message}
|
| 33 |
],
|
| 34 |
-
temperature=
|
| 35 |
max_tokens=1024,
|
| 36 |
-
top_p=
|
| 37 |
-
stream=
|
| 38 |
)
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
return "抱歉,我無法生成有效的回應。"
|
| 47 |
-
else:
|
| 48 |
-
return "抱歉,API 回應的結構不符合預期。"
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
return f"發生錯誤:{str(e)}"
|
|
@@ -59,7 +56,7 @@ def gradio_chatbot():
|
|
| 59 |
return history, history
|
| 60 |
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
-
gr.Markdown("###
|
| 63 |
chatbot = gr.Chatbot()
|
| 64 |
user_input = gr.Textbox(placeholder="輸入你的訊息...")
|
| 65 |
submit_button = gr.Button("送出")
|
|
|
|
| 23 |
try:
|
| 24 |
# 調用 API
|
| 25 |
completion = client.chat.completions.create(
|
| 26 |
+
model="llama3-groq-70b-8192-tool-use-preview",
|
| 27 |
messages=[
|
| 28 |
{
|
| 29 |
"role": "system",
|
| 30 |
+
"content": "你是一位虛擬助手,專注於回答使用者的提問,提供準確而簡潔的資訊。"
|
| 31 |
},
|
| 32 |
{"role": "user", "content": user_message}
|
| 33 |
],
|
| 34 |
+
temperature=0.5,
|
| 35 |
max_tokens=1024,
|
| 36 |
+
top_p=0.65,
|
| 37 |
+
stream=True
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# 解析流式回應
|
| 41 |
+
response = ""
|
| 42 |
+
for chunk in completion:
|
| 43 |
+
delta_content = chunk.choices[0].delta.get("content", "")
|
| 44 |
+
response += delta_content
|
| 45 |
+
return response
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
return f"發生錯誤:{str(e)}"
|
|
|
|
| 56 |
return history, history
|
| 57 |
|
| 58 |
with gr.Blocks() as demo:
|
| 59 |
+
gr.Markdown("### Groq Chatbot")
|
| 60 |
chatbot = gr.Chatbot()
|
| 61 |
user_input = gr.Textbox(placeholder="輸入你的訊息...")
|
| 62 |
submit_button = gr.Button("送出")
|