Spaces:
Sleeping
Sleeping
Update app.py
Browse files22 unmodified lines23242526272829303132333435363738394041424344454647484950517 unmodified lines5960616263646522 unmodified linestry:# 調用 APIcompletion = client.chat.completions.create(model="llama-3.1-70b-versatile",messages=[{"role": "system","content": "「你是一位國中國文老師,使用的語言為繁體中文(zh-tw)。你的興趣是讓學生喜歡成語,信手拈來都能用成語融入生活情境。無論學生問你什麼問題,你都會把話題引導到國文成語相關的討論上。」"},{"role": "user", "content": user_message}],temperature=1,max_tokens=1024,top_p=1,stream=False)# 驗證並解析回應if "choices" in completion and len(completion["choices"]) > 0:response = completion["choices"][0].get("message", {}).get("content", None)if response:return responseelse:return "抱歉,我無法生成有效的回應。"else:return "抱歉,API 回應的結構不符合預期。"except Exception as e:return f"發生錯誤:{str(e)}"7 unmodified linesreturn history, historywith gr.Blocks() as demo:gr.Markdown("### 成語遊戲 Chatbot")chatbot = gr.Chatbot()user_input = gr.Textbox(placeholder="輸入你的訊息...")submit_button = gr.Button("送出")22 unmodified lines23242526272829303132333435363738394041424344454647487 unmodified lines5657585960616222 unmodified linestry:# 調用 APIcompletion = client.chat.completions.create(model="llama3-groq-70b-8192-tool-use-preview",messages=[{"role": "system","content": "你是一位虛擬助手,專注於回答使用者的提問,提供準確而簡潔的資訊。"},{"role": "user", "content": user_message}],temperature=0.5,max_tokens=1024,top_p=0.65,stream=True)# 解析流式回應response = ""for chunk in completion:delta_content = chunk.choices[0].delta.get("content", "")response += delta_contentreturn responseexcept Exception as e:return f"發生錯誤:{str(e)}"7 unmodified linesreturn history, historywith gr.Blocks() as demo:gr.Markdown("### Groq Chatbot")chatbot = gr.Chatbot()user_input = gr.Textbox(placeholder="輸入你的訊息...")submit_button = gr.Button("送出")
app.py+12-15