mychatbot / app.py
huchiahsi's picture
mod4
677a5ba
raw
history blame
1.67 kB
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")
# 回應函數(符合 type="messages" 的格式)
def respond(
message,
history: list[dict],
system_message,
max_tokens,
temperature,
top_p,
api_key="GEMINI_API_KEY",
):
response = chat.send_message(message)
# 使用 OpenAI-style 格式的 history
user_entry = {"role": "user", "content": message}
assistant_entry = {"role": "assistant", "content": response.text}
updated_history = history + [user_entry, assistant_entry]
return response.text, updated_history
# Gradio Chat Interface
demo = gr.ChatInterface(
fn=respond,
type="messages", # ✅ 告訴 Gradio 使用 role/content 格式
chatbot=gr.Chatbot(), # 不用再指定 type,會自動使用 interface 的
textbox=gr.Textbox(placeholder="輸入訊息...", container=False, scale=7),
title="Gemini 2 Chat (記得上下文)",
description="使用 Gemini 2.0 Flash 建立的多輪對話助理。",
theme="soft",
cache_examples=False,
additional_inputs=[
gr.Textbox(value="你是只會說繁體中文的助理。You are a friendly Chatbot.", label="System message"),
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
],
)
if __name__ == "__main__":
demo.launch()