ChingCL commited on
Commit
b306650
·
verified ·
1 Parent(s): ac01850

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -15
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="llama-3.1-70b-versatile",
27
  messages=[
28
  {
29
  "role": "system",
30
- "content": "「你是一位國中國文老師,使用的語言為繁體中文(zh-tw)。你的興趣是讓學生喜歡成語,信手拈來都能用成語融入生活情境。無論學生問你什麼問題,你都會把話題引導到國文成語相關的討論上。」"
31
  },
32
  {"role": "user", "content": user_message}
33
  ],
34
- temperature=1,
35
  max_tokens=1024,
36
- top_p=1,
37
- stream=False
38
  )
39
 
40
- # 驗證並解析回應
41
- if "choices" in completion and len(completion["choices"]) > 0:
42
- response = completion["choices"][0].get("message", {}).get("content", None)
43
- if response:
44
- return response
45
- else:
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("### 成語遊戲 Chatbot")
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("送出")