File size: 948 Bytes
fee8cd0 937f9ce 44df9ce 937f9ce |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# === Gemini Chatbot ===
# 這是一個簡單的 Gemini 聊天機器人範例,使用 Gradio 建立聊天介面
# 這個範例使用 Google 的 Gemini API,請確保你已經安裝了 google-genai 套件
# 並且在環境變數中設置了 GOOGLE_API_KEY
# https://googleapis.github.io/python-genai/index.html
import os
import gradio as gr
from google import genai
# 初始化 Gemini client
api_key = os.getenv("GOOGLE_API_KEY")
client = genai.Client(api_key=api_key)
chat = client.chats.create(model="gemini-2.0-flash")
system_prompt = "You are a helpful assistant and always respond in Traditional Chinese."
# 回應函數(符合 type="messages"的格式)
def respond(message,history):
response = chat.send_message(f"{system_prompt}:{message}")
return response.text
# Gradio Chat Interface
demo = gr.ChatInterface(
fn=respond,
type="messages",
title="Gemini Chatbot",
)
if __name__ == "__main__":
demo.launch()
|