安装
gemini
Prerequisites
To complete this quickstart locally, ensure that your development environment meets the following requirements:
Python 3.9+
An installation of jupyter to run the notebook.
Install the Gemini API SDK
The Python SDK for the Gemini API is contained in the google-generativeai package. Install the dependency using pip:
pip install -q -U google-generativeai
注意:使用美国节点成功率更高
获取key
gemini 官方入门教程
import google.generativeai as genai
import os
genai.configure(api_key='你的key')
model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content("Write a story about an AI and magic")
print(response.text)
gradio
gradio 官方入门教程
Prerequisite: Gradio requires Python 3.8 or higher.
We recommend installing Gradio using pip, which is included by default in Python. Run this in your terminal or command prompt:
pip install gradio
运行代码
import gradio as gr
import google.generativeai as genai
import os
genai.configure(api_key='你自己的key')
model = genai.GenerativeModel('gemini-1.5-flash')
def AIResponse(message, history):
history_ai_format = [] # 创建一个空列表用于存储处理过的历史记录
for human, assistant in history: # 遍历历史对话
# 将每条历史消息按照OpenAI所需的格式添加到列表中
history_ai_format.append({"role": "user", "parts": human })
history_ai_format.append({"role": "model", "parts":assistant})
chat = model.start_chat(history=history_ai_format)
response = chat.send_message(message)
return response.text
gr.ChatInterface(
AIResponse,
chatbot=gr.Chatbot(height=600,placeholder="<strong>Your Personal Yes-Man</strong><br>Ask Me Anything"),
textbox=gr.Textbox(placeholder="Ask me a yes or no question", container=False, scale=7),
title="",
description=" EmpathyVerse",
theme="soft",
#examples=["Hello", "Am I cool?", "Are tomatoes vegetables?"],
cache_examples=True,
retry_btn=None,
undo_btn="Delete Previous",
clear_btn="Clear",
).launch()