使用gemni api
Browse files
app.py
CHANGED
@@ -8,9 +8,10 @@ api_key = os.getenv("GOOGLE_API_KEY")
|
|
8 |
client = genai.Client(api_key=api_key)
|
9 |
chat = client.chats.create(model="gemini-2.0-flash")
|
10 |
|
|
|
11 |
def respond(
|
12 |
message,
|
13 |
-
history: list[
|
14 |
system_message,
|
15 |
max_tokens,
|
16 |
temperature,
|
@@ -26,19 +27,21 @@ def respond(
|
|
26 |
history = chat.get_history()
|
27 |
|
28 |
# Format the response and history for display
|
29 |
-
formatted_history =
|
30 |
-
|
31 |
-
|
32 |
|
33 |
return response.text, formatted_history
|
34 |
|
35 |
"""
|
36 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
37 |
"""
|
|
|
38 |
demo = gr.ChatInterface(
|
39 |
respond,
|
|
|
40 |
additional_inputs=[
|
41 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
42 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
43 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
44 |
gr.Slider(
|
|
|
8 |
client = genai.Client(api_key=api_key)
|
9 |
chat = client.chats.create(model="gemini-2.0-flash")
|
10 |
|
11 |
+
# Removed helper functions and simplified the respond function.
|
12 |
def respond(
|
13 |
message,
|
14 |
+
history: list[dict],
|
15 |
system_message,
|
16 |
max_tokens,
|
17 |
temperature,
|
|
|
27 |
history = chat.get_history()
|
28 |
|
29 |
# Format the response and history for display
|
30 |
+
formatted_history = [
|
31 |
+
{"role": msg.role, "content": msg.parts[0].text} for msg in history
|
32 |
+
]
|
33 |
|
34 |
return response.text, formatted_history
|
35 |
|
36 |
"""
|
37 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
38 |
"""
|
39 |
+
# Update the ChatInterface to use type='messages' and fix the respond function to handle history properly.
|
40 |
demo = gr.ChatInterface(
|
41 |
respond,
|
42 |
+
type="messages", # Updated to use OpenAI-style 'role' and 'content' keys
|
43 |
additional_inputs=[
|
44 |
+
gr.Textbox(value="你是只會說繁體中文的助理。You are a friendly Chatbot.", label="System message"),
|
45 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
46 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
47 |
gr.Slider(
|