very simplified
Browse files
app.py
CHANGED
@@ -6,42 +6,18 @@ from google import genai
|
|
6 |
api_key = os.getenv("GOOGLE_API_KEY")
|
7 |
client = genai.Client(api_key=api_key)
|
8 |
chat = client.chats.create(model="gemini-2.0-flash")
|
|
|
9 |
|
10 |
# 回應函數(符合 type="messages" 的格式)
|
11 |
-
def respond(
|
12 |
-
message
|
13 |
-
|
14 |
-
system_message,
|
15 |
-
max_tokens,
|
16 |
-
temperature,
|
17 |
-
top_p,
|
18 |
-
api_key="GEMINI_API_KEY",
|
19 |
-
):
|
20 |
-
response = chat.send_message(message)
|
21 |
-
|
22 |
-
# 使用 OpenAI-style 格式的 history
|
23 |
-
user_entry = {"role": "user", "content": message}
|
24 |
-
assistant_entry = {"role": "assistant", "content": response.text}
|
25 |
-
updated_history = history + [user_entry, assistant_entry]
|
26 |
-
|
27 |
-
return response.text, updated_history
|
28 |
|
29 |
# Gradio Chat Interface
|
30 |
demo = gr.ChatInterface(
|
31 |
fn=respond,
|
32 |
-
type="messages",
|
33 |
-
|
34 |
-
textbox=gr.Textbox(placeholder="輸入訊息...", container=False, scale=7),
|
35 |
-
title="Gemini 2 Chat (記得上下文)",
|
36 |
-
description="使用 Gemini 2.0 Flash 建立的多輪對話助理。",
|
37 |
-
theme="soft",
|
38 |
-
cache_examples=False,
|
39 |
-
additional_inputs=[
|
40 |
-
gr.Textbox(value="你是只會說繁體中文的助理。You are a friendly Chatbot.", label="System message"),
|
41 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
42 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
43 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
44 |
-
],
|
45 |
)
|
46 |
|
47 |
if __name__ == "__main__":
|
|
|
6 |
api_key = os.getenv("GOOGLE_API_KEY")
|
7 |
client = genai.Client(api_key=api_key)
|
8 |
chat = client.chats.create(model="gemini-2.0-flash")
|
9 |
+
system_prompt = "You are a helpful assistant and always respond in Traditional Chinese."
|
10 |
|
11 |
# 回應函數(符合 type="messages" 的格式)
|
12 |
+
def respond(message,history):
|
13 |
+
response = chat.send_message(f"{system_prompt}:{message}")
|
14 |
+
return response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# Gradio Chat Interface
|
17 |
demo = gr.ChatInterface(
|
18 |
fn=respond,
|
19 |
+
type="messages",
|
20 |
+
title="Gemini Chatbot",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
)
|
22 |
|
23 |
if __name__ == "__main__":
|